Fix issues related to checkstyle enforcement for module
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / servicebindings / flowbased / config / helpers / FlowBasedIngressServicesConfigUnbindHelper.java
index f6efe45a0243c243b8fc0a3ffad3202960f8d32d..0f16a23a81677a01de9653f8ed023ef54fd09304 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -8,9 +8,13 @@
 package org.opendaylight.genius.interfacemanager.servicebindings.flowbased.config.helpers;
 
 import com.google.common.util.concurrent.ListenableFuture;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.genius.interfacemanager.IfmConstants;
 import org.opendaylight.genius.interfacemanager.IfmUtil;
 import org.opendaylight.genius.interfacemanager.InterfacemgrProvider;
 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
@@ -21,36 +25,31 @@ import org.opendaylight.genius.mdsalutil.NwConstants;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
-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.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
 public class FlowBasedIngressServicesConfigUnbindHelper implements FlowBasedServicesConfigRemovable {
     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedIngressServicesConfigUnbindHelper.class);
 
-    private InterfacemgrProvider interfaceMgrProvider;
+    private final InterfacemgrProvider interfaceMgrProvider;
     private static volatile FlowBasedServicesConfigRemovable flowBasedIngressServicesRemovable;
 
     private FlowBasedIngressServicesConfigUnbindHelper(InterfacemgrProvider interfaceMgrProvider) {
         this.interfaceMgrProvider = interfaceMgrProvider;
     }
 
-    public static void intitializeFlowBasedIngressServicesConfigRemoveHelper(InterfacemgrProvider interfaceMgrProvider) {
+    public static void intitializeFlowBasedIngressServicesConfigRemoveHelper(
+            InterfacemgrProvider interfaceMgrProvider) {
         if (flowBasedIngressServicesRemovable == null) {
             synchronized (FlowBasedIngressServicesConfigUnbindHelper.class) {
                 if (flowBasedIngressServicesRemovable == null) {
-                    flowBasedIngressServicesRemovable = new FlowBasedIngressServicesConfigUnbindHelper(interfaceMgrProvider);
+                    flowBasedIngressServicesRemovable = new FlowBasedIngressServicesConfigUnbindHelper(
+                            interfaceMgrProvider);
                 }
             }
         }
@@ -67,51 +66,59 @@ public class FlowBasedIngressServicesConfigUnbindHelper implements FlowBasedServ
         flowBasedIngressServicesRemovable = null;
     }
 
+    @Override
     public List<ListenableFuture<Void>> unbindService(InstanceIdentifier<BoundServices> instanceIdentifier,
-                                                             BoundServices boundServiceOld) {
+            BoundServices boundServiceOld) {
+
         List<ListenableFuture<Void>> futures = new ArrayList<>();
         DataBroker dataBroker = interfaceMgrProvider.getDataBroker();
-        String interfaceName =
-                InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getInterfaceName();
-        Class<? extends ServiceModeBase> serviceMode = InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getServiceMode();
+        String interfaceName = InstanceIdentifier.keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class))
+                .getInterfaceName();
+        Class<? extends ServiceModeBase> serviceMode = InstanceIdentifier
+                .keyOf(instanceIdentifier.firstIdentifierOf(ServicesInfo.class)).getServiceMode();
 
         // Get the Parent ServiceInfo
-        ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(interfaceName, serviceMode, dataBroker);
+        ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(interfaceName, serviceMode,
+                dataBroker);
         if (servicesInfo == null) {
             LOG.error("Reached Impossible part in the code for bound service: {}", boundServiceOld);
             return futures;
         }
 
-        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
-                InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
-        if (ifState == null || ifState.getType() == null || ifState.getOperStatus() == OperStatus.Down) {
-            LOG.info("Not unbinding Service since operstatus is DOWN for Interface: {}", interfaceName);
+        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
+            .ietf.interfaces.rev140508.interfaces.state.Interface ifState = InterfaceManagerCommonUtils
+                .getInterfaceStateFromOperDS(interfaceName, dataBroker);
+        if (ifState == null) {
+            LOG.info("Interface not operational, not unbinding Service for Interface: {}", interfaceName);
             return futures;
         }
         List<BoundServices> boundServices = servicesInfo.getBoundServices();
 
         // Split based on type of interface....
-        if (ifState.getType().isAssignableFrom(L2vlan.class)) {
+        if (L2vlan.class.equals(ifState.getType())) {
             return unbindServiceOnVlan(boundServiceOld, boundServices, ifState, dataBroker);
-        } else if (ifState.getType().isAssignableFrom(Tunnel.class)) {
+        } else if (Tunnel.class.equals(ifState.getType())) {
             return unbindServiceOnTunnel(boundServiceOld, boundServices, ifState, dataBroker);
         }
         return futures;
     }
 
-    private static List<ListenableFuture<Void>> unbindServiceOnVlan(
-            BoundServices boundServiceOld,
-            List<BoundServices> boundServices, org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifaceState,
+    private static List<ListenableFuture<Void>> unbindServiceOnVlan(BoundServices boundServiceOld,
+            List<BoundServices> boundServices,
+            org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
+                .ietf.interfaces.rev140508.interfaces.state.Interface ifaceState,
             DataBroker dataBroker) {
-
+        LOG.info("unbinding ingress service {} for vlan port: {}", boundServiceOld.getServiceName(),
+                ifaceState.getName());
         List<ListenableFuture<Void>> futures = new ArrayList<>();
-        WriteTransaction t = dataBroker.newWriteOnlyTransaction();
+        WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
         BigInteger dpId = FlowBasedServicesUtils.getDpnIdFromInterface(ifaceState);
         if (boundServices.isEmpty()) {
             // Remove default entry from Lport Dispatcher Table.
-            FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), boundServiceOld, t, NwConstants.DEFAULT_SERVICE_INDEX);
-            if (t != null) {
-                futures.add(t.submit());
+            FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), boundServiceOld, tx,
+                    NwConstants.DEFAULT_SERVICE_INDEX);
+            if (tx != null) {
+                futures.add(tx.submit());
             }
             return futures;
         }
@@ -120,51 +127,67 @@ public class FlowBasedIngressServicesConfigUnbindHelper implements FlowBasedServ
         BoundServices high = highLow[1];
         // This means the one removed was the highest priority service
         if (high == null) {
-            LOG.trace("Deleting table entry for service {}, match service index {}", boundServiceOld, NwConstants.DEFAULT_SERVICE_INDEX);
-            FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), boundServiceOld, t, NwConstants.DEFAULT_SERVICE_INDEX);
+            LOG.trace("Deleting ingress dispatcher table entry for service {}, match service index {}", boundServiceOld,
+                    NwConstants.DEFAULT_SERVICE_INDEX);
+            FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), boundServiceOld, tx,
+                    NwConstants.DEFAULT_SERVICE_INDEX);
             if (low != null) {
-                //delete the lower services flow entry.
-                LOG.trace("Deleting table entry for lower service {}, match service index {}", low, low.getServicePriority());
-                FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), low, t, low.getServicePriority());
+                // delete the lower services flow entry.
+                LOG.trace("Deleting ingress dispatcher table entry for lower service {}, match service index {}", low,
+                        low.getServicePriority());
+                FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), low, tx,
+                        low.getServicePriority());
                 BoundServices lower = FlowBasedServicesUtils.getHighAndLowPriorityService(boundServices, low)[0];
-                short lowerServiceIndex = (short) ((lower!=null) ? lower.getServicePriority() : low.getServicePriority() + 1);
-                LOG.trace("Installing new entry for lower service {}, match service index {}, update service index {}", low, NwConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
-                FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, low, ifaceState.getName(), t, ifaceState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
+                short lowerServiceIndex = (short) (lower != null ? lower.getServicePriority()
+                        : low.getServicePriority() + 1);
+                LOG.trace(
+                        "Installing new ingress dispatcher table entry for lower service {}, match service index "
+                                + "{}, update service index {}",
+                        low, NwConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
+                FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, low, ifaceState.getName(), tx,
+                        ifaceState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
             }
         } else {
-            LOG.trace("Deleting table entry for service {}, match service index {}", boundServiceOld, boundServiceOld.getServicePriority());
-            FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), boundServiceOld, t, boundServiceOld.getServicePriority());
-            short lowerServiceIndex = (short) ((low!=null) ? low.getServicePriority() : boundServiceOld.getServicePriority() + 1);
+            LOG.trace("Deleting ingress dispatcher table entry for service {}, match service index {}", boundServiceOld,
+                    boundServiceOld.getServicePriority());
+            FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), boundServiceOld, tx,
+                    boundServiceOld.getServicePriority());
+            short lowerServiceIndex = (short) (low != null ? low.getServicePriority()
+                    : boundServiceOld.getServicePriority() + 1);
             BoundServices highest = FlowBasedServicesUtils.getHighestPriorityService(boundServices);
             if (high.equals(highest)) {
-                LOG.trace("Update the existing higher service {}, match service index {}, update service index {}", high, NwConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
-                FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifaceState.getName(),t, ifaceState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
+                LOG.trace("Update the existing higher service {}, match service index {}, update service index {}",
+                        high, NwConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
+                FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifaceState.getName(), tx,
+                        ifaceState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, lowerServiceIndex);
             } else {
-                LOG.trace("Update the existing higher service {}, match service index {}, update service index {}", high, high.getServicePriority(), lowerServiceIndex);
-                FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifaceState.getName(),t, ifaceState.getIfIndex(), high.getServicePriority(), lowerServiceIndex);
+                LOG.trace("Update the existing higher service {}, match service index {}, update service index {}",
+                        high, high.getServicePriority(), lowerServiceIndex);
+                FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, high, ifaceState.getName(), tx,
+                        ifaceState.getIfIndex(), high.getServicePriority(), lowerServiceIndex);
             }
         }
-        futures.add(t.submit());
+        futures.add(tx.submit());
         return futures;
     }
 
-    private static List<ListenableFuture<Void>> unbindServiceOnTunnel(
-            BoundServices boundServiceOld,
-            List<BoundServices> boundServices, org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState,
+    private static List<ListenableFuture<Void>> unbindServiceOnTunnel(BoundServices boundServiceOld,
+            List<BoundServices> boundServices,
+            org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
+                .ietf.interfaces.rev140508.interfaces.state.Interface ifState,
             DataBroker dataBroker) {
         List<ListenableFuture<Void>> futures = new ArrayList<>();
 
-        Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(ifState.getName(), dataBroker);
-        WriteTransaction t = dataBroker.newWriteOnlyTransaction();
+        WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(ifState);
-        long portNo = Long.parseLong(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
+
         BigInteger dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
 
         if (boundServices.isEmpty()) {
             // Remove entry from Ingress Table.
-            FlowBasedServicesUtils.removeIngressFlow(ifState.getName(), boundServiceOld, dpId, t);
-            if (t != null) {
-                futures.add(t.submit());
+            FlowBasedServicesUtils.removeIngressFlow(ifState.getName(), boundServiceOld, dpId, tx);
+            if (tx != null) {
+                futures.add(tx.submit());
             }
             return futures;
         }
@@ -179,26 +202,30 @@ public class FlowBasedIngressServicesConfigUnbindHelper implements FlowBasedServ
         }
 
         if (highestPriority < boundServiceOld.getServicePriority()) {
-            FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifState.getName(), boundServiceOld, t, boundServiceOld.getServicePriority());
-            if (t != null) {
-                futures.add(t.submit());
+            FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifState.getName(), boundServiceOld, tx,
+                    boundServiceOld.getServicePriority());
+            if (tx != null) {
+                futures.add(tx.submit());
             }
             return futures;
         }
 
-        List<MatchInfo> matches = null;
-        matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable (dpId, portNo);
+        List<MatchInfo> matches;
+        long portNo = IfmUtil.getPortNumberFromNodeConnectorId(nodeConnectorId);
+        matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable(dpId, portNo);
 
         BoundServices toBeMoved = tmpServicesMap.get(highestPriority);
-        FlowBasedServicesUtils.removeIngressFlow(iface.getName(), boundServiceOld, dpId, t);
-        FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, toBeMoved, t,
-                matches, ifState.getIfIndex(), NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
-        FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), toBeMoved, t, toBeMoved.getServicePriority());
-
-        if (t != null) {
-            futures.add(t.submit());
+        Interface iface =
+                InterfaceManagerCommonUtils.getInterfaceFromConfigDS(ifState.getName(), dataBroker);
+        FlowBasedServicesUtils.removeIngressFlow(iface.getName(), boundServiceOld, dpId, tx);
+        FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, toBeMoved, tx, matches, ifState.getIfIndex(),
+                NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
+        FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), toBeMoved, tx,
+                toBeMoved.getServicePriority());
+
+        if (tx != null) {
+            futures.add(tx.submit());
         }
         return futures;
     }
-
-}
\ No newline at end of file
+}