Bump version odlparent->6.0.0,mdsal->5.0.3
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / servicebindings / flowbased / state / helpers / FlowBasedEgressServicesStateBindHelper.java
index d98f3b42bcabc0d00082fd8369dec3afd5301127..565ff9dea3db2e2d596c69563c319c39b6ff5478 100644 (file)
@@ -8,94 +8,72 @@
 package org.opendaylight.genius.interfacemanager.servicebindings.flowbased.state.helpers;
 
 import com.google.common.util.concurrent.ListenableFuture;
-import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.apache.aries.blueprint.annotation.service.Reference;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.genius.infra.Datastore.Configuration;
+import org.opendaylight.genius.infra.TypedReadWriteTransaction;
 import org.opendaylight.genius.interfacemanager.IfmUtil;
-import org.opendaylight.genius.interfacemanager.InterfacemgrProvider;
 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
-import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.state.factory.FlowBasedServicesStateAddable;
 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
 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.state.Interface;
 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.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class FlowBasedEgressServicesStateBindHelper implements FlowBasedServicesStateAddable {
-    private static final Logger LOG = LoggerFactory.getLogger(FlowBasedEgressServicesStateBindHelper.class);
-
-    private final InterfacemgrProvider interfaceMgrProvider;
-    private static volatile FlowBasedServicesStateAddable flowBasedServicesStateAddable;
+@Singleton
+public class FlowBasedEgressServicesStateBindHelper extends AbstractFlowBasedServicesStateBindHelper {
 
-    private FlowBasedEgressServicesStateBindHelper(InterfacemgrProvider interfaceMgrProvider) {
-        this.interfaceMgrProvider = interfaceMgrProvider;
-    }
+    private static final Logger LOG = LoggerFactory.getLogger(FlowBasedEgressServicesStateBindHelper.class);
 
-    public static void intitializeFlowBasedEgressServicesStateBindHelper(InterfacemgrProvider interfaceMgrProvider) {
-        if (flowBasedServicesStateAddable == null) {
-            synchronized (FlowBasedEgressServicesStateBindHelper.class) {
-                if (flowBasedServicesStateAddable == null) {
-                    flowBasedServicesStateAddable = new FlowBasedEgressServicesStateBindHelper(interfaceMgrProvider);
-                }
-            }
-        }
-    }
+    private final InterfaceManagerCommonUtils interfaceManagerCommonUtils;
 
-    public static FlowBasedServicesStateAddable getFlowBasedEgressServicesStateBindHelper() {
-        if (flowBasedServicesStateAddable == null) {
-            LOG.error("OvsInterfaceConfigAdd Renderer is not initialized");
-        }
-        return flowBasedServicesStateAddable;
+    @Inject
+    public FlowBasedEgressServicesStateBindHelper(@Reference final DataBroker dataBroker,
+            final InterfaceManagerCommonUtils interfaceManagerCommonUtils) {
+        super(dataBroker);
+        this.interfaceManagerCommonUtils = interfaceManagerCommonUtils;
     }
 
     @Override
-    public List<ListenableFuture<Void>> bindServicesOnInterface(Interface ifaceState, List<BoundServices> allServices) {
-        LOG.debug("binding services on interface {}", ifaceState.getName());
-        if (L2vlan.class.equals(ifaceState.getType()) || Tunnel.class.equals(ifaceState.getType())) {
-            return bindServices(allServices, ifaceState, interfaceMgrProvider.getDataBroker());
-        }
-        return null;
-    }
-
-    private static List<ListenableFuture<Void>> bindServices(List<BoundServices> allServices, Interface ifState,
-            DataBroker dataBroker) {
+    public void bindServicesOnInterface(TypedReadWriteTransaction<Configuration> tx, List<BoundServices> allServices,
+                                        Interface ifState) {
         LOG.info("bind all egress services for interface: {}", ifState.getName());
-
         NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(ifState);
-        BigInteger dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
-        WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
-        Collections.sort(allServices, (serviceInfo1, serviceInfo2) -> serviceInfo1.getServicePriority()
-                .compareTo(serviceInfo2.getServicePriority()));
+        Uint64 dpId = IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId);
+        allServices.sort(Comparator.comparing(BoundServices::getServicePriority));
         BoundServices highestPriority = allServices.remove(0);
-        short nextServiceIndex = (short) (allServices.size() > 0 ? allServices.get(0).getServicePriority()
-                : highestPriority.getServicePriority() + 1);
+        short nextServiceIndex = (short) (allServices.size() > 0 ? allServices.get(0).getServicePriority().toJava()
+                : highestPriority.getServicePriority().toJava() + 1);
         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
-            .ietf.interfaces.rev140508.interfaces.Interface iface = InterfaceManagerCommonUtils
-                .getInterfaceFromConfigDS(ifState.getName(), dataBroker);
-        FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, highestPriority, ifState.getName(), writeTransaction,
+                .ietf.interfaces.rev140508.interfaces.Interface iface = interfaceManagerCommonUtils
+                .getInterfaceFromConfigDS(ifState.getName());
+        FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, highestPriority, ifState.getName(), tx,
                 ifState.getIfIndex(), NwConstants.DEFAULT_SERVICE_INDEX, nextServiceIndex, iface);
         BoundServices prev = null;
         for (BoundServices boundService : allServices) {
             if (prev != null) {
-                FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, prev, ifState.getName(), writeTransaction,
-                        ifState.getIfIndex(), prev.getServicePriority(), boundService.getServicePriority(), iface);
+                FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, prev, ifState.getName(), tx,
+                        ifState.getIfIndex(), prev.getServicePriority().toJava(),
+                        boundService.getServicePriority().toJava(), iface);
             }
             prev = boundService;
         }
         if (prev != null) {
-            FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, prev, ifState.getName(), writeTransaction,
-                    ifState.getIfIndex(),
-                    prev.getServicePriority(), (short) (prev.getServicePriority() + 1), iface);
+            FlowBasedServicesUtils.installEgressDispatcherFlows(dpId, prev, ifState.getName(), tx,
+                    ifState.getIfIndex(), prev.getServicePriority().toJava(),
+                    (short) (prev.getServicePriority().toJava() + 1), iface);
         }
-        List<ListenableFuture<Void>> futures = new ArrayList<>();
-        futures.add(writeTransaction.submit());
-        return futures;
+    }
+
+    @Override
+    public void bindServicesOnInterfaceType(List<ListenableFuture<Void>> futures, Uint64 dpnId, String ifaceName) {
+        // TODO: No-op?
     }
 }