2 * Copyright (c) 2015 - 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
8 package org.opendaylight.vpnservice.neutronvpn;
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.SettableFuture;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
17 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
18 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInstances;
19 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
20 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.VpnTargets;
21 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.VpnTargetsBuilder;
22 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets.VpnTarget;
23 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets
25 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets.VpnTargetKey;
26 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
27 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceBuilder;
28 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceKey;
29 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.vpn.instance
31 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
32 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceBuilder;
33 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.Adjacencies;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.AdjacenciesBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.Adjacency;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.AdjacencyBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.AdjacencyKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.PortKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.lockmanager.rev150819.LockManagerService;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.AssociateNetworksInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.AssociateNetworksOutput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.AssociateNetworksOutputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.AssociateRouterInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.CreateL3VPNInput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.CreateL3VPNOutput;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.CreateL3VPNOutputBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DeleteL3VPNInput;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DeleteL3VPNOutput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DeleteL3VPNOutputBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DissociateNetworksInput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DissociateNetworksOutput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DissociateNetworksOutputBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DissociateRouterInput;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.GetL3VPNInput;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.GetL3VPNInputBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.GetL3VPNOutput;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.GetL3VPNOutputBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.L3vpnInstance;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.NeutronvpnService;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.Subnetmaps;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.VpnMaps;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.createl3vpn.input.L3vpn;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.getl3vpn.output.L3vpnInstances;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.getl3vpn.output
79 .L3vpnInstancesBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.networkmaps.NetworkMap;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.networkmaps.NetworkMapBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.networkmaps.NetworkMapKey;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.neutron.port.data
84 .PortFixedipToPortNameBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.neutron.port.data
86 .PortNameToPortUuidBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.subnetmaps.Subnetmap;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.subnetmaps.SubnetmapBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.subnetmaps.SubnetmapKey;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.vpnmaps.VpnMap;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.vpnmaps.VpnMapBuilder;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.vpnmaps.VpnMapKey;
93 import org.opendaylight.yangtools.yang.binding.DataObject;
94 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
95 import org.opendaylight.yangtools.yang.common.RpcError;
96 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
97 import org.opendaylight.yangtools.yang.common.RpcResult;
98 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
99 import org.slf4j.Logger;
100 import org.slf4j.LoggerFactory;
102 import java.util.ArrayList;
103 import java.util.Arrays;
104 import java.util.List;
105 import java.util.concurrent.ExecutionException;
106 import java.util.concurrent.Future;
108 public class NeutronvpnManager implements NeutronvpnService, AutoCloseable {
110 private static final Logger logger = LoggerFactory.getLogger(NeutronvpnManager.class);
111 private final DataBroker broker;
112 private LockManagerService lockManager;
113 IMdsalApiManager mdsalUtil;
116 * @param db - dataBroker reference
117 * @param mdsalManager - MDSAL Util API access
119 public NeutronvpnManager(final DataBroker db, IMdsalApiManager mdsalManager) {
121 mdsalUtil = mdsalManager;
124 public void setLockManager(LockManagerService lockManager) {
125 this.lockManager = lockManager;
129 public void close() throws Exception {
130 logger.info("Neutron VPN Manager Closed");
133 protected Subnetmap updateSubnetNode(Uuid subnetId, Uuid tenantId, Uuid networkId, Uuid routerId, Uuid vpnId,
137 SubnetmapBuilder builder = null;
139 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).
140 child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
141 Optional<Subnetmap> sn = read(LogicalDatastoreType.CONFIGURATION, id);
142 logger.debug("updating Subnet :read: ");
143 if (sn.isPresent()) {
144 builder = new SubnetmapBuilder(sn.get());
145 logger.debug("updating Subnet :existing: ");
147 builder = new SubnetmapBuilder().setKey(new SubnetmapKey(subnetId)).setId(subnetId);
148 logger.debug("updating Subnet :new: ");
151 if (routerId != null) {
152 builder.setRouterId(routerId);
154 if (networkId != null) {
155 builder.setNetworkId(networkId);
158 builder.setVpnId(vpnId);
160 if (tenantId != null) {
161 builder.setTenantId(tenantId);
164 if (portId != null) {
165 List<Uuid> portList = builder.getPortList();
166 if (portList == null) {
167 portList = new ArrayList<Uuid>();
169 portList.add(portId);
170 builder.setPortList(portList);
173 Subnetmap subnetmap = builder.build();
174 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap);
175 logger.debug("Created/Updated subnetmap node: {} ", subnetId.getValue());
178 } catch (Exception e) {
179 logger.error("Update local subnetmap failed for node: {} {} {} {} {} {} ",
180 subnetId.getValue(), tenantId.getValue(), networkId.getValue(), routerId.getValue(), vpnId
181 .getValue(), portId.getValue());
182 throw new RuntimeException(e);
187 protected Subnetmap removeFromSubnetNode(Uuid subnetId, Uuid networkId, Uuid routerId, Uuid vpnId, Uuid portId) {
188 Subnetmap subnetmap = null;
190 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).
191 child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
192 Optional<Subnetmap> sn = read(LogicalDatastoreType.CONFIGURATION, id);
194 if (sn.isPresent()) {
195 SubnetmapBuilder builder = new SubnetmapBuilder(sn.get());
197 if (routerId != null) {
198 builder.setRouterId(null);
200 if (networkId != null) {
201 builder.setNetworkId(null);
204 builder.setVpnId(null);
206 if (portId != null && builder.getPortList() != null) {
207 List<Uuid> portList = builder.getPortList();
208 portList.remove(portId);
209 builder.setPortList(portList);
212 subnetmap = builder.build();
213 logger.debug("Removing from existing subnetmap node: {} ", subnetId.getValue());
214 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap);
216 logger.warn("remove from non-existing subnetmap node: {} ", subnetId.getValue());
218 } catch (Exception e) {
219 logger.error("Remove from subnetmap failed for node: {} {} {} {} {} {} ", subnetId.getValue(), networkId
220 .getValue(), routerId.getValue(), vpnId.getValue(), portId.getValue());
221 throw new RuntimeException(e);
227 private void createSubnetToNetworkMapping(Uuid subnetId, Uuid networkId) {
229 InstanceIdentifier networkMapIdentifier = NeutronvpnUtils.buildNetworkMapIdentifier(networkId);
230 Optional<NetworkMap> optionalNetworkMap = read(LogicalDatastoreType.CONFIGURATION, networkMapIdentifier);
231 NetworkMapBuilder nwMapBuilder = null;
232 if (optionalNetworkMap.isPresent()) {
233 nwMapBuilder = new NetworkMapBuilder(optionalNetworkMap.get());
235 nwMapBuilder = new NetworkMapBuilder().setKey(new NetworkMapKey(networkId)).setNetworkId(networkId);
236 logger.debug("Adding a new network node in NetworkMaps DS for network {}", networkId.getValue());
238 List<Uuid> subnetIdList = nwMapBuilder.getSubnetIdList();
239 if (subnetIdList == null) {
240 subnetIdList = new ArrayList<>();
242 subnetIdList.add(subnetId);
243 nwMapBuilder.setSubnetIdList(subnetIdList);
244 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, networkMapIdentifier, nwMapBuilder.build());
245 logger.debug("Created subnet-network mapping for subnet {} network {}", subnetId.getValue(),
246 networkId.getValue());
247 } catch (Exception e) {
248 logger.error("Create subnet-network mapping failed for subnet {} network {}", subnetId.getValue(),
249 networkId.getValue());
253 private void deleteSubnetToNetworkMapping(Uuid subnetId, Uuid networkId) {
255 InstanceIdentifier networkMapIdentifier = NeutronvpnUtils.buildNetworkMapIdentifier(networkId);
256 Optional<NetworkMap> optionalNetworkMap = read(LogicalDatastoreType.CONFIGURATION, networkMapIdentifier);
257 if (optionalNetworkMap.isPresent()) {
258 NetworkMapBuilder nwMapBuilder = new NetworkMapBuilder(optionalNetworkMap.get());
259 List<Uuid> subnetIdList = nwMapBuilder.getSubnetIdList();
260 if (subnetIdList.remove(subnetId)) {
261 if (subnetIdList.size() == 0) {
262 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, networkMapIdentifier);
263 logger.debug("Deleted network node in NetworkMaps DS for network {}", subnetId.getValue(),
264 networkId.getValue());
266 nwMapBuilder.setSubnetIdList(subnetIdList);
267 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, networkMapIdentifier,
268 nwMapBuilder.build());
269 logger.debug("Deleted subnet-network mapping for subnet {} network {}", subnetId.getValue(),
270 networkId.getValue());
273 logger.error("Subnet {} is not mapped to network {}", subnetId.getValue(), networkId.getValue());
276 logger.error("network {} not present for subnet {} ", networkId, subnetId);
278 } catch (Exception e) {
279 logger.error("Delete subnet-network mapping failed for subnet {} network {}", subnetId.getValue(),
280 networkId.getValue());
284 private void updateVpnInstanceNode(String name, List<String> rd, List<String> irt, List<String> ert) {
287 VpnInstanceBuilder builder = null;
288 List<VpnTarget> vpnTargetList = new ArrayList<VpnTarget>();
289 InstanceIdentifier<VpnInstance> vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class).
290 child(VpnInstance.class, new VpnInstanceKey(name)).build();
291 Optional<VpnInstance> optionalVpn = read(LogicalDatastoreType.CONFIGURATION, vpnIdentifier);
292 logger.debug("Creating/Updating a new vpn-instance node: {} ", name);
293 if (optionalVpn.isPresent()) {
294 builder = new VpnInstanceBuilder(optionalVpn.get());
295 logger.debug("updating existing vpninstance node");
297 builder = new VpnInstanceBuilder().setKey(new VpnInstanceKey(name)).setVpnInstanceName(name);
300 if (irt != null && !irt.isEmpty()) {
301 if (ert != null && !ert.isEmpty()) {
302 List<String> commonRT = new ArrayList<String>(irt);
303 commonRT.retainAll(ert);
305 for (String common : commonRT) {
308 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(common)).setVrfRTValue
309 (common).setVrfRTType(VpnTarget.VrfRTType.Both).build();
310 vpnTargetList.add(vpnTarget);
313 for (String importRT : irt) {
314 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(importRT)).setVrfRTValue
315 (importRT).setVrfRTType(VpnTarget.VrfRTType.ImportExtcommunity).build();
316 vpnTargetList.add(vpnTarget);
320 if (ert != null && !ert.isEmpty()) {
321 for (String exportRT : ert) {
322 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(exportRT)).setVrfRTValue
323 (exportRT).setVrfRTType(VpnTarget.VrfRTType.ExportExtcommunity).build();
324 vpnTargetList.add(vpnTarget);
328 VpnTargets vpnTargets = new VpnTargetsBuilder().setVpnTarget(vpnTargetList).build();
330 Ipv4FamilyBuilder ipv4vpnBuilder = new Ipv4FamilyBuilder().setVpnTargets(vpnTargets);
332 if (rd != null && !rd.isEmpty()) {
333 ipv4vpnBuilder.setRouteDistinguisher(rd.get(0));
336 VpnInstance newVpn = builder.setIpv4Family(ipv4vpnBuilder.build()).build();
337 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier, newVpn);
338 logger.debug("Created/Updated vpn-instance for {} ", name);
339 } catch (Exception e) {
340 logger.error("Update VPN Instance node failed for node: {} {} {} {}", name, rd, irt, ert);
341 throw new RuntimeException(e);
345 private void deleteVpnMapsNode(Uuid vpnid) {
346 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
347 .child(VpnMap.class, new VpnMapKey(vpnid)).build();
348 logger.debug("removing vpnMaps node: {} ", vpnid.getValue());
349 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier);
352 private void updateVpnMaps(Uuid vpnId, String name, Uuid router, Uuid tenantId, List<Uuid> networks) {
353 VpnMapBuilder builder;
354 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
355 .child(VpnMap.class, new VpnMapKey(vpnId)).build();
356 Optional<VpnMap> optionalVpnMap = read(LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier);
357 if (optionalVpnMap.isPresent()) {
358 builder = new VpnMapBuilder(optionalVpnMap.get());
360 builder = new VpnMapBuilder().setKey(new VpnMapKey(vpnId)).setVpnId(vpnId);
364 builder.setName(name);
366 if (tenantId != null) {
367 builder.setTenantId(tenantId);
369 if (router != null) {
370 builder.setRouterId(router);
372 if (networks != null) {
373 List<Uuid> nwList = builder.getNetworkIds();
374 if (nwList == null) {
375 nwList = new ArrayList<Uuid>();
377 nwList.addAll(networks);
378 builder.setNetworkIds(nwList);
381 logger.debug("Creating/Updating vpnMaps node: {} ", vpnId.getValue());
382 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, builder.build());
383 logger.debug("VPNMaps DS updated for VPN {} ", vpnId.getValue());
386 private void clearFromVpnMaps(Uuid id, Uuid router, List<Uuid> networks) {
387 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
388 .child(VpnMap.class, new VpnMapKey(id)).build();
389 Optional<VpnMap> optionalVpnMap = read(LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier);
390 if (optionalVpnMap.isPresent()) {
391 VpnMap vpnMap = optionalVpnMap.get();
392 VpnMapBuilder vpnMapBuilder = new VpnMapBuilder(vpnMap);
393 if (router != null) {
394 if (vpnMap.getNetworkIds() == null && router.equals(vpnMap.getVpnId())) {
395 // remove entire node in case of internal VPN
396 logger.debug("removing vpnMaps node: {} ", id);
397 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier);
400 vpnMapBuilder.setRouterId(null);
402 if (networks != null) {
403 List<Uuid> vpnNw = vpnMap.getNetworkIds();
404 for (Uuid nw : networks) {
407 if (vpnNw.isEmpty()) {
408 logger.debug("setting networks null in vpnMaps node: {} ", id.getValue());
409 vpnMapBuilder.setNetworkIds(null);
411 vpnMapBuilder.setNetworkIds(vpnNw);
415 logger.debug("clearing from vpnMaps node: {} ", id.getValue());
416 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, vpnMapBuilder.build());
418 logger.error("VPN : {} not found", id.getValue());
420 logger.debug("VPNMaps DS clear success for VPN {} ", id.getValue());
423 private void createOfPortInterface(Port port, int portVlanId) {
424 String name = NeutronvpnUtils.uuidToTapPortName(port.getUuid());
425 //String ifname = new StringBuilder(name).append(":").append(Integer.toString(portVlanId)).toString();
426 //Network network = NeutronvpnUtils.getNeutronNetwork(broker, port.getNetworkId());
427 //Boolean isVlanTransparent = network.isVlanTransparent();
429 logger.debug("Creating OFPort Interface {}", name);
430 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(name);
432 Optional<Interface> optionalInf = read(LogicalDatastoreType.CONFIGURATION, interfaceIdentifier);
433 if (!optionalInf.isPresent()) {
434 // handle these for trunkport extensions : portVlanId, isVlanTransparent
435 Interface inf = new InterfaceBuilder().setEnabled(true).setName(name).setType(L2vlan.class).build();
436 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier, inf);
438 logger.error("Interface {} is already present", name);
440 } catch (Exception e) {
441 logger.error("failed to create interface {} due to the exception {} ", name, e.getMessage());
444 InstanceIdentifier portIdentifier = NeutronvpnUtils.buildPortNameToPortUuidIdentifier(name);
445 PortNameToPortUuidBuilder builder = new PortNameToPortUuidBuilder().setPortName(name).setPortId(port.getUuid());
446 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, portIdentifier, builder.build());
447 logger.debug("name-uuid map for port with name: {}, uuid: {} added to NeutronPortData DS", name, port.getUuid
451 private void deleteOfPortInterface(Port port, int portVlanId) {
452 String name = NeutronvpnUtils.uuidToTapPortName(port.getUuid());
453 //String ifname = new StringBuilder(name).append(":").append(Integer.toString(portVlanId)).toString();
454 logger.debug("Removing OFPort Interface {}", name);
455 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(name);
457 Optional<Interface> optionalInf = read(LogicalDatastoreType.CONFIGURATION, interfaceIdentifier);
458 if (optionalInf.isPresent()) {
459 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier);
461 logger.error("Interface {} is not present", name);
463 } catch (Exception e) {
464 logger.error("Failed to delete interface {} due to the exception {}", name, e.getMessage());
467 InstanceIdentifier portIdentifier = NeutronvpnUtils.buildPortNameToPortUuidIdentifier(name);
468 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, portIdentifier);
469 logger.debug("name-uuid map for port with name: {}, uuid: {} deleted from NeutronPortData DS", name, port
473 private void deleteVpnInstance(Uuid vpnId) {
475 InstanceIdentifier<VpnInstance> vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class).
476 child(VpnInstance.class, new VpnInstanceKey(vpnId.getValue())).build();
477 logger.debug("removing vpn Instance {}", vpnId.getValue());
478 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier);
482 private void createVpnInterface(Uuid vpnId, Port port) {
484 if (vpnId == null || port == null) {
487 String portname = NeutronvpnUtils.uuidToTapPortName(port.getUuid());
488 String name = new StringBuilder(portname).append(":0").toString();
489 List<Adjacency> adjList = new ArrayList<Adjacency>();
490 InstanceIdentifier<VpnInterface> vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
491 child(VpnInterface.class, new VpnInterfaceKey(name)).build();
492 // find router associated to vpn
493 Uuid routerId = NeutronvpnUtils.getRouterforVpn(broker, vpnId);
495 if (routerId != null) {
496 rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
498 // find all subnets to which this port is associated
499 List<FixedIps> ips = port.getFixedIps();
500 // create adjacency list
501 for (FixedIps ip : ips) {
502 // create vm adjacency
503 StringBuilder IpPrefixBuild = new StringBuilder(ip.getIpAddress().getIpv4Address().getValue());
504 String IpPrefix = IpPrefixBuild.append("/32").toString();
505 Adjacency vmAdj = new AdjacencyBuilder().setKey(new AdjacencyKey(IpPrefix)).setIpAddress(IpPrefix)
506 .setMacAddress(port.getMacAddress()).build();
508 // create extra route adjacency
509 if (rtr != null && rtr.getRoutes() != null) {
510 List<String> routeList = rtr.getRoutes();
511 List<Adjacency> erAdjList = addAdjacencyforExtraRoute(routeList, false, name);
512 if (erAdjList != null) {
513 adjList.addAll(erAdjList);
517 // create vpn-interface on this neutron port
518 Adjacencies adjs = new AdjacenciesBuilder().setAdjacency(adjList).build();
519 VpnInterfaceBuilder vpnb = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(name)).
520 setName(name).setVpnInstanceName(vpnId.getValue()).addAugmentation(Adjacencies.class, adjs);
521 VpnInterface vpnIf = vpnb.build();
523 NeutronvpnUtils.lockVpnInterface(lockManager, name);
525 logger.debug("Creating vpn interface {}", vpnIf);
526 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
527 } catch (Exception ex) {
528 logger.error("Creation of vpninterface {} failed due to {}", name, ex);
530 NeutronvpnUtils.unlockVpnInterface(lockManager, name);
534 private void deleteVpnInterface(Port port) {
537 String pname = NeutronvpnUtils.uuidToTapPortName(port.getUuid());
538 String name = new StringBuilder(pname).append(":0").toString();
539 InstanceIdentifier<VpnInterface> vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
540 child(VpnInterface.class, new VpnInterfaceKey(name)).build();
542 NeutronvpnUtils.lockVpnInterface(lockManager, name);
544 logger.debug("Deleting vpn interface {}", name);
545 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier);
546 } catch (Exception ex) {
547 logger.error("Deletion of vpninterface {} failed due to {}", name, ex);
549 NeutronvpnUtils.unlockVpnInterface(lockManager, name);
554 // adds port to subnet list and creates vpnInterface
555 private Uuid addPortToSubnets(Port port) {
556 Uuid subnetId = null;
558 String name = NeutronvpnUtils.uuidToTapPortName(port.getUuid());
560 // find all subnets to which this port is associated
561 List<FixedIps> ips = port.getFixedIps();
562 for (FixedIps ip : ips) {
563 String ipValue = ip.getIpAddress().getIpv4Address().getValue();
565 InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
566 PortFixedipToPortNameBuilder builder = new PortFixedipToPortNameBuilder().setPortFixedip(ipValue)
568 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, builder.build());
569 logger.debug("fixedIp-name map for neutron port with fixedIp: {}, name: {} added to NeutronPortData DS",
572 subnetId = ip.getSubnetId();
573 Subnetmap subnetmap = updateSubnetNode(subnetId, null, null, null, null, port.getUuid());
574 if (vpnId == null && subnetmap != null) {
575 vpnId = subnetmap.getVpnId();
581 private Uuid removePortFromSubnets(Port port) {
582 Uuid subnetId = null;
585 // find all Subnets to which this port is associated
586 List<FixedIps> ips = port.getFixedIps();
587 for (FixedIps ip : ips) {
588 String ipValue = ip.getIpAddress().getIpv4Address().getValue();
590 InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
591 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, id);
592 logger.debug("fixedIp-name map for neutron port with fixedIp: {} deleted from NeutronPortData DS",
595 subnetId = ip.getSubnetId();
596 Subnetmap subnetmap = removeFromSubnetNode(subnetId, null, null, null, port.getUuid());
597 if (vpnId == null && subnetmap != null) {
598 vpnId = subnetmap.getVpnId();
604 protected void handleNeutronSubnetCreated(Uuid subnetId, Uuid networkId, Uuid tenantId) {
605 updateSubnetNode(subnetId, tenantId, networkId, null, null, null);
606 if (networkId != null && NeutronvpnUtils.getNeutronNetwork(broker, networkId) != null) {
607 createSubnetToNetworkMapping(subnetId, networkId);
611 protected void handleNeutronSubnetDeleted(Uuid subnetId, Uuid networkId, Uuid tenantId) {
612 Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, networkId);
614 removeSubnetFromVpn(vpnId, subnetId);
616 if (networkId != null) {
617 deleteSubnetToNetworkMapping(subnetId, networkId);
621 protected void handleNeutronSubnetUpdated(Uuid subnetId, Uuid networkId, Uuid tenantId) {
622 Uuid oldNetworkId = NeutronvpnUtils.getSubnetmap(broker, subnetId).getNetworkId();
623 if (oldNetworkId != null && !oldNetworkId.equals(networkId)) {
624 deleteSubnetToNetworkMapping(subnetId, oldNetworkId);
626 if (networkId != null && !networkId.equals(oldNetworkId)) {
627 createSubnetToNetworkMapping(subnetId, networkId);
629 updateSubnetNode(subnetId, tenantId, networkId, null, null, null);
632 protected void handleNeutronPortCreated(Port port) {
633 logger.info("Of-port-interface creation");
634 int portVlanId = NeutronvpnUtils.getVlanFromNeutronPort(port);
635 // Create of-port interface for this neutron port
636 createOfPortInterface(port, portVlanId);
637 logger.debug("Add port to subnet");
638 // add port to local Subnets DS
639 Uuid vpnId = addPortToSubnets(port);
642 // create vpn-interface on this neutron port
643 logger.debug("Adding VPN Interface");
644 createVpnInterface(vpnId, port);
648 protected void handleNeutronPortDeleted(Port port) {
649 logger.debug("Of-port-interface removal");
650 logger.debug("Remove port from subnet");
651 // remove port from local Subnets DS
652 Uuid vpnId = removePortFromSubnets(port);
655 // remove vpn-interface for this neutron port
656 logger.debug("removing VPN Interface");
657 deleteVpnInterface(port);
659 int portVlanId = NeutronvpnUtils.getVlanFromNeutronPort(port);
660 // Remove of-port interface for this neutron port
661 deleteOfPortInterface(port, portVlanId);
665 protected void handleNeutronPortUpdated(Port portoriginal, Port portupdate) {
666 logger.debug("Add port to subnet");
667 // add port FixedIPs to local Subnets DS
668 Uuid vpnIdup = addPortToSubnets(portupdate);
670 if (vpnIdup != null) {
671 createVpnInterface(vpnIdup, portupdate);
674 // remove port FixedIPs from local Subnets DS
675 Uuid vpnIdor = removePortFromSubnets(portoriginal);
677 if (vpnIdor != null) {
678 deleteVpnInterface(portoriginal);
682 public void createL3Vpn(Uuid vpn, String name, Uuid tenant, List<String> rd, List<String> irt, List<String> ert,
683 Uuid router, List<Uuid> networks) {
685 // Update VPN Instance node
686 updateVpnInstanceNode(vpn.getValue(), rd, irt, ert);
688 // Update local vpn-subnet DS
689 updateVpnMaps(vpn, name, router, tenant, networks);
691 if (router != null) {
692 associateRouterToVpn(vpn, router);
694 if (networks != null) {
695 associateNetworksToVpn(vpn, networks);
700 public Future<RpcResult<CreateL3VPNOutput>> createL3VPN(CreateL3VPNInput input) {
702 CreateL3VPNOutputBuilder opBuilder = new CreateL3VPNOutputBuilder();
703 SettableFuture<RpcResult<CreateL3VPNOutput>> result = SettableFuture.create();
704 List<RpcError> errorList = new ArrayList<RpcError>();
705 int failurecount = 0;
706 int warningcount = 0;
708 List<L3vpn> vpns = input.getL3vpn();
709 for (L3vpn vpn : vpns) {
712 if (vpn.getRouteDistinguisher() == null || vpn.getImportRT() == null || vpn.getExportRT() == null) {
713 msg = String.format("Creation of L3VPN failed for VPN %s due to absence of RD/iRT/eRT input",
714 vpn.getId().getValue());
716 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
717 errorList.add(error);
721 if (vpn.getRouteDistinguisher().size() > 1) {
722 msg = String.format("Creation of L3VPN failed for VPN %s due to multiple RD input %s",
723 vpn.getId().getValue(), vpn.getRouteDistinguisher());
725 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
726 errorList.add(error);
731 createL3Vpn(vpn.getId(), vpn.getName(), vpn.getTenantId(), vpn.getRouteDistinguisher(),
732 vpn.getImportRT(), vpn.getExportRT(), vpn.getRouterId(), vpn.getNetworkIds());
733 } catch (Exception ex) {
734 msg = String.format("Creation of L3VPN failed for VPN %s", vpn.getId().getValue());
735 logger.error(msg, ex.getMessage());
736 error = RpcResultBuilder.newError(ErrorType.APPLICATION, msg, ex.getMessage());
737 errorList.add(error);
741 // if at least one succeeds; result is success
742 // if none succeeds; result is failure
743 if (failurecount + warningcount == vpns.size()) {
744 result.set(RpcResultBuilder.<CreateL3VPNOutput>failed().withRpcErrors(errorList).build());
746 List<String> errorResponseList = new ArrayList<>();
747 if (!errorList.isEmpty()) {
748 for (RpcError rpcError : errorList) {
749 String errorResponse = String.format("ErrorType: " + rpcError.getErrorType() + ", " + "ErrorTag: " +
750 rpcError.getTag() + ", " + "ErrorMessage: " + rpcError.getMessage());
751 errorResponseList.add(errorResponse);
754 errorResponseList.add("Operation successful with no errors");
756 opBuilder.setResponse(errorResponseList);
757 result.set(RpcResultBuilder.<CreateL3VPNOutput>success().withResult(opBuilder.build()).build());
763 public Future<RpcResult<GetL3VPNOutput>> getL3VPN(GetL3VPNInput input) {
765 GetL3VPNOutputBuilder opBuilder = new GetL3VPNOutputBuilder();
766 SettableFuture<RpcResult<GetL3VPNOutput>> result = SettableFuture.create();
767 Uuid inputVpnId = input.getId();
768 List<VpnInstance> vpns = new ArrayList<VpnInstance>();
771 if (inputVpnId == null) {
773 InstanceIdentifier<VpnInstances> vpnsIdentifier =
774 InstanceIdentifier.builder(VpnInstances.class).build();
775 Optional<VpnInstances> optionalVpns = read(LogicalDatastoreType.CONFIGURATION, vpnsIdentifier);
776 if (optionalVpns.isPresent()) {
777 for (VpnInstance vpn : optionalVpns.get().getVpnInstance()) {
782 result.set(RpcResultBuilder.<GetL3VPNOutput>failed()
783 .withWarning(ErrorType.PROTOCOL, "", "No VPN is present").build());
787 String name = inputVpnId.getValue();
788 InstanceIdentifier<VpnInstance> vpnIdentifier =
789 InstanceIdentifier.builder(VpnInstances.class)
790 .child(VpnInstance.class, new VpnInstanceKey(name)).build();
791 // read VpnInstance Info
792 Optional<VpnInstance> optionalVpn = read(LogicalDatastoreType.CONFIGURATION, vpnIdentifier);
793 if (optionalVpn.isPresent()) {
794 vpns.add(optionalVpn.get());
796 String message = String.format("GetL3VPN failed because VPN %s is not present", name);
797 logger.error(message);
798 result.set(RpcResultBuilder.<GetL3VPNOutput>failed()
799 .withWarning(ErrorType.PROTOCOL, "invalid-value", message).build());
802 List<L3vpnInstances> l3vpnList = new ArrayList<L3vpnInstances>();
803 for (VpnInstance vpnInstance : vpns) {
804 Uuid vpnId = new Uuid(vpnInstance.getVpnInstanceName());
806 InstanceIdentifier<VpnMap> vpnMapIdentifier =
807 InstanceIdentifier.builder(VpnMaps.class)
808 .child(VpnMap.class, new VpnMapKey(vpnId)).build();
809 L3vpnInstancesBuilder l3vpn = new L3vpnInstancesBuilder();
811 List<String> rd = Arrays.asList(vpnInstance.getIpv4Family().getRouteDistinguisher().split(","));
812 List<VpnTarget> vpnTargetList = vpnInstance.getIpv4Family().getVpnTargets().getVpnTarget();
814 List<String> ertList = new ArrayList<String>();
815 List<String> irtList = new ArrayList<String>();
817 for (VpnTarget vpnTarget : vpnTargetList) {
818 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ExportExtcommunity) {
819 ertList.add(vpnTarget.getVrfRTValue());
821 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ImportExtcommunity) {
822 irtList.add(vpnTarget.getVrfRTValue());
824 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.Both) {
825 ertList.add(vpnTarget.getVrfRTValue());
826 irtList.add(vpnTarget.getVrfRTValue());
830 l3vpn.setId(vpnId).setRouteDistinguisher(rd).setImportRT(irtList).setExportRT(ertList);
831 Optional<VpnMap> optionalVpnMap =
832 read(LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier);
833 if (optionalVpnMap.isPresent()) {
834 VpnMap vpnMap = optionalVpnMap.get();
835 l3vpn.setRouterId(vpnMap.getRouterId()).setNetworkIds(vpnMap.getNetworkIds())
836 .setTenantId(vpnMap.getTenantId()).setName(vpnMap.getName());
838 l3vpnList.add(l3vpn.build());
841 opBuilder.setL3vpnInstances(l3vpnList);
842 result.set(RpcResultBuilder.<GetL3VPNOutput>success().withResult(opBuilder.build()).build());
844 } catch (Exception ex) {
845 String message = String.format("GetL3VPN failed due to %s", ex.getMessage());
846 logger.error(message);
847 result.set(RpcResultBuilder.<GetL3VPNOutput>failed().withError(ErrorType.APPLICATION, message).build());
854 public Future<RpcResult<DeleteL3VPNOutput>> deleteL3VPN(DeleteL3VPNInput input) {
856 DeleteL3VPNOutputBuilder opBuilder = new DeleteL3VPNOutputBuilder();
857 SettableFuture<RpcResult<DeleteL3VPNOutput>> result = SettableFuture.create();
858 List<RpcError> errorList = new ArrayList<RpcError>();
860 int failurecount = 0;
861 int warningcount = 0;
862 List<Uuid> vpns = input.getId();
863 for (Uuid vpn : vpns) {
867 InstanceIdentifier<VpnInstance> vpnIdentifier =
868 InstanceIdentifier.builder(VpnInstances.class)
869 .child(VpnInstance.class, new VpnInstanceKey(vpn.getValue())).build();
870 Optional<VpnInstance> optionalVpn = read(LogicalDatastoreType.CONFIGURATION, vpnIdentifier);
871 if (optionalVpn.isPresent()) {
874 msg = String.format("VPN with vpnid: %s does not exist", vpn.getValue());
876 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-value", msg);
877 errorList.add(error);
880 } catch (Exception ex) {
881 msg = String.format("Deletion of L3VPN failed when deleting for uuid %s", vpn.getValue());
882 logger.error(msg, ex.getMessage());
883 error = RpcResultBuilder.newError(ErrorType.APPLICATION, msg, ex.getMessage());
884 errorList.add(error);
888 // if at least one succeeds; result is success
889 // if none succeeds; result is failure
890 if (failurecount + warningcount == vpns.size()) {
891 result.set(RpcResultBuilder.<DeleteL3VPNOutput>failed().withRpcErrors(errorList).build());
893 List<String> errorResponseList = new ArrayList<>();
894 if (!errorList.isEmpty()) {
895 for (RpcError rpcError : errorList) {
896 String errorResponse = String.format("ErrorType: " + rpcError.getErrorType() + ", " + "ErrorTag: " +
897 rpcError.getTag() + ", " + "ErrorMessage: " + rpcError.getMessage());
898 errorResponseList.add(errorResponse);
901 errorResponseList.add("Operation successful with no errors");
903 opBuilder.setResponse(errorResponseList);
904 result.set(RpcResultBuilder.<DeleteL3VPNOutput>success().withResult(opBuilder.build()).build());
909 protected void addSubnetToVpn(Uuid vpnId, Uuid subnet) {
910 logger.debug("Adding subnet {} to vpn {}", subnet.getValue(), vpnId.getValue());
911 Subnetmap sn = updateSubnetNode(subnet, null, null, null, vpnId, null);
912 // Check if there are ports on this subnet and add corresponding vpn-interfaces
913 List<Uuid> portList = sn.getPortList();
914 if (portList != null) {
915 for (Uuid port : sn.getPortList()) {
916 logger.debug("adding vpn-interface for port {}", port.getValue());
917 createVpnInterface(vpnId, getNeutronPort(port));
922 protected List<Adjacency> addAdjacencyforExtraRoute(List<String> routeList, boolean rtrUp, String vpnifname) {
924 List<Adjacency> adjList = new ArrayList<Adjacency>();
925 for (String route : routeList) {
926 // assuming extra route is strictly in the format "nexthop destination" > "10.1.1.10 40.0.1.0/24"
927 String[] parts = route.split(" ");
928 if (parts.length == 2) {
929 String nextHop = parts[0];
930 String destination = parts[1];
932 String tapPortName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop);
933 String ifname = new StringBuilder(tapPortName).append(":0").toString();
934 logger.trace("Adding extra route with nexthop {}, destination {}, ifName {}", nextHop,
935 destination, ifname);
936 Adjacency erAdj = new AdjacencyBuilder().setIpAddress(destination).setNextHopIp(nextHop).setKey
937 (new AdjacencyKey(destination)).build();
938 if (rtrUp == false) {
939 if (ifname.equals(vpnifname)) {
944 InstanceIdentifier<VpnInterface> vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
945 child(VpnInterface.class, new VpnInterfaceKey(ifname)).build();
946 Optional<VpnInterface> optionalVpnInterface = read(LogicalDatastoreType.CONFIGURATION,
948 if (optionalVpnInterface.isPresent()) {
949 Adjacencies erAdjs = new AdjacenciesBuilder().setAdjacency(Arrays.asList(erAdj)).build();
950 VpnInterface vpnIf = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(ifname))
951 .addAugmentation(Adjacencies.class, erAdjs).build();
952 MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
953 logger.trace("extra route {} added successfully", route);
955 logger.error("VM adjacency for interface {} not present ; cannot add extra route adjacency",
959 logger.error("Incorrect input received for extra route. {}", parts);
963 } catch (Exception e) {
964 logger.error("exception in adding extra route: {}" + e);
969 protected void removeAdjacencyforExtraRoute(List<String> routeList) {
971 for (String route : routeList) {
972 // assuming extra route is strictly in the format "nexthop destination" > "10.1.1.10 40.0.1.0/24"
973 String[] parts = route.split(" ");
974 if (parts.length == 2) {
975 String nextHop = parts[0];
976 String destination = parts[1];
978 String tapPortName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop);
979 String ifname = new StringBuilder(tapPortName).append(":0").toString();
980 logger.trace("Removing extra route with nexthop {}, destination {}, ifName {}", nextHop,
981 destination, ifname);
982 InstanceIdentifier<Adjacency> adjacencyIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
983 child(VpnInterface.class, new VpnInterfaceKey(ifname)).augmentation(Adjacencies.class)
984 .child(Adjacency.class, new AdjacencyKey(destination)).build();
985 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, adjacencyIdentifier);
986 logger.trace("extra route {} deleted successfully", route);
988 logger.error("Incorrect input received for extra route. {}", parts);
991 } catch (Exception e) {
992 logger.error("exception in deleting extra route: {}" + e);
996 protected void addPortToVpn(Uuid vpnId, Uuid port) {
997 logger.debug("Adding Port to vpn node...");
998 createVpnInterface(vpnId, getNeutronPort(port));
1001 protected void removeL3Vpn(Uuid id) {
1002 // read VPN networks
1003 VpnMap vpnMap = NeutronvpnUtils.getVpnMap(broker, id);
1004 Uuid router = vpnMap.getRouterId();
1005 // dissociate router
1006 if (router != null) {
1007 dissociateRouterFromVpn(id, router);
1009 // dissociate networks
1010 if (!id.equals(router)) {
1011 dissociateNetworksFromVpn(id, vpnMap.getNetworkIds());
1013 // remove entire vpnMaps node
1014 deleteVpnMapsNode(id);
1016 // remove vpn-instance
1017 deleteVpnInstance(id);
1020 protected void removePortFromVpn(Uuid vpnId, Uuid port) {
1021 logger.debug("Removing Port from vpn node...");
1022 deleteVpnInterface(getNeutronPort(port));
1025 protected void removeSubnetFromVpn(Uuid vpnId, Uuid subnet) {
1026 logger.debug("Removing subnet {} from vpn {}", subnet.getValue(), vpnId.getValue());
1027 Subnetmap sn = NeutronvpnUtils.getSubnetmap(broker, subnet);
1029 // Check if there are ports on this subnet; remove corresponding vpn-interfaces
1030 List<Uuid> portList = sn.getPortList();
1031 if (portList != null) {
1032 for (Uuid port : sn.getPortList()) {
1033 logger.debug("removing vpn-interface for port {}", port.getValue());
1034 deleteVpnInterface(getNeutronPort(port));
1037 // update subnet-vpn association
1038 removeFromSubnetNode(subnet, null, null, vpnId, null);
1040 logger.warn("Subnetmap for subnet {} not found", subnet.getValue());
1044 protected void associateRouterToVpn(Uuid vpn, Uuid router) {
1046 // remove existing Router-VPN
1047 if (!vpn.equals(router)) {
1048 removeL3Vpn(router);
1050 updateVpnMaps(vpn, null, router, null, null);
1052 List<Uuid> routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, router);
1053 logger.debug("Adding subnets...");
1054 for (Uuid subnet : routerSubnets) {
1055 addSubnetToVpn(vpn, subnet);
1059 protected void dissociateRouterFromVpn(Uuid vpn, Uuid router) {
1060 clearFromVpnMaps(vpn, router, null);
1062 // fetching sn from SubnetmapDS for internal VPN because sn already deleted from RouterIf DS on router deletion
1063 List<Uuid> routerSubnets = (vpn.equals(router)) ? getSubnetsforVpn(vpn) :
1064 NeutronvpnUtils.getNeutronRouterSubnetIds(broker, router);
1066 logger.debug("dissociateRouter vpn {} router {} Removing subnets...", vpn.getValue(), router.getValue());
1067 if (routerSubnets != null) {
1068 for (Uuid subnet : routerSubnets) {
1069 removeSubnetFromVpn(vpn, subnet);
1072 // create Router-VPN for this router
1073 if (!vpn.equals(router)) {
1074 logger.debug("Re-creating vpn-router...");
1075 createL3Vpn(router, null, null, null, null, null, router, null);
1079 protected List<String> associateNetworksToVpn(Uuid vpn, List<Uuid> networks) {
1080 List<String> failed = new ArrayList<String>();
1081 if (!networks.isEmpty()) {
1082 // store in Data Base
1083 updateVpnMaps(vpn, null, null, null, networks);
1084 // process corresponding subnets for VPN
1085 for (Uuid nw : networks) {
1086 if (NeutronvpnUtils.getNeutronNetwork(broker, nw) == null) {
1087 failed.add(nw.getValue());
1089 List<Uuid> networkSubnets = NeutronvpnUtils.getSubnetIdsFromNetworkId(broker, nw);
1090 logger.debug("Adding network subnets...");
1091 if (networkSubnets != null) {
1092 for (Uuid subnet : networkSubnets) {
1093 addSubnetToVpn(vpn, subnet);
1102 protected List<String> dissociateNetworksFromVpn(Uuid vpn, List<Uuid> networks) {
1103 List<String> failed = new ArrayList<String>();
1104 if (networks != null && !networks.isEmpty()) {
1105 // store in Data Base
1106 clearFromVpnMaps(vpn, null, networks);
1107 // process corresponding subnets for VPN
1108 for (Uuid nw : networks) {
1109 if (NeutronvpnUtils.getNeutronNetwork(broker, nw) == null) {
1110 failed.add(nw.getValue());
1112 List<Uuid> networkSubnets = NeutronvpnUtils.getSubnetIdsFromNetworkId(broker, nw);
1113 logger.debug("Removing network subnets...");
1114 if (networkSubnets != null) {
1115 for (Uuid subnet : networkSubnets) {
1116 removeSubnetFromVpn(vpn, subnet);
1126 public Future<RpcResult<AssociateNetworksOutput>> associateNetworks(AssociateNetworksInput input) {
1128 AssociateNetworksOutputBuilder opBuilder = new AssociateNetworksOutputBuilder();
1129 SettableFuture<RpcResult<AssociateNetworksOutput>> result = SettableFuture.create();
1130 logger.debug("associateNetworks {}", input);
1131 StringBuilder returnMsg = new StringBuilder();
1132 Uuid vpnId = input.getVpnId();
1135 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1136 List<Uuid> netIds = input.getNetworkId();
1137 if (netIds != null && !netIds.isEmpty()) {
1138 List<String> failed = associateNetworksToVpn(vpnId, netIds);
1139 if (!failed.isEmpty()) {
1140 returnMsg.append("network(s) not found : ").append(failed);
1144 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1146 if (returnMsg.length() != 0) {
1147 String message = String.format("associate Networks to vpn %s failed due to %s", vpnId.getValue(),
1149 logger.error(message);
1150 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1152 opBuilder.setResponse(errorResponse);
1153 result.set(RpcResultBuilder.<AssociateNetworksOutput>success().withResult(opBuilder.build()).build());
1155 result.set(RpcResultBuilder.<AssociateNetworksOutput>success().build());
1157 } catch (Exception ex) {
1158 String message = String.format("associate Networks to vpn %s failed due to %s", input.getVpnId().getValue(),
1160 logger.error(message);
1161 result.set(RpcResultBuilder.<AssociateNetworksOutput>failed().withError(ErrorType.APPLICATION, message)
1164 logger.debug("associateNetworks returns..");
1169 public Future<RpcResult<Void>> associateRouter(AssociateRouterInput input) {
1171 SettableFuture<RpcResult<Void>> result = SettableFuture.create();
1172 logger.debug("associateRouter {}", input);
1173 StringBuilder returnMsg = new StringBuilder();
1174 Uuid vpnId = input.getVpnId();
1175 Uuid routerId = input.getRouterId();
1177 if (routerId != null && vpnId != null) {
1178 Router rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
1179 VpnMap vpnMap = NeutronvpnUtils.getVpnMap(broker, vpnId);
1180 if (rtr != null && vpnMap != null) {
1181 if (vpnMap.getRouterId() != null) {
1182 returnMsg.append("vpn ").append(vpnId.getValue()).append(" already associated to router ")
1183 .append(vpnMap.getRouterId().getValue());
1185 associateRouterToVpn(vpnId, routerId);
1188 returnMsg.append("router not found : ").append(routerId.getValue());
1191 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1193 if (returnMsg.length() != 0) {
1194 String message = String.format("associate router to vpn %s failed due to %s", routerId.getValue(),
1196 logger.error(message);
1197 result.set(RpcResultBuilder.<Void>failed().withWarning(ErrorType.PROTOCOL, "invalid-value", message)
1200 result.set(RpcResultBuilder.<Void>success().build());
1202 } catch (Exception ex) {
1203 String message = String.format("associate router %s to vpn %s failed due to %s", routerId.getValue(),
1204 vpnId.getValue(), ex.getMessage());
1205 logger.error(message);
1206 result.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, message).build());
1208 logger.debug("associateRouter returns..");
1213 public Future<RpcResult<DissociateNetworksOutput>> dissociateNetworks(DissociateNetworksInput input) {
1215 DissociateNetworksOutputBuilder opBuilder = new DissociateNetworksOutputBuilder();
1216 SettableFuture<RpcResult<DissociateNetworksOutput>> result = SettableFuture.create();
1218 logger.debug("dissociateNetworks {}", input);
1219 StringBuilder returnMsg = new StringBuilder();
1220 Uuid vpnId = input.getVpnId();
1223 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1224 List<Uuid> netIds = input.getNetworkId();
1225 if (netIds != null && !netIds.isEmpty()) {
1226 List<String> failed = dissociateNetworksFromVpn(vpnId, netIds);
1227 if (!failed.isEmpty()) {
1228 returnMsg.append("netowrk(s) not found : ").append(failed);
1232 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1234 if (returnMsg.length() != 0) {
1235 String message = String.format("disssociate Networks to vpn %s failed due to %s", vpnId.getValue(),
1237 logger.error(message);
1238 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1240 opBuilder.setResponse(errorResponse);
1241 result.set(RpcResultBuilder.<DissociateNetworksOutput>success().withResult(opBuilder.build()).build());
1243 result.set(RpcResultBuilder.<DissociateNetworksOutput>success().build());
1245 } catch (Exception ex) {
1246 String message = String.format("dissociate Networks to vpn %s failed due to %s", input.getVpnId().
1247 getValue(), ex.getMessage());
1248 logger.error(message);
1249 result.set(RpcResultBuilder.<DissociateNetworksOutput>failed().withError(ErrorType.APPLICATION, message)
1252 logger.debug("dissociateNetworks returns..");
1257 public Future<RpcResult<Void>> dissociateRouter(DissociateRouterInput input) {
1259 SettableFuture<RpcResult<Void>> result = SettableFuture.create();
1261 logger.debug("dissociateRouter {}", input);
1262 StringBuilder returnMsg = new StringBuilder();
1263 Uuid vpnId = input.getVpnId();
1264 Uuid routerId = input.getRouterId();
1266 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1267 if (routerId != null) {
1268 Router rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
1270 dissociateRouterFromVpn(vpnId, routerId);
1272 returnMsg.append("router not found : ").append(routerId.getValue());
1276 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1278 if (returnMsg.length() != 0) {
1279 String message = String.format("disssociate router %s to vpn %s failed due to %s", routerId.getValue(),
1280 vpnId.getValue(), returnMsg);
1281 logger.error(message);
1282 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1284 result.set(RpcResultBuilder.<Void>failed().withWarning(ErrorType.PROTOCOL, "invalid-value", message)
1287 result.set(RpcResultBuilder.<Void>success().build());
1289 } catch (Exception ex) {
1290 String message = String.format("disssociate router %s to vpn %s failed due to %s", routerId.getValue(),
1291 vpnId.getValue(), ex.getMessage());
1292 logger.error(message);
1293 result.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, message).build());
1295 logger.debug("dissociateRouter returns..");
1300 private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType, InstanceIdentifier<T> path) {
1302 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
1304 Optional<T> result = Optional.absent();
1306 result = tx.read(datastoreType, path).get();
1307 } catch (Exception e) {
1308 throw new RuntimeException(e);
1314 protected Subnet getNeutronSubnet(Uuid subnetId) {
1315 InstanceIdentifier<Subnet> inst = InstanceIdentifier.create(Neutron.class).
1316 child(Subnets.class).child(Subnet.class, new SubnetKey(subnetId));
1317 Optional<Subnet> sn = read(LogicalDatastoreType.CONFIGURATION, inst);
1319 if (sn.isPresent()) {
1325 protected IpAddress getNeutronSubnetGateway(Uuid subnetId) {
1326 Subnet sn = getNeutronSubnet(subnetId);
1328 return sn.getGatewayIp();
1333 protected Port getNeutronPort(String name) {
1334 Uuid portId = NeutronvpnUtils.getNeutronPortIdfromPortName(broker, name);
1335 if (portId != null) {
1336 InstanceIdentifier<Port> pid = InstanceIdentifier.create(Neutron.class).
1337 child(Ports.class).child(Port.class, new PortKey(portId));
1338 Optional<Port> optPort = read(LogicalDatastoreType.CONFIGURATION, pid);
1339 if (optPort.isPresent()) {
1340 return optPort.get();
1343 logger.error("Port {} not Found!!", name);
1348 protected Port getNeutronPort(Uuid portId) {
1349 InstanceIdentifier<Port> pid = InstanceIdentifier.create(Neutron.class).
1350 child(Ports.class).child(Port.class, new PortKey(portId));
1351 Optional<Port> optPort = read(LogicalDatastoreType.CONFIGURATION, pid);
1352 if (optPort.isPresent()) {
1353 return optPort.get();
1358 protected List<Uuid> getSubnetsforVpn(Uuid vpnid) {
1359 List<Uuid> subnets = new ArrayList<Uuid>();
1361 InstanceIdentifier<Subnetmaps> subnetmapsid = InstanceIdentifier.builder(Subnetmaps.class).build();
1362 Optional<Subnetmaps> subnetmaps = read(LogicalDatastoreType.CONFIGURATION, subnetmapsid);
1363 if (subnetmaps.isPresent()) {
1364 Subnetmaps smaps = subnetmaps.get();
1365 List<Subnetmap> subnetMapList = smaps.getSubnetmap();
1366 for (Subnetmap subnetMap : subnetMapList) {
1367 if (subnetMap.getVpnId() != null && subnetMap.getVpnId().equals(vpnid)) {
1368 subnets.add(subnetMap.getId());
1375 public List<String> showNeutronPortsCLI() {
1376 List<String> result = new ArrayList<String>();
1377 result.add(String.format(" %-22s %-22s %-22s %-6s ", "PortName", "Mac Address", "IP Address",
1379 result.add("---------------------------------------------------------------------------------------");
1380 InstanceIdentifier<Ports> portidentifier = InstanceIdentifier.create(Neutron.class).child(Ports.class);
1382 Optional<Ports> ports = read(LogicalDatastoreType.CONFIGURATION, portidentifier);
1383 if (ports.isPresent()) {
1384 List<Port> portList = ports.get().getPort();
1385 for (Port port : portList) {
1386 result.add(String.format(" %-22s %-22s %-22s %-6s ", NeutronvpnUtils.uuidToTapPortName(port
1387 .getUuid()), port.getMacAddress(), port.getFixedIps().get(0).getIpAddress().getIpv4Address()
1388 .getValue(), getIPPrefixFromPort(port)));
1391 } catch (Exception e) {
1392 logger.trace("Failed to retrieve neutronPorts info : ", e);
1393 System.out.println("Failed to retrieve neutronPorts info : " + e.getMessage());
1398 private Short getIPPrefixFromPort(Port port) {
1399 Short prefix = new Short((short) 0);
1402 Uuid subnetUUID = port.getFixedIps().get(0).getSubnetId();
1404 org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets
1405 .SubnetKey subnetkey = new org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets
1406 .rev150712.subnets.attributes.subnets.SubnetKey(subnetUUID);
1407 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets
1408 .attributes.subnets.Subnet> subnetidentifier = InstanceIdentifier.create(Neutron.class).child(org
1409 .opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets
1410 .class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets
1411 .attributes.subnets.Subnet.class, subnetkey);
1412 Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes
1413 .subnets.Subnet> subnet = read(LogicalDatastoreType.CONFIGURATION, subnetidentifier);
1415 if (subnet.isPresent()) {
1416 cidr = subnet.get().getCidr();
1417 // Extract the prefix length from cidr
1418 String[] parts = cidr.split("/");
1419 if ((parts.length == 2)) {
1420 prefix = Short.valueOf(parts[1]);
1423 logger.trace("Could not retrieve prefix from subnet CIDR");
1424 System.out.println("Could not retrieve prefix from subnet CIDR");
1427 logger.trace("Unable to read on subnet datastore");
1429 } catch (Exception e) {
1430 logger.trace("Failed to retrieve IP prefix from port : ", e);
1431 System.out.println("Failed to retrieve IP prefix from port : " + e.getMessage());
1436 public List<String> showVpnConfigCLI(Uuid vpnuuid) {
1437 List<String> result = new ArrayList<String>();
1438 if (vpnuuid == null) {
1439 System.out.println("");
1440 System.out.println("Displaying VPN config for all VPNs");
1441 System.out.println("To display VPN config for a particular VPN, use the following syntax");
1442 System.out.println(getshowVpnConfigCLIHelp());
1445 RpcResult<GetL3VPNOutput> rpcResult = getL3VPN(new GetL3VPNInputBuilder().setId(vpnuuid).build()).get();
1446 if (rpcResult.isSuccessful()) {
1448 result.add(String.format(" %-37s %-37s %-7s ", "VPN ID", "Tenant ID", "RD"));
1450 result.add(String.format(" %-80s ", "Import-RTs"));
1452 result.add(String.format(" %-80s ", "Export-RTs"));
1454 result.add(String.format(" %-76s ", "Subnet IDs"));
1456 result.add("------------------------------------------------------------------------------------");
1458 List<L3vpnInstances> VpnList = rpcResult.getResult().getL3vpnInstances();
1459 for (L3vpnInstance Vpn : VpnList) {
1460 String tenantId = Vpn.getTenantId() != null ? Vpn.getTenantId().getValue() : "\" " +
1462 result.add(String.format(" %-37s %-37s %-7s ", Vpn.getId().getValue(), tenantId, Vpn
1463 .getRouteDistinguisher()));
1465 result.add(String.format(" %-80s ", Vpn.getImportRT()));
1467 result.add(String.format(" %-80s ", Vpn.getExportRT()));
1470 Uuid vpnid = Vpn.getId();
1471 List<Uuid> subnetList = getSubnetsforVpn(vpnid);
1472 if (!subnetList.isEmpty()) {
1473 for (Uuid subnetuuid : subnetList) {
1474 result.add(String.format(" %-76s ", subnetuuid.getValue()));
1477 result.add(String.format(" %-76s ", "\" \""));
1480 result.add("----------------------------------------");
1484 String errortag = rpcResult.getErrors().iterator().next().getTag();
1485 if (errortag == "") {
1486 System.out.println("");
1487 System.out.println("No VPN has been configured yet");
1488 } else if (errortag == "invalid-value") {
1489 System.out.println("");
1490 System.out.println("VPN " + vpnuuid.getValue() + " is not present");
1492 System.out.println("error getting VPN info : " + rpcResult.getErrors());
1493 System.out.println(getshowVpnConfigCLIHelp());
1496 } catch (InterruptedException | ExecutionException e) {
1497 logger.trace("error getting VPN info : ", e);
1498 System.out.println("error getting VPN info : " + e.getMessage());
1503 private String getshowVpnConfigCLIHelp() {
1504 StringBuilder help = new StringBuilder("Usage:");
1505 help.append("display vpn-config [-vid/--vpnid <id>]");
1506 return help.toString();