Bug 7142 - all VpnPortIpToPort entries are lost from ODL cache after
[netvirt.git] / vpnservice / natservice / natservice-impl / src / main / java / org / opendaylight / netvirt / natservice / internal / NatUtil.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.natservice.internal;
10
11 import java.math.BigInteger;
12
13 import com.google.common.collect.Lists;
14 import com.google.common.collect.Sets;
15
16 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
17 import org.opendaylight.genius.mdsalutil.ActionInfo;
18 import org.opendaylight.genius.mdsalutil.ActionType;
19 import org.opendaylight.genius.mdsalutil.FlowEntity;
20 import org.opendaylight.genius.mdsalutil.MDSALUtil;
21 import org.opendaylight.genius.mdsalutil.MatchInfo;
22 import org.opendaylight.genius.mdsalutil.NwConstants;
23 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
24 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
25 import org.opendaylight.netvirt.neutronvpn.api.utils.NeutronConstants;
26 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
27 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
28 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.VpnInstanceOpData;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.VpnInstanceToVpnId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.LearntVpnVipToPortData;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.learnt.vpn.vip.to.port.data.LearntVpnVipToPort;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.learnt.vpn.vip.to.port.data.LearntVpnVipToPortKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetDpidFromInterfaceInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetDpidFromInterfaceInputBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetDpidFromInterfaceOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceInputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ExtRouters;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ExternalIpsCounter;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ExternalNetworks;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.FloatingIpPortInfo;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProviderTypes;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.FloatingIpInfo;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpMap;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpPortMap;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.NaptSwitches;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProtocolTypes;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.RouterIdName;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.RouterToVpnMapping;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.SnatintIpPortMap;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.RoutersKey;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.external.ips.counter.ExternalCounters;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.external.ips.counter.ExternalCountersKey;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.external.ips.counter.external.counters.ExternalIpCounter;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.router.ports.ports.InternalToExternalPortMap;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.router.ports.ports.InternalToExternalPortMapKey;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.port.info.FloatingIpIdToPortMapping;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.port.info.FloatingIpIdToPortMappingKey;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMap;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.IntextIpProtocolType;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.IntextIpProtocolTypeKey;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.IpPortMap;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.IpPortMapKey;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.ip.port.map.IpPortExternal;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.router.id.name.RouterIds;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.router.id.name.RouterIdsKey;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.router.to.vpn.mapping.Routermapping;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.router.to.vpn.mapping.RoutermappingKey;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.IntipPortMap;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.IntipPortMapKey;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.IpPort;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.IpPortKey;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoType;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoTypeKey;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.NeutronRouterDpns;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.VpnIdToVpnInstance;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.NetworkMaps;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.NeutronVpnPortipPortData;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.VpnMaps;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.networkmaps.NetworkMap;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.networkmaps.NetworkMapKey;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.neutron.vpn.portip.port.data.VpnPortipToPort;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.neutron.vpn.portip.port.data.VpnPortipToPortKey;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.RouterDpnList;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.RouterDpnListKey;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.RouterDpnListBuilder;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.DpnVpninterfacesList;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.SubnetmapKey;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.vpnmaps.VpnMap;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.vpnmaps.VpnMapKey;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetKey;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.add.group.input.buckets.bucket.action.action.NxActionResubmitRpcAddGroupCase;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nodes.node.table.flow.instructions.instruction.instruction.apply.actions._case.apply.actions.action.action.NxActionRegLoadNodesNodeTableFlowApplyActionsCase;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.reg.load.grouping.NxRegLoad;
115 import org.opendaylight.yangtools.yang.binding.DataObject;
116 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
117 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
118 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
119 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
120 import com.google.common.base.Optional;
121 import org.opendaylight.netvirt.bgpmanager.api.IBgpManager;
122 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.external.networks.Networks;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.external.networks.NetworksKey;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.RouterPorts;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.RouterPortsKey;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.router.ports.Ports;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.router.ports.PortsKey;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitch;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitchKey;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.id.to.vpn.instance.VpnIds;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.id.to.vpn.instance.VpnIdsKey;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntryKey;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.DpnEndpoints;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfo;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfoKey;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.IpPortMapping;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.IpPortMappingKey;
141 import org.opendaylight.yangtools.yang.common.RpcResult;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.dpn.routers.DpnRoutersList;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.dpn.routers.DpnRoutersListKey;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.dpn.routers.dpn.routers.list.RoutersList;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.dpn.vpninterfaces.list.RouterInterfacesBuilder;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.dpn.vpninterfaces.list.RouterInterfacesKey;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.DpnVpninterfacesListBuilder;
148 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.DpnVpninterfacesListKey;
149 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.dpn.routers.DpnRoutersListBuilder;
150 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.dpn.routers.dpn.routers.list.RoutersListBuilder;
151 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.dpn.routers.dpn.routers.list.RoutersListKey;
152 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.DpnRouters;
153 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
154 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
155 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
156
157
158 import org.slf4j.Logger;
159 import org.slf4j.LoggerFactory;
160
161 import java.net.InetAddress;
162 import java.net.UnknownHostException;
163 import java.util.ArrayList;
164 import java.util.Arrays;
165 import java.util.Collections;
166 import java.util.HashMap;
167 import java.util.HashSet;
168 import java.util.List;
169 import java.util.Objects;
170 import java.util.Set;
171 import java.util.concurrent.ExecutionException;
172 import java.util.concurrent.Future;
173 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
174 import com.google.common.util.concurrent.FutureCallback;
175 import com.google.common.util.concurrent.Futures;
176
177 public class NatUtil {
178
179     private static String OF_URI_SEPARATOR = ":";
180     private static final Logger LOG = LoggerFactory.getLogger(NatUtil.class);
181
182     /*
183         getCookieSnatFlow() computes and returns a unique cookie value for the NAT flows using the router ID as the reference value.
184      */
185     public static BigInteger getCookieSnatFlow(long routerId) {
186         return NatConstants.COOKIE_NAPT_BASE.add(new BigInteger("0110000", 16)).add(
187                 BigInteger.valueOf(routerId));
188     }
189
190     /*
191         getCookieNaptFlow() computes and returns a unique cookie value for the NAPT flows using the router ID as the reference value.
192     */
193     public static BigInteger getCookieNaptFlow(long routerId) {
194         return NatConstants.COOKIE_NAPT_BASE.add(new BigInteger("0111000", 16)).add(
195                 BigInteger.valueOf(routerId));
196     }
197
198     /*
199         getVpnId() returns the VPN ID from the VPN name
200      */
201     public static long getVpnId(DataBroker broker, String vpnName) {
202         if(vpnName == null) {
203             return NatConstants.INVALID_ID;
204         }
205
206         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstance> id
207                 = getVpnInstanceToVpnIdIdentifier(vpnName);
208         Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstance> vpnInstance
209                 = read(broker, LogicalDatastoreType.CONFIGURATION, id);
210
211
212         long vpnId = NatConstants.INVALID_ID;
213         if(vpnInstance.isPresent()) {
214             Long vpnIdAsLong = vpnInstance.get().getVpnId();
215             if(vpnIdAsLong != null){
216                 vpnId = vpnIdAsLong;
217             }
218         }
219         return vpnId;
220     }
221
222     public static Long getVpnId(DataBroker broker, long routerId){
223         //Get the external network ID from the ExternalRouter model
224         Uuid networkId = NatUtil.getNetworkIdFromRouterId(broker, routerId);
225         if(networkId == null ){
226             LOG.error("NAT Service : networkId is null");
227             return null;
228         }
229
230         //Get the VPN ID from the ExternalNetworks model
231         Uuid vpnUuid = NatUtil.getVpnIdfromNetworkId(broker, networkId);
232         if(vpnUuid == null ){
233             LOG.error("NAT Service : vpnUuid is null");
234             return null;
235         }
236         Long vpnId = NatUtil.getVpnId(broker, vpnUuid.getValue());
237         return vpnId;
238     }
239
240     static InstanceIdentifier<RouterPorts> getRouterPortsId(String routerId) {
241         return InstanceIdentifier.builder(FloatingIpInfo.class).child(RouterPorts.class, new RouterPortsKey(routerId)).build();
242     }
243
244     static InstanceIdentifier<Routermapping> getRouterVpnMappingId(String routerId) {
245         return InstanceIdentifier.builder(RouterToVpnMapping.class).child(Routermapping.class, new RoutermappingKey(routerId)).build();
246     }
247
248     static InstanceIdentifier<Ports> getPortsIdentifier(String routerId, String portName) {
249         return InstanceIdentifier.builder(FloatingIpInfo.class).child(RouterPorts.class, new RouterPortsKey(routerId))
250                 .child(Ports.class, new PortsKey(portName)).build();
251     }
252
253     static InstanceIdentifier<InternalToExternalPortMap> getIntExtPortMapIdentifier(String routerId, String portName,
254                                                                                     String internalIp) {
255         return InstanceIdentifier.builder(FloatingIpInfo.class).child(RouterPorts.class, new RouterPortsKey(routerId))
256                 .child(Ports.class, new PortsKey(portName))
257                 .child(InternalToExternalPortMap.class, new InternalToExternalPortMapKey(internalIp)).build();
258     }
259
260     static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstance>
261     getVpnInstanceToVpnIdIdentifier(String vpnName) {
262         return InstanceIdentifier.builder(VpnInstanceToVpnId.class)
263                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstance.class,
264                         new org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstanceKey(vpnName)).build();
265     }
266
267     static String getVpnInstanceFromVpnIdentifier(DataBroker broker, long vpnId) {
268         InstanceIdentifier<VpnIds> id = InstanceIdentifier.builder(VpnIdToVpnInstance.class)
269                 .child(VpnIds.class, new VpnIdsKey(Long.valueOf(vpnId))).build();
270         Optional<VpnIds> vpnInstance = read(broker, LogicalDatastoreType.CONFIGURATION, id);
271         return vpnInstance.isPresent() ? vpnInstance.get().getVpnInstanceName() : null;
272     }
273
274     /*
275        getFlowRef() returns a string identfier for the SNAT flows using the router ID as the reference.
276     */
277     public static String getFlowRef(BigInteger dpnId, short tableId, long routerID, String ip) {
278         return new StringBuffer().append(NatConstants.NAPT_FLOWID_PREFIX).append(dpnId).append(NatConstants.FLOWID_SEPARATOR).
279                 append(tableId).append(NatConstants.FLOWID_SEPARATOR).append(routerID)
280                 .append(NatConstants.FLOWID_SEPARATOR).append(ip).toString();
281     }
282
283     public static String getNaptFlowRef(BigInteger dpnId, short tableId, String routerID, String ip, int port) {
284         return new StringBuffer().append(NatConstants.NAPT_FLOWID_PREFIX).append(dpnId).append(NatConstants.FLOWID_SEPARATOR).
285                 append(tableId).append(NatConstants.FLOWID_SEPARATOR).append(routerID).append(NatConstants.FLOWID_SEPARATOR).append(ip).
286                 append(NatConstants.FLOWID_SEPARATOR).append(port).toString();
287     }
288
289     /*
290         getNetworkIdFromRouterId() returns the network-id from the below model using the router-id as the key
291                container ext-routers {
292                    list routers {
293                        key router-name;
294                        leaf router-name { type string; }
295                        leaf network-id { type yang:uuid; }
296                        leaf enable-snat { type boolean; }
297                        leaf-list external-ips {
298                             type string; //format - ipaddress\prefixlength
299                        }
300                        leaf-list subnet-ids { type yang:uuid; }
301                    }
302                }
303
304     */
305     static Uuid getNetworkIdFromRouterId(DataBroker broker, long routerId) {
306         String routerName = getRouterName(broker, routerId);
307         InstanceIdentifier id = buildRouterIdentifier(routerName);
308         Optional<Routers> routerData = read(broker, LogicalDatastoreType.CONFIGURATION, id);
309         if (routerData.isPresent()) {
310             return routerData.get().getNetworkId();
311         }
312         return null;
313     }
314
315     static InstanceIdentifier<Routers> buildRouterIdentifier(String routerId) {
316         InstanceIdentifier<Routers> routerInstanceIndentifier = InstanceIdentifier.builder(ExtRouters.class).child
317                 (Routers.class, new RoutersKey(routerId)).build();
318         return routerInstanceIndentifier;
319     }
320
321     /*
322      * getEnableSnatFromRouterId() returns IsSnatEnabled true is routerID is present in external n/w otherwise returns false
323      */
324     static boolean isSnatEnabledForRouterId(DataBroker broker, String routerId){
325         InstanceIdentifier id = buildRouterIdentifier(routerId);
326         Optional<Routers> routerData = read(broker, LogicalDatastoreType.CONFIGURATION, id);
327         if (routerData.isPresent()) {
328             return routerData.get().isEnableSnat();
329         }
330         return false;
331     }
332     /*
333         getVpnIdfromNetworkId() returns the vpnid from the below model using the network ID as the key.
334             container external-networks {
335                 list networks  {
336                     key id;
337                     leaf id {
338                         type yang:uuid;
339                     }
340                     leaf vpnid { type yang:uuid; }
341                     leaf-list router-ids { type yang:uuid; }
342                     leaf-list subnet-ids{ type yang:uuid; }
343                 }
344             }
345     */
346     public static Uuid getVpnIdfromNetworkId(DataBroker broker, Uuid networkId) {
347         InstanceIdentifier<Networks> id = buildNetworkIdentifier(networkId);
348         Optional<Networks> networkData = read(broker, LogicalDatastoreType.CONFIGURATION, id);
349         if (networkData.isPresent()) {
350             return networkData.get().getVpnid();
351         }
352         return null;
353     }
354
355     public static ProviderTypes getProviderTypefromNetworkId(DataBroker broker, Uuid networkId) {
356         InstanceIdentifier<Networks> id = buildNetworkIdentifier(networkId);
357         Optional<Networks> networkData = read(broker, LogicalDatastoreType.CONFIGURATION, id);
358         if ((networkData.isPresent()) && (networkData.get() != null)) {
359             return networkData.get().getProviderNetworkType();
360         }
361         return null;
362     }
363
364     public static List<Uuid> getRouterIdsfromNetworkId(DataBroker broker, Uuid networkId) {
365         InstanceIdentifier<Networks> id = buildNetworkIdentifier(networkId);
366         Optional<Networks> networkData = read(broker, LogicalDatastoreType.CONFIGURATION, id);
367         return networkData.isPresent() ? networkData.get().getRouterIds() : Collections.emptyList();
368     }
369
370     static String getAssociatedExternalNetwork(DataBroker dataBroker, String routerId) {
371         InstanceIdentifier<Routers> id = NatUtil.buildRouterIdentifier(routerId);
372         Optional<Routers> routerData = NatUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, id);
373         if (routerData.isPresent()) {
374             Uuid networkId = routerData.get().getNetworkId();
375             if(networkId != null) {
376                 return networkId.getValue();
377             }
378         }
379         return null;
380     }
381
382     private static InstanceIdentifier<Networks> buildNetworkIdentifier(Uuid networkId) {
383         InstanceIdentifier<Networks> network = InstanceIdentifier.builder(ExternalNetworks.class).child
384                 (Networks.class, new NetworksKey(networkId)).build();
385         return network;
386     }
387
388
389
390
391     /*
392         getNaptSwitchesDpnIdsfromRouterId() returns the primary-switch-id and the secondary-switch-id in a array using the router-id; as the key.
393             container napt-switches {
394                 list router-to-napt-switch {
395                     key router-id;
396                     leaf router-id { type uint32; }
397                     leaf primary-switch-id { type uint64; }
398                     leaf secondary-switch-id { type uint64; }
399                 }
400             }
401     */
402     public static BigInteger getPrimaryNaptfromRouterId(DataBroker broker, Long routerId) {
403         // convert routerId to Name
404         String routerName = getRouterName(broker, routerId);
405         InstanceIdentifier id = buildNaptSwitchIdentifier(routerName);
406         Optional<RouterToNaptSwitch> routerToNaptSwitchData = read(broker, LogicalDatastoreType.CONFIGURATION, id);
407         if (routerToNaptSwitchData.isPresent()) {
408             RouterToNaptSwitch routerToNaptSwitchInstance = routerToNaptSwitchData.get();
409             return routerToNaptSwitchInstance.getPrimarySwitchId();
410         }
411         return null;
412     }
413
414     private static InstanceIdentifier<RouterToNaptSwitch> buildNaptSwitchIdentifier(String routerId) {
415         InstanceIdentifier<RouterToNaptSwitch> rtrNaptSw = InstanceIdentifier.builder(NaptSwitches.class).child
416                 (RouterToNaptSwitch.class, new RouterToNaptSwitchKey(routerId)).build();
417         return rtrNaptSw;
418     }
419
420     public static String getRouterName(DataBroker broker, Long routerId) {
421         InstanceIdentifier id = buildRouterIdentifier(routerId);
422         Optional<RouterIds> routerIdsData = read(broker, LogicalDatastoreType.CONFIGURATION, id);
423         if (routerIdsData.isPresent()) {
424             RouterIds routerIdsInstance = routerIdsData.get();
425             return routerIdsInstance.getRouterName();
426         }
427         return null;
428     }
429
430     private static InstanceIdentifier<RouterIds> buildRouterIdentifier(Long routerId) {
431         InstanceIdentifier<RouterIds> routerIds = InstanceIdentifier.builder(RouterIdName.class).child
432                 (RouterIds.class, new RouterIdsKey(routerId)).build();
433         return routerIds;
434     }
435
436     public static <T extends DataObject> Optional<T> read(DataBroker broker, LogicalDatastoreType datastoreType,
437                                                           InstanceIdentifier<T> path) {
438
439         ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
440
441         Optional<T> result = Optional.absent();
442         try {
443             result = tx.read(datastoreType, path).get();
444         } catch (Exception e) {
445             throw new RuntimeException(e);
446         }
447
448         return result;
449     }
450
451     static InstanceIdentifier<VpnInstanceOpDataEntry> getVpnInstanceOpDataIdentifier(String vrfId) {
452         return InstanceIdentifier.builder(VpnInstanceOpData.class)
453                 .child(VpnInstanceOpDataEntry.class, new VpnInstanceOpDataEntryKey(vrfId)).build();
454     }
455
456     public static long readVpnId(DataBroker broker, String vpnName) {
457
458         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstance> id
459                 = getVpnInstanceToVpnIdIdentifier(vpnName);
460         Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstance> vpnInstance
461                 = read(broker, LogicalDatastoreType.CONFIGURATION, id);
462
463         long vpnId = NatConstants.INVALID_ID;
464         if(vpnInstance.isPresent()) {
465             vpnId = vpnInstance.get().getVpnId();
466         }
467         return vpnId;
468     }
469
470     public static FlowEntity buildFlowEntity(BigInteger dpnId, short tableId, BigInteger cookie) {
471         FlowEntity flowEntity = new FlowEntity(dpnId);
472         flowEntity.setTableId(tableId);
473         flowEntity.setCookie(cookie);
474         return flowEntity;
475     }
476
477     public static long getIpAddress(byte[] rawIpAddress) {
478         return (((rawIpAddress[0] & 0xFF) << (3 * 8)) + ((rawIpAddress[1] & 0xFF) << (2 * 8))
479                 + ((rawIpAddress[2] & 0xFF) << (1 * 8)) + (rawIpAddress[3] & 0xFF)) & 0xffffffffL;
480     }
481
482     public static String getFlowRef(BigInteger dpnId, short tableId, InetAddress destPrefix) {
483         return new StringBuilder(64).append(NatConstants.FLOWID_PREFIX).append(dpnId).append(NwConstants.FLOWID_SEPARATOR)
484                 .append(tableId).append(NwConstants.FLOWID_SEPARATOR)
485                 .append(destPrefix.getHostAddress()).toString();
486     }
487
488     public static String getEndpointIpAddressForDPN(DataBroker broker, BigInteger dpnId) {
489         String nextHopIp = null;
490         InstanceIdentifier<DPNTEPsInfo> tunnelInfoId =
491                 InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, new DPNTEPsInfoKey(dpnId)).build();
492         Optional<DPNTEPsInfo> tunnelInfo = read(broker, LogicalDatastoreType.CONFIGURATION, tunnelInfoId);
493         if (tunnelInfo.isPresent()) {
494             List<TunnelEndPoints> nexthopIpList = tunnelInfo.get().getTunnelEndPoints();
495             if (nexthopIpList != null && !nexthopIpList.isEmpty()) {
496                 nextHopIp = nexthopIpList.get(0).getIpAddress().getIpv4Address().getValue();
497             }
498         }
499         return nextHopIp;
500     }
501
502     /*
503         getVpnRd returns the rd (route distinguisher) which is the VRF ID from the below model using the vpnName
504             list vpn-instance {
505                 key "vpn-instance-name"
506                 leaf vpn-instance-name {
507                     type string;
508                 }
509                 leaf vpn-id {
510                     type uint32;
511                 }
512                 leaf vrf-id {
513                     type string;
514                 }
515             }
516     */
517     public static String getVpnRd(DataBroker broker, String vpnName) {
518
519         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstance> id
520                 = getVpnInstanceToVpnIdIdentifier(vpnName);
521         Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstance> vpnInstance
522                 = read(broker, LogicalDatastoreType.CONFIGURATION, id);
523
524         String rd = null;
525         if(vpnInstance.isPresent()) {
526             rd = vpnInstance.get().getVrfId();
527         }
528         return rd;
529     }
530
531     /*  getExternalIPPortMap() returns the internal IP and the port for the querried router ID, external IP and the port.
532         container intext-ip-port-map {
533         config true;
534         list ip-port-mapping {
535             key router-id;
536             leaf router-id { type uint32; }
537             list intext-ip-protocol-type {
538                 key protocol;
539                 leaf protocol { type protocol-types; }
540                 list ip-port-map {
541                     key ip-port-internal;
542                     description "internal to external ip-port mapping";
543                     leaf ip-port-internal { type string; }
544                     container ip-port-external {
545                        uses ip-port-entity;
546                     }
547                 }
548             }
549          }
550        }
551     */
552     public static IpPortExternal getExternalIpPortMap(DataBroker broker, Long routerId, String internalIpAddress, String internalPort, NAPTEntryEvent.Protocol protocol) {
553         ProtocolTypes protocolType = NatUtil.getProtocolType(protocol);
554         InstanceIdentifier ipPortMapId = buildIpToPortMapIdentifier(routerId, internalIpAddress, internalPort, protocolType);
555         Optional<IpPortMap> ipPortMapData = read(broker, LogicalDatastoreType.CONFIGURATION, ipPortMapId);
556         if (ipPortMapData.isPresent()) {
557             IpPortMap ipPortMapInstance = ipPortMapData.get();
558             return ipPortMapInstance.getIpPortExternal();
559         }
560         return null;
561     }
562
563     private static InstanceIdentifier<IpPortMap> buildIpToPortMapIdentifier(Long routerId, String internalIpAddress, String internalPort , ProtocolTypes protocolType) {
564         InstanceIdentifier<IpPortMap> ipPortMapId = InstanceIdentifier.builder(IntextIpPortMap.class).child
565                 (IpPortMapping.class, new IpPortMappingKey(routerId)).child(IntextIpProtocolType.class, new IntextIpProtocolTypeKey(protocolType))
566                 .child(IpPortMap.class, new IpPortMapKey(internalIpAddress + ":" + internalPort)).build();
567         return ipPortMapId;
568     }
569
570     public static FlowEntity buildFlowEntity(BigInteger dpnId, short tableId, String flowId, int priority, String flowName,
571                                              BigInteger cookie, List<MatchInfo> listMatchInfo) {
572
573         FlowEntity flowEntity = new FlowEntity(dpnId);
574         flowEntity.setTableId(tableId);
575         flowEntity.setFlowId(flowId);
576         flowEntity.setPriority(priority);
577         flowEntity.setFlowName(flowName);
578         flowEntity.setCookie(cookie);
579         flowEntity.setMatchInfoList(listMatchInfo);
580         return flowEntity;
581     }
582
583     static boolean isVpnInterfaceConfigured(DataBroker broker, String interfaceName)
584     {
585         InstanceIdentifier<VpnInterface> interfaceId = getVpnInterfaceIdentifier(interfaceName);
586         Optional<VpnInterface> configuredVpnInterface = read(broker, LogicalDatastoreType.CONFIGURATION, interfaceId);
587
588         if (configuredVpnInterface.isPresent()) {
589             return true;
590         }
591         return false;
592     }
593
594     static InstanceIdentifier<VpnInterface> getVpnInterfaceIdentifier(String vpnInterfaceName) {
595         return InstanceIdentifier.builder(VpnInterfaces.class)
596                 .child(VpnInterface.class, new VpnInterfaceKey(vpnInterfaceName)).build();
597     }
598
599     static VpnInterface getConfiguredVpnInterface(DataBroker broker, String interfaceName) {
600         InstanceIdentifier<VpnInterface> interfaceId = getVpnInterfaceIdentifier(interfaceName);
601         Optional<VpnInterface> configuredVpnInterface = read(broker, LogicalDatastoreType.CONFIGURATION, interfaceId);
602
603         if (configuredVpnInterface.isPresent()) {
604             return configuredVpnInterface.get();
605         }
606         return null;
607     }
608
609     public static String getDpnFromNodeConnectorId(NodeConnectorId portId) {
610         /*
611          * NodeConnectorId is of form 'openflow:dpnid:portnum'
612          */
613         String[] split = portId.getValue().split(OF_URI_SEPARATOR);
614         if (split == null || split.length != 3) {
615             return null;
616         }
617         return split[1];
618     }
619
620     public static BigInteger getDpIdFromInterface(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState) {
621         String lowerLayerIf = ifState.getLowerLayerIf().get(0);
622         NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
623         return new BigInteger(getDpnFromNodeConnectorId(nodeConnectorId));
624     }
625
626     /*
627     container vpnMaps {
628         list vpnMap {
629             key vpn-id;
630             leaf vpn-id {
631                 type    yang:uuid;
632                 description "vpn-id";
633             }
634             leaf name {
635                 type  string;
636                 description "vpn name";
637             }
638             leaf tenant-id {
639                 type    yang:uuid;
640                 description "The UUID of the tenant that will own the subnet.";
641             }
642
643             leaf router-id {
644               type    yang:uuid;
645               description "UUID of router ";
646             }
647             leaf-list network_ids {
648               type    yang:uuid;
649               description "UUID representing the network ";
650             }
651         }
652     }
653     Method returns router Id associated to a VPN
654      */
655
656     public static String getRouterIdfromVpnInstance(DataBroker broker,String vpnName){
657         InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
658                 .child(VpnMap.class, new VpnMapKey(new Uuid(vpnName))).build();
659         Optional<VpnMap> optionalVpnMap = read(broker, LogicalDatastoreType.CONFIGURATION,
660                 vpnMapIdentifier);
661         if (optionalVpnMap.isPresent()) {
662             Uuid routerId = optionalVpnMap.get().getRouterId();
663             if (routerId != null) {
664                 return routerId.getValue();
665             }
666         }
667         return null;
668     }
669
670     static Uuid getVpnForRouter(DataBroker broker, String routerId) {
671         InstanceIdentifier<VpnMaps> vpnMapsIdentifier = InstanceIdentifier.builder(VpnMaps.class).build();
672         Optional<VpnMaps> optionalVpnMaps = read(broker, LogicalDatastoreType.CONFIGURATION,
673                 vpnMapsIdentifier);
674         if (optionalVpnMaps.isPresent() && optionalVpnMaps.get().getVpnMap() != null) {
675             List<VpnMap> allMaps = optionalVpnMaps.get().getVpnMap();
676             if (routerId != null) {
677                 for (VpnMap vpnMap : allMaps) {
678                     if (vpnMap.getRouterId() != null &&
679                             routerId.equals(vpnMap.getRouterId().getValue()) &&
680                             !routerId.equals(vpnMap.getVpnId().getValue())) {
681                         return vpnMap.getVpnId();
682                     }
683                 }
684             }
685         }
686         return null;
687     }
688
689     static long getAssociatedVpn(DataBroker broker, String routerName) {
690         InstanceIdentifier<Routermapping> routerMappingId = NatUtil.getRouterVpnMappingId(routerName);
691         Optional<Routermapping> optRouterMapping = NatUtil.read(broker, LogicalDatastoreType.OPERATIONAL, routerMappingId);
692         if(optRouterMapping.isPresent()) {
693             Routermapping routerMapping = optRouterMapping.get();
694             return routerMapping.getVpnId();
695         }
696         return NatConstants.INVALID_ID;
697     }
698
699     public static String getAssociatedVPN(DataBroker dataBroker, Uuid networkId, Logger log) {
700         Uuid vpnUuid = NatUtil.getVpnIdfromNetworkId(dataBroker, networkId);
701         if(vpnUuid == null ){
702             log.error("No VPN instance associated with ext network {}", networkId);
703             return null;
704         }
705         return vpnUuid.getValue();
706     }
707
708     public static void addPrefixToBGP(DataBroker broker,
709                                       IBgpManager bgpManager,
710                                       IFibManager fibManager,
711                                       String rd,
712                                       String prefix,
713                                       String nextHopIp,
714                                       long label,
715                                       Logger log,
716                                       RouteOrigin origin) {
717         try {
718             LOG.info("ADD: Adding Fib entry rd {} prefix {} nextHop {} label {}", rd, prefix, nextHopIp, label);
719             if (nextHopIp == null)
720             {
721                 log.error("addPrefix failed since nextHopIp cannot be null.");
722                 return;
723             }
724             fibManager.addOrUpdateFibEntry(broker, rd, prefix, Arrays.asList(nextHopIp), (int)label, origin, null);
725             bgpManager.advertisePrefix(rd, prefix, Arrays.asList(nextHopIp), (int)label);
726             LOG.info("ADD: Added Fib entry rd {} prefix {} nextHop {} label {}", rd, prefix, nextHopIp, label);
727         } catch(Exception e) {
728             log.error("Add prefix failed", e);
729         }
730     }
731
732     static InstanceIdentifier<Ports> buildPortToIpMapIdentifier(String routerId, String portName) {
733         InstanceIdentifier<Ports> ipPortMapId = InstanceIdentifier.builder(FloatingIpInfo.class).child
734                 (RouterPorts.class, new RouterPortsKey(routerId)).child(Ports.class, new PortsKey(portName)).build();
735         return ipPortMapId;
736     }
737
738     static InstanceIdentifier<RouterPorts> buildRouterPortsIdentifier(String routerId) {
739         InstanceIdentifier<RouterPorts> routerInstanceIndentifier = InstanceIdentifier.builder(FloatingIpInfo.class).child
740                 (RouterPorts.class, new RouterPortsKey(routerId)).build();
741         return routerInstanceIndentifier;
742     }
743
744     /* container snatint-ip-port-map {
745         list intip-port-map {
746             key router-id;
747             leaf router-id { type uint32; }
748             list ip-port {
749                 key internal-ip;
750                 leaf internal-ip { type string; }
751                 list int-ip-proto-type {
752                     key protocol;
753                     leaf protocol { type protocol-types; }
754                     leaf-list ports { type uint16; }
755                 }
756             }
757         }
758     }
759     Method returns InternalIp port List
760     */
761
762     public static List<Integer> getInternalIpPortListInfo(DataBroker dataBroker,Long routerId, String internalIpAddress, ProtocolTypes protocolType){
763         Optional<IntIpProtoType> optionalIpProtoType = read(dataBroker, LogicalDatastoreType.CONFIGURATION, buildSnatIntIpPortIdentifier(routerId, internalIpAddress, protocolType));
764         if (optionalIpProtoType.isPresent()) {
765             return optionalIpProtoType.get().getPorts();
766         }
767         return null;
768     }
769
770     public static InstanceIdentifier<IntIpProtoType> buildSnatIntIpPortIdentifier(Long routerId, String internalIpAddress, ProtocolTypes protocolType) {
771         InstanceIdentifier<IntIpProtoType> intIpProtocolTypeId = InstanceIdentifier.builder(SnatintIpPortMap.class).child
772                 (IntipPortMap.class, new IntipPortMapKey(routerId)).child(IpPort.class, new IpPortKey(internalIpAddress)).child
773                 (IntIpProtoType.class, new IntIpProtoTypeKey(protocolType)).build();
774         return intIpProtocolTypeId;
775     }
776
777     public static ProtocolTypes getProtocolType(NAPTEntryEvent.Protocol protocol) {
778         ProtocolTypes protocolType = ProtocolTypes.TCP.toString().equals(protocol.toString()) ? ProtocolTypes.TCP : ProtocolTypes.UDP;
779         return protocolType;
780     }
781
782     public static InstanceIdentifier<NaptSwitches> getNaptSwitchesIdentifier() {
783         return InstanceIdentifier.create(NaptSwitches.class);
784     }
785
786     public static InstanceIdentifier<RouterToNaptSwitch> buildNaptSwitchRouterIdentifier(String routerId) {
787         return InstanceIdentifier.create(NaptSwitches.class).child(RouterToNaptSwitch.class, new RouterToNaptSwitchKey(routerId));
788     }
789
790     public static String toStringIpAddress(byte[] ipAddress, Logger log)
791     {
792         String ip = "";
793         if (ipAddress == null) {
794             return ip;
795         }
796
797         try {
798             ip = InetAddress.getByAddress(ipAddress).getHostAddress();
799         } catch(UnknownHostException e) {
800             log.error("NAT Service : Caught exception during toStringIpAddress()");
801         }
802
803         return ip;
804     }
805
806     public static String getGroupIdKey(String routerName){
807         String groupIdKey = new String("snatmiss." + routerName);
808         return groupIdKey;
809     }
810
811     public static long createGroupId(String groupIdKey,IdManagerService idManager) {
812         AllocateIdInput getIdInput = new AllocateIdInputBuilder()
813                 .setPoolName(NatConstants.SNAT_IDPOOL_NAME).setIdKey(groupIdKey)
814                 .build();
815         try {
816             Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
817             RpcResult<AllocateIdOutput> rpcResult = result.get();
818             return rpcResult.getResult().getIdValue();
819         } catch (NullPointerException | InterruptedException | ExecutionException e) {
820             LOG.trace("", e);
821         }
822         return 0;
823     }
824
825     public static void removePrefixFromBGP(DataBroker broker , IBgpManager bgpManager, IFibManager fibManager, String rd, String prefix, Logger log) {
826         try {
827             LOG.info("REMOVE: Removing Fib entry rd {} prefix {}", rd, prefix);
828             fibManager.removeFibEntry(broker, rd, prefix, null);
829             bgpManager.withdrawPrefix(rd, prefix);
830             LOG.info("REMOVE: Removed Fib entry rd {} prefix {}", rd, prefix);
831         } catch(Exception e) {
832             log.error("Delete prefix failed", e);
833         }
834     }
835
836     public static FlowEntity buildFlowEntity(BigInteger dpnId, short tableId, BigInteger cookie, String flowId) {
837         FlowEntity flowEntity = new FlowEntity(dpnId);
838         flowEntity.setTableId(tableId);
839         flowEntity.setCookie(cookie);
840         flowEntity.setFlowId(flowId);
841         return flowEntity;
842     }
843
844     public static FlowEntity buildFlowEntity(BigInteger dpnId, short tableId, String flowId) {
845         FlowEntity flowEntity = new FlowEntity(dpnId);
846         flowEntity.setTableId(tableId);
847         flowEntity.setFlowId(flowId);
848         return flowEntity;
849     }
850
851     public static IpPortMapping getIportMapping(DataBroker broker, long routerId) {
852         Optional<IpPortMapping> getIportMappingData = read(broker, LogicalDatastoreType.CONFIGURATION, getIportMappingIdentifier(routerId));
853         if(getIportMappingData.isPresent()) {
854             return getIportMappingData.get();
855         }
856         return null;
857     }
858
859     public static InstanceIdentifier<IpPortMapping> getIportMappingIdentifier(long routerId) {
860         return InstanceIdentifier.builder(IntextIpPortMap.class).child(IpPortMapping.class, new IpPortMappingKey(routerId)).build();
861     }
862
863     public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping> getIpMappingBuilder(Long routerId) {
864         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping> idBuilder = InstanceIdentifier.builder(IntextIpMap.class)
865                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping.class, new org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMappingKey(routerId)).build();
866         return idBuilder;
867     }
868
869     public static List<String> getExternalIpsForRouter(DataBroker dataBroker,Long routerId) {
870         Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping> ipMappingOptional = read(dataBroker,
871                 LogicalDatastoreType.OPERATIONAL, getIpMappingBuilder(routerId));
872         List<String> externalIps = new ArrayList<>();
873         if (ipMappingOptional.isPresent()) {
874             List<IpMap> ipMaps = ipMappingOptional.get().getIpMap();
875             for (IpMap ipMap : ipMaps) {
876                 externalIps.add(ipMap.getExternalIp());
877             }
878             //remove duplicates
879             Set<String> uniqueExternalIps = Sets.newHashSet(externalIps);
880             externalIps = Lists.newArrayList(uniqueExternalIps);
881             return externalIps;
882         }
883         return null;
884     }
885
886     public static HashMap<String,Long> getExternalIpsLabelForRouter(DataBroker dataBroker,Long routerId) {
887         Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping> ipMappingOptional = read(dataBroker,
888                 LogicalDatastoreType.OPERATIONAL, getIpMappingBuilder(routerId));
889         HashMap<String,Long> externalIpsLabel = new HashMap<>();
890         if (ipMappingOptional.isPresent()) {
891             List<IpMap> ipMaps = ipMappingOptional.get().getIpMap();
892             for (IpMap ipMap : ipMaps) {
893                 externalIpsLabel.put(ipMap.getExternalIp(), ipMap.getLabel());
894             }
895             return externalIpsLabel;
896         }
897         return null;
898     }
899     /*
900     container external-ips-counter {
901         config false;
902         list external-counters{
903             key segment-id;
904             leaf segment-id { type uint32; }
905             list external-ip-counter {
906                 key external-ip;
907                 leaf external-ip { type string; }
908                 leaf counter { type uint8; }
909             }
910         }
911     }
912     */
913
914     public static String getLeastLoadedExternalIp(DataBroker dataBroker, long segmentId){
915         String leastLoadedExternalIp =  null;
916         InstanceIdentifier<ExternalCounters> id = InstanceIdentifier.builder(ExternalIpsCounter.class).child(ExternalCounters.class, new ExternalCountersKey(segmentId)).build();
917         Optional <ExternalCounters> externalCountersData = MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, id);
918         if (externalCountersData.isPresent()) {
919             ExternalCounters externalCounter = externalCountersData.get();
920             List<ExternalIpCounter> externalIpCounterList = externalCounter.getExternalIpCounter();
921             short countOfLstLoadExtIp = 32767;
922             for(ExternalIpCounter externalIpCounter : externalIpCounterList){
923                 String curExternalIp = externalIpCounter.getExternalIp();
924                 short countOfCurExtIp  = externalIpCounter.getCounter();
925                 if( countOfCurExtIp < countOfLstLoadExtIp ){
926                     countOfLstLoadExtIp = countOfCurExtIp;
927                     leastLoadedExternalIp = curExternalIp;
928                 }
929             }
930         }
931         return leastLoadedExternalIp;
932     }
933
934     public static String[] getSubnetIpAndPrefix(DataBroker dataBroker, Uuid subnetId){
935         String subnetIP = getSubnetIp(dataBroker, subnetId);
936         if(subnetId != null){
937             return getSubnetIpAndPrefix(subnetIP);
938         }
939         return null;
940     }
941
942     public static String getSubnetIp(DataBroker dataBroker, Uuid subnetId){
943         InstanceIdentifier<Subnetmap> subnetmapId = InstanceIdentifier
944                 .builder(Subnetmaps.class)
945                 .child(Subnetmap.class, new SubnetmapKey(subnetId))
946                 .build();
947         Optional<Subnetmap> removedSubnet = read(dataBroker, LogicalDatastoreType.CONFIGURATION, subnetmapId);
948         if(removedSubnet.isPresent()) {
949             Subnetmap subnetMapEntry = removedSubnet.get();
950             return subnetMapEntry.getSubnetIp();
951         }
952         return null;
953
954     }
955     public static String[] getSubnetIpAndPrefix(String subnetString){
956         String[] subnetSplit = subnetString.split("/");
957         String subnetIp = subnetSplit[0];
958         String subnetPrefix = "0";
959         if (subnetSplit.length == 2) {
960             subnetPrefix = subnetSplit[1];
961         }
962         return new String[] {subnetIp, subnetPrefix};
963     }
964
965     public static String[] getExternalIpAndPrefix(String leastLoadedExtIpAddr){
966         String[] leastLoadedExtIpAddrSplit = leastLoadedExtIpAddr.split("/");
967         String leastLoadedExtIp = leastLoadedExtIpAddrSplit[0];
968         String leastLoadedExtIpPrefix = String.valueOf(NatConstants.DEFAULT_PREFIX);
969         if (leastLoadedExtIpAddrSplit.length == 2) {
970             leastLoadedExtIpPrefix = leastLoadedExtIpAddrSplit[1];
971         }
972         return new String[] {leastLoadedExtIp, leastLoadedExtIpPrefix};
973     }
974
975     public static List<BigInteger> getDpnsForRouter(DataBroker dataBroker, String routerUuid){
976         InstanceIdentifier id = InstanceIdentifier.builder(NeutronRouterDpns.class).child(RouterDpnList.class, new RouterDpnListKey(routerUuid)).build();
977         Optional<RouterDpnList> routerDpnListData = read(dataBroker, LogicalDatastoreType.OPERATIONAL, id);
978         List<BigInteger> dpns = new ArrayList<>();
979         if (routerDpnListData.isPresent()) {
980             List<DpnVpninterfacesList> dpnVpninterfacesList = routerDpnListData.get().getDpnVpninterfacesList();
981             for (DpnVpninterfacesList dpnVpnInterface : dpnVpninterfacesList) {
982                 dpns.add(dpnVpnInterface.getDpnId());
983             }
984             return dpns;
985         }
986         return null;
987     }
988
989     public static long getBgpVpnId(DataBroker dataBroker, String routerName){
990         long bgpVpnId = NatConstants.INVALID_ID;
991         Uuid bgpVpnUuid = NatUtil.getVpnForRouter(dataBroker, routerName);
992         if(bgpVpnUuid != null){
993             bgpVpnId = NatUtil.getVpnId(dataBroker, bgpVpnUuid.getValue());
994         }
995         return bgpVpnId;
996     }
997
998     static org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.router.interfaces.RouterInterface
999     getConfiguredRouterInterface(DataBroker broker, String interfaceName) {
1000         Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.router.interfaces.RouterInterface> optRouterInterface =
1001                 read(broker, LogicalDatastoreType.CONFIGURATION, NatUtil
1002                 .getRouterInterfaceId(interfaceName));
1003         if(optRouterInterface.isPresent()) {
1004             return optRouterInterface.get();
1005         }
1006         return null;
1007     }
1008
1009     static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.router.interfaces.RouterInterface>
1010     getRouterInterfaceId(String interfaceName) {
1011         return InstanceIdentifier.builder(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.RouterInterfaces.class)
1012                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.router.interfaces.RouterInterface.class,
1013                         new org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.router.interfaces.RouterInterfaceKey(interfaceName)).build();
1014     }
1015
1016     static void addToNeutronRouterDpnsMap(DataBroker broker, String routerName, String interfaceName,
1017                                      OdlInterfaceRpcService ifaceMgrRpcService, WriteTransaction writeOperTxn) {
1018         BigInteger dpId = getDpnForInterface(ifaceMgrRpcService, interfaceName);
1019         if(dpId.equals(BigInteger.ZERO)) {
1020             LOG.warn("NAT Service : Could not retrieve dp id for interface {} to handle router {} association model", interfaceName, routerName);
1021             return;
1022         }
1023
1024         LOG.debug("NAT Service : Adding the Router {} and DPN {} for the Interface {} in the ODL-L3VPN : NeutronRouterDpn map",
1025                 routerName, dpId, interfaceName);
1026         InstanceIdentifier<DpnVpninterfacesList> dpnVpnInterfacesListIdentifier = getRouterDpnId(routerName, dpId);
1027
1028         Optional<DpnVpninterfacesList> optionalDpnVpninterfacesList = read(broker, LogicalDatastoreType
1029                 .OPERATIONAL, dpnVpnInterfacesListIdentifier);
1030         org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.dpn.vpninterfaces.list.RouterInterfaces routerInterface =
1031                 new RouterInterfacesBuilder().setKey(new RouterInterfacesKey(interfaceName)).setInterface(interfaceName).build();
1032         if (optionalDpnVpninterfacesList.isPresent()) {
1033             LOG.debug("NAT Service : RouterDpnList already present for the Router {} and DPN {} for the Interface {} in the " +
1034                     "ODL-L3VPN : NeutronRouterDpn map", routerName, dpId, interfaceName);
1035             writeOperTxn.merge(LogicalDatastoreType.OPERATIONAL, dpnVpnInterfacesListIdentifier.child(
1036                     org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.dpn.vpninterfaces.list.RouterInterfaces.class, new RouterInterfacesKey(interfaceName)), routerInterface, true);
1037         } else {
1038             LOG.debug("NAT Service : Building new RouterDpnList for the Router {} and DPN {} for the Interface {} in the " +
1039                     "ODL-L3VPN : NeutronRouterDpn map", routerName, dpId, interfaceName);
1040             RouterDpnListBuilder routerDpnListBuilder = new RouterDpnListBuilder();
1041             routerDpnListBuilder.setRouterId(routerName);
1042             DpnVpninterfacesListBuilder dpnVpnList = new DpnVpninterfacesListBuilder().setDpnId(dpId);
1043             List<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.dpn.vpninterfaces.list.RouterInterfaces> routerInterfaces =  new ArrayList<>();
1044             routerInterfaces.add(routerInterface);
1045             dpnVpnList.setRouterInterfaces(routerInterfaces);
1046             routerDpnListBuilder.setDpnVpninterfacesList(Arrays.asList(dpnVpnList.build()));
1047             writeOperTxn.merge(LogicalDatastoreType.OPERATIONAL,
1048                     getRouterId(routerName),
1049                     routerDpnListBuilder.build(), true);
1050         }
1051     }
1052
1053     static void addToDpnRoutersMap(DataBroker broker, String routerName, String interfaceName,
1054                                           OdlInterfaceRpcService ifaceMgrRpcService, WriteTransaction writeOperTxn) {
1055         BigInteger dpId = getDpnForInterface(ifaceMgrRpcService, interfaceName);
1056         if(dpId.equals(BigInteger.ZERO)) {
1057             LOG.warn("NAT Service : Could not retrieve dp id for interface {} to handle router {} association model", interfaceName, routerName);
1058             return;
1059         }
1060
1061         LOG.debug("NAT Service : Adding the DPN {} and router {} for the Interface {} in the ODL-L3VPN : " +
1062                         "DPNRouters map",
1063                 dpId, routerName, interfaceName);
1064         InstanceIdentifier<DpnRoutersList> dpnRoutersListIdentifier = getDpnRoutersId(dpId);
1065
1066         Optional<DpnRoutersList> optionalDpnRoutersList = read(broker, LogicalDatastoreType.OPERATIONAL, dpnRoutersListIdentifier);
1067
1068         if (optionalDpnRoutersList.isPresent()) {
1069             RoutersList routersList = new RoutersListBuilder().setKey(new RoutersListKey(routerName)).setRouter(routerName)
1070                     .build();
1071             List<RoutersList> routersListFromDs = optionalDpnRoutersList.get().getRoutersList();
1072             if(!routersListFromDs.contains(routersList)) {
1073                 LOG.debug("NAT Service : Router {} not present for the DPN {}" +
1074                         " in the ODL-L3VPN : DPNRouters map", routerName, dpId);
1075                 writeOperTxn.merge(LogicalDatastoreType.OPERATIONAL, dpnRoutersListIdentifier.child(RoutersList.class, new
1076                         RoutersListKey(routerName)), routersList, true);
1077             }else{
1078                 LOG.debug("NAT Service : Router {} already mapped to the DPN {} in the ODL-L3VPN : DPNRouters map",
1079                         routerName, dpId);
1080             }
1081         } else {
1082             LOG.debug("NAT Service : Building new DPNRoutersList for the Router {} present in the DPN {} " +
1083                     "ODL-L3VPN : DPNRouters map", routerName, dpId);
1084             DpnRoutersListBuilder dpnRoutersListBuilder = new DpnRoutersListBuilder();
1085             dpnRoutersListBuilder.setDpnId(dpId);
1086             RoutersListBuilder routersListBuilder = new RoutersListBuilder();
1087             routersListBuilder.setRouter(routerName);
1088             dpnRoutersListBuilder.setRoutersList(Arrays.asList(routersListBuilder.build()));
1089             writeOperTxn.merge(LogicalDatastoreType.OPERATIONAL,
1090                     getDpnRoutersId(dpId),
1091                     dpnRoutersListBuilder.build(), true);
1092         }
1093     }
1094
1095     static void removeFromNeutronRouterDpnsMap(DataBroker broker, String routerName, String interfaceName,
1096                                                   BigInteger dpId, WriteTransaction writeOperTxn) {
1097         if(dpId.equals(BigInteger.ZERO)) {
1098             LOG.warn("NAT Service : Could not retrieve dp id for interface {} to handle router {} dissociation model", interfaceName, routerName);
1099             return;
1100         }
1101         InstanceIdentifier<DpnVpninterfacesList> routerDpnListIdentifier = getRouterDpnId(routerName, dpId);
1102         Optional<DpnVpninterfacesList> optionalRouterDpnList = NatUtil.read(broker, LogicalDatastoreType
1103                 .OPERATIONAL, routerDpnListIdentifier);
1104         if (optionalRouterDpnList.isPresent()) {
1105             List<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.dpn.vpninterfaces.list.RouterInterfaces> routerInterfaces = optionalRouterDpnList.get().getRouterInterfaces();
1106             org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.dpn.vpninterfaces.list.RouterInterfaces routerInterface = new RouterInterfacesBuilder().setKey(new RouterInterfacesKey(interfaceName)).setInterface(interfaceName).build();
1107             if (routerInterfaces != null && routerInterfaces.remove(routerInterface)) {
1108                 if (routerInterfaces.isEmpty()) {
1109                     writeOperTxn.delete(LogicalDatastoreType.OPERATIONAL, routerDpnListIdentifier);
1110                 } else {
1111                     writeOperTxn.delete(LogicalDatastoreType.OPERATIONAL, routerDpnListIdentifier.child(
1112                             org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.dpn.vpninterfaces.list.RouterInterfaces.class,
1113                             new RouterInterfacesKey(interfaceName)));
1114                 }
1115             }
1116         }
1117     }
1118
1119     static void removeFromNeutronRouterDpnsMap(DataBroker broker, String routerName,
1120                                                BigInteger dpId, WriteTransaction writeOperTxn) {
1121         if(dpId.equals(BigInteger.ZERO)) {
1122             LOG.warn("NAT Service : DPN ID is invalid for the router {} ", routerName);
1123             return;
1124         }
1125
1126         InstanceIdentifier<DpnVpninterfacesList> routerDpnListIdentifier = getRouterDpnId(routerName, dpId);
1127         Optional<DpnVpninterfacesList> optionalRouterDpnList = NatUtil.read(broker, LogicalDatastoreType
1128                 .OPERATIONAL, routerDpnListIdentifier);
1129         if (optionalRouterDpnList.isPresent()) {
1130             LOG.debug("NAT Service : Removing the dpn-vpninterfaces-list from the odl-l3vpn:neutron-router-dpns model " +
1131                             "for the router {}", routerName);
1132             writeOperTxn.delete(LogicalDatastoreType.OPERATIONAL, routerDpnListIdentifier);
1133         }else{
1134             LOG.debug("NAT Service : dpn-vpninterfaces-list does not exist in the odl-l3vpn:neutron-router-dpns model " +
1135                     "for the router {}", routerName);
1136         }
1137     }
1138
1139     static void removeFromNeutronRouterDpnsMap(DataBroker broker, String routerName, String vpnInterfaceName,
1140                                                   OdlInterfaceRpcService ifaceMgrRpcService, WriteTransaction writeOperTxn) {
1141         BigInteger dpId = getDpnForInterface(ifaceMgrRpcService, vpnInterfaceName);
1142         if(dpId.equals(BigInteger.ZERO)) {
1143             LOG.warn("NAT Service : Could not retrieve dp id for interface {} to handle router {} dissociation model", vpnInterfaceName, routerName);
1144             return;
1145         }
1146         InstanceIdentifier<DpnVpninterfacesList> routerDpnListIdentifier = getRouterDpnId(routerName, dpId);
1147         Optional<DpnVpninterfacesList> optionalRouterDpnList = read(broker, LogicalDatastoreType
1148                 .OPERATIONAL, routerDpnListIdentifier);
1149         if (optionalRouterDpnList.isPresent()) {
1150             List<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.dpn.vpninterfaces.list.RouterInterfaces> routerInterfaces = optionalRouterDpnList.get().getRouterInterfaces();
1151             org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.dpn.vpninterfaces.list.RouterInterfaces routerInterface = new RouterInterfacesBuilder().setKey(new RouterInterfacesKey(vpnInterfaceName)).setInterface(vpnInterfaceName).build();
1152
1153             if (routerInterfaces != null && routerInterfaces.remove(routerInterface)) {
1154                 if (routerInterfaces.isEmpty()) {
1155                     if (writeOperTxn != null) {
1156                         writeOperTxn.delete(LogicalDatastoreType.OPERATIONAL, routerDpnListIdentifier);
1157                     } else {
1158                         MDSALUtil.syncDelete(broker, LogicalDatastoreType.OPERATIONAL, routerDpnListIdentifier);
1159                     }
1160                 } else {
1161                     if (writeOperTxn != null) {
1162                         writeOperTxn.delete(LogicalDatastoreType.OPERATIONAL, routerDpnListIdentifier.child(
1163                                 org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.dpn.vpninterfaces.list.RouterInterfaces.class,
1164                                 new RouterInterfacesKey(vpnInterfaceName)));
1165                     } else {
1166                         MDSALUtil.syncDelete(broker, LogicalDatastoreType.OPERATIONAL, routerDpnListIdentifier.child(
1167                                 org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.dpn.vpninterfaces.list.RouterInterfaces.class,
1168                                 new RouterInterfacesKey(vpnInterfaceName)));
1169                     }
1170                 }
1171             }
1172         }
1173     }
1174
1175     static void removeFromDpnRoutersMap(DataBroker broker, String routerName, String vpnInterfaceName,
1176                                         OdlInterfaceRpcService ifaceMgrRpcService, WriteTransaction writeOperTxn) {
1177         BigInteger dpId = getDpnForInterface(ifaceMgrRpcService, vpnInterfaceName);
1178         if (dpId.equals(BigInteger.ZERO)) {
1179             LOG.warn("NAT Service : removeFromDpnRoutersMap() : Could not retrieve DPN ID for interface {} to handle router {} dissociation model",
1180                     vpnInterfaceName, routerName);
1181             return;
1182         }
1183         removeFromDpnRoutersMap(broker, routerName, vpnInterfaceName, dpId, ifaceMgrRpcService, writeOperTxn);
1184     }
1185
1186     static void removeFromDpnRoutersMap(DataBroker broker, String routerName, String vpnInterfaceName, BigInteger curDpnId,
1187                                            OdlInterfaceRpcService ifaceMgrRpcService, WriteTransaction writeOperTxn) {
1188         /*
1189             1) Get the DpnRoutersList for the DPN.
1190             2) Get the RoutersList identifier for the DPN and router.
1191             3) Get the VPN interfaces for the router (routerList) through which it is connected to the DPN.
1192             4) If the removed VPN interface is the only interface through which the router is connected to the DPN,
1193              then remove RouterList.
1194          */
1195
1196         LOG.debug("NAT Service : removeFromDpnRoutersMap() : Removing the DPN {} and router {} for the Interface {}" +
1197                 " in the ODL-L3VPN : DPNRouters map", curDpnId, routerName, vpnInterfaceName);
1198
1199         //Get the dpn-routers-list instance for the current DPN.
1200         InstanceIdentifier<DpnRoutersList> dpnRoutersListIdentifier = getDpnRoutersId(curDpnId);
1201         Optional<DpnRoutersList> dpnRoutersListData = read(broker, LogicalDatastoreType.OPERATIONAL,
1202                 dpnRoutersListIdentifier);
1203
1204         if (dpnRoutersListData == null || !dpnRoutersListData.isPresent()) {
1205             LOG.debug("NAT Service : dpn-routers-list is not present for DPN {} in the ODL-L3VPN:dpn-routers model",
1206                     curDpnId);
1207             return;
1208         }
1209
1210         //Get the routers-list instance for the router on the current DPN only
1211         InstanceIdentifier<RoutersList> routersListIdentifier = getRoutersList(curDpnId, routerName);
1212         Optional<RoutersList> routersListData = read(broker, LogicalDatastoreType.OPERATIONAL, routersListIdentifier);
1213
1214         if (routersListData == null || !routersListData.isPresent()) {
1215             LOG.debug("NAT Service : routers-list is not present for the DPN {} in the ODL-L3VPN:dpn-routers model",
1216                     curDpnId);
1217             return;
1218         }
1219
1220         LOG.debug("NAT Service : Get the interfaces for the router {} from the NeutronVPN - router-interfaces-map",
1221                 routerName);
1222         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.
1223                 interfaces.map.RouterInterfaces> routerInterfacesId = getRoutersInterfacesIdentifier(routerName);
1224         Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.
1225                 RouterInterfaces> routerInterfacesData = read(broker, LogicalDatastoreType.CONFIGURATION,
1226                 routerInterfacesId);
1227
1228         if (routerInterfacesData == null || !routerInterfacesData.isPresent()) {
1229             LOG.debug("NAT Service : Unable to get the routers list for the DPN {}. Possibly all subnets removed" +
1230                     " from router {} OR Router {} has been deleted. Hence DPN router model WILL be cleared ", curDpnId,
1231                     routerName, routerName);
1232             writeOperTxn.delete(LogicalDatastoreType.OPERATIONAL, routersListIdentifier);
1233             return;
1234         }
1235
1236         //Get the VM interfaces for the router on the current DPN only.
1237         List<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.router.interfaces.Interfaces> vmInterfaces =
1238                 routerInterfacesData.get().getInterfaces();
1239         if (vmInterfaces == null) {
1240             LOG.debug("NAT Service : VM interfaces are not present for the router {} in the NeutronVPN - router-interfaces-map", routerName);
1241             return;
1242         }
1243
1244         //If the removed VPN interface is the only interface through which the router is connected to the DPN, then remove RouterList.
1245         for (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.router.interfaces.Interfaces vmInterface :
1246                 vmInterfaces) {
1247             String vmInterfaceName = vmInterface.getInterfaceId();
1248             BigInteger vmDpnId = getDpnForInterface(ifaceMgrRpcService, vmInterfaceName);
1249             if (vmDpnId.equals(BigInteger.ZERO) || !vmDpnId.equals(curDpnId)) {
1250                 LOG.debug("NAT Service : DPN ID {} for the removed interface {} is not the same as that of the DPN ID for the checked interface {} ",
1251                         curDpnId, vpnInterfaceName, vmDpnId, vmInterfaceName);
1252                 continue;
1253             }
1254             if(!vmInterfaceName.equalsIgnoreCase(vpnInterfaceName)) {
1255                 LOG.debug("NAT Service : Router {} is present in the DPN {} through the other interface {} " +
1256                         "Hence DPN router model WOULD NOT be cleared", routerName, curDpnId, vmInterfaceName);
1257                 return;
1258             }
1259         }
1260         LOG.debug("NAT Service : Router {} is present in the DPN {} only through the interface {} " +
1261                 "Hence DPN router model WILL be cleared. Possibly last VM for the router " +
1262                 "deleted in the DPN", routerName, curDpnId);
1263         writeOperTxn.delete(LogicalDatastoreType.OPERATIONAL, routersListIdentifier);
1264
1265     }
1266
1267     private static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.RouterInterfaces>
1268         getRoutersInterfacesIdentifier(String routerName){
1269         return InstanceIdentifier.builder(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterInterfacesMap.class)
1270                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.RouterInterfaces.class,
1271                         new org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.RouterInterfacesKey(new Uuid(routerName)))
1272                 .build();
1273     }
1274
1275     private static InstanceIdentifier<RoutersList> getRoutersList(BigInteger dpnId, String routerName) {
1276         return InstanceIdentifier.builder(DpnRouters.class)
1277                 .child(DpnRoutersList.class, new DpnRoutersListKey(dpnId))
1278                 .child(RoutersList.class, new RoutersListKey(routerName)).build();
1279     }
1280
1281     public static BigInteger getDpnForInterface(OdlInterfaceRpcService interfaceManagerRpcService, String ifName) {
1282         BigInteger nodeId = BigInteger.ZERO;
1283         try {
1284             GetDpidFromInterfaceInput
1285                     dpIdInput =
1286                     new GetDpidFromInterfaceInputBuilder().setIntfName(ifName).build();
1287             Future<RpcResult<GetDpidFromInterfaceOutput>>
1288                     dpIdOutput =
1289                     interfaceManagerRpcService.getDpidFromInterface(dpIdInput);
1290             RpcResult<GetDpidFromInterfaceOutput> dpIdResult = dpIdOutput.get();
1291             if (dpIdResult.isSuccessful()) {
1292                 nodeId = dpIdResult.getResult().getDpid();
1293             } else {
1294                 LOG.error("NAT Service : Could not retrieve DPN Id for interface {}", ifName);
1295             }
1296         } catch (NullPointerException | InterruptedException | ExecutionException e) {
1297             LOG.error("NAT Service : Exception when getting dpn for interface {}", ifName,  e);
1298         }
1299         return nodeId;
1300     }
1301
1302     public static List<ActionInfo> getEgressActionsForInterface(OdlInterfaceRpcService interfaceManager, String ifName,
1303             Long tunnelKey) {
1304         return getEgressActionsForInterface(interfaceManager, ifName, tunnelKey, 0);
1305     }
1306
1307     public static List<ActionInfo> getEgressActionsForInterface(OdlInterfaceRpcService interfaceManager, String ifName,
1308             Long tunnelKey, int pos) {
1309         LOG.debug("NAT Service : getEgressActionsForInterface called for interface {}", ifName);
1310         GetEgressActionsForInterfaceInputBuilder egressActionsBuilder = new GetEgressActionsForInterfaceInputBuilder()
1311                 .setIntfName(ifName);
1312         if (tunnelKey != null) {
1313             egressActionsBuilder.setTunnelKey(tunnelKey);
1314         }
1315
1316         List<ActionInfo> listActionInfo = new ArrayList<>();
1317         try {
1318             Future<RpcResult<GetEgressActionsForInterfaceOutput>> result = interfaceManager
1319                     .getEgressActionsForInterface(egressActionsBuilder.build());
1320             RpcResult<GetEgressActionsForInterfaceOutput> rpcResult = result.get();
1321             if (!rpcResult.isSuccessful()) {
1322                 LOG.warn("RPC Call to Get egress actions for interface {} returned with Errors {}", ifName,
1323                         rpcResult.getErrors());
1324             } else {
1325                 List<Action> actions = rpcResult.getResult().getAction();
1326                 for (Action action : actions) {
1327                     org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action actionClass = action
1328                             .getAction();
1329                     if (actionClass instanceof OutputActionCase) {
1330                         listActionInfo
1331                                 .add(new ActionInfo(ActionType.output, new String[] { ((OutputActionCase) actionClass)
1332                                         .getOutputAction().getOutputNodeConnector().getValue() }, pos++));
1333                     } else if (actionClass instanceof PushVlanActionCase) {
1334                         listActionInfo.add(new ActionInfo(ActionType.push_vlan, new String[] {}, pos++));
1335                     } else if (actionClass instanceof SetFieldCase) {
1336                         if (((SetFieldCase) actionClass).getSetField().getVlanMatch() != null) {
1337                             int vlanVid = ((SetFieldCase) actionClass).getSetField().getVlanMatch().getVlanId()
1338                                     .getVlanId().getValue();
1339                             listActionInfo.add(new ActionInfo(ActionType.set_field_vlan_vid,
1340                                     new String[] { Long.toString(vlanVid) }, pos++));
1341                         }
1342                     } else if (actionClass instanceof NxActionResubmitRpcAddGroupCase) {
1343                         Short tableId = ((NxActionResubmitRpcAddGroupCase)actionClass).getNxResubmit().getTable();
1344                         listActionInfo.add(new ActionInfo(ActionType.nx_resubmit,
1345                             new String[] { tableId.toString() }, pos++));
1346                     } else if (actionClass instanceof NxActionRegLoadNodesNodeTableFlowApplyActionsCase) {
1347                         NxRegLoad nxRegLoad =
1348                             ((NxActionRegLoadNodesNodeTableFlowApplyActionsCase)actionClass).getNxRegLoad();
1349                         listActionInfo.add(new ActionInfo(ActionType.nx_load_reg_6,
1350                             new String[] { nxRegLoad.getDst().getStart().toString(),
1351                                 nxRegLoad.getDst().getEnd().toString(),
1352                                 nxRegLoad.getValue().toString(10)}, pos++));
1353                     }
1354                 }
1355             }
1356         } catch (InterruptedException | ExecutionException e) {
1357             LOG.warn("Exception when egress actions for interface {}", ifName, e);
1358         }
1359         return listActionInfo;
1360     }
1361
1362     public static Port getNeutronPortForRouterGetewayIp(DataBroker broker, IpAddress targetIP) {
1363         return getNeutronPortForIp(broker, targetIP, NeutronConstants.DEVICE_OWNER_GATEWAY_INF);
1364     }
1365
1366     public static List<Port> getNeutronPorts(DataBroker broker) {
1367         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports>
1368         portsIdentifier = InstanceIdentifier
1369                 .create(Neutron.class)
1370                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports.class);
1371         Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports> portsOptional = read(
1372                 broker, LogicalDatastoreType.CONFIGURATION, portsIdentifier);
1373
1374         if (!portsOptional.isPresent() || portsOptional.get().getPort() == null) {
1375             LOG.trace("No neutron ports found");
1376             return Collections.EMPTY_LIST;
1377         }
1378
1379         return portsOptional.get().getPort();
1380
1381     }
1382
1383     public static Port getNeutronPortForIp(DataBroker broker,
1384         IpAddress targetIP, String deviceType) {
1385         List<Port> ports = getNeutronPorts(
1386                 broker);
1387
1388         for (Port port : ports) {
1389             if (deviceType.equals(port.getDeviceOwner()) && port.getFixedIps() != null) {
1390                 for (FixedIps ip : port.getFixedIps()) {
1391                     if (Objects.equals(ip.getIpAddress(), targetIP)) {
1392                         return port;
1393                     }
1394                 }
1395             }
1396         }
1397
1398         return null;
1399     }
1400
1401     public static Uuid getSubnetIdForFloatingIp(Port port, IpAddress targetIP) {
1402         if (port == null) {
1403             return null;
1404         }
1405         for (FixedIps ip : port.getFixedIps()) {
1406             if (Objects.equals(ip.getIpAddress(), targetIP)) {
1407                 return ip.getSubnetId();
1408             }
1409         }
1410
1411         return null;
1412     }
1413
1414     public static Subnetmap getSubnetMap(DataBroker broker, Uuid subnetId) {
1415         InstanceIdentifier<Subnetmap> subnetmapId = InstanceIdentifier.builder(Subnetmaps.class)
1416                 .child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
1417         Optional<Subnetmap> subnetOpt = read(broker, LogicalDatastoreType.CONFIGURATION, subnetmapId);
1418         return subnetOpt.isPresent() ? subnetOpt.get() : null;
1419     }
1420
1421     public static List<Uuid> getSubnetIdsFromNetworkId(DataBroker broker, Uuid networkId) {
1422         InstanceIdentifier<NetworkMap> id = InstanceIdentifier.builder(NetworkMaps.class)
1423                 .child(NetworkMap.class, new NetworkMapKey(networkId)).build();
1424         Optional<NetworkMap> optionalNetworkMap = read(broker, LogicalDatastoreType.CONFIGURATION, id);
1425         return optionalNetworkMap.isPresent() ? optionalNetworkMap.get().getSubnetIdList() : null;
1426     }
1427
1428     public static String getSubnetGwMac(DataBroker broker, Uuid subnetId, String vpnName) {
1429         if (subnetId == null) {
1430             return null;
1431         }
1432
1433         InstanceIdentifier<Subnet> subnetInst = InstanceIdentifier.create(Neutron.class).child(Subnets.class)
1434                 .child(Subnet.class, new SubnetKey(subnetId));
1435         Optional<Subnet> subnetOpt = read(broker, LogicalDatastoreType.CONFIGURATION, subnetInst);
1436         if (!subnetOpt.isPresent()) {
1437             return null;
1438         }
1439
1440         IpAddress gatewayIp = subnetOpt.get().getGatewayIp();
1441         if (gatewayIp == null) {
1442             LOG.trace("No GW ip found for subnet {}", subnetId.getValue());
1443             return null;
1444         }
1445
1446         InstanceIdentifier<VpnPortipToPort> portIpInst = InstanceIdentifier.builder(NeutronVpnPortipPortData.class)
1447                 .child(VpnPortipToPort.class, new VpnPortipToPortKey(gatewayIp.getIpv4Address().getValue(), vpnName))
1448                 .build();
1449         Optional<VpnPortipToPort> portIpToPortOpt = read(broker, LogicalDatastoreType.CONFIGURATION, portIpInst);
1450         if (portIpToPortOpt.isPresent()) {
1451             return portIpToPortOpt.get().getMacAddress();
1452         }
1453
1454         InstanceIdentifier<LearntVpnVipToPort> learntIpInst = InstanceIdentifier.builder(LearntVpnVipToPortData.class)
1455                 .child(LearntVpnVipToPort.class, new LearntVpnVipToPortKey(gatewayIp.getIpv4Address().getValue(), vpnName))
1456                 .build();
1457         Optional<LearntVpnVipToPort> learntIpToPortOpt = read(broker, LogicalDatastoreType.OPERATIONAL, learntIpInst);
1458         if (learntIpToPortOpt.isPresent()) {
1459             return learntIpToPortOpt.get().getMacAddress();
1460         }
1461
1462         LOG.error("No resolution was found to GW ip {} in subnet {}", gatewayIp, subnetId.getValue());
1463         return null;
1464     }
1465
1466     public static boolean isIPv6Subnet(String prefix) {
1467         IpPrefix ipPrefix = new IpPrefix(prefix.toCharArray());
1468         if (ipPrefix.getIpv6Prefix() != null) {
1469             return true;
1470         }
1471         return false;
1472     }
1473
1474     static InstanceIdentifier<DpnRoutersList> getDpnRoutersId(BigInteger dpnId) {
1475         return InstanceIdentifier.builder(DpnRouters.class)
1476                 .child(DpnRoutersList.class, new DpnRoutersListKey(dpnId)).build();
1477     }
1478
1479     static InstanceIdentifier<DpnVpninterfacesList> getRouterDpnId(String routerName, BigInteger dpnId) {
1480         return InstanceIdentifier.builder(NeutronRouterDpns.class)
1481                 .child(RouterDpnList.class, new RouterDpnListKey(routerName))
1482                 .child(DpnVpninterfacesList.class, new DpnVpninterfacesListKey(dpnId)).build();
1483     }
1484
1485     static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface getInterface(DataBroker broker, String interfaceName) {
1486         Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface> optInterface =
1487                 read(broker, LogicalDatastoreType.CONFIGURATION, getInterfaceIdentifier(interfaceName));
1488         if(optInterface.isPresent()) {
1489             return optInterface.get();
1490         }
1491         return null;
1492     }
1493
1494     static InstanceIdentifier<RouterDpnList> getRouterId(String routerName) {
1495         return InstanceIdentifier.builder(NeutronRouterDpns.class)
1496                 .child(RouterDpnList.class, new RouterDpnListKey(routerName)).build();
1497     }
1498
1499     protected static String getFloatingIpPortMacFromFloatingIpId(DataBroker broker, Uuid floatingIpId) {
1500         InstanceIdentifier id = buildfloatingIpIdToPortMappingIdentifier(floatingIpId);
1501         Optional<FloatingIpIdToPortMapping> optFloatingIpIdToPortMapping = read(broker, LogicalDatastoreType
1502                         .CONFIGURATION, id);
1503         if (optFloatingIpIdToPortMapping.isPresent()) {
1504             return optFloatingIpIdToPortMapping.get().getFloatingIpPortMacAddress();
1505         }
1506         return null;
1507     }
1508
1509     protected static Uuid getFloatingIpPortSubnetIdFromFloatingIpId(DataBroker broker, Uuid floatingIpId) {
1510         InstanceIdentifier id = buildfloatingIpIdToPortMappingIdentifier(floatingIpId);
1511         Optional<FloatingIpIdToPortMapping> optFloatingIpIdToPortMapping = read(broker, LogicalDatastoreType
1512                 .CONFIGURATION, id);
1513         if (optFloatingIpIdToPortMapping.isPresent()) {
1514             return optFloatingIpIdToPortMapping.get().getFloatingIpPortSubnetId();
1515         }
1516         return null;
1517     }
1518
1519     static InstanceIdentifier<FloatingIpIdToPortMapping> buildfloatingIpIdToPortMappingIdentifier (Uuid floatingIpId) {
1520         return InstanceIdentifier.builder(FloatingIpPortInfo.class).child(FloatingIpIdToPortMapping.class, new
1521                 FloatingIpIdToPortMappingKey(floatingIpId)).build();
1522     }
1523
1524     static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface> getInterfaceIdentifier(String interfaceName) {
1525         return InstanceIdentifier.builder(Interfaces.class)
1526                 .child(
1527                         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface.class, new InterfaceKey(interfaceName)).build();
1528     }
1529     static final FutureCallback<Void> DEFAULT_CALLBACK =
1530             new FutureCallback<Void>() {
1531                 @Override
1532                 public void onSuccess(Void result) {
1533                     LOG.debug("NAT Service : Success in Datastore operation");
1534                 }
1535
1536                 @Override
1537                 public void onFailure(Throwable error) {
1538                     LOG.error("NAT Service : Error in Datastore operation", error);
1539                 }
1540
1541                 ;
1542             };
1543
1544     static <T extends DataObject> void delete(DataBroker broker, LogicalDatastoreType datastoreType,
1545                                                      InstanceIdentifier<T> path) {
1546         delete(broker, datastoreType, path, DEFAULT_CALLBACK);
1547     }
1548
1549    static <T extends DataObject> void delete(DataBroker broker, LogicalDatastoreType datastoreType,
1550                                                      InstanceIdentifier<T> path, FutureCallback<Void> callback) {
1551         WriteTransaction tx = broker.newWriteOnlyTransaction();
1552         tx.delete(datastoreType, path);
1553         Futures.addCallback(tx.submit(), callback);
1554     }
1555     static Interface getInterfaceStateFromOperDS(DataBroker dataBroker, String interfaceName) {
1556         InstanceIdentifier<Interface> ifStateId =
1557                 buildStateInterfaceId(interfaceName);
1558         Optional<Interface> ifStateOptional = read(dataBroker, LogicalDatastoreType.OPERATIONAL, ifStateId);
1559         if (ifStateOptional.isPresent()) {
1560             return ifStateOptional.get();
1561         }
1562
1563         return null;
1564     }
1565
1566     static InstanceIdentifier<Interface>
1567     buildStateInterfaceId(String interfaceName) {
1568         InstanceIdentifier.InstanceIdentifierBuilder<Interface> idBuilder =
1569                 InstanceIdentifier.builder(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState.class)
1570                         .child(Interface.class,
1571                                 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey(interfaceName));
1572         InstanceIdentifier<Interface> id = idBuilder.build();
1573         return id;
1574     }
1575 }