Merge dev/fluorine work across to master
[unimgr.git] / cisco-xr-driver / src / main / java / org / opendaylight / unimgr / mef / nrp / cisco / xr / TopologyDataHandler.java
index 3c170ad3c87ff631c728f0da7fb7ab0321c50b17..20995a4db6373fa7f4a0b1eb4410110f30d3df4b 100644 (file)
@@ -8,55 +8,35 @@
 
 package org.opendaylight.unimgr.mef.nrp.cisco.xr;
 
-import static org.opendaylight.unimgr.utils.CapabilitiesService.Capability.Mode.AND;
-import static org.opendaylight.unimgr.utils.CapabilitiesService.NodeContext.NodeCapability.NETCONF;
-import static org.opendaylight.unimgr.utils.CapabilitiesService.NodeContext.NodeCapability.NETCONF_CISCO_IOX_IFMGR;
-import static org.opendaylight.unimgr.utils.CapabilitiesService.NodeContext.NodeCapability.NETCONF_CISCO_IOX_L2VPN;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Objects;
-import java.util.concurrent.TimeUnit;
-import java.util.function.Function;
-import java.util.function.Predicate;
-import java.util.regex.Pattern;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
-
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.controller.md.sal.binding.api.MountPoint;
-import org.opendaylight.controller.md.sal.binding.api.MountPointService;
-import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
+import com.google.common.base.Optional;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.MoreExecutors;
+import org.opendaylight.controller.md.sal.binding.api.*;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.unimgr.mef.nrp.api.TopologyManager;
 import org.opendaylight.unimgr.mef.nrp.cisco.xr.common.helper.InterfaceHelper;
+import org.opendaylight.unimgr.mef.nrp.cisco.xr.common.util.XrCapabilitiesService;
+import org.opendaylight.unimgr.mef.nrp.cisco.xr.l2vpn.driver.XrDriverBuilder;
 import org.opendaylight.unimgr.mef.nrp.common.NrpDao;
 import org.opendaylight.unimgr.mef.nrp.common.TapiUtils;
-import org.opendaylight.unimgr.utils.CapabilitiesService;
-import org.opendaylight.unimgr.utils.DriverConstants;
 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.ifmgr.cfg.rev150730.InterfaceConfigurations;
 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.ifmgr.cfg.rev150730._interface.configurations.InterfaceConfiguration;
 import org.opendaylight.yang.gen.v1.http.cisco.com.ns.yang.cisco.ios.xr.ifmgr.cfg.rev150730._interface.configurations.InterfaceConfigurationKey;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.common.rev171113.ETH;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.common.rev171113.PortDirection;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.common.rev171113.PortRole;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.common.rev171113.Uuid;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.common.rev171113.context.attrs.ServiceInterfacePoint;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.common.rev171113.context.attrs.ServiceInterfacePointBuilder;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.common.rev171113.service._interface.point.LayerProtocolBuilder;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.topology.rev171113.node.OwnedNodeEdgePoint;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.topology.rev171113.node.OwnedNodeEdgePointBuilder;
-import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.tapi.topology.rev171113.node.OwnedNodeEdgePointKey;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev180307.LayerProtocolName;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev180307.PortDirection;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev180307.PortRole;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev180307.Uuid;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev180307.tapi.context.ServiceInterfacePoint;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev180307.tapi.context.ServiceInterfacePointBuilder;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev180307.node.OwnedNodeEdgePoint;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev180307.node.OwnedNodeEdgePointBuilder;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev180307.node.OwnedNodeEdgePointKey;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev180307.node.edge.point.MappedServiceInterfacePoint;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
@@ -70,12 +50,21 @@ import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Optional;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
-import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.Futures;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.opendaylight.unimgr.mef.nrp.cisco.xr.common.util.XrCapabilitiesService.NodeCapability.*;
+import static org.opendaylight.unimgr.utils.CapabilitiesService.Capability.Mode.AND;
 
 /**
  * @author bartosz.michalik@amartus.com
@@ -88,6 +77,7 @@ public class TopologyDataHandler implements DataTreeChangeListener<Node> {
                     .child(Topology.class,
                             new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())));
 
+    private final int MAX_RETRIALS = 5;
 
     private final  TopologyManager topologyManager;
 
@@ -106,7 +96,7 @@ public class TopologyDataHandler implements DataTreeChangeListener<Node> {
 
 
     private ListenerRegistration<TopologyDataHandler> registration;
-    private CapabilitiesService capabilitiesService;
+    private XrCapabilitiesService capabilitiesService;
 
 
     public TopologyDataHandler(TopologyManager topologyManager, DataBroker dataBroker, MountPointService mountService) {
@@ -118,37 +108,40 @@ public class TopologyDataHandler implements DataTreeChangeListener<Node> {
     }
 
     public void init() {
-        LOG.debug("initializing topology handler for {}", DriverConstants.XR_NODE);
+        LOG.debug("initializing topology handler for {}", XrDriverBuilder.XR_NODE);
+        initializeWithRetrial(MAX_RETRIALS);
+    }
+
+    private void initializeWithRetrial(int retrialCouter) {
         ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
 
         NrpDao dao = new NrpDao(tx);
-        dao.createNode(topologyManager.getSystemTopologyId(), DriverConstants.XR_NODE, ETH.class, null);
+        dao.createNode(topologyManager.getSystemTopologyId(), XrDriverBuilder.XR_NODE, LayerProtocolName.ETH, null);
 
         Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
             @Override
             public void onSuccess(@Nullable Void result) {
-                LOG.info("Node {} created", DriverConstants.XR_NODE);
-                capabilitiesService = new CapabilitiesService(dataBroker);
+                LOG.info("Node {} created", XrDriverBuilder.XR_NODE);
+                capabilitiesService = new XrCapabilitiesService(dataBroker);
                 registerNetconfTreeListener();
-
             }
 
             @Override
             public void onFailure(Throwable t) {
-                LOG.error("No node created due to the error", t);
-                try {
-                    TimeUnit.SECONDS.sleep(3);
-                } catch (InterruptedException _e) {
-
+                if(retrialCouter != 0) {
+                    try {
+                        TimeUnit.MILLISECONDS.sleep(500);
+                    } catch (InterruptedException _e) { }
+                    if(retrialCouter != MAX_RETRIALS) {
+                        LOG.debug("Retrying initialization of {} for {} time", XrDriverBuilder.XR_NODE, MAX_RETRIALS - retrialCouter + 1);
+                    }
+                    initializeWithRetrial(retrialCouter - 1);
+                } else {
+                    LOG.error("No node created due to the error", t);
                 }
-                LOG.info("retrying initialization of Topology handler for {}", DriverConstants.XR_NODE);
-                init();
-            }
-        });
-
-
-
 
+            }
+        }, MoreExecutors.directExecutor());
     }
 
     public void close() {
@@ -200,15 +193,13 @@ public class TopologyDataHandler implements DataTreeChangeListener<Node> {
             ServiceInterfacePoint sip = new ServiceInterfacePointBuilder()
                     .setUuid(new Uuid("sip:" + nep.getUuid().getValue()))
 //                    .setState(St)
-                    .setLayerProtocol(Collections.singletonList(new LayerProtocolBuilder()
-                            .setLocalId("eth")
-                            .setLayerProtocolName(ETH.class)
-                            .build()))
+                    .setLayerProtocolName(Collections.singletonList(LayerProtocolName.ETH))
                     .build();
             dao.addSip(sip);
-            nep = new OwnedNodeEdgePointBuilder(nep).setMappedServiceInterfacePoint(Collections.singletonList(sip.getUuid())).build();
-            LOG.trace("Adding nep {} to {} node", nep.getUuid(), DriverConstants.XR_NODE);
-            dao.updateNep(DriverConstants.XR_NODE, nep);
+            MappedServiceInterfacePoint sipRef = TapiUtils.toSipRef(sip.getUuid(), MappedServiceInterfacePoint.class);
+            nep = new OwnedNodeEdgePointBuilder(nep).setMappedServiceInterfacePoint(Collections.singletonList(sipRef)).build();
+            LOG.trace("Adding nep {} to {} node", nep.getUuid(), XrDriverBuilder.XR_NODE);
+            dao.updateNep(XrDriverBuilder.XR_NODE, nep);
         });
 
         Futures.addCallback(topoTx.submit(), new FutureCallback<Void>() {
@@ -229,14 +220,14 @@ public class TopologyDataHandler implements DataTreeChangeListener<Node> {
     private Pattern gbPort = Pattern.compile(".*(GigabitEthernet|TenGigE)[^.]+$");
 
     final Predicate<InterfaceConfiguration> isNep = ic -> {
-        final String name = ic.getKey().getInterfaceName().getValue();
+        final String name = ic.key().getInterfaceName().getValue();
         return gbPort.matcher(name).matches();
     };
 
     private List<OwnedNodeEdgePoint> toTp(Collection<Node> nodes) {
         OwnedNodeEdgePointBuilder tpBuilder = new OwnedNodeEdgePointBuilder();
         return nodes.stream().flatMap(cn -> {
-            final NodeKey key = cn.getKey();
+            final NodeKey key = cn.key();
             try {
                 KeyedInstanceIdentifier<Node, NodeKey> id = mountIds.get(key);
                 Optional<MountPoint> mountPoint = mountService.getMountPoint(id);
@@ -252,16 +243,16 @@ public class TopologyDataHandler implements DataTreeChangeListener<Node> {
                                 })
                                 .filter(isNep::test)
                                 .map(i -> {
-                                    InterfaceConfigurationKey ikey = i.getKey();
+                                    InterfaceConfigurationKey ikey = i.key();
                                     LOG.debug("found {} interface", ikey);
 
                                     Uuid tpId = new Uuid(cn.getNodeId().getValue() + ":" + ikey.getInterfaceName().getValue());
                                     return tpBuilder
                                             .setUuid(tpId)
-                                            .setKey(new OwnedNodeEdgePointKey(tpId))
+                                            .withKey(new OwnedNodeEdgePointKey(tpId))
                                             .setLinkPortDirection(PortDirection.BIDIRECTIONAL)
                                             .setLinkPortRole(PortRole.SYMMETRIC)
-                                            .setLayerProtocol(Collections.singletonList(TapiUtils.toNepPN(ETH.class)))
+                                            .setLayerProtocolName(LayerProtocolName.ETH)
                                             .build();
                                 }).collect(Collectors.toList());