Make some classes final and not instantiables
[genius.git] / interfacemanager / interfacemanager-api / src / main / java / org / opendaylight / genius / interfacemanager / globals / InterfaceServiceUtil.java
index 4fc637e7a79ef241befd6c00fd823578ff34b059..2726bb8d4357af5dde56b39c29caa4e81448c547 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,
 
 package org.opendaylight.genius.interfacemanager.globals;
 
+import com.google.common.base.Optional;
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
-
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.genius.mdsalutil.FlowInfoKey;
 import org.opendaylight.genius.mdsalutil.GroupInfoKey;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
-import org.opendaylight.genius.mdsalutil.MatchFieldType;
 import org.opendaylight.genius.mdsalutil.MatchInfo;
+import org.opendaylight.genius.mdsalutil.MatchInfoBase;
 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
+import org.opendaylight.genius.mdsalutil.matches.MatchInPort;
+import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
+import org.opendaylight.genius.mdsalutil.matches.MatchVlanVid;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
 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.InterfaceKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeIngress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceTypeFlowBased;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflowBuilder;
 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.ServicesInfoBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfoKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
 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.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceTypeFlowBased;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflow;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflowBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesKey;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
-import com.google.common.base.Optional;
+public final class InterfaceServiceUtil {
 
-public class InterfaceServiceUtil {
+    private InterfaceServiceUtil() {
+    }
 
     public static ServicesInfo buildServiceInfo(String serviceName, short serviceIndex, int servicePriority,
             BigInteger cookie, List<Instruction> instructions) {
-        List<BoundServices>  boundService = new ArrayList<BoundServices>();
-        boundService.add(new BoundServicesBuilder().setServicePriority((short)servicePriority).setServiceName(serviceName).build());
-        return new ServicesInfoBuilder().setBoundServices(boundService).setKey(new ServicesInfoKey(serviceName, ServiceModeIngress.class)).build();
-    }
-
-    public static BoundServices getBoundServices(String serviceName, short servicePriority, int flowPriority,
-                                                 BigInteger cookie, List<Instruction> instructions) {
-        StypeOpenflowBuilder augBuilder = new StypeOpenflowBuilder().setFlowCookie(cookie).setFlowPriority(flowPriority).setInstruction(instructions);
-        return new BoundServicesBuilder().setKey(new BoundServicesKey(servicePriority))
-                .setServiceName(serviceName).setServicePriority(servicePriority)
-                .setServiceType(ServiceTypeFlowBased.class).addAugmentation(StypeOpenflow.class, augBuilder.build()).build();
+        List<BoundServices> boundService = new ArrayList<>();
+        boundService.add(new BoundServicesBuilder().setServicePriority((short) servicePriority)
+                .setServiceName(serviceName).build());
+        return new ServicesInfoBuilder().setBoundServices(boundService)
+                .setKey(new ServicesInfoKey(serviceName, ServiceModeIngress.class)).build();
     }
 
     public static ServicesInfo buildServiceInfo(String serviceName, short serviceIndex, int servicePriority,
             BigInteger cookie) {
-        List<BoundServices>  boundService = new ArrayList<BoundServices>();
-        boundService.add(new BoundServicesBuilder().setServicePriority((short)servicePriority).setServiceName(serviceName).build());
-        return new ServicesInfoBuilder().setBoundServices(boundService).setKey(new ServicesInfoKey(serviceName, ServiceModeIngress.class)).build();
+        List<BoundServices> boundService = new ArrayList<>();
+        boundService.add(new BoundServicesBuilder().setServicePriority((short) servicePriority)
+                .setServiceName(serviceName).build());
+        return new ServicesInfoBuilder().setBoundServices(boundService)
+                .setKey(new ServicesInfoKey(serviceName, ServiceModeIngress.class)).build();
+    }
+
+    public static BoundServices getBoundServices(String serviceName, short servicePriority, int flowPriority,
+            BigInteger cookie, List<Instruction> instructions) {
+        StypeOpenflowBuilder augBuilder = new StypeOpenflowBuilder().setFlowCookie(cookie).setFlowPriority(flowPriority)
+                .setInstruction(instructions);
+        return new BoundServicesBuilder().setKey(new BoundServicesKey(servicePriority)).setServiceName(serviceName)
+                .setServicePriority(servicePriority).setServiceType(ServiceTypeFlowBased.class)
+                .addAugmentation(StypeOpenflow.class, augBuilder.build()).build();
     }
 
-    public static List<MatchInfo> getMatchInfoForVlanLPort(BigInteger dpId, long portNo, long vlanId, boolean isVlanTransparent) {
-        List<MatchInfo> matches = new ArrayList<MatchInfo>();
-        matches.add(new MatchInfo(MatchFieldType.in_port, new BigInteger[] {dpId, BigInteger.valueOf(portNo)}));
+    public static List<MatchInfo> getMatchInfoForVlanLPort(BigInteger dpId, long portNo, long vlanId,
+            boolean isVlanTransparent) {
+        List<MatchInfo> matches = new ArrayList<>();
+        matches.add(new MatchInPort(dpId, portNo));
         if (vlanId != 0 && !isVlanTransparent) {
-            matches.add(new MatchInfo(MatchFieldType.vlan_vid, new long[] { vlanId }));
+            matches.add(new MatchVlanVid((int) vlanId));
         }
         return matches;
     }
 
+    /**
+     * If matches contains MatchMetadata in its list and match is of type MatchMetadata, then this
+     * function will merge the MatchMetadatas using "or" of the masks and the values, otherwise it will add
+     * the match to the matches list.
+     *
+     * @param matches - matches list
+     * @param match - metadata or other match
+     */
+    public static void mergeMetadataMatchsOrAdd(List<MatchInfoBase> matches, MatchInfoBase match) {
+        Iterator<MatchInfoBase> iter = matches.iterator();
+        while (iter.hasNext()) {
+            MatchInfoBase match2 = iter.next();
+            if (match2 instanceof MatchMetadata) {
+                if (match instanceof MatchMetadata) {
+                    MatchMetadata metadataMatch = (MatchMetadata) match;
+                    BigInteger value = MetaDataUtil.mergeMetadataValues(((MatchMetadata) match2).getMetadata(),
+                            metadataMatch.getMetadata());
+                    BigInteger mask = MetaDataUtil.mergeMetadataMask(((MatchMetadata) match2).getMask(),
+                            metadataMatch.getMask());
+                    match = new MatchMetadata(value, mask);
+                    iter.remove();
+                }
+                break;
+            }
+        }
+        matches.add(match);
+    }
+
     public static short getVlanId(String interfaceName, DataBroker broker) {
         InstanceIdentifier<Interface> id = InstanceIdentifier.builder(Interfaces.class)
                 .child(Interface.class, new InterfaceKey(interfaceName)).build();
         Optional<Interface> ifInstance = MDSALUtil.read(LogicalDatastoreType.CONFIGURATION, id, broker);
         if (ifInstance.isPresent()) {
-            IfL2vlan vlanIface =ifInstance.get().getAugmentation(IfL2vlan.class);
-            short vlanId = vlanIface.getVlanId() == null ? 0 : vlanIface.getVlanId().getValue().shortValue();
-            return vlanId;
+            IfL2vlan vlanIface = ifInstance.get().getAugmentation(IfL2vlan.class);
+            return vlanIface.getVlanId() == null ? 0 : vlanIface.getVlanId().getValue().shortValue();
         }
         return -1;
     }
 
-    public static Set<Object> getStatRequestKeys(BigInteger dpId, short tableId, List<MatchInfo> matches, String flowId, long groupId) {
-        Set<Object> statRequestKeys = new HashSet<Object>();
+    public static Set<Object> getStatRequestKeys(BigInteger dpId, short tableId, List<MatchInfo> matches, String flowId,
+            long groupId) {
+        Set<Object> statRequestKeys = new HashSet<>();
         statRequestKeys.add(getFlowStatisticsKey(dpId, tableId, matches, flowId));
         statRequestKeys.add(getGroupStatisticsKey(dpId, groupId));
         return statRequestKeys;
@@ -98,16 +136,16 @@ public class InterfaceServiceUtil {
         return new GroupInfoKey(dpId, groupId);
     }
 
-    public static FlowInfoKey getFlowStatisticsKey(BigInteger dpId, short tableId, List<MatchInfo> matches, String flowId) {
+    public static FlowInfoKey getFlowStatisticsKey(BigInteger dpId, short tableId, List<MatchInfo> matches,
+            String flowId) {
         return new FlowInfoKey(dpId, tableId, MDSALUtil.buildMatches(matches), flowId);
     }
 
     public static List<MatchInfo> getLPortDispatcherMatches(short serviceIndex, int interfaceTag) {
-        List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
-        mkMatches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
+        List<MatchInfo> mkMatches = new ArrayList<>();
+        mkMatches.add(new MatchMetadata(
                  MetaDataUtil.getMetaDataForLPortDispatcher(interfaceTag, serviceIndex),
-                 MetaDataUtil.getMetaDataMaskForLPortDispatcher() }));
+                 MetaDataUtil.getMetaDataMaskForLPortDispatcher()));
         return mkMatches;
     }
-
 }