2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.natservice.internal;
11 import java.math.BigInteger;
13 import com.google.common.collect.Lists;
14 import com.google.common.collect.Sets;
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;
158 import org.slf4j.Logger;
159 import org.slf4j.LoggerFactory;
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;
177 public class NatUtil {
179 private static String OF_URI_SEPARATOR = ":";
180 private static final Logger LOG = LoggerFactory.getLogger(NatUtil.class);
183 getCookieSnatFlow() computes and returns a unique cookie value for the NAT flows using the router ID as the reference value.
185 public static BigInteger getCookieSnatFlow(long routerId) {
186 return NatConstants.COOKIE_NAPT_BASE.add(new BigInteger("0110000", 16)).add(
187 BigInteger.valueOf(routerId));
191 getCookieNaptFlow() computes and returns a unique cookie value for the NAPT flows using the router ID as the reference value.
193 public static BigInteger getCookieNaptFlow(long routerId) {
194 return NatConstants.COOKIE_NAPT_BASE.add(new BigInteger("0111000", 16)).add(
195 BigInteger.valueOf(routerId));
199 getVpnId() returns the VPN ID from the VPN name
201 public static long getVpnId(DataBroker broker, String vpnName) {
202 if(vpnName == null) {
203 return NatConstants.INVALID_ID;
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);
212 long vpnId = NatConstants.INVALID_ID;
213 if(vpnInstance.isPresent()) {
214 Long vpnIdAsLong = vpnInstance.get().getVpnId();
215 if(vpnIdAsLong != null){
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");
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");
236 Long vpnId = NatUtil.getVpnId(broker, vpnUuid.getValue());
240 static InstanceIdentifier<RouterPorts> getRouterPortsId(String routerId) {
241 return InstanceIdentifier.builder(FloatingIpInfo.class).child(RouterPorts.class, new RouterPortsKey(routerId)).build();
244 static InstanceIdentifier<Routermapping> getRouterVpnMappingId(String routerId) {
245 return InstanceIdentifier.builder(RouterToVpnMapping.class).child(Routermapping.class, new RoutermappingKey(routerId)).build();
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();
253 static InstanceIdentifier<InternalToExternalPortMap> getIntExtPortMapIdentifier(String routerId, String portName,
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();
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();
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;
275 getFlowRef() returns a string identfier for the SNAT flows using the router ID as the reference.
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();
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();
290 getNetworkIdFromRouterId() returns the network-id from the below model using the router-id as the key
291 container ext-routers {
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
300 leaf-list subnet-ids { type yang:uuid; }
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();
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;
322 * getEnableSnatFromRouterId() returns IsSnatEnabled true is routerID is present in external n/w otherwise returns false
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();
333 getVpnIdfromNetworkId() returns the vpnid from the below model using the network ID as the key.
334 container external-networks {
340 leaf vpnid { type yang:uuid; }
341 leaf-list router-ids { type yang:uuid; }
342 leaf-list subnet-ids{ type yang:uuid; }
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();
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();
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();
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();
382 private static InstanceIdentifier<Networks> buildNetworkIdentifier(Uuid networkId) {
383 InstanceIdentifier<Networks> network = InstanceIdentifier.builder(ExternalNetworks.class).child
384 (Networks.class, new NetworksKey(networkId)).build();
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 {
396 leaf router-id { type uint32; }
397 leaf primary-switch-id { type uint64; }
398 leaf secondary-switch-id { type uint64; }
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();
414 private static InstanceIdentifier<RouterToNaptSwitch> buildNaptSwitchIdentifier(String routerId) {
415 InstanceIdentifier<RouterToNaptSwitch> rtrNaptSw = InstanceIdentifier.builder(NaptSwitches.class).child
416 (RouterToNaptSwitch.class, new RouterToNaptSwitchKey(routerId)).build();
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();
430 private static InstanceIdentifier<RouterIds> buildRouterIdentifier(Long routerId) {
431 InstanceIdentifier<RouterIds> routerIds = InstanceIdentifier.builder(RouterIdName.class).child
432 (RouterIds.class, new RouterIdsKey(routerId)).build();
436 public static <T extends DataObject> Optional<T> read(DataBroker broker, LogicalDatastoreType datastoreType,
437 InstanceIdentifier<T> path) {
439 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
441 Optional<T> result = Optional.absent();
443 result = tx.read(datastoreType, path).get();
444 } catch (Exception e) {
445 throw new RuntimeException(e);
451 static InstanceIdentifier<VpnInstanceOpDataEntry> getVpnInstanceOpDataIdentifier(String vrfId) {
452 return InstanceIdentifier.builder(VpnInstanceOpData.class)
453 .child(VpnInstanceOpDataEntry.class, new VpnInstanceOpDataEntryKey(vrfId)).build();
456 public static long readVpnId(DataBroker broker, String vpnName) {
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);
463 long vpnId = NatConstants.INVALID_ID;
464 if(vpnInstance.isPresent()) {
465 vpnId = vpnInstance.get().getVpnId();
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);
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;
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();
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();
503 getVpnRd returns the rd (route distinguisher) which is the VRF ID from the below model using the vpnName
505 key "vpn-instance-name"
506 leaf vpn-instance-name {
517 public static String getVpnRd(DataBroker broker, String vpnName) {
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);
525 if(vpnInstance.isPresent()) {
526 rd = vpnInstance.get().getVrfId();
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 {
534 list ip-port-mapping {
536 leaf router-id { type uint32; }
537 list intext-ip-protocol-type {
539 leaf protocol { type protocol-types; }
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 {
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();
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();
570 public static FlowEntity buildFlowEntity(BigInteger dpnId, short tableId, String flowId, int priority, String flowName,
571 BigInteger cookie, List<MatchInfo> listMatchInfo) {
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);
583 static boolean isVpnInterfaceConfigured(DataBroker broker, String interfaceName)
585 InstanceIdentifier<VpnInterface> interfaceId = getVpnInterfaceIdentifier(interfaceName);
586 Optional<VpnInterface> configuredVpnInterface = read(broker, LogicalDatastoreType.CONFIGURATION, interfaceId);
588 if (configuredVpnInterface.isPresent()) {
594 static InstanceIdentifier<VpnInterface> getVpnInterfaceIdentifier(String vpnInterfaceName) {
595 return InstanceIdentifier.builder(VpnInterfaces.class)
596 .child(VpnInterface.class, new VpnInterfaceKey(vpnInterfaceName)).build();
599 static VpnInterface getConfiguredVpnInterface(DataBroker broker, String interfaceName) {
600 InstanceIdentifier<VpnInterface> interfaceId = getVpnInterfaceIdentifier(interfaceName);
601 Optional<VpnInterface> configuredVpnInterface = read(broker, LogicalDatastoreType.CONFIGURATION, interfaceId);
603 if (configuredVpnInterface.isPresent()) {
604 return configuredVpnInterface.get();
609 public static String getDpnFromNodeConnectorId(NodeConnectorId portId) {
611 * NodeConnectorId is of form 'openflow:dpnid:portnum'
613 String[] split = portId.getValue().split(OF_URI_SEPARATOR);
614 if (split == null || split.length != 3) {
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));
632 description "vpn-id";
636 description "vpn name";
640 description "The UUID of the tenant that will own the subnet.";
645 description "UUID of router ";
647 leaf-list network_ids {
649 description "UUID representing the network ";
653 Method returns router Id associated to a VPN
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,
661 if (optionalVpnMap.isPresent()) {
662 Uuid routerId = optionalVpnMap.get().getRouterId();
663 if (routerId != null) {
664 return routerId.getValue();
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,
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();
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();
696 return NatConstants.INVALID_ID;
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);
705 return vpnUuid.getValue();
708 public static void addPrefixToBGP(DataBroker broker,
709 IBgpManager bgpManager,
710 IFibManager fibManager,
716 RouteOrigin origin) {
718 LOG.info("ADD: Adding Fib entry rd {} prefix {} nextHop {} label {}", rd, prefix, nextHopIp, label);
719 if (nextHopIp == null)
721 log.error("addPrefix failed since nextHopIp cannot be null.");
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);
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();
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;
744 /* container snatint-ip-port-map {
745 list intip-port-map {
747 leaf router-id { type uint32; }
750 leaf internal-ip { type string; }
751 list int-ip-proto-type {
753 leaf protocol { type protocol-types; }
754 leaf-list ports { type uint16; }
759 Method returns InternalIp port List
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();
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;
777 public static ProtocolTypes getProtocolType(NAPTEntryEvent.Protocol protocol) {
778 ProtocolTypes protocolType = ProtocolTypes.TCP.toString().equals(protocol.toString()) ? ProtocolTypes.TCP : ProtocolTypes.UDP;
782 public static InstanceIdentifier<NaptSwitches> getNaptSwitchesIdentifier() {
783 return InstanceIdentifier.create(NaptSwitches.class);
786 public static InstanceIdentifier<RouterToNaptSwitch> buildNaptSwitchRouterIdentifier(String routerId) {
787 return InstanceIdentifier.create(NaptSwitches.class).child(RouterToNaptSwitch.class, new RouterToNaptSwitchKey(routerId));
790 public static String toStringIpAddress(byte[] ipAddress, Logger log)
793 if (ipAddress == null) {
798 ip = InetAddress.getByAddress(ipAddress).getHostAddress();
799 } catch(UnknownHostException e) {
800 log.error("NAT Service : Caught exception during toStringIpAddress()");
806 public static String getGroupIdKey(String routerName){
807 String groupIdKey = new String("snatmiss." + routerName);
811 public static long createGroupId(String groupIdKey,IdManagerService idManager) {
812 AllocateIdInput getIdInput = new AllocateIdInputBuilder()
813 .setPoolName(NatConstants.SNAT_IDPOOL_NAME).setIdKey(groupIdKey)
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) {
825 public static void removePrefixFromBGP(DataBroker broker , IBgpManager bgpManager, IFibManager fibManager, String rd, String prefix, Logger log) {
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);
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);
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);
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();
859 public static InstanceIdentifier<IpPortMapping> getIportMappingIdentifier(long routerId) {
860 return InstanceIdentifier.builder(IntextIpPortMap.class).child(IpPortMapping.class, new IpPortMappingKey(routerId)).build();
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();
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());
879 Set<String> uniqueExternalIps = Sets.newHashSet(externalIps);
880 externalIps = Lists.newArrayList(uniqueExternalIps);
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());
895 return externalIpsLabel;
900 container external-ips-counter {
902 list external-counters{
904 leaf segment-id { type uint32; }
905 list external-ip-counter {
907 leaf external-ip { type string; }
908 leaf counter { type uint8; }
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;
931 return leastLoadedExternalIp;
934 public static String[] getSubnetIpAndPrefix(DataBroker dataBroker, Uuid subnetId){
935 String subnetIP = getSubnetIp(dataBroker, subnetId);
936 if(subnetId != null){
937 return getSubnetIpAndPrefix(subnetIP);
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))
947 Optional<Subnetmap> removedSubnet = read(dataBroker, LogicalDatastoreType.CONFIGURATION, subnetmapId);
948 if(removedSubnet.isPresent()) {
949 Subnetmap subnetMapEntry = removedSubnet.get();
950 return subnetMapEntry.getSubnetIp();
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];
962 return new String[] {subnetIp, subnetPrefix};
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];
972 return new String[] {leastLoadedExtIp, leastLoadedExtIpPrefix};
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());
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());
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();
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();
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);
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);
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);
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);
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);
1061 LOG.debug("NAT Service : Adding the DPN {} and router {} for the Interface {} in the ODL-L3VPN : " +
1063 dpId, routerName, interfaceName);
1064 InstanceIdentifier<DpnRoutersList> dpnRoutersListIdentifier = getDpnRoutersId(dpId);
1066 Optional<DpnRoutersList> optionalDpnRoutersList = read(broker, LogicalDatastoreType.OPERATIONAL, dpnRoutersListIdentifier);
1068 if (optionalDpnRoutersList.isPresent()) {
1069 RoutersList routersList = new RoutersListBuilder().setKey(new RoutersListKey(routerName)).setRouter(routerName)
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);
1078 LOG.debug("NAT Service : Router {} already mapped to the DPN {} in the ODL-L3VPN : DPNRouters map",
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);
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);
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);
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)));
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);
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);
1134 LOG.debug("NAT Service : dpn-vpninterfaces-list does not exist in the odl-l3vpn:neutron-router-dpns model " +
1135 "for the router {}", routerName);
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);
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();
1153 if (routerInterfaces != null && routerInterfaces.remove(routerInterface)) {
1154 if (routerInterfaces.isEmpty()) {
1155 if (writeOperTxn != null) {
1156 writeOperTxn.delete(LogicalDatastoreType.OPERATIONAL, routerDpnListIdentifier);
1158 MDSALUtil.syncDelete(broker, LogicalDatastoreType.OPERATIONAL, routerDpnListIdentifier);
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)));
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)));
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);
1183 removeFromDpnRoutersMap(broker, routerName, vpnInterfaceName, dpId, ifaceMgrRpcService, writeOperTxn);
1186 static void removeFromDpnRoutersMap(DataBroker broker, String routerName, String vpnInterfaceName, BigInteger curDpnId,
1187 OdlInterfaceRpcService ifaceMgrRpcService, WriteTransaction writeOperTxn) {
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.
1196 LOG.debug("NAT Service : removeFromDpnRoutersMap() : Removing the DPN {} and router {} for the Interface {}" +
1197 " in the ODL-L3VPN : DPNRouters map", curDpnId, routerName, vpnInterfaceName);
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);
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",
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);
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",
1220 LOG.debug("NAT Service : Get the interfaces for the router {} from the NeutronVPN - router-interfaces-map",
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);
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);
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);
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 :
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);
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);
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);
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)))
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();
1281 public static BigInteger getDpnForInterface(OdlInterfaceRpcService interfaceManagerRpcService, String ifName) {
1282 BigInteger nodeId = BigInteger.ZERO;
1284 GetDpidFromInterfaceInput
1286 new GetDpidFromInterfaceInputBuilder().setIntfName(ifName).build();
1287 Future<RpcResult<GetDpidFromInterfaceOutput>>
1289 interfaceManagerRpcService.getDpidFromInterface(dpIdInput);
1290 RpcResult<GetDpidFromInterfaceOutput> dpIdResult = dpIdOutput.get();
1291 if (dpIdResult.isSuccessful()) {
1292 nodeId = dpIdResult.getResult().getDpid();
1294 LOG.error("NAT Service : Could not retrieve DPN Id for interface {}", ifName);
1296 } catch (NullPointerException | InterruptedException | ExecutionException e) {
1297 LOG.error("NAT Service : Exception when getting dpn for interface {}", ifName, e);
1302 public static List<ActionInfo> getEgressActionsForInterface(OdlInterfaceRpcService interfaceManager, String ifName,
1304 return getEgressActionsForInterface(interfaceManager, ifName, tunnelKey, 0);
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);
1316 List<ActionInfo> listActionInfo = new ArrayList<>();
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());
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
1329 if (actionClass instanceof OutputActionCase) {
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++));
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++));
1356 } catch (InterruptedException | ExecutionException e) {
1357 LOG.warn("Exception when egress actions for interface {}", ifName, e);
1359 return listActionInfo;
1362 public static Port getNeutronPortForRouterGetewayIp(DataBroker broker, IpAddress targetIP) {
1363 return getNeutronPortForIp(broker, targetIP, NeutronConstants.DEVICE_OWNER_GATEWAY_INF);
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);
1374 if (!portsOptional.isPresent() || portsOptional.get().getPort() == null) {
1375 LOG.trace("No neutron ports found");
1376 return Collections.EMPTY_LIST;
1379 return portsOptional.get().getPort();
1383 public static Port getNeutronPortForIp(DataBroker broker,
1384 IpAddress targetIP, String deviceType) {
1385 List<Port> ports = getNeutronPorts(
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)) {
1401 public static Uuid getSubnetIdForFloatingIp(Port port, IpAddress targetIP) {
1405 for (FixedIps ip : port.getFixedIps()) {
1406 if (Objects.equals(ip.getIpAddress(), targetIP)) {
1407 return ip.getSubnetId();
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;
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;
1428 public static String getSubnetGwMac(DataBroker broker, Uuid subnetId, String vpnName) {
1429 if (subnetId == null) {
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()) {
1440 IpAddress gatewayIp = subnetOpt.get().getGatewayIp();
1441 if (gatewayIp == null) {
1442 LOG.trace("No GW ip found for subnet {}", subnetId.getValue());
1446 InstanceIdentifier<VpnPortipToPort> portIpInst = InstanceIdentifier.builder(NeutronVpnPortipPortData.class)
1447 .child(VpnPortipToPort.class, new VpnPortipToPortKey(gatewayIp.getIpv4Address().getValue(), vpnName))
1449 Optional<VpnPortipToPort> portIpToPortOpt = read(broker, LogicalDatastoreType.CONFIGURATION, portIpInst);
1450 if (portIpToPortOpt.isPresent()) {
1451 return portIpToPortOpt.get().getMacAddress();
1454 InstanceIdentifier<LearntVpnVipToPort> learntIpInst = InstanceIdentifier.builder(LearntVpnVipToPortData.class)
1455 .child(LearntVpnVipToPort.class, new LearntVpnVipToPortKey(gatewayIp.getIpv4Address().getValue(), vpnName))
1457 Optional<LearntVpnVipToPort> learntIpToPortOpt = read(broker, LogicalDatastoreType.OPERATIONAL, learntIpInst);
1458 if (learntIpToPortOpt.isPresent()) {
1459 return learntIpToPortOpt.get().getMacAddress();
1462 LOG.error("No resolution was found to GW ip {} in subnet {}", gatewayIp, subnetId.getValue());
1466 public static boolean isIPv6Subnet(String prefix) {
1467 IpPrefix ipPrefix = new IpPrefix(prefix.toCharArray());
1468 if (ipPrefix.getIpv6Prefix() != null) {
1474 static InstanceIdentifier<DpnRoutersList> getDpnRoutersId(BigInteger dpnId) {
1475 return InstanceIdentifier.builder(DpnRouters.class)
1476 .child(DpnRoutersList.class, new DpnRoutersListKey(dpnId)).build();
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();
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();
1494 static InstanceIdentifier<RouterDpnList> getRouterId(String routerName) {
1495 return InstanceIdentifier.builder(NeutronRouterDpns.class)
1496 .child(RouterDpnList.class, new RouterDpnListKey(routerName)).build();
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();
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();
1519 static InstanceIdentifier<FloatingIpIdToPortMapping> buildfloatingIpIdToPortMappingIdentifier (Uuid floatingIpId) {
1520 return InstanceIdentifier.builder(FloatingIpPortInfo.class).child(FloatingIpIdToPortMapping.class, new
1521 FloatingIpIdToPortMappingKey(floatingIpId)).build();
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)
1527 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface.class, new InterfaceKey(interfaceName)).build();
1529 static final FutureCallback<Void> DEFAULT_CALLBACK =
1530 new FutureCallback<Void>() {
1532 public void onSuccess(Void result) {
1533 LOG.debug("NAT Service : Success in Datastore operation");
1537 public void onFailure(Throwable error) {
1538 LOG.error("NAT Service : Error in Datastore operation", error);
1544 static <T extends DataObject> void delete(DataBroker broker, LogicalDatastoreType datastoreType,
1545 InstanceIdentifier<T> path) {
1546 delete(broker, datastoreType, path, DEFAULT_CALLBACK);
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);
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();
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();