Merge "fix blue print to init InterfaceStateToTansportZoneListener"
[netvirt.git] / vpnservice / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / utils / AclServiceUtils.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.netvirt.aclservice.utils;
10
11 import com.google.common.base.Optional;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.Future;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.opendaylight.genius.mdsalutil.MDSALUtil;
25 import org.opendaylight.genius.mdsalutil.MatchFieldType;
26 import org.opendaylight.genius.mdsalutil.MatchInfo;
27 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
28 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
29 import org.opendaylight.genius.mdsalutil.NwConstants;
30 import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
31 import org.opendaylight.genius.mdsalutil.NxMatchInfo;
32 import org.opendaylight.genius.mdsalutil.packet.IPProtocols;
33 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.AccessLists;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.Ipv4Acl;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.AclKey;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetDpidFromInterfaceInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetDpidFromInterfaceInputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetDpidFromInterfaceOutput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceBindings;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceTypeFlowBased;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflow;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflowBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfoKey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesKey;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.InterfaceAcl;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.IpPrefixOrAddress;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
66 import org.opendaylight.yangtools.yang.binding.DataObject;
67 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
68 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
69 import org.opendaylight.yangtools.yang.common.RpcResult;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
72
73 @SuppressWarnings("deprecation")
74 public final class AclServiceUtils {
75
76     private static final Logger LOG = LoggerFactory.getLogger(AclServiceUtils.class);
77
78     private AclServiceUtils() { }
79
80     /**
81      * Retrieves the Interface from the datastore.
82      * @param broker the data broker
83      * @param interfaceName the interface name
84      * @return the interface.
85      */
86     public static Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
87         .Interface> getInterface(DataBroker broker, String interfaceName) {
88         return read(broker, LogicalDatastoreType.CONFIGURATION, getInterfaceIdentifier(interfaceName));
89     }
90
91     /**
92      * Builds the interface identifier.
93      * @param interfaceName the interface name.
94      * @return the interface identifier.
95      */
96     public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508
97         .interfaces.Interface> getInterfaceIdentifier(String interfaceName) {
98         return InstanceIdentifier.builder(Interfaces.class)
99                 .child(
100                     org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
101                     .Interface.class, new InterfaceKey(interfaceName)).build();
102     }
103
104     /**
105      * Retrieves the object from the datastore.
106      * @param broker the data broker.
107      * @param datastoreType the data store type.
108      * @param path the wild card path.
109      * @return the required object.
110      */
111     public static <T extends DataObject> Optional<T> read(
112             DataBroker broker, LogicalDatastoreType datastoreType, InstanceIdentifier<T> path) {
113
114         Optional<T> result = Optional.absent();
115         ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
116         try {
117             result = tx.read(datastoreType, path).checkedGet();
118         } catch (ReadFailedException e) {
119             LOG.warn("Failed to read InstanceIdentifier {} from {}", path, datastoreType, e);
120         } finally {
121             tx.close();
122         }
123         return result;
124     }
125
126     /**
127      * Retrieves the acl matching the key from the data store.
128      *
129      * @param broker the data broker
130      * @param aclKey the acl key
131      * @return the acl
132      */
133     public static Acl getAcl(DataBroker broker, String aclKey) {
134         Optional<Acl> optAcl = read(broker,
135             LogicalDatastoreType.CONFIGURATION, getAclInstanceIdentifier(aclKey));
136         if (optAcl.isPresent()) {
137             return optAcl.get();
138         }
139         return null;
140     }
141
142     /** Creates the Acl instance identifier.
143      *
144      * @param aclKey the acl key
145      * @return the instance identifier
146      */
147     public static InstanceIdentifier<Acl> getAclInstanceIdentifier(String aclKey) {
148         return InstanceIdentifier
149                 .builder(AccessLists.class)
150                 .child(Acl.class,
151                         new AclKey(aclKey,Ipv4Acl.class))
152                 .build();
153     }
154
155     /**
156      * Get the data path number for the interface.
157      * @param interfaceManagerRpcService interfaceManagerRpcService instance.
158      * @param ifName the interface name.
159      * @return the dpn.
160      */
161     public static BigInteger getDpnForInterface(OdlInterfaceRpcService interfaceManagerRpcService, String ifName) {
162         BigInteger nodeId = BigInteger.ZERO;
163         try {
164             GetDpidFromInterfaceInput dpIdInput =
165                     new GetDpidFromInterfaceInputBuilder().setIntfName(ifName).build();
166             Future<RpcResult<GetDpidFromInterfaceOutput>> dpIdOutput =
167                     interfaceManagerRpcService.getDpidFromInterface(dpIdInput);
168             RpcResult<GetDpidFromInterfaceOutput> dpIdResult = dpIdOutput.get();
169             if (dpIdResult.isSuccessful()) {
170                 nodeId = dpIdResult.getResult().getDpid();
171             } else {
172                 LOG.error("Could not retrieve DPN Id for interface {}", ifName);
173             }
174         } catch (NullPointerException | InterruptedException | ExecutionException e) {
175             LOG.error("Exception when getting dpn for interface {}", ifName,  e);
176         }
177         return nodeId;
178     }
179
180     /**
181      * Retrieves the interface state.
182      * @param dataBroker the data broker.
183      * @param interfaceName the interface name.
184      * @return the interface state.
185      */
186     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
187         .Interface getInterfaceStateFromOperDS(DataBroker dataBroker, String interfaceName) {
188         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508
189             .interfaces.state.Interface> ifStateId = buildStateInterfaceId(interfaceName);
190         Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508
191             .interfaces.state.Interface> ifStateOptional = MDSALUtil.read(LogicalDatastoreType
192                 .OPERATIONAL, ifStateId, dataBroker);
193         if (!ifStateOptional.isPresent()) {
194             return null;
195         }
196
197         return ifStateOptional.get();
198     }
199
200     /**
201      * Build the interface state.
202      * @param interfaceName the interface name.
203      * @return the interface state.
204      */
205     public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508
206         .interfaces.state.Interface> buildStateInterfaceId(String interfaceName) {
207         InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508
208             .interfaces.state.Interface> idBuilder = InstanceIdentifier.builder(InterfacesState.class)
209             .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
210             .state.Interface.class, new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces
211             .rev140508.interfaces.state.InterfaceKey(interfaceName));
212         return idBuilder.build();
213     }
214
215     /**
216      * Checks whether port security is enabled for the port.
217      * @param port the port.
218      * @return the port security is enabled/not.
219      */
220     public static boolean isPortSecurityEnabled(AclInterface port) {
221         return port.isPortSecurityEnabled();
222     }
223
224     /**
225      * Checks whether port security is enabled for the port.
226      * @param port the port.
227      * @return the list of security groups.
228      */
229     public static List<Uuid> getInterfaceAcls(Interface port) {
230         if (port == null) {
231             LOG.error("Port is Null");
232             return null;
233         }
234         InterfaceAcl aclInPort = port.getAugmentation(InterfaceAcl.class);
235         if (aclInPort == null) {
236             LOG.error("getSecurityGroupInPortList: no security group associated}",
237                 port.getName());
238             return null;
239         }
240         return aclInPort.getSecurityGroups();
241     }
242
243     /**
244      * Retrieves the security rule attribute augmentation from the access list.
245      * @param ace the access list entry
246      * @return the security rule attributes
247      */
248     public static SecurityRuleAttr  getAccesssListAttributes(Ace ace) {
249         if (ace == null) {
250             LOG.error("Ace is Null");
251             return null;
252         }
253         SecurityRuleAttr aceAttributes = ace.getAugmentation(SecurityRuleAttr.class);
254         if (aceAttributes == null) {
255             LOG.error("Ace is null");
256             return null;
257         }
258         return aceAttributes;
259     }
260
261     /**
262      * Returns the DHCP match.
263      *
264      * @param srcPort the source port.
265      * @param dstPort the destination port.
266      * @param lportTag the lport tag
267      * @return list of matches.
268      */
269     public static List<MatchInfoBase> buildDhcpMatches(int srcPort, int dstPort, int lportTag) {
270         List<MatchInfoBase> matches = new ArrayList<>(6);
271         matches.add(new MatchInfo(MatchFieldType.eth_type,
272                 new long[] { NwConstants.ETHTYPE_IPV4 }));
273         matches.add(new MatchInfo(MatchFieldType.ip_proto,
274                 new long[] { IPProtocols.UDP.intValue() }));
275         matches.add(new MatchInfo(MatchFieldType.udp_dst,
276                 new long[] { dstPort }));
277         matches.add(new MatchInfo(MatchFieldType.udp_src,
278                 new long[] { srcPort}));
279         matches.add(AclServiceUtils.buildLPortTagMatch(lportTag));
280         return matches;
281     }
282
283     /**
284      * Builds the service id.
285      *
286      * @param interfaceName the interface name
287      * @param serviceIndex the service index
288      * @param serviceMode the service mode
289      * @return the instance identifier
290      */
291     public static InstanceIdentifier<BoundServices> buildServiceId(String interfaceName, short serviceIndex,
292             Class<? extends ServiceModeBase> serviceMode) {
293         return InstanceIdentifier.builder(ServiceBindings.class)
294                 .child(ServicesInfo.class, new ServicesInfoKey(interfaceName, serviceMode))
295                 .child(BoundServices.class, new BoundServicesKey(serviceIndex)).build();
296     }
297
298     /**
299      * Gets the bound services.
300      *
301      * @param serviceName the service name
302      * @param servicePriority the service priority
303      * @param flowPriority the flow priority
304      * @param cookie the cookie
305      * @param instructions the instructions
306      * @return the bound services
307      */
308     public static BoundServices getBoundServices(String serviceName, short servicePriority, int flowPriority,
309             BigInteger cookie, List<Instruction> instructions) {
310         StypeOpenflowBuilder augBuilder = new StypeOpenflowBuilder().setFlowCookie(cookie).setFlowPriority(flowPriority)
311                 .setInstruction(instructions);
312         return new BoundServicesBuilder().setKey(new BoundServicesKey(servicePriority)).setServiceName(serviceName)
313                 .setServicePriority(servicePriority).setServiceType(ServiceTypeFlowBased.class)
314                 .addAugmentation(StypeOpenflow.class, augBuilder.build()).build();
315     }
316
317     public static List<Uuid> getUpdatedAclList(List<Uuid> updatedAclList, List<Uuid> currentAclList) {
318         if (updatedAclList == null) {
319             return null;
320         }
321         List<Uuid> newAclList = new ArrayList<>(updatedAclList);
322         if (currentAclList == null) {
323             return newAclList;
324         }
325         List<Uuid> origAclList = new ArrayList<>(currentAclList);
326         for (Iterator<Uuid> iterator = newAclList.iterator(); iterator.hasNext();) {
327             Uuid updatedAclUuid = iterator.next();
328             for (Uuid currentAclUuid :origAclList) {
329                 if (updatedAclUuid.getValue().equals(currentAclUuid.getValue())) {
330                     iterator.remove();
331                 }
332             }
333         }
334         return newAclList;
335     }
336
337     public static List<AllowedAddressPairs> getUpdatedAllowedAddressPairs(
338             List<AllowedAddressPairs> updatedAllowedAddressPairs,
339             List<AllowedAddressPairs> currentAllowedAddressPairs) {
340         if (updatedAllowedAddressPairs == null) {
341             return null;
342         }
343         List<AllowedAddressPairs> newAllowedAddressPairs = new ArrayList<>(updatedAllowedAddressPairs);
344         if (currentAllowedAddressPairs == null) {
345             return newAllowedAddressPairs;
346         }
347         List<AllowedAddressPairs> origAllowedAddressPairs = new ArrayList<>(currentAllowedAddressPairs);
348         for (Iterator<AllowedAddressPairs> iterator = newAllowedAddressPairs.iterator(); iterator.hasNext();) {
349             AllowedAddressPairs updatedAllowedAddressPair = iterator.next();
350             for (AllowedAddressPairs currentAllowedAddressPair : origAllowedAddressPairs) {
351                 if (updatedAllowedAddressPair.getKey().equals(currentAllowedAddressPair.getKey())) {
352                     iterator.remove();
353                     break;
354                 }
355             }
356         }
357         return newAllowedAddressPairs;
358     }
359
360     public static List<AllowedAddressPairs> getPortAllowedAddresses(Interface port) {
361         if (port == null) {
362             LOG.error("Port is Null");
363             return null;
364         }
365         InterfaceAcl aclInPort = port.getAugmentation(InterfaceAcl.class);
366         if (aclInPort == null) {
367             LOG.error("getSecurityGroupInPortList: no security group associated to Interface port: {}", port.getName());
368             return null;
369         }
370         return aclInPort.getAllowedAddressPairs();
371     }
372
373     public static BigInteger getDpIdFromIterfaceState(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
374             .interfaces.rev140508.interfaces.state.Interface interfaceState) {
375         BigInteger dpId = null;
376         List<String> ofportIds = interfaceState.getLowerLayerIf();
377         if (ofportIds != null && !ofportIds.isEmpty()) {
378             NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
379             dpId = BigInteger.valueOf(MDSALUtil.getDpnIdFromPortName(nodeConnectorId));
380         }
381         return dpId;
382     }
383
384     /**
385      * Builds the ip matches.
386      *
387      * @param ipPrefixOrAddress the ip prefix or address
388      * @param ipv4MatchType the ipv4 match type
389      * @return the list
390      */
391     public static List<MatchInfoBase> buildIpMatches(IpPrefixOrAddress ipPrefixOrAddress,
392             MatchFieldType ipv4MatchType) {
393         List<MatchInfoBase> flowMatches = new ArrayList<>();
394         flowMatches.add(new MatchInfo(MatchFieldType.eth_type, new long[] {NwConstants.ETHTYPE_IPV4}));
395         IpPrefix ipPrefix = ipPrefixOrAddress.getIpPrefix();
396         if (ipPrefix != null) {
397             if (ipPrefix.getIpv4Prefix().getValue() != null) {
398                 String[] ipaddressValues = ipPrefix.getIpv4Prefix().getValue().split("/");
399                 flowMatches.add(new MatchInfo(ipv4MatchType, new String[] {ipaddressValues[0], ipaddressValues[1]}));
400             } else {
401                 // Handle IPv6
402             }
403         } else {
404             IpAddress ipAddress = ipPrefixOrAddress.getIpAddress();
405             if (ipAddress.getIpv4Address() != null) {
406                 flowMatches
407                         .add(new MatchInfo(ipv4MatchType, new String[] {ipAddress.getIpv4Address().getValue(), "32"}));
408             } else {
409                 // Handle IPv6
410             }
411         }
412         return flowMatches;
413     }
414
415     /**
416      * Gets the lport tag match.
417      *
418      * @param lportTag the lport tag
419      * @return the lport tag match
420      */
421     public static MatchInfo buildLPortTagMatch(int lportTag) {
422         return new MatchInfo(MatchFieldType.metadata,
423                 new BigInteger[] {MetaDataUtil.getLportTagMetaData(lportTag), MetaDataUtil.METADATA_MASK_LPORT_TAG});
424     }
425
426     public static List<Ace> getAceWithRemoteAclId(DataBroker dataBroker, AclInterface port, Uuid remoteAcl) {
427         List<Ace> remoteAclRuleList = new ArrayList<>();
428         List<Uuid> aclList = port.getSecurityGroups();
429         for (Uuid aclId : aclList) {
430             Acl acl = getAcl(dataBroker, aclId.getValue());
431             List<Ace> aceList = acl.getAccessListEntries().getAce();
432             for (Ace ace : aceList) {
433                 Uuid tempRemoteAcl = getAccesssListAttributes(ace).getRemoteGroupId();
434                 if (tempRemoteAcl != null && tempRemoteAcl.equals(remoteAcl)) {
435                     remoteAclRuleList.add(ace);
436                 }
437             }
438         }
439         return remoteAclRuleList;
440     }
441
442     public static Map<String, List<MatchInfoBase>> getFlowForRemoteAcl(Uuid remoteAclId, String ignoreInterfaceId,
443                                                                        Map<String, List<MatchInfoBase>>
444                                                                                flowMatchesMap, boolean
445                                                                                isSourceIpMacMatch) {
446         List<AclInterface> interfaceList = AclDataUtil.getInterfaceList(remoteAclId);
447         if (flowMatchesMap == null || interfaceList == null || interfaceList.isEmpty()) {
448             return null;
449         }
450         Map<String, List<MatchInfoBase>> updatedFlowMatchesMap = new HashMap<>();
451         for (String flowName : flowMatchesMap.keySet()) {
452             List<MatchInfoBase> flows = flowMatchesMap.get(flowName);
453             for (AclInterface port : interfaceList) {
454                 if (port.getInterfaceId().equals(ignoreInterfaceId)) {
455                     continue;
456                 }
457                 //get allow address pair
458                 List<AllowedAddressPairs> allowedAddressPair = port.getAllowedAddressPairs();
459                 // iterate over allow address pair and update match type
460                 for (AllowedAddressPairs aap : allowedAddressPair) {
461                     List<MatchInfoBase> matchInfoBaseList = updateAAPMatches(isSourceIpMacMatch, flows, aap);
462                     String flowId = flowName + "_remoteACL_interface_" + port.getInterfaceId() + "_aap_" + aap.getKey();
463                     updatedFlowMatchesMap.put(flowId, matchInfoBaseList);
464                 }
465
466             }
467
468         }
469         return updatedFlowMatchesMap;
470     }
471
472     public static Map<String, List<MatchInfoBase>> getFlowForAllowedAddresses(List<AllowedAddressPairs>
473                                                                                       syncAllowedAddresses,
474                                                                               Map<String, List<MatchInfoBase>>
475                                                                                       flowMatchesMap, boolean
476                                                                                       isSourceIpMacMatch) {
477         if (flowMatchesMap == null) {
478             return null;
479         }
480         Map<String, List<MatchInfoBase>> updatedFlowMatchesMap = new HashMap<>();
481         for (String flowName : flowMatchesMap.keySet()) {
482             List<MatchInfoBase> flows = flowMatchesMap.get(flowName);
483             // iterate over allow address pair and update match type
484             for (AllowedAddressPairs aap : syncAllowedAddresses) {
485                 List<MatchInfoBase> matchInfoBaseList = updateAAPMatches(isSourceIpMacMatch, flows, aap);
486                 String flowId = flowName + "_remoteACL_interface_aap_" + aap.getKey();
487                 updatedFlowMatchesMap.put(flowId, matchInfoBaseList);
488             }
489
490         }
491         return updatedFlowMatchesMap;
492     }
493
494     private static List<MatchInfoBase> updateAAPMatches(boolean isSourceIpMacMatch, List<MatchInfoBase> flows,
495                                                         AllowedAddressPairs aap) {
496         List<MatchInfoBase> matchInfoBaseList;
497         if (isSourceIpMacMatch) {
498             flows.remove(MatchFieldType.ipv4_source);
499             matchInfoBaseList = AclServiceUtils.buildIpMatches(aap.getIpAddress(), MatchFieldType.ipv4_source);
500         } else {
501             flows.remove(MatchFieldType.ipv4_destination);
502             matchInfoBaseList = AclServiceUtils.buildIpMatches(aap.getIpAddress(), MatchFieldType.ipv4_destination);
503         }
504         matchInfoBaseList.addAll(flows);
505         return matchInfoBaseList;
506     }
507
508     public static MatchInfoBase popMatchInfoByType(List<MatchInfoBase> flows, MatchFieldType type) {
509         MatchInfoBase mib = getMatchInfoByType(flows, type);
510         if (mib != null) {
511             flows.remove(mib);
512         }
513         return mib;
514     }
515
516     public static MatchInfoBase getMatchInfoByType(List<MatchInfoBase> flows, MatchFieldType type) {
517         for (MatchInfoBase mib : flows) {
518             if (mib instanceof MatchInfo) {
519                 if (((MatchInfo)mib).getMatchField() == type) {
520                     return mib;
521                 }
522             }
523         }
524         return null;
525     }
526
527     public static MatchInfoBase getMatchInfoByType(List<MatchInfoBase> flows, NxMatchFieldType type) {
528         for (MatchInfoBase mib : flows) {
529             if (mib instanceof NxMatchInfo) {
530                 if (((NxMatchInfo)mib).getMatchField() == type) {
531                     return mib;
532                 }
533             }
534         }
535         return null;
536     }
537
538     public static boolean containsMatchFieldType(List<MatchInfoBase> flows, MatchFieldType type) {
539         MatchInfoBase mib = getMatchInfoByType(flows, type);
540         if (mib != null) {
541             return true;
542         }
543         return false;
544     }
545
546     public static boolean containsMatchFieldType(List<MatchInfoBase> flows, NxMatchFieldType type) {
547         MatchInfoBase mib = getMatchInfoByType(flows, type);
548         if (mib != null) {
549             return true;
550         }
551         return false;
552     }
553 }