InstructionInfo redesign
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / impl / ItmUtils.java
index 1ca7132afb1baccca1b6fb3c6dc99340d9ae8f3e..41b0de91def73526d41bee2b47172beaf1374f2c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright © 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,
@@ -34,14 +34,14 @@ import org.opendaylight.genius.itm.api.IITMProvider;
 import org.opendaylight.genius.itm.confighelpers.HwVtep;
 import org.opendaylight.genius.itm.globals.ITMConstants;
 import org.opendaylight.genius.mdsalutil.ActionInfo;
-import org.opendaylight.genius.mdsalutil.ActionType;
 import org.opendaylight.genius.mdsalutil.FlowEntity;
 import org.opendaylight.genius.mdsalutil.InstructionInfo;
-import org.opendaylight.genius.mdsalutil.InstructionType;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
 import org.opendaylight.genius.mdsalutil.MatchFieldType;
 import org.opendaylight.genius.mdsalutil.MatchInfo;
 import org.opendaylight.genius.mdsalutil.NwConstants;
+import org.opendaylight.genius.mdsalutil.actions.ActionPuntToController;
+import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
 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.inet.types.rev130715.IpAddress;
@@ -96,6 +96,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPointsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPointsKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.tunnel.end.points.TzMembership;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.tunnel.end.points.TzMembershipBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnel;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnelBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnelKey;
@@ -131,10 +133,12 @@ public class ItmUtils {
     private static final Logger LOG = LoggerFactory.getLogger(ItmUtils.class);
 
     public static final FutureCallback<Void> DEFAULT_CALLBACK = new FutureCallback<Void>() {
+        @Override
         public void onSuccess(Void result) {
             LOG.debug("Success in Datastore write operation");
         }
 
+        @Override
         public void onFailure(Throwable error) {
             LOG.error("Error in Datastore write operation", error);
         }
@@ -142,17 +146,11 @@ public class ItmUtils {
 
     public static <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
                                                           InstanceIdentifier<T> path, DataBroker broker) {
-
-        ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
-
-        Optional<T> result = Optional.absent();
-        try {
-            result = tx.read(datastoreType, path).get();
+        try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
+            return tx.read(datastoreType, path).get();
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
-
-        return result;
     }
 
     public static <T extends DataObject> void asyncWrite(LogicalDatastoreType datastoreType,
@@ -240,12 +238,15 @@ public class ItmUtils {
         return new DPNTEPsInfoBuilder().setKey(new DPNTEPsInfoKey(dpId)).setTunnelEndPoints(endpoints).build();
     }
 
-    public static TunnelEndPoints createTunnelEndPoints(BigInteger dpnId, IpAddress ipAddress, String portName, int vlanId,
-                                                        IpPrefix prefix, IpAddress gwAddress, String zoneName, Class<? extends TunnelTypeBase>  tunnel_type) {
+    public static TunnelEndPoints createTunnelEndPoints(BigInteger dpnId, IpAddress ipAddress, String portName,
+                                                        boolean isOfTunnel, int vlanId, IpPrefix prefix,
+                                                        IpAddress gwAddress, List<TzMembership> zones,
+                                                        Class<? extends TunnelTypeBase>  tunnel_type) {
         // when Interface Mgr provides support to take in Dpn Id
         return new TunnelEndPointsBuilder().setKey(new TunnelEndPointsKey(ipAddress, portName,tunnel_type, vlanId))
-                .setSubnetMask(prefix).setGwIpAddress(gwAddress).setTransportZone(zoneName)
-                .setInterfaceName(ItmUtils.getInterfaceName(dpnId, portName, vlanId)).setTunnelType(tunnel_type).build();
+                .setSubnetMask(prefix).setGwIpAddress(gwAddress).setTzMembership(zones)
+                .setOptionOfTunnel(isOfTunnel).setInterfaceName(ItmUtils.getInterfaceName(dpnId, portName, vlanId))
+                .setTunnelType(tunnel_type).build();
     }
 
     public static DpnEndpoints createDpnEndpoints(List<DPNTEPsInfo> dpnTepInfo) {
@@ -259,8 +260,12 @@ public class ItmUtils {
         return id;
     }
 
-    public static Interface buildTunnelInterface(BigInteger dpn, String ifName, String desc, boolean enabled, Class<? extends TunnelTypeBase> tunType,
-                                                 IpAddress localIp, IpAddress remoteIp, IpAddress gatewayIp,Integer vlanId, boolean internal, Boolean monitorEnabled, Class<? extends TunnelMonitoringTypeBase> monitorProtocol, Integer monitorInterval) {
+    public static Interface buildTunnelInterface(BigInteger dpn, String ifName, String desc, boolean enabled,
+                                                 Class<? extends TunnelTypeBase> tunType, IpAddress localIp,
+                                                 IpAddress remoteIp, IpAddress gatewayIp, Integer vlanId,
+                                                 boolean internal, Boolean monitorEnabled,
+                                                 Class<? extends TunnelMonitoringTypeBase> monitorProtocol,
+                                                 Integer monitorInterval, boolean useOfTunnel) {
         InterfaceBuilder builder = new InterfaceBuilder().setKey(new InterfaceKey(ifName)).setName(ifName)
                 .setDescription(desc).setEnabled(enabled).setType(Tunnel.class);
         ParentRefs parentRefs = new ParentRefsBuilder().setDatapathNodeIdentifier(dpn).build();
@@ -272,14 +277,16 @@ public class ItmUtils {
         }
         LOG.debug("buildTunnelInterface: monitorProtocol = {} and monitorInterval = {}",monitorProtocol.getName(),monitorInterval);
 
-       
-        if(monitorInterval!= null)
+
+        if(monitorInterval != null) {
             monitoringInterval = monitorInterval.longValue();
-        
-        IfTunnel tunnel = new IfTunnelBuilder().setTunnelDestination(remoteIp).setTunnelGateway(
-                        gatewayIp).setTunnelSource(localIp)
-                        .setTunnelInterfaceType(tunType).setInternal(internal).setMonitorEnabled(
-                monitorEnabled).setMonitorProtocol(monitorProtocol).setMonitorInterval(monitoringInterval).build();
+        }
+
+        IfTunnel tunnel = new IfTunnelBuilder().setTunnelDestination(remoteIp).setTunnelGateway(gatewayIp)
+                .setTunnelSource(localIp).setTunnelInterfaceType(tunType).setInternal(internal)
+                .setMonitorEnabled(monitorEnabled).setMonitorProtocol(monitorProtocol)
+                .setMonitorInterval(monitoringInterval).setTunnelRemoteIpFlow(useOfTunnel)
+                .build();
         builder.addAugmentation(IfTunnel.class, tunnel);
         return builder.build();
     }
@@ -300,12 +307,15 @@ public class ItmUtils {
         Long monitoringInterval = (long) ITMConstants.DEFAULT_MONITOR_INTERVAL;
         Boolean monitoringEnabled = true;
         Class<? extends TunnelMonitoringTypeBase> monitoringProtocol = ITMConstants.DEFAULT_MONITOR_PROTOCOL;
-        if(monitor_interval!= null)
+        if(monitor_interval!= null) {
             monitoringInterval = monitor_interval.longValue();
-        if(monitor_enabled!=null  )
+        }
+        if(monitor_enabled!=null  ) {
             monitoringEnabled = monitor_enabled;
-        if(monitorProtocol!=null)
+        }
+        if(monitorProtocol!=null) {
             monitoringProtocol = monitorProtocol;
+        }
         IfTunnel tunnel = new IfTunnelBuilder().setTunnelDestination(destIp).setTunnelGateway(gWIp).setTunnelSource(
                 srcIp).setMonitorEnabled(monitoringEnabled).setMonitorProtocol(monitorProtocol).setMonitorInterval(100L).
                 setTunnelInterfaceType(tunType).setInternal(false).build();
@@ -340,8 +350,9 @@ public class ItmUtils {
 
         // Read the Mesh Information from Cache if not read from the DS
         dpnTEPs = getTunnelMeshInfo() ;
-        if( dpnTEPs != null )
+        if( dpnTEPs != null ) {
             return dpnTEPs ;
+        }
 
         // Read the EndPoint Info from the operational database
         InstanceIdentifierBuilder<DpnEndpoints> depBuilder = InstanceIdentifier.builder( DpnEndpoints.class);
@@ -351,8 +362,9 @@ public class ItmUtils {
             DpnEndpoints tn= dpnEps.get() ;
             dpnTEPs = tn.getDPNTEPsInfo();
             LOG.debug( "Read from CONFIGURATION datastore - No. of Dpns " , dpnTEPs.size() );
-        }else
+        } else {
             LOG.debug( "No Dpn information in CONFIGURATION datastore "  );
+        }
         return dpnTEPs ;
     }
 
@@ -414,8 +426,9 @@ public class ItmUtils {
         if( null != meshedDpnList) {
             for(BigInteger dpnId : dpnIds) {
                 for( DPNTEPsInfo teps : meshedDpnList ) {
-                    if( dpnId.equals(teps.getDPNID()))
+                    if( dpnId.equals(teps.getDPNID())) {
                         cfgDpnList.add( teps) ;
+                    }
                 }
             }
         }
@@ -426,8 +439,7 @@ public class ItmUtils {
         String logmsg = addFlag ? "Installing" : "Removing";
         LOG.trace( logmsg + " PUNT to Controller flow in DPN {} ", dpnId );
         List<ActionInfo> listActionInfo = new ArrayList<>();
-        listActionInfo.add(new ActionInfo(ActionType.punt_to_controller,
-                new String[] {}));
+        listActionInfo.add(new ActionPuntToController());
 
         try {
             List<MatchInfo> mkMatches = new ArrayList<>();
@@ -436,8 +448,7 @@ public class ItmUtils {
                     BigInteger.valueOf(ITMConstants.LLDP_SERVICE_ID) }));
 
             List<InstructionInfo> mkInstructions = new ArrayList<>();
-            mkInstructions.add(new InstructionInfo(InstructionType.apply_actions,
-                    listActionInfo));
+            mkInstructions.add(new InstructionApplyActions(listActionInfo));
 
             FlowEntity terminatingServiceTableFlowEntity = MDSALUtil
                     .buildFlowEntity(
@@ -448,10 +459,11 @@ public class ItmUtils {
                             0, 0, ITMConstants.COOKIE_ITM
                                     .add(BigInteger.valueOf(ITMConstants.LLDP_SERVICE_ID)),
                             mkMatches, mkInstructions);
-            if(addFlag)
+            if(addFlag) {
                 mdsalManager.installFlow(terminatingServiceTableFlowEntity);
-            else
+            } else {
                 mdsalManager.removeFlow(terminatingServiceTableFlowEntity);
+            }
         } catch (Exception e) {
             LOG.error("Error while setting up Table 36 for {}", dpnId, e);
         }
@@ -504,7 +516,7 @@ public class ItmUtils {
     private static void validateForSingleGreTep(String schemaName, List<BigInteger> lstDpnsForAdd,
                                                 List<VtepConfigSchema> existingSchemas) {
         for (VtepConfigSchema existingSchema : emptyIfNull(existingSchemas)) {
-            if ((TunnelTypeGre.class).equals(existingSchema.getTunnelType())
+            if (TunnelTypeGre.class.equals(existingSchema.getTunnelType())
                     && !StringUtils.equalsIgnoreCase(schemaName, existingSchema.getSchemaName())) {
                 List<BigInteger> lstConflictingDpns = new ArrayList<>(getDpnIdList(existingSchema.getDpnIds()));
                 lstConflictingDpns.retainAll(emptyIfNull(lstDpnsForAdd));
@@ -520,7 +532,7 @@ public class ItmUtils {
         Preconditions.checkNotNull(schema);
         Preconditions.checkArgument(StringUtils.isNotBlank(schema.getSchemaName()));
         Preconditions.checkArgument(StringUtils.isNotBlank(schema.getPortName()));
-        Preconditions.checkArgument((schema.getVlanId() >= 0 && schema.getVlanId() < 4095),
+        Preconditions.checkArgument(schema.getVlanId() >= 0 && schema.getVlanId() < 4095,
                 "Invalid VLAN ID, range (0-4094)");
         Preconditions.checkArgument(StringUtils.isNotBlank(schema.getTransportZoneName()));
         Preconditions.checkNotNull(schema.getSubnet());
@@ -570,10 +582,11 @@ public class ItmUtils {
         IpAddress gatewayIpObj = StringUtils.isBlank(gatewayIp) ? null : new IpAddress(gatewayIp.toCharArray());
         IpPrefix subnet = StringUtils.isBlank(subnetMask) ? null : new IpPrefix(subnetMask.toCharArray());
         Class<? extends TunnelTypeBase> tunType ;
-        if( tunnelType.equals(ITMConstants.TUNNEL_TYPE_VXLAN))
+        if( tunnelType.equals(ITMConstants.TUNNEL_TYPE_VXLAN)) {
             tunType = TunnelTypeVxlan.class ;
-        else
+        } else {
             tunType = TunnelTypeGre.class ;
+        }
         VtepConfigSchemaBuilder schemaBuilder = new VtepConfigSchemaBuilder().setSchemaName(schemaName)
                 .setPortName(portName).setVlanId(vlanId).setSubnet(subnet).setGatewayIp(gatewayIpObj)
                 .setTransportZoneName(transportZone).setTunnelType(tunType).setDpnIds(getDpnIdsListFromBigInt(dpnIds))
@@ -629,7 +642,7 @@ public class ItmUtils {
     private static int[] toIpArray(int val) {
         int[] ret = new int[4];
         for (int j = 3; j >= 0; --j) {
-            ret[j] |= ((val >>> 8 * (3 - j)) & (0xff));
+            ret[j] |= val >>> 8 * (3 - j) & 0xff;
         }
         return ret;
     }
@@ -687,10 +700,10 @@ public class ItmUtils {
         return schema;
     }
     public static String getSubnetCidrAsString(IpPrefix subnet) {
-        return (subnet == null) ? StringUtils.EMPTY : String.valueOf(subnet.getValue());
+        return subnet == null ? StringUtils.EMPTY : String.valueOf(subnet.getValue());
     }
     public static <T> List<T> emptyIfNull(List<T> list) {
-        return (list == null) ? Collections.emptyList() : list;
+        return list == null ? Collections.emptyList() : list;
     }
     public static <T> boolean isEmpty(Collection<T> collection) {
         return collection == null || collection.isEmpty();
@@ -757,17 +770,19 @@ public class ItmUtils {
     public static Boolean readMonitoringStateFromCache(DataBroker dataBroker) {
         InstanceIdentifier<TunnelMonitorParams> iid = InstanceIdentifier.create(TunnelMonitorParams.class);
         TunnelMonitorParams tunnelMonitorParams = (TunnelMonitorParams) DataStoreCache.get(ITMConstants.ITM_MONIRORING_PARAMS_CACHE_NAME,iid,"MonitorParams",dataBroker,true);
-        if(tunnelMonitorParams!=null)
+        if(tunnelMonitorParams!=null) {
             return tunnelMonitorParams.isEnabled();
-        else
+        } else {
             return ITMConstants.DEFAULT_MONITOR_ENABLED;
+        }
     }
 
     public static Integer readMonitorIntervalfromCache(DataBroker dataBroker) {
         InstanceIdentifier<TunnelMonitorInterval> iid = InstanceIdentifier.create(TunnelMonitorInterval.class);
         TunnelMonitorInterval tunnelMonitorIOptional = (TunnelMonitorInterval)DataStoreCache.get(ITMConstants.ITM_MONIRORING_PARAMS_CACHE_NAME,iid,"Interval",dataBroker,true);
-        if(tunnelMonitorIOptional!=null)
+        if(tunnelMonitorIOptional!=null) {
             return tunnelMonitorIOptional.getInterval();
+        }
         return null;
 
     }
@@ -777,10 +792,11 @@ public class ItmUtils {
         LOG.debug("determineMonitorInterval: monitorInterval from DS = {}", monitorInterval);
         if(monitorInterval==null){
             Class<? extends TunnelMonitoringTypeBase> monitorProtocol = determineMonitorProtocol(dataBroker);
-            if(monitorProtocol.isAssignableFrom(TunnelMonitoringTypeBfd.class))
+            if(monitorProtocol.isAssignableFrom(TunnelMonitoringTypeBfd.class)) {
                 monitorInterval = ITMConstants.BFD_DEFAULT_MONITOR_INTERVAL;
-            else
+            } else {
                 monitorInterval = ITMConstants.DEFAULT_MONITOR_INTERVAL;
+            }
         }
         LOG.debug("determineMonitorInterval: monitorInterval = {}", monitorInterval);
         InstanceIdentifier<TunnelMonitorInterval> iid = InstanceIdentifier.builder(TunnelMonitorInterval.class).build();
@@ -788,9 +804,9 @@ public class ItmUtils {
         ItmUtils.asyncUpdate(LogicalDatastoreType.OPERATIONAL,iid, intervalBuilder, dataBroker, ItmUtils.DEFAULT_CALLBACK);
         return monitorInterval;
     }
-    
+
     public static List<String> getInternalTunnelInterfaces(DataBroker dataBroker){
-        List<String> tunnelList = new ArrayList<String>();
+        List<String> tunnelList = new ArrayList<>();
         Collection<String> internalInterfaces = itmCache.getAllInternalInterfaces();
         if(internalInterfaces == null) {
             updateTunnelsCache(dataBroker);
@@ -810,10 +826,11 @@ public class ItmUtils {
         InstanceIdentifier<TransportZone> path = InstanceIdentifier.builder(TransportZones.class).
                 child(TransportZone.class, new TransportZoneKey(tzone)).build();
         Optional<TransportZone> tZoneOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
-        Class<? extends TunnelTypeBase> tunType = tZoneOptional.get().getTunnelType();
         if (tZoneOptional.isPresent()) {
-            if (tZoneOptional.get().getSubnets() != null && !tZoneOptional.get().getSubnets().isEmpty()) {
-                for (Subnets sub : tZoneOptional.get().getSubnets()) {
+            TransportZone transportZone = tZoneOptional.get();
+            Class<? extends TunnelTypeBase> tunType = transportZone.getTunnelType();
+            if (transportZone.getSubnets() != null && !transportZone.getSubnets().isEmpty()) {
+                for (Subnets sub : transportZone.getSubnets()) {
                     if (sub.getVteps() != null && !sub.getVteps().isEmpty()) {
                         for (Vteps vtepLocal : sub.getVteps()) {
                             for (Vteps vtepRemote : sub.getVteps()) {
@@ -825,8 +842,9 @@ public class ItmUtils {
                                     Optional<InternalTunnel> TunnelsOptional =
                                             ItmUtils.read(LogicalDatastoreType.CONFIGURATION, intIID, dataBroker);
                                     if (TunnelsOptional.isPresent()) {
-                                        LOG.trace("Internal Tunnel added {}",TunnelsOptional.get().getTunnelInterfaceName());
-                                        tunnels.add(TunnelsOptional.get().getTunnelInterfaceName());
+                                        String tunnelInterfaceName = TunnelsOptional.get().getTunnelInterfaceName();
+                                        LOG.trace("Internal Tunnel added {}", tunnelInterfaceName);
+                                        tunnels.add(tunnelInterfaceName);
                                     }
                                 }
                             }
@@ -842,13 +860,13 @@ public class ItmUtils {
                     }
                 }
             }
-        }
-        if (hwVtepsExist) {
-            for (HwVtep hwVtep : hwVteps) {
-                for (HwVtep hwVtepOther : hwVteps) {
-                    if (!hwVtep.getHwIp().equals(hwVtepOther.getHwIp())) {
-                        tunnels.add(getExtTunnel(hwVtep.getNode_id(), hwVtepOther.getNode_id(), tunType, dataBroker));
-                        tunnels.add(getExtTunnel(hwVtepOther.getNode_id(), hwVtep.getNode_id(), tunType, dataBroker));
+            if (hwVtepsExist) {
+                for (HwVtep hwVtep : hwVteps) {
+                    for (HwVtep hwVtepOther : hwVteps) {
+                        if (!hwVtep.getHwIp().equals(hwVtepOther.getHwIp())) {
+                            tunnels.add(getExtTunnel(hwVtep.getNode_id(), hwVtepOther.getNode_id(), tunType, dataBroker));
+                            tunnels.add(getExtTunnel(hwVtepOther.getNode_id(), hwVtep.getNode_id(), tunType, dataBroker));
+                        }
                     }
                 }
             }
@@ -863,22 +881,25 @@ public class ItmUtils {
                 child(TransportZone.class, new TransportZoneKey(tzone)).build();
         Optional<TransportZone> tZoneOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
         if (tZoneOptional.isPresent()) {
-            if (tZoneOptional.get().getSubnets() != null && !tZoneOptional.get().getSubnets().isEmpty()) {
-                for (Subnets sub : tZoneOptional.get().getSubnets()) {
+            TransportZone transportZone = tZoneOptional.get();
+            if (transportZone.getSubnets() != null && !transportZone.getSubnets().isEmpty()) {
+                for (Subnets sub : transportZone.getSubnets()) {
                     if (sub.getVteps() != null && !sub.getVteps().isEmpty()) {
                         for (Vteps vtepLocal : sub.getVteps()) {
                             for (Vteps vtepRemote : sub.getVteps()) {
                                 if (!vtepLocal.equals(vtepRemote)) {
-                                    InternalTunnelKey key = new InternalTunnelKey(vtepRemote.getDpnId(), vtepLocal.getDpnId(), tZoneOptional.get().getTunnelType());
+                                    InternalTunnelKey key =
+                                            new InternalTunnelKey(vtepRemote.getDpnId(), vtepLocal.getDpnId(),
+                                                    transportZone.getTunnelType());
                                     InstanceIdentifier<InternalTunnel> intIID =
                                             InstanceIdentifier.builder(TunnelList.class).
                                                     child(InternalTunnel.class, key).build();
-                                    Optional<InternalTunnel> TunnelsOptional =
+                                    Optional<InternalTunnel> tunnelsOptional =
                                             ItmUtils.read(LogicalDatastoreType.CONFIGURATION, intIID, dataBroker);
-                                    if (TunnelsOptional.isPresent()) {
-                                        LOG.trace("Internal Tunnel added {}",
-                                                TunnelsOptional.get().getTunnelInterfaceName());
-                                        tunnels.add(TunnelsOptional.get().getTunnelInterfaceName());
+                                    if (tunnelsOptional.isPresent()) {
+                                        String tunnelInterfaceName = tunnelsOptional.get().getTunnelInterfaceName();
+                                        LOG.trace("Internal Tunnel added {}", tunnelInterfaceName);
+                                        tunnels.add(tunnelInterfaceName);
                                     }
                                 }
                             }
@@ -895,11 +916,12 @@ public class ItmUtils {
         ExternalTunnelKey key = getExternalTunnelKey(dpId, node_id, tunType);
         InstanceIdentifier<ExternalTunnel> intIID = InstanceIdentifier.builder(ExternalTunnelList.class).
                 child(ExternalTunnel.class, key).build();
-        Optional<ExternalTunnel> TunnelsOptional =
+        Optional<ExternalTunnel> tunnelsOptional =
                 ItmUtils.read(LogicalDatastoreType.CONFIGURATION, intIID, dataBroker);
-        if (TunnelsOptional.isPresent()) {
-            LOG.trace("ext tunnel returned {} ",TunnelsOptional.get().getTunnelInterfaceName());
-            return TunnelsOptional.get().getTunnelInterfaceName();
+        if (tunnelsOptional.isPresent()) {
+            String tunnelInterfaceName = tunnelsOptional.get().getTunnelInterfaceName();
+            LOG.trace("ext tunnel returned {} ", tunnelInterfaceName);
+            return tunnelInterfaceName;
         }
         return null;
     }
@@ -923,11 +945,7 @@ public class ItmUtils {
     }
     public static TunnelList getAllInternalTunnels(DataBroker broker) {
         InstanceIdentifier<TunnelList> tunnelListInstanceIdentifier = InstanceIdentifier.builder(TunnelList.class).build();
-        Optional<TunnelList> tunnelList = read(LogicalDatastoreType.CONFIGURATION, tunnelListInstanceIdentifier, broker);
-        if (tunnelList.isPresent()) {
-            return tunnelList.get();
-        }
-        return null;
+        return read(LogicalDatastoreType.CONFIGURATION, tunnelListInstanceIdentifier, broker).orNull();
     }
     public static InternalTunnel getInternalTunnel(String interfaceName, DataBroker broker) {
         InternalTunnel internalTunnel = null;
@@ -968,18 +986,19 @@ public class ItmUtils {
     }
     public static String convertTunnelTypetoString(Class<? extends TunnelTypeBase> tunType ) {
         String tunnelType = ITMConstants.TUNNEL_TYPE_VXLAN;
-        if( tunType.equals(TunnelTypeVxlan.class))
+        if( tunType.equals(TunnelTypeVxlan.class)) {
             tunnelType = ITMConstants.TUNNEL_TYPE_VXLAN ;
-        else if( tunType.equals(TunnelTypeGre.class) )
+        } else if( tunType.equals(TunnelTypeGre.class) ) {
             tunnelType = ITMConstants.TUNNEL_TYPE_GRE ;
-        else if (tunnelType.equals(TunnelTypeMplsOverGre.class))
+        } else if (tunnelType.equals(TunnelTypeMplsOverGre.class)) {
             tunnelType = ITMConstants.TUNNEL_TYPE_MPLSoGRE;
+        }
         return tunnelType ;
     }
 
 
     public static boolean isItmIfType(Class<? extends InterfaceType> ifType) {
-        return (ifType != null) && (ifType.isAssignableFrom(Tunnel.class));
+        return ifType != null && ifType.isAssignableFrom(Tunnel.class);
     }
 
     public static StateTunnelListKey getTunnelStateKey( org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface iface) {
@@ -1046,20 +1065,23 @@ public class ItmUtils {
     public static Class<? extends TunnelMonitoringTypeBase> readMonitoringProtocolFromCache(DataBroker dataBroker) {
         InstanceIdentifier<TunnelMonitorParams> iid = InstanceIdentifier.create(TunnelMonitorParams.class);
         TunnelMonitorParams tunnelMonitorParams = (TunnelMonitorParams) DataStoreCache.get(ITMConstants.ITM_MONIRORING_PARAMS_CACHE_NAME,iid,"MonitorParams",dataBroker,true);
-        if(tunnelMonitorParams!=null)
+        if(tunnelMonitorParams!=null) {
             return tunnelMonitorParams.getMonitorProtocol();
+        }
         return null;
     }
 
     public static Class<? extends TunnelMonitoringTypeBase> determineMonitorProtocol(DataBroker dataBroker) {
         Class<? extends TunnelMonitoringTypeBase> monitoringProtocol = ItmUtils.readMonitoringProtocolFromCache(dataBroker);
         LOG.debug("determineMonitorProtocol: monitorProtocol from DS = {}", monitoringProtocol);
-        if(monitoringProtocol==null)
+        if(monitoringProtocol==null) {
             monitoringProtocol = ITMConstants.DEFAULT_MONITOR_PROTOCOL;
+        }
         LOG.debug("determineMonitorProtocol: monitorProtocol = {}", monitoringProtocol);
         Boolean monitorState = ItmUtils.readMonitoringStateFromCache(dataBroker);
-        if(monitorState==null)
+        if(monitorState==null) {
             monitorState = true;
+        }
         LOG.debug("determineMonitorProtocol: monitorState = {}", monitorState);
         InstanceIdentifier<TunnelMonitorParams> iid = InstanceIdentifier.builder(TunnelMonitorParams.class).build();
         TunnelMonitorParams protocolBuilder = new TunnelMonitorParamsBuilder().setEnabled(monitorState).setMonitorProtocol(monitoringProtocol).build();
@@ -1073,9 +1095,34 @@ public class ItmUtils {
         if(dcGatewayIpListConfig.isPresent()){
             DcGatewayIpList containerList = dcGatewayIpListConfig.get();
             if(containerList != null){
-                return (containerList.getDcGatewayIp());
+                return containerList.getDcGatewayIp();
             }
         }
         return null;
     }
+
+    public static boolean falseIfNull(Boolean value) {
+        return value == null ? false : value;
+    }
+
+    public static <T> List<T> getIntersection(List<T> list1, List<T> list2) {
+        List<T> list = new ArrayList<>();
+        for (T t : list1) {
+            if(list2.contains(t)) {
+                list.add(t);
+            }
+        }
+        return list;
+    }
+
+    public static void addTransportZoneMembership(List<TzMembership> zones, String zoneName) {
+        zones.add(new TzMembershipBuilder().setZoneName(zoneName).build());
+    }
+
+    public static  List<TzMembership> createTransportZoneMembership(String zoneName) {
+        List<TzMembership> zones = new ArrayList<>();
+        zones.add(new TzMembershipBuilder().setZoneName(zoneName).build());
+        return zones;
+    }
+
 }