Merge "L2 Gw create changes related to ITM Tunnels creation in neutronvpn module"
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / commons / InterfaceMetaUtils.java
1 /*
2  * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.vpnservice.interfacemgr.commons;
9
10 import com.google.common.base.Optional;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.vpnservice.interfacemgr.IfmConstants;
15 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.*;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._if.indexes._interface.map.IfIndexInterface;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._if.indexes._interface.map.IfIndexInterfaceBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._if.indexes._interface.map.IfIndexInterfaceKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntry;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntryKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntry;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntryKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntryBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntryKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntry;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntryBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntryKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.tunnel.instance._interface.map.TunnelInstanceInterface;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.tunnel.instance._interface.map.TunnelInstanceInterfaceBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.tunnel.instance._interface.map.TunnelInstanceInterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 import java.math.BigInteger;
48
49 public class InterfaceMetaUtils {
50     private static final Logger LOG = LoggerFactory.getLogger(InterfaceMetaUtils.class);
51     public static InstanceIdentifier<BridgeRefEntry> getBridgeRefEntryIdentifier(BridgeRefEntryKey bridgeRefEntryKey) {
52         InstanceIdentifier.InstanceIdentifierBuilder<BridgeRefEntry> bridgeRefEntryInstanceIdentifierBuilder =
53                 InstanceIdentifier.builder(BridgeRefInfo.class)
54                         .child(BridgeRefEntry.class, bridgeRefEntryKey);
55         return bridgeRefEntryInstanceIdentifierBuilder.build();
56     }
57
58     public static BridgeRefEntry getBridgeRefEntryFromOperDS(InstanceIdentifier<BridgeRefEntry> dpnBridgeEntryIid,
59                                                              DataBroker dataBroker) {
60         Optional<BridgeRefEntry> bridgeRefEntryOptional =
61                 IfmUtil.read(LogicalDatastoreType.OPERATIONAL, dpnBridgeEntryIid, dataBroker);
62         if (!bridgeRefEntryOptional.isPresent()) {
63             return null;
64         }
65         return bridgeRefEntryOptional.get();
66     }
67
68     public static BridgeRefEntry getBridgeReferenceForInterface(Interface interfaceInfo,
69                                                              DataBroker dataBroker) {
70         ParentRefs parentRefs = interfaceInfo.getAugmentation(ParentRefs.class);
71         BigInteger dpn = parentRefs.getDatapathNodeIdentifier();
72         BridgeRefEntryKey BridgeRefEntryKey = new BridgeRefEntryKey(dpn);
73         InstanceIdentifier<BridgeRefEntry> dpnBridgeEntryIid = getBridgeRefEntryIdentifier(BridgeRefEntryKey);
74         BridgeRefEntry bridgeRefEntry = getBridgeRefEntryFromOperDS(dpnBridgeEntryIid, dataBroker);
75         return bridgeRefEntry;
76     }
77
78     public static boolean bridgeExists(BridgeRefEntry bridgeRefEntry,
79                                                                 DataBroker dataBroker) {
80         if (bridgeRefEntry != null && bridgeRefEntry.getBridgeReference() != null) {
81             InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid =
82                     (InstanceIdentifier<OvsdbBridgeAugmentation>) bridgeRefEntry.getBridgeReference().getValue();
83             Optional<OvsdbBridgeAugmentation> bridgeNodeOptional =
84                     IfmUtil.read(LogicalDatastoreType.OPERATIONAL, bridgeIid, dataBroker);
85             if (bridgeNodeOptional.isPresent()) {
86                 return true;
87             }
88         }
89         return false;
90     }
91     public static InstanceIdentifier<BridgeEntry> getBridgeEntryIdentifier(BridgeEntryKey bridgeEntryKey) {
92         InstanceIdentifier.InstanceIdentifierBuilder<BridgeEntry> bridgeEntryIdBuilder =
93                 InstanceIdentifier.builder(BridgeInterfaceInfo.class).child(BridgeEntry.class, bridgeEntryKey);
94         return bridgeEntryIdBuilder.build();
95     }
96
97     public static BridgeEntry getBridgeEntryFromConfigDS(BigInteger dpnId,
98                                                          DataBroker dataBroker) {
99         BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(dpnId);
100         InstanceIdentifier<BridgeEntry> bridgeEntryInstanceIdentifier =
101                 InterfaceMetaUtils.getBridgeEntryIdentifier(bridgeEntryKey);
102         return getBridgeEntryFromConfigDS(bridgeEntryInstanceIdentifier,
103                         dataBroker);
104     }
105
106     public static BridgeEntry getBridgeEntryFromConfigDS(InstanceIdentifier<BridgeEntry> bridgeEntryInstanceIdentifier,
107                                                          DataBroker dataBroker) {
108         Optional<BridgeEntry> bridgeEntryOptional =
109                 IfmUtil.read(LogicalDatastoreType.CONFIGURATION, bridgeEntryInstanceIdentifier, dataBroker);
110         if (!bridgeEntryOptional.isPresent()) {
111             return null;
112         }
113         return bridgeEntryOptional.get();
114     }
115
116     public static InstanceIdentifier<BridgeInterfaceEntry> getBridgeInterfaceEntryIdentifier(BridgeEntryKey bridgeEntryKey,
117                                                                                     BridgeInterfaceEntryKey bridgeInterfaceEntryKey) {
118         return InstanceIdentifier.builder(BridgeInterfaceInfo.class)
119                         .child(BridgeEntry.class, bridgeEntryKey)
120                         .child(BridgeInterfaceEntry.class, bridgeInterfaceEntryKey).build();
121
122     }
123
124     public static BridgeInterfaceEntry getBridgeInterfaceEntryFromConfigDS(
125             InstanceIdentifier<BridgeInterfaceEntry> bridgeInterfaceEntryInstanceIdentifier, DataBroker dataBroker) {
126         Optional<BridgeInterfaceEntry> bridgeInterfaceEntryOptional =
127                 IfmUtil.read(LogicalDatastoreType.CONFIGURATION, bridgeInterfaceEntryInstanceIdentifier, dataBroker);
128         if (!bridgeInterfaceEntryOptional.isPresent()) {
129             return null;
130         }
131         return bridgeInterfaceEntryOptional.get();
132     }
133
134
135     public static void createBridgeInterfaceEntryInConfigDS(BridgeEntryKey bridgeEntryKey,
136                                                              BridgeInterfaceEntryKey bridgeInterfaceEntryKey,
137                                                              String childInterface,
138                                                              WriteTransaction t) {
139         InstanceIdentifier<BridgeInterfaceEntry> bridgeInterfaceEntryIid =
140                 InterfaceMetaUtils.getBridgeInterfaceEntryIdentifier(bridgeEntryKey, bridgeInterfaceEntryKey);
141         BridgeInterfaceEntryBuilder entryBuilder = new BridgeInterfaceEntryBuilder().setKey(bridgeInterfaceEntryKey)
142                 .setInterfaceName(childInterface);
143         t.put(LogicalDatastoreType.CONFIGURATION, bridgeInterfaceEntryIid, entryBuilder.build(), true);
144     }
145
146     public static InstanceIdentifier<InterfaceParentEntry> getInterfaceParentEntryIdentifier(
147             InterfaceParentEntryKey interfaceParentEntryKey) {
148         InstanceIdentifier.InstanceIdentifierBuilder<InterfaceParentEntry> intfIdBuilder =
149                 InstanceIdentifier.builder(InterfaceChildInfo.class)
150                         .child(InterfaceParentEntry.class, interfaceParentEntryKey);
151         return intfIdBuilder.build();
152     }
153
154     public static InstanceIdentifier<InterfaceChildEntry> getInterfaceChildEntryIdentifier(
155             InterfaceParentEntryKey interfaceParentEntryKey, InterfaceChildEntryKey interfaceChildEntryKey) {
156         InstanceIdentifier.InstanceIdentifierBuilder<InterfaceChildEntry> intfIdBuilder =
157                 InstanceIdentifier.builder(InterfaceChildInfo.class)
158                         .child(InterfaceParentEntry.class, interfaceParentEntryKey)
159                         .child(InterfaceChildEntry.class, interfaceChildEntryKey);
160         return intfIdBuilder.build();
161     }
162
163     public static InterfaceParentEntry getInterfaceParentEntryFromConfigDS(
164             InterfaceParentEntryKey interfaceParentEntryKey, DataBroker dataBroker) {
165         InstanceIdentifier<InterfaceParentEntry> intfParentIid =
166                 getInterfaceParentEntryIdentifier(interfaceParentEntryKey);
167
168         return getInterfaceParentEntryFromConfigDS(intfParentIid, dataBroker);
169     }
170
171     public static InterfaceParentEntry getInterfaceParentEntryFromConfigDS(
172             InstanceIdentifier<InterfaceParentEntry> intfId, DataBroker dataBroker) {
173         Optional<InterfaceParentEntry> interfaceParentEntryOptional =
174                 IfmUtil.read(LogicalDatastoreType.CONFIGURATION, intfId, dataBroker);
175         if (!interfaceParentEntryOptional.isPresent()) {
176             return null;
177         }
178         return interfaceParentEntryOptional.get();
179     }
180
181     public static InterfaceChildEntry getInterfaceChildEntryFromConfigDS(InterfaceParentEntryKey interfaceParentEntryKey,
182                                                                          InterfaceChildEntryKey interfaceChildEntryKey,
183                                                                          DataBroker dataBroker) {
184         InstanceIdentifier<InterfaceChildEntry> intfChildIid =
185                 getInterfaceChildEntryIdentifier(interfaceParentEntryKey, interfaceChildEntryKey);
186
187         return getInterfaceChildEntryFromConfigDS(intfChildIid, dataBroker);
188     }
189
190     public static InterfaceChildEntry getInterfaceChildEntryFromConfigDS(
191             InstanceIdentifier<InterfaceChildEntry> intfChildIid, DataBroker dataBroker) {
192         Optional<InterfaceChildEntry> interfaceChildEntryOptional =
193                 IfmUtil.read(LogicalDatastoreType.CONFIGURATION, intfChildIid, dataBroker);
194         if (!interfaceChildEntryOptional.isPresent()) {
195             return null;
196         }
197         return interfaceChildEntryOptional.get();
198     }
199
200     public static void createLportTagInterfaceMap(WriteTransaction t, String infName, Integer ifIndex) {
201         LOG.debug("creating lport tag to interface map for {}",infName);
202         InstanceIdentifier<IfIndexInterface> id = InstanceIdentifier.builder(IfIndexesInterfaceMap.class).child(IfIndexInterface.class, new IfIndexInterfaceKey(ifIndex)).build();
203         IfIndexInterface ifIndexInterface = new IfIndexInterfaceBuilder().setIfIndex(ifIndex).setKey(new IfIndexInterfaceKey(ifIndex)).setInterfaceName(infName).build();
204         t.put(LogicalDatastoreType.OPERATIONAL, id, ifIndexInterface, true);
205     }
206
207     public static void removeLportTagInterfaceMap(WriteTransaction t, IdManagerService idManager, DataBroker broker, String infName, Integer ifIndex) {
208         InstanceIdentifier<IfIndexInterface> id = InstanceIdentifier.builder(IfIndexesInterfaceMap.class).child(IfIndexInterface.class, new IfIndexInterfaceKey(ifIndex)).build();
209         Optional<IfIndexInterface> ifIndexesInterface = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker);
210         if(ifIndexesInterface.isPresent()) {
211            LOG.debug("removing lport tag to interface map for {}",infName);
212            t.delete(LogicalDatastoreType.OPERATIONAL, id);
213         }
214         IfmUtil.releaseId(idManager, IfmConstants.IFM_IDPOOL_NAME, infName);
215     }
216
217     public static void createBridgeRefEntry(BigInteger dpnId, InstanceIdentifier<?> bridgeIid,
218                                             WriteTransaction tx){
219         LOG.debug("Creating bridge ref entry for dpn: {} bridge: {}",
220                 dpnId, bridgeIid);
221         BridgeRefEntryKey bridgeRefEntryKey = new BridgeRefEntryKey(dpnId);
222         InstanceIdentifier<BridgeRefEntry> bridgeEntryId =
223                 InterfaceMetaUtils.getBridgeRefEntryIdentifier(bridgeRefEntryKey);
224         BridgeRefEntryBuilder tunnelDpnBridgeEntryBuilder =
225                 new BridgeRefEntryBuilder().setKey(bridgeRefEntryKey).setDpid(dpnId)
226                         .setBridgeReference(new OvsdbBridgeRef(bridgeIid));
227         tx.put(LogicalDatastoreType.OPERATIONAL, bridgeEntryId, tunnelDpnBridgeEntryBuilder.build(), true);
228     }
229     public static void deleteBridgeRefEntry(BigInteger dpnId,
230                                             WriteTransaction tx) {
231         LOG.debug("Deleting bridge ref entry for dpn: {}",
232                 dpnId);
233         BridgeRefEntryKey bridgeRefEntryKey = new BridgeRefEntryKey(dpnId);
234         InstanceIdentifier<BridgeRefEntry> bridgeEntryId =
235                 InterfaceMetaUtils.getBridgeRefEntryIdentifier(bridgeRefEntryKey);
236         tx.delete(LogicalDatastoreType.OPERATIONAL, bridgeEntryId);
237     }
238
239     public static void createTunnelToInterfaceMap(String tunnelInstanceId,
240                                                   String infName,
241                                                   WriteTransaction transaction) {
242         LOG.debug("creating tunnel instance identifier to interface map for {}",infName);
243         InstanceIdentifier<TunnelInstanceInterface> id = InstanceIdentifier.builder(TunnelInstanceInterfaceMap.class).
244                 child(TunnelInstanceInterface.class, new TunnelInstanceInterfaceKey(tunnelInstanceId)).build();
245         TunnelInstanceInterface tunnelInstanceInterface = new TunnelInstanceInterfaceBuilder().
246                 setTunnelInstanceIdentifier(tunnelInstanceId).setKey(new TunnelInstanceInterfaceKey(tunnelInstanceId)).setInterfaceName(infName).build();
247         transaction.put(LogicalDatastoreType.OPERATIONAL, id, tunnelInstanceInterface, true);
248
249     }
250
251     public static void createTunnelToInterfaceMap(String infName,InstanceIdentifier<Node> nodeId,
252                                                   WriteTransaction transaction,
253                                                   IfTunnel ifTunnel){
254         InstanceIdentifier<Tunnels> tunnelsInstanceIdentifier = org.opendaylight.vpnservice.interfacemgr.renderer.hwvtep.utilities.SouthboundUtils.
255                 createTunnelsInstanceIdentifier(nodeId,
256                 ifTunnel.getTunnelSource(), ifTunnel.getTunnelDestination());
257         createTunnelToInterfaceMap(tunnelsInstanceIdentifier.toString(), infName, transaction);
258     }
259
260     public static void removeTunnelToInterfaceMap(InstanceIdentifier<Node> nodeId,
261                                                   WriteTransaction transaction,
262                                                   IfTunnel ifTunnel){
263         InstanceIdentifier<Tunnels> tunnelsInstanceIdentifier = org.opendaylight.vpnservice.interfacemgr.renderer.hwvtep.utilities.SouthboundUtils.
264                 createTunnelsInstanceIdentifier(nodeId,
265                         ifTunnel.getTunnelSource(), ifTunnel.getTunnelDestination());
266         transaction.delete(LogicalDatastoreType.OPERATIONAL, tunnelsInstanceIdentifier);
267     }
268
269     public static String getInterfaceForTunnelInstanceIdentifier(String tunnelInstanceId,
270                                                   DataBroker dataBroker) {
271         InstanceIdentifier<TunnelInstanceInterface> id = InstanceIdentifier.builder(TunnelInstanceInterfaceMap.class).
272                 child(TunnelInstanceInterface.class, new TunnelInstanceInterfaceKey(tunnelInstanceId)).build();
273         Optional<TunnelInstanceInterface> tunnelInstanceInterfaceOptional = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, dataBroker);
274         if(tunnelInstanceInterfaceOptional.isPresent()){
275             return tunnelInstanceInterfaceOptional.get().getInterfaceName();
276         }
277         return null;
278     }
279
280 }