/**
  * Creates new overlay item or adds new underlay item into existing overlay item if the condition
  * for correlation is satisfied
  *
  * @param newItem - new underlay item on which the correlation is created
  */
 private void checkForPossibleAggregation(UnderlayItem newItem, String topologyId) {
   for (TopologyStore ts : topoStoreProvider.getTopologyStores()) {
     if ((!ts.getId().equals(topologyId)) || ts.isAggregateInside()) {
       for (Entry<YangInstanceIdentifier, UnderlayItem> topoStoreEntry :
           ts.getUnderlayItems().entrySet()) {
         UnderlayItem topoStoreItem = topoStoreEntry.getValue();
         if (newItem.getCorrelationItem() == CorrelationItemEnum.Link) {
           checkForPossibleAggregationOfLinks(newItem, topoStoreItem);
         } else {
           if (scriptEngine != null) {
             if (aggregableWithScript(newItem, topoStoreItem)) {
               aggregateItems(newItem, topoStoreItem);
               return;
             }
           } else if (!newItem.equals(topoStoreItem)
               && matchTargetFields(newItem, topoStoreItem)) {
             // no previous aggregation on this node
             aggregateItems(newItem, topoStoreItem);
             return;
           }
         }
       }
     }
   }
   if (wrapSingleItem()) {
     List<UnderlayItem> itemsToAggregate = new ArrayList<>();
     itemsToAggregate.add(newItem);
     OverlayItem overlayItem = new OverlayItem(itemsToAggregate, newItem.getCorrelationItem());
     newItem.setOverlayItem(overlayItem);
     topologyManager.addOverlayItem(overlayItem);
   }
 }
 @Override
 public void processCreatedChanges(
     YangInstanceIdentifier identifier, UnderlayItem createdItem, final String topologyId) {
   LOG.trace("Processing createdChanges");
   for (TopologyStore ts : topoStoreProvider.getTopologyStores()) {
     // Link aggregation has always only one topology store
     if (ts.getId().equals(topologyId)
         || createdItem.getCorrelationItem() == CorrelationItemEnum.Link) {
       ts.getUnderlayItems().put(identifier, createdItem);
     }
   }
   checkForPossibleAggregation(createdItem, topologyId);
 }
 @Override
 public void processUpdatedChanges(
     YangInstanceIdentifier identifier, UnderlayItem updatedItem, String topologyId) {
   LOG.trace("Processing updatedChanges");
   for (TopologyStore ts : topoStoreProvider.getTopologyStores()) {
     if (ts.getId().equals(topologyId)) {
       LOG.debug("Updating overlay item");
       UnderlayItem underlayItem = ts.getUnderlayItems().get(identifier);
       Preconditions.checkNotNull(
           underlayItem, "Updated underlay item not found in the Topology store");
       underlayItem.setItem(updatedItem.getItem());
       if (underlayItem.getCorrelationItem() == CorrelationItemEnum.Link) {
         if (underlayItem instanceof ComputedLink && updatedItem instanceof ComputedLink) {
           ComputedLink underlayLink = (ComputedLink) underlayItem;
           ComputedLink updatedLink = (ComputedLink) updatedItem;
           if (!matchTargetFields(underlayItem, updatedItem)
               || !underlayLink.getSrcNode().equals(updatedLink.getSrcNode())
               || !underlayLink.getDstNode().equals(updatedLink.getDstNode())) {
             underlayLink.setLeafNodes(updatedLink.getLeafNodes());
             underlayLink.setSrcNode(updatedLink.getSrcNode());
             underlayLink.setDstNode(updatedLink.getDstNode());
             if (underlayItem.getOverlayItem() != null) {
               removeUnderlayItemFromOverlayItem(underlayItem);
             }
             checkForPossibleAggregationOfLinks(updatedLink, underlayLink);
           } else if (underlayItem.getOverlayItem() != null) {
             // in case that only Link value was changed
             topologyManager.updateOverlayItem(underlayItem.getOverlayItem());
           }
           break;
         }
       } else {
         // if Leaf Node was changed
         if (!matchTargetFields(underlayItem, updatedItem)) {
           underlayItem.setLeafNodes(updatedItem.getLeafNodes());
           if (underlayItem.getOverlayItem() != null) {
             removeUnderlayItemFromOverlayItem(underlayItem);
           }
           checkForPossibleAggregation(underlayItem, topologyId);
         } else if (underlayItem.getOverlayItem() != null) {
           // in case that only Node value was changed
           topologyManager.updateOverlayItem(underlayItem.getOverlayItem());
         }
         break;
       }
     }
   }
 }
 /**
  * @param newItem item received from notification
  * @param topoStoreItem item already stored in topostore
  */
 private void aggregateItems(UnderlayItem newItem, UnderlayItem topoStoreItem) {
   if (topoStoreItem.getOverlayItem() == null) {
     LOG.debug("Creating new Logical Node");
     // create new logical node
     List<UnderlayItem> nodesToAggregate = new ArrayList<>();
     nodesToAggregate.add(newItem);
     nodesToAggregate.add(topoStoreItem);
     OverlayItem overlayItem =
         new OverlayItem(nodesToAggregate, topoStoreItem.getCorrelationItem());
     topoStoreItem.setOverlayItem(overlayItem);
     newItem.setOverlayItem(overlayItem);
     topologyManager.addOverlayItem(overlayItem);
     return;
   }
   LOG.debug("Adding physical node to existing Logical Node");
   // add new physical node into existing logical node
   OverlayItem overlayItem = topoStoreItem.getOverlayItem();
   newItem.setOverlayItem(overlayItem);
   overlayItem.addUnderlayItem(newItem);
   topologyManager.updateOverlayItem(overlayItem);
 }
 private OverlayItem wrapUnderlayItem(UnderlayItem underlayItem) {
   List<UnderlayItem> underlayItems = Collections.singletonList(underlayItem);
   OverlayItem overlayItem = new OverlayItem(underlayItems, underlayItem.getCorrelationItem());
   return overlayItem;
 }