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
9 package org.opendaylight.vpnservice.neutronvpn;
11 import com.google.common.base.Optional;
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.Routers;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.RouterKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.Networks;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.NetworkKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.lockmanager.rev150819.LockManagerService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.lockmanager.rev150819.TimeUnits;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.lockmanager.rev150819.TryLockInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.lockmanager.rev150819.TryLockInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.lockmanager.rev150819.UnlockInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.lockmanager.rev150819.UnlockInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.NetworkMaps;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.NeutronPortData;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.Subnetmaps;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.VpnMaps;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.networkmaps.NetworkMap;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.networkmaps.NetworkMapKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.neutron.port.data
42 .PortFixedipToPortName;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.neutron.port.data
44 .PortFixedipToPortNameKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.neutron.port.data
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.neutron.port.data
48 .PortNameToPortUuidKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.subnetmaps.Subnetmap;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.subnetmaps.SubnetmapKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.vpnmaps.VpnMap;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.vpnmaps.VpnMapKey;
53 import org.opendaylight.yangtools.yang.binding.DataObject;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.opendaylight.yangtools.yang.common.RpcResult;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 import java.util.ArrayList;
60 import java.util.List;
61 import java.util.concurrent.ExecutionException;
62 import java.util.concurrent.Future;
64 //import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.port.ext.rev151125.TrunkportExt;
65 //import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.port.ext.rev151125.TrunkportTypeBase;
66 //import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.port.ext.rev151125.TrunkportTypeSubport;
68 public class NeutronvpnUtils {
70 private static final Logger logger = LoggerFactory.getLogger(NeutronvpnUtils.class);
72 protected static Subnetmap getSubnetmap(DataBroker broker, Uuid subnetId) {
73 InstanceIdentifier id = buildSubnetMapIdentifier(subnetId);
74 Optional<Subnetmap> sn = read(broker, LogicalDatastoreType.CONFIGURATION, id);
82 protected static VpnMap getVpnMap(DataBroker broker, Uuid id) {
83 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
84 .child(VpnMap.class, new VpnMapKey(id)).build();
85 Optional<VpnMap> optionalVpnMap = read(broker, LogicalDatastoreType.CONFIGURATION,
87 if (optionalVpnMap.isPresent()) {
88 return optionalVpnMap.get();
90 logger.error("getVpnMap failed, VPN {} not present", id.getValue());
94 protected static Uuid getVpnForNetwork(DataBroker broker, Uuid network) {
95 InstanceIdentifier<VpnMaps> vpnMapsIdentifier = InstanceIdentifier.builder(VpnMaps.class).build();
96 Optional<VpnMaps> optionalVpnMaps = read(broker, LogicalDatastoreType.CONFIGURATION,
98 if (optionalVpnMaps.isPresent()) {
99 VpnMaps vpnMaps = optionalVpnMaps.get();
100 List<VpnMap> allMaps = vpnMaps.getVpnMap();
101 for (VpnMap vpnMap : allMaps) {
102 if (vpnMap.getNetworkIds().contains(network)) {
103 return vpnMap.getVpnId();
110 protected static Uuid getVpnForRouter(DataBroker broker, Uuid router) {
111 InstanceIdentifier<VpnMaps> vpnMapsIdentifier = InstanceIdentifier.builder(VpnMaps.class).build();
112 Optional<VpnMaps> optionalVpnMaps = read(broker, LogicalDatastoreType.CONFIGURATION,
114 if (optionalVpnMaps.isPresent()) {
115 VpnMaps vpnNets = optionalVpnMaps.get();
116 List<VpnMap> allMaps = vpnNets.getVpnMap();
117 if (router != null) {
118 for (VpnMap vpnMap : allMaps) {
119 if (router.equals(vpnMap.getRouterId())) {
120 return vpnMap.getVpnId();
128 protected static Uuid getRouterforVpn(DataBroker broker, Uuid vpnId) {
129 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
130 .child(VpnMap.class, new VpnMapKey(vpnId)).build();
131 Optional<VpnMap> optionalVpnMap = read(broker, LogicalDatastoreType.CONFIGURATION,
133 if (optionalVpnMap.isPresent()) {
134 VpnMap vpnMap = optionalVpnMap.get();
135 return vpnMap.getRouterId();
140 protected static Uuid getNeutronPortIdfromPortName(DataBroker broker, String portname) {
141 InstanceIdentifier id = buildPortNameToPortUuidIdentifier(portname);
142 Optional<PortNameToPortUuid> portNameToPortUuidData = read(broker, LogicalDatastoreType.CONFIGURATION, id);
143 if (portNameToPortUuidData.isPresent()) {
144 return portNameToPortUuidData.get().getPortId();
149 protected static String getNeutronPortNamefromPortFixedIp(DataBroker broker, String fixedIp) {
150 InstanceIdentifier id = buildFixedIpToPortNameIdentifier(fixedIp);
151 Optional<PortFixedipToPortName> portFixedipToPortNameData = read(broker, LogicalDatastoreType.CONFIGURATION,
153 if (portFixedipToPortNameData.isPresent()) {
154 return portFixedipToPortNameData.get().getPortName();
159 protected static List<Uuid> getSubnetIdsFromNetworkId(DataBroker broker, Uuid networkId) {
160 InstanceIdentifier id = buildNetworkMapIdentifier(networkId);
161 Optional<NetworkMap> optionalNetworkMap = read(broker, LogicalDatastoreType.CONFIGURATION,
163 if (optionalNetworkMap.isPresent()) {
164 return optionalNetworkMap.get().getSubnetIdList();
170 //Will be done once integrated with TrunkPort Extensions
171 protected static int getVlanFromNeutronPort(Port port) {
174 TrunkportExt trunkportExt = port.getAugmentation(TrunkportExt.class);
175 if (trunkportExt != null) {
176 Class<? extends TrunkportTypeBase> trunkportType = trunkportExt.getType();
177 if (trunkportType != null && trunkportType.isAssignableFrom(TrunkportTypeSubport.class)) {
178 vlanId = trunkportExt.getVid();
185 protected static Router getNeutronRouter(DataBroker broker, Uuid routerId) {
187 InstanceIdentifier<Router> inst = InstanceIdentifier.create(Neutron.class).
188 child(Routers.class).child(Router.class, new RouterKey(routerId));
190 Optional<Router> rtr = read(broker, LogicalDatastoreType.CONFIGURATION, inst);
191 if (rtr.isPresent()) {
197 protected static Network getNeutronNetwork(DataBroker broker, Uuid networkId) {
198 logger.debug("getNeutronNetwork for {}", networkId.getValue());
199 InstanceIdentifier<Network> inst = InstanceIdentifier.create(Neutron.class).
200 child(Networks.class).child(Network.class, new NetworkKey(networkId));
202 Optional<Network> net = read(broker, LogicalDatastoreType.CONFIGURATION, inst);
203 if (net.isPresent()) {
209 protected static List<Uuid> getNeutronRouterSubnetIds(DataBroker broker, Uuid routerId) {
210 logger.info("getNeutronRouterSubnetIds for {}", routerId.getValue());
212 List<Uuid> subnetNames = new ArrayList<Uuid>();
213 Router router = getNeutronRouter(broker, routerId);
214 if (router != null) {
215 List<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router
216 .Interfaces> ifs = router.getInterfaces();
217 if (!ifs.isEmpty()) {
218 for (org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers
219 .router.Interfaces iff : ifs) {
220 subnetNames.add(iff.getSubnetId());
224 logger.info("returning from getNeutronRouterSubnetIds for {}", routerId.getValue());
228 protected static String uuidToTapPortName(Uuid id) {
229 String tapId = id.getValue().substring(0, 11);
230 return new StringBuilder().append("tap").append(tapId).toString();
233 protected static void lockVpnInterface(LockManagerService lockManager, String vpnInterfaceName) {
234 TryLockInput input = new TryLockInputBuilder().setLockName(vpnInterfaceName).setTime(5L).setTimeUnit
235 (TimeUnits.Milliseconds).build();
236 Future<RpcResult<Void>> result = lockManager.tryLock(input);
238 if ((result != null) && (result.get().isSuccessful())) {
239 logger.debug("Acquired lock for vpninterface {}", vpnInterfaceName);
241 throw new RuntimeException(String.format("Unable to getLock for vpninterface %s", vpnInterfaceName));
243 } catch (InterruptedException | ExecutionException e) {
244 logger.error("Unable to getLock for vpninterface {}", vpnInterfaceName);
245 throw new RuntimeException(String.format("Unable to getLock for vpninterface %s", vpnInterfaceName), e
250 protected static void unlockVpnInterface(LockManagerService lockManager, String vpnInterfaceName) {
251 UnlockInput input = new UnlockInputBuilder().setLockName(vpnInterfaceName).build();
252 Future<RpcResult<Void>> result = lockManager.unlock(input);
254 if ((result != null) && (result.get().isSuccessful())) {
255 logger.debug("Unlocked vpninterface{}", vpnInterfaceName);
257 logger.debug("Unable to unlock vpninterface {}", vpnInterfaceName);
259 } catch (InterruptedException | ExecutionException e) {
260 logger.error("Unable to unlock vpninterface {}", vpnInterfaceName);
261 throw new RuntimeException(String.format("Unable to unlock vpninterface %s", vpnInterfaceName), e
266 static InstanceIdentifier<PortNameToPortUuid> buildPortNameToPortUuidIdentifier(String portname) {
267 InstanceIdentifier<PortNameToPortUuid> id = InstanceIdentifier.builder(NeutronPortData.class).child
268 (PortNameToPortUuid.class, new PortNameToPortUuidKey(portname)).build();
272 static InstanceIdentifier<PortFixedipToPortName> buildFixedIpToPortNameIdentifier(String fixedIp) {
273 InstanceIdentifier<PortFixedipToPortName> id = InstanceIdentifier.builder(NeutronPortData.class).child
274 (PortFixedipToPortName.class, new PortFixedipToPortNameKey(fixedIp)).build();
278 static InstanceIdentifier<NetworkMap> buildNetworkMapIdentifier(Uuid networkId) {
279 InstanceIdentifier<NetworkMap> id = InstanceIdentifier.builder(NetworkMaps.class).child(NetworkMap.class, new
280 NetworkMapKey(networkId)).build();
284 static InstanceIdentifier<Subnetmap> buildSubnetMapIdentifier(Uuid subnetId) {
285 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).child(Subnetmap.class, new
286 SubnetmapKey(subnetId)).build();
290 static InstanceIdentifier<Interface> buildVlanInterfaceIdentifier(String interfaceName) {
291 InstanceIdentifier<Interface> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, new
292 InterfaceKey(interfaceName)).build();
296 static <T extends DataObject> Optional<T> read(DataBroker broker, LogicalDatastoreType datastoreType,
297 InstanceIdentifier<T> path) {
299 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
301 Optional<T> result = Optional.absent();
303 result = tx.read(datastoreType, path).get();
304 } catch (Exception e) {
305 throw new RuntimeException(e);