Ingress/Egress Service Binding Renderers
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / InterfacemgrProvider.java
index f31c6ea741c1ead1c32300e8ba713f55bbc952ca..e463b46b5daf8a9825fcd1bde9c31d761fed1afe 100644 (file)
@@ -39,6 +39,7 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeIngress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceBindings;
@@ -106,6 +107,10 @@ public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable
         this.notificationService = notificationService;
     }
 
+    public DataBroker getDataBroker(){
+        return this.dataBroker;
+    }
+
     @Override
     public void onSessionInitiated(ProviderContext session) {
         LOG.info("InterfacemgrProvider Session Initiated");
@@ -120,7 +125,7 @@ public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable
             rpcRegistration = getRpcProviderRegistry().addRpcImplementation(
                     OdlInterfaceRpcService.class, interfaceManagerRpcService);
 
-            interfaceConfigListener = new InterfaceConfigListener(dataBroker, idManager,alivenessManager, mdsalManager);
+            interfaceConfigListener = new InterfaceConfigListener(dataBroker, idManager, alivenessManager, mdsalManager);
             interfaceConfigListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
 
             interfaceInventoryStateListener = new InterfaceInventoryStateListener(dataBroker, idManager, mdsalManager, alivenessManager);
@@ -130,12 +135,12 @@ public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable
             topologyStateListener.registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
 
             hwVTEPTunnelsStateListener = new HwVTEPTunnelsStateListener(dataBroker);
-            hwVTEPTunnelsStateListener.registerListener(LogicalDatastoreType.OPERATIONAL,dataBroker);
+            hwVTEPTunnelsStateListener.registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
 
             terminationPointStateListener = new TerminationPointStateListener(dataBroker);
             terminationPointStateListener.registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
 
-            flowBasedServicesConfigListener = new FlowBasedServicesConfigListener(dataBroker);
+            flowBasedServicesConfigListener = new FlowBasedServicesConfigListener(this);
             flowBasedServicesConfigListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
 
             flowBasedServicesInterfaceStateListener =
@@ -143,7 +148,7 @@ public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable
             flowBasedServicesInterfaceStateListener.registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
 
             vlanMemberConfigListener =
-                    new VlanMemberConfigListener(dataBroker, idManager, alivenessManager,mdsalManager);
+                    new VlanMemberConfigListener(dataBroker, idManager, alivenessManager, mdsalManager);
             vlanMemberConfigListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
 
             hwVTEPConfigListener = new HwVTEPConfigListener(dataBroker);
@@ -177,7 +182,7 @@ public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable
                 LOG.debug("Created IdPool for InterfaceMgr");
             }
         } catch (InterruptedException | ExecutionException e) {
-            LOG.error("Failed to create idPool for InterfaceMgr",e);
+            LOG.error("Failed to create idPool for InterfaceMgr", e);
         }
     }
 
@@ -198,11 +203,11 @@ public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable
         Future<RpcResult<GetPortFromInterfaceOutput>> output = interfaceManagerRpcService.getPortFromInterface(input);
         try {
             RpcResult<GetPortFromInterfaceOutput> port = output.get();
-            if(port.isSuccessful()){
+            if (port.isSuccessful()) {
                 return port.getResult().getPortno();
             }
-        }catch(NullPointerException | InterruptedException | ExecutionException e){
-            LOG.warn("Exception when getting port for interface",e);
+        } catch (NullPointerException | InterruptedException | ExecutionException e) {
+            LOG.warn("Exception when getting port for interface", e);
         }
         return null;
     }
@@ -213,23 +218,22 @@ public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable
         Future<RpcResult<GetPortFromInterfaceOutput>> output = interfaceManagerRpcService.getPortFromInterface(input);
         try {
             RpcResult<GetPortFromInterfaceOutput> port = output.get();
-            if(port.isSuccessful()){
+            if (port.isSuccessful()) {
                 return port.getResult().getPortno();
             }
-        }catch(NullPointerException | InterruptedException | ExecutionException e){
-            LOG.warn("Exception when getting port for interface",e);
+        } catch (NullPointerException | InterruptedException | ExecutionException e) {
+            LOG.warn("Exception when getting port for interface", e);
         }
         return null;
     }
 
     @Override
     public InterfaceInfo getInterfaceInfo(String interfaceName) {
-        //FIXME [ELANBE] This is not working yet, fix this
 
         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface
-                ifState = InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName,dataBroker);
+                ifState = InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
 
-        if(ifState == null) {
+        if (ifState == null) {
             LOG.error("Interface {} is not present", interfaceName);
             return null;
         }
@@ -241,30 +245,23 @@ public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable
             return null;
         }
 
-        NodeConnectorId ncId = IfmUtil.getNodeConnectorIdFromInterface(intf, dataBroker);
+        NodeConnectorId ncId = IfmUtil.getNodeConnectorIdFromInterface(intf.getName(), dataBroker);
         InterfaceInfo.InterfaceType interfaceType = IfmUtil.getInterfaceType(intf);
-        InterfaceInfo interfaceInfo = null;
+        InterfaceInfo interfaceInfo = new InterfaceInfo(interfaceName);
         BigInteger dpId = org.opendaylight.genius.interfacemanager.globals.IfmConstants.INVALID_DPID;
         Integer portNo = org.opendaylight.genius.interfacemanager.globals.IfmConstants.INVALID_PORT_NO;
-        if (ncId !=null ) {
+        if (ncId != null) {
             dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(ncId));
             portNo = Integer.parseInt(IfmUtil.getPortNoFromNodeConnectorId(ncId));
         }
-
-        if(interfaceType == InterfaceInfo.InterfaceType.VLAN_INTERFACE){
+        if (interfaceType == InterfaceInfo.InterfaceType.VLAN_INTERFACE) {
             interfaceInfo = IfmUtil.getVlanInterfaceInfo(interfaceName, intf, dpId);
-        } else if (interfaceType == InterfaceInfo.InterfaceType.VXLAN_TRUNK_INTERFACE || interfaceType == InterfaceInfo.InterfaceType.GRE_TRUNK_INTERFACE) {/*
-            trunkInterfaceInfo trunkInterfaceInfo = (TrunkInterfaceInfo) ConfigIfmUtil.getTrunkInterfaceInfo(ifName, ConfigIfmUtil.getInterfaceByIfName(dataBroker, ifName));
-            String higherLayerIf = inf.getHigherLayerIf().get(0);
-            Interface vlanInterface = ConfigIfmUtil.getInterfaceByIfName(dataBroker, higherLayerIf);
-            trunkInterfaceInfo.setPortName(vlanInterface.getAugmentation(BaseConfig.class).getParentInterface());
-            trunkInterfaceManager.updateTargetMacAddressInInterfaceInfo(trunkInterfaceInfo, trunkInterface);
-            if (trunkInterface.getPhysAddress() != null) {
-                trunkInterfaceInfo.setLocalMacAddress(trunkInterface.getPhysAddress().getValue());
-            }
-            interfaceInfo = trunkInterfaceInfo;
-            interfaceInfo.setL2domainGroupId(IfmUtil.getGroupId(OperationalIfmUtil.getInterfaceStateByIfName(dataBroker, higherLayerIf).getIfIndex(), InterfaceType.VLAN_INTERFACE));
-        */} else {
+        } else if (interfaceType == InterfaceInfo.InterfaceType.VXLAN_TRUNK_INTERFACE ||
+                interfaceType == InterfaceInfo.InterfaceType.GRE_TRUNK_INTERFACE) {
+            // TODO : since there is no logical grouping for tunnel interfaces, there is no need
+            // for this code as of now. will be revisited once the support comes
+
+        } else {
             LOG.error("Type of Interface {} is unknown", interfaceName);
             return null;
         }
@@ -327,22 +324,30 @@ public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable
         interfaceInfo.setAdminState((ifState.getAdminStatus() == AdminStatus.Up) ? InterfaceAdminState.ENABLED : InterfaceAdminState.DISABLED);
         interfaceInfo.setInterfaceName(interfaceName);
         interfaceInfo.setInterfaceTag(lportTag);
-        interfaceInfo.setOpState((ifState.getOperStatus() == OperStatus.Up) ? InterfaceInfo.InterfaceOpState.UP : InterfaceInfo.InterfaceOpState.DOWN);
+        interfaceInfo.setOpState((ifState.getOperStatus() == OperStatus.Down) ? InterfaceInfo.InterfaceOpState.DOWN :
+                ifState.getOperStatus() == OperStatus.Unknown ? InterfaceInfo.InterfaceOpState.UNKNOWN : InterfaceInfo.InterfaceOpState.UP);
         return interfaceInfo;
     }
 
+    @Override
+    public Interface getInterfaceInfoFromConfigDataStore(String interfaceName) {
+        Interface intf = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(interfaceName), dataBroker);
+        return intf;
+    }
+
+    @Override
     public void createVLANInterface(String interfaceName, String portName, BigInteger dpId, Integer vlanId,
                                     String description, IfL2vlan.L2vlanMode l2vlanMode) throws InterfaceAlreadyExistsException {
 
-        LOG.info("Create VLAN interface : {}",interfaceName);
+        LOG.info("Create VLAN interface : {}", interfaceName);
         InstanceIdentifier<Interface> interfaceInstanceIdentifier = InterfaceManagerCommonUtils.getInterfaceIdentifier(new InterfaceKey(interfaceName));
         Interface interfaceOptional = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(interfaceName), dataBroker);
         if (interfaceOptional != null) {
-            LOG.debug("VLAN interface is already exist",interfaceOptional.getDescription());
+            LOG.debug("VLAN interface is already exist", interfaceOptional.getDescription());
             throw new InterfaceAlreadyExistsException(interfaceOptional.getName());
         }
         IfL2vlanBuilder l2vlanBuilder = new IfL2vlanBuilder().setL2vlanMode(l2vlanMode);
-        if(vlanId > 0){
+        if (vlanId > 0) {
             l2vlanBuilder.setVlanId(new VlanId(vlanId));
         }
         ParentRefs parentRefs = new ParentRefsBuilder().setParentInterface(portName).build();
@@ -353,20 +358,23 @@ public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable
         t.put(LogicalDatastoreType.CONFIGURATION, interfaceInstanceIdentifier, inf, true);
     }
 
-    public void bindService(String interfaceName, BoundServices serviceInfo){
-        LOG.info("Binding Service : {}",interfaceName);
+    @Override
+    public void bindService(String interfaceName, BoundServices serviceInfo) {
+        LOG.info("Binding Service : {}", interfaceName);
         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
-        InstanceIdentifier<BoundServices> boundServicesInstanceIdentifier = InstanceIdentifier.builder(ServiceBindings.class).child(ServicesInfo.class, new ServicesInfoKey(interfaceName))
+        InstanceIdentifier<BoundServices> boundServicesInstanceIdentifier = InstanceIdentifier.builder(ServiceBindings.class)
+                .child(ServicesInfo.class, new ServicesInfoKey(interfaceName, ServiceModeIngress.class))
                 .child(BoundServices.class, new BoundServicesKey(serviceInfo.getServicePriority())).build();
-       // List<BoundServices> services = (List<BoundServices>)serviceInfo.getBoundServices();
         t.put(LogicalDatastoreType.CONFIGURATION, boundServicesInstanceIdentifier, serviceInfo, true);
         t.submit();
     }
 
-    public void unbindService(String interfaceName, BoundServices serviceInfo){
-        LOG.info("Unbinding Service  : {}",interfaceName);
+    @Override
+    public void unbindService(String interfaceName, BoundServices serviceInfo) {
+        LOG.info("Unbinding Service  : {}", interfaceName);
         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
-        InstanceIdentifier<BoundServices> boundServicesInstanceIdentifier = InstanceIdentifier.builder(ServiceBindings.class).child(ServicesInfo.class, new ServicesInfoKey(interfaceName))
+        InstanceIdentifier<BoundServices> boundServicesInstanceIdentifier = InstanceIdentifier.builder(ServiceBindings.class)
+                .child(ServicesInfo.class, new ServicesInfoKey(interfaceName, ServiceModeIngress.class))
                 .child(BoundServices.class, new BoundServicesKey(serviceInfo.getServicePriority())).build();
         t.delete(LogicalDatastoreType.CONFIGURATION, boundServicesInstanceIdentifier);
         t.submit();
@@ -378,11 +386,11 @@ public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable
         Future<RpcResult<GetDpidFromInterfaceOutput>> output = interfaceManagerRpcService.getDpidFromInterface(input);
         try {
             RpcResult<GetDpidFromInterfaceOutput> dpn = output.get();
-            if(dpn.isSuccessful()){
+            if (dpn.isSuccessful()) {
                 return dpn.getResult().getDpid();
             }
-        }catch(NullPointerException | InterruptedException | ExecutionException e){
-            LOG.warn("Exception when getting port for interface",e);
+        } catch (NullPointerException | InterruptedException | ExecutionException e) {
+            LOG.warn("Exception when getting port for interface", e);
         }
         return null;
     }
@@ -393,14 +401,14 @@ public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable
         Future<RpcResult<GetEndpointIpForDpnOutput>> output = interfaceManagerRpcService.getEndpointIpForDpn(input);
         try {
             RpcResult<GetEndpointIpForDpnOutput> ipForDpnOutputRpcResult = output.get();
-            if(ipForDpnOutputRpcResult.isSuccessful()){
+            if (ipForDpnOutputRpcResult.isSuccessful()) {
                 List<IpAddress> localIps = ipForDpnOutputRpcResult.getResult().getLocalIps();
-                if(!localIps.isEmpty()) {
+                if (!localIps.isEmpty()) {
                     return localIps.get(0).getIpv4Address().getValue();
                 }
             }
-        }catch(NullPointerException | InterruptedException | ExecutionException e){
-            LOG.warn("Exception when getting port for interface",e);
+        } catch (NullPointerException | InterruptedException | ExecutionException e) {
+            LOG.warn("Exception when getting port for interface", e);
         }
         return null;
     }
@@ -418,8 +426,8 @@ public class InterfacemgrProvider implements BindingAwareProvider, AutoCloseable
     @Override
     public List<Interface> getVlanInterfaces() {
         List<Interface> vlanList = new ArrayList<Interface>();
-        InstanceIdentifier<Interfaces> interfacesInstanceIdentifier =  InstanceIdentifier.builder(Interfaces.class).build();
-        Optional<Interfaces> interfacesOptional  = IfmUtil.read(LogicalDatastoreType.CONFIGURATION, interfacesInstanceIdentifier, dataBroker);
+        InstanceIdentifier<Interfaces> interfacesInstanceIdentifier = InstanceIdentifier.builder(Interfaces.class).build();
+        Optional<Interfaces> interfacesOptional = IfmUtil.read(LogicalDatastoreType.CONFIGURATION, interfacesInstanceIdentifier, dataBroker);
         if (!interfacesOptional.isPresent()) {
             return vlanList;
         }