Make some classes final and not instantiables
[genius.git] / interfacemanager / interfacemanager-api / src / main / java / org / opendaylight / genius / interfacemanager / globals / InterfaceServiceUtil.java
index 0ef44a5a6d48f36f903226350f01806e2e5c3aa1..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,
@@ -12,6 +12,7 @@ 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;
@@ -20,6 +21,7 @@ import org.opendaylight.genius.mdsalutil.FlowInfoKey;
 import org.opendaylight.genius.mdsalutil.GroupInfoKey;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
 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;
@@ -41,7 +43,10 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.ser
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesKey;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
-public class InterfaceServiceUtil {
+public final class InterfaceServiceUtil {
+
+    private InterfaceServiceUtil() {
+    }
 
     public static ServicesInfo buildServiceInfo(String serviceName, short serviceIndex, int servicePriority,
             BigInteger cookie, List<Instruction> instructions) {
@@ -80,6 +85,34 @@ public class InterfaceServiceUtil {
         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();