MRI version bumpup for Aluminium
[netvirt.git] / vpnmanager / api / src / main / java / org / opendaylight / netvirt / vpnmanager / api / VpnHelper.java
1 /*
2  * Copyright © 2015, 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netvirt.vpnmanager.api;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.Optional;
14 import java.util.concurrent.ExecutionException;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.ReadTransaction;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.VpnInstanceOpData;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.VpnInstanceToVpnId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntryKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnList;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnListKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.VpnInstances;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.VpnInterfaces;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.instances.VpnInstance;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.instances.VpnInstanceKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.interfaces.VpnInterface;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.interfaces.VpnInterfaceKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.interfaces.vpn._interface.VpnInstanceNames;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.interfaces.vpn._interface.VpnInstanceNames.AssociatedSubnetType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.interfaces.vpn._interface.VpnInstanceNamesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.SubnetmapKey;
39 import org.opendaylight.yangtools.yang.binding.DataObject;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.common.Uint64;
42
43 public final class VpnHelper {
44     private VpnHelper() {
45
46     }
47
48     //FIXME: Implement caches for DS reads
49     @Nullable
50     public static VpnInstance getVpnInstance(DataBroker broker, String vpnInstanceName) {
51         InstanceIdentifier<VpnInstance> id = InstanceIdentifier.builder(VpnInstances.class).child(VpnInstance.class,
52                 new VpnInstanceKey(vpnInstanceName)).build();
53         Optional<VpnInstance> vpnInstance = read(broker, LogicalDatastoreType.CONFIGURATION, id);
54         return (vpnInstance.isPresent()) ? vpnInstance.get() : null;
55     }
56
57     public static <T extends DataObject> Optional<T> read(DataBroker broker, LogicalDatastoreType datastoreType,
58                                                           InstanceIdentifier<T> path) {
59         try (ReadTransaction tx = broker.newReadOnlyTransaction()) {
60             return tx.read(datastoreType, path).get();
61         } catch (InterruptedException | ExecutionException e) {
62             throw new RuntimeException(e);
63         }
64     }
65
66     public static List<VpnInstance> getAllVpnInstances(DataBroker broker) {
67         InstanceIdentifier<VpnInstances> id = InstanceIdentifier.builder(VpnInstances.class).build();
68         Optional<VpnInstances> optVpnInstances = read(broker, LogicalDatastoreType.CONFIGURATION, id);
69         if (optVpnInstances.isPresent() && optVpnInstances.get().getVpnInstance() != null) {
70             return new ArrayList<VpnInstance>(optVpnInstances.get().getVpnInstance().values());
71         } else {
72             return Collections.emptyList();
73         }
74     }
75
76     /**
77      * Retrieves the dataplane identifier of a specific VPN, searching by its
78      * VpnInstance name.
79      *
80      * @param broker dataBroker service reference
81      * @param vpnName Name of the VPN
82      * @return the dataplane identifier of the VPN, the VrfTag.
83      */
84     public static long getVpnId(DataBroker broker, String vpnName) {
85         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn
86                 .id.VpnInstance>
87                 id
88                 = getVpnInstanceToVpnIdIdentifier(vpnName);
89         Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id
90                 .VpnInstance>
91                 vpnInstance
92                 = read(broker, LogicalDatastoreType.CONFIGURATION, id);
93         long vpnId = -1;
94         if (vpnInstance.isPresent()) {
95             vpnId = vpnInstance.get().getVpnId().toJava();
96         }
97         return vpnId;
98     }
99
100     static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911
101             .vpn.instance.to.vpn.id.VpnInstance> getVpnInstanceToVpnIdIdentifier(String vpnName) {
102         return InstanceIdentifier.builder(VpnInstanceToVpnId.class).child(org.opendaylight.yang.gen.v1.urn
103                 .opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id.VpnInstance.class,
104                 new org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.to.vpn.id
105                         .VpnInstanceKey(vpnName)).build();
106     }
107
108     @Nullable
109     public static VpnInterface getVpnInterface(DataBroker broker, String vpnInterfaceName) {
110         InstanceIdentifier<VpnInterface> id = getVpnInterfaceIdentifier(vpnInterfaceName);
111         Optional<VpnInterface> vpnInterface = read(broker, LogicalDatastoreType.CONFIGURATION, id);
112         return vpnInterface.isPresent() ? vpnInterface.get() : null;
113     }
114
115     static InstanceIdentifier<VpnInterface> getVpnInterfaceIdentifier(String vpnInterfaceName) {
116         return InstanceIdentifier.builder(VpnInterfaces.class)
117                 .child(VpnInterface.class, new VpnInterfaceKey(vpnInterfaceName)).build();
118     }
119
120     @Nullable
121     public static String getFirstVpnNameFromVpnInterface(final VpnInterface original) {
122         List<VpnInstanceNames> optList = new ArrayList<VpnInstanceNames>(original.getVpnInstanceNames().values());
123         if (optList != null && !optList.isEmpty()) {
124             return optList.get(0).getVpnName();
125         } else {
126             return null;
127         }
128     }
129
130     @NonNull
131     public static List<String> getVpnInterfaceVpnInstanceNamesString(@Nullable List<VpnInstanceNames> vpnInstanceList) {
132         List<String> listVpn = new ArrayList<>();
133         if (vpnInstanceList != null) {
134             for (VpnInstanceNames vpnInterfaceVpnInstance : vpnInstanceList) {
135                 listVpn.add(vpnInterfaceVpnInstance.getVpnName());
136             }
137         }
138         return listVpn;
139     }
140
141     public static VpnInstanceNames getVpnInterfaceVpnInstanceNames(String vpnName, AssociatedSubnetType subnetType) {
142         return new VpnInstanceNamesBuilder().setVpnName(vpnName).setAssociatedSubnetType(subnetType).build();
143     }
144
145     public static void removeVpnInterfaceVpnInstanceNamesFromList(String vpnName,
146                                List<VpnInstanceNames> vpnInstanceList) {
147         if (vpnInstanceList != null) {
148             vpnInstanceList.removeIf(instance -> vpnName.equals(instance.getVpnName()));
149         }
150     }
151
152     public static boolean doesVpnInterfaceBelongToVpnInstance(String vpnName,
153                                                           List<VpnInstanceNames> vpnInstanceList) {
154         if (vpnInstanceList != null) {
155             for (VpnInstanceNames vpnInstance : vpnInstanceList) {
156                 if (vpnName.equals(vpnInstance.getVpnName())) {
157                     return true;
158                 }
159             }
160         }
161         return false;
162     }
163
164     public static boolean isSubnetPartOfVpn(Subnetmap sn, String vpnName) {
165         if (vpnName == null || sn == null) {
166             return false;
167         }
168         if (sn.getVpnId() == null || !sn.getVpnId().getValue().equals(vpnName)) {
169             return false;
170         }
171         return true;
172     }
173
174     static InstanceIdentifier<Subnetmap> buildSubnetmapIdentifier(Uuid subnetId) {
175         return InstanceIdentifier.builder(Subnetmaps.class)
176         .child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
177
178     }
179
180     /** Get Subnetmap from its Uuid.
181      * @param broker the data broker for look for data
182      * @param subnetUuid the subnet's Uuid
183      * @return the Subnetmap of Uuid or null if it is not found
184      */
185     @Nullable
186     public static Subnetmap getSubnetmapFromItsUuid(DataBroker broker, Uuid subnetUuid) {
187         Subnetmap sn = null;
188         InstanceIdentifier<Subnetmap> id = buildSubnetmapIdentifier(subnetUuid);
189         Optional<Subnetmap> optionalSn = read(broker, LogicalDatastoreType.CONFIGURATION, id);
190         if (optionalSn.isPresent()) {
191             sn = optionalSn.get();
192         }
193         return sn;
194     }
195
196     public static InstanceIdentifier<VpnToDpnList> getVpnToDpnListIdentifier(String rd, Uint64 dpnId) {
197         return InstanceIdentifier.builder(VpnInstanceOpData.class)
198                 .child(VpnInstanceOpDataEntry.class, new VpnInstanceOpDataEntryKey(rd))
199                 .child(VpnToDpnList.class, new VpnToDpnListKey(dpnId)).build();
200     }
201
202 }