Bug 5463 : Fixing datapath-id changing behaviour seen while testing with mininet
[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 com.google.common.util.concurrent.ListenableFuture;
12 import org.eclipse.xtend.lib.annotations.Data;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.idmanager.IdManager;
17 import org.opendaylight.vpnservice.interfacemgr.IfmConstants;
18 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
19 import org.opendaylight.vpnservice.interfacemgr.renderer.ovs.utilities.SouthboundUtils;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.BridgeInterfaceInfo;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.BridgeRefInfo;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.IfIndexesInterfaceMap;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.InterfaceChildInfo;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._if.indexes._interface.map.IfIndexInterface;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._if.indexes._interface.map.IfIndexInterfaceBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._if.indexes._interface.map.IfIndexInterfaceKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntry;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntryKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntry;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntryKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntry;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntryBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntryKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntry;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntryBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntryKey;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
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 import java.util.ArrayList;
49 import java.util.List;
50
51 public class InterfaceMetaUtils {
52     private static final Logger LOG = LoggerFactory.getLogger(InterfaceMetaUtils.class);
53     public static InstanceIdentifier<BridgeRefEntry> getBridgeRefEntryIdentifier(BridgeRefEntryKey bridgeRefEntryKey) {
54         InstanceIdentifier.InstanceIdentifierBuilder<BridgeRefEntry> bridgeRefEntryInstanceIdentifierBuilder =
55                 InstanceIdentifier.builder(BridgeRefInfo.class)
56                         .child(BridgeRefEntry.class, bridgeRefEntryKey);
57         return bridgeRefEntryInstanceIdentifierBuilder.build();
58     }
59
60
61     public static BridgeRefEntry getBridgeRefEntryFromOperDS(InstanceIdentifier<BridgeRefEntry> dpnBridgeEntryIid,
62                                                              DataBroker dataBroker) {
63         Optional<BridgeRefEntry> bridgeRefEntryOptional =
64                 IfmUtil.read(LogicalDatastoreType.OPERATIONAL, dpnBridgeEntryIid, dataBroker);
65         if (!bridgeRefEntryOptional.isPresent()) {
66             return null;
67         }
68         return bridgeRefEntryOptional.get();
69     }
70
71     public static InstanceIdentifier<BridgeEntry> getBridgeEntryIdentifier(BridgeEntryKey bridgeEntryKey) {
72         InstanceIdentifier.InstanceIdentifierBuilder<BridgeEntry> bridgeEntryIdBuilder =
73                 InstanceIdentifier.builder(BridgeInterfaceInfo.class).child(BridgeEntry.class, bridgeEntryKey);
74         return bridgeEntryIdBuilder.build();
75     }
76
77     public static BridgeEntry getBridgeEntryFromConfigDS(BigInteger dpnId,
78                                                          DataBroker dataBroker) {
79         BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(dpnId);
80         InstanceIdentifier<BridgeEntry> bridgeEntryInstanceIdentifier =
81                 InterfaceMetaUtils.getBridgeEntryIdentifier(bridgeEntryKey);
82         return getBridgeEntryFromConfigDS(bridgeEntryInstanceIdentifier,
83                         dataBroker);
84     }
85
86     public static BridgeEntry getBridgeEntryFromConfigDS(InstanceIdentifier<BridgeEntry> bridgeEntryInstanceIdentifier,
87                                                          DataBroker dataBroker) {
88         Optional<BridgeEntry> bridgeEntryOptional =
89                 IfmUtil.read(LogicalDatastoreType.CONFIGURATION, bridgeEntryInstanceIdentifier, dataBroker);
90         if (!bridgeEntryOptional.isPresent()) {
91             return null;
92         }
93         return bridgeEntryOptional.get();
94     }
95
96     public static InstanceIdentifier<BridgeInterfaceEntry> getBridgeInterfaceEntryIdentifier(BridgeEntryKey bridgeEntryKey,
97                                                                                     BridgeInterfaceEntryKey bridgeInterfaceEntryKey) {
98         return InstanceIdentifier.builder(BridgeInterfaceInfo.class)
99                         .child(BridgeEntry.class, bridgeEntryKey)
100                         .child(BridgeInterfaceEntry.class, bridgeInterfaceEntryKey).build();
101
102     }
103
104     public static BridgeInterfaceEntry getBridgeInterfaceEntryFromConfigDS(
105             InstanceIdentifier<BridgeInterfaceEntry> bridgeInterfaceEntryInstanceIdentifier, DataBroker dataBroker) {
106         Optional<BridgeInterfaceEntry> bridgeInterfaceEntryOptional =
107                 IfmUtil.read(LogicalDatastoreType.CONFIGURATION, bridgeInterfaceEntryInstanceIdentifier, dataBroker);
108         if (!bridgeInterfaceEntryOptional.isPresent()) {
109             return null;
110         }
111         return bridgeInterfaceEntryOptional.get();
112     }
113
114
115     public static void createBridgeInterfaceEntryInConfigDS(BridgeEntryKey bridgeEntryKey,
116                                                              BridgeInterfaceEntryKey bridgeInterfaceEntryKey,
117                                                              String childInterface,
118                                                              WriteTransaction t) {
119         InstanceIdentifier<BridgeInterfaceEntry> bridgeInterfaceEntryIid =
120                 InterfaceMetaUtils.getBridgeInterfaceEntryIdentifier(bridgeEntryKey, bridgeInterfaceEntryKey);
121         BridgeInterfaceEntryBuilder entryBuilder = new BridgeInterfaceEntryBuilder().setKey(bridgeInterfaceEntryKey)
122                 .setInterfaceName(childInterface);
123         t.put(LogicalDatastoreType.CONFIGURATION, bridgeInterfaceEntryIid, entryBuilder.build(), true);
124     }
125
126     public static InstanceIdentifier<InterfaceParentEntry> getInterfaceParentEntryIdentifier(
127             InterfaceParentEntryKey interfaceParentEntryKey) {
128         InstanceIdentifier.InstanceIdentifierBuilder<InterfaceParentEntry> intfIdBuilder =
129                 InstanceIdentifier.builder(InterfaceChildInfo.class)
130                         .child(InterfaceParentEntry.class, interfaceParentEntryKey);
131         return intfIdBuilder.build();
132     }
133
134     public static InstanceIdentifier<InterfaceChildEntry> getInterfaceChildEntryIdentifier(
135             InterfaceParentEntryKey interfaceParentEntryKey, InterfaceChildEntryKey interfaceChildEntryKey) {
136         InstanceIdentifier.InstanceIdentifierBuilder<InterfaceChildEntry> intfIdBuilder =
137                 InstanceIdentifier.builder(InterfaceChildInfo.class)
138                         .child(InterfaceParentEntry.class, interfaceParentEntryKey)
139                         .child(InterfaceChildEntry.class, interfaceChildEntryKey);
140         return intfIdBuilder.build();
141     }
142
143     public static InterfaceParentEntry getInterfaceParentEntryFromConfigDS(
144             InterfaceParentEntryKey interfaceParentEntryKey, DataBroker dataBroker) {
145         InstanceIdentifier<InterfaceParentEntry> intfParentIid =
146                 getInterfaceParentEntryIdentifier(interfaceParentEntryKey);
147
148         return getInterfaceParentEntryFromConfigDS(intfParentIid, dataBroker);
149     }
150
151     public static InterfaceParentEntry getInterfaceParentEntryFromConfigDS(
152             InstanceIdentifier<InterfaceParentEntry> intfId, DataBroker dataBroker) {
153         Optional<InterfaceParentEntry> interfaceParentEntryOptional =
154                 IfmUtil.read(LogicalDatastoreType.CONFIGURATION, intfId, dataBroker);
155         if (!interfaceParentEntryOptional.isPresent()) {
156             return null;
157         }
158         return interfaceParentEntryOptional.get();
159     }
160
161     public static InterfaceChildEntry getInterfaceChildEntryFromConfigDS(InterfaceParentEntryKey interfaceParentEntryKey,
162                                                                          InterfaceChildEntryKey interfaceChildEntryKey,
163                                                                          DataBroker dataBroker) {
164         InstanceIdentifier<InterfaceChildEntry> intfChildIid =
165                 getInterfaceChildEntryIdentifier(interfaceParentEntryKey, interfaceChildEntryKey);
166
167         return getInterfaceChildEntryFromConfigDS(intfChildIid, dataBroker);
168     }
169
170     public static InterfaceChildEntry getInterfaceChildEntryFromConfigDS(
171             InstanceIdentifier<InterfaceChildEntry> intfChildIid, DataBroker dataBroker) {
172         Optional<InterfaceChildEntry> interfaceChildEntryOptional =
173                 IfmUtil.read(LogicalDatastoreType.CONFIGURATION, intfChildIid, dataBroker);
174         if (!interfaceChildEntryOptional.isPresent()) {
175             return null;
176         }
177         return interfaceChildEntryOptional.get();
178     }
179
180     public static void createLportTagInterfaceMap(WriteTransaction t, String infName, Integer ifIndex) {
181         LOG.debug("creating lport tag to interface map for {}",infName);
182         InstanceIdentifier<IfIndexInterface> id = InstanceIdentifier.builder(IfIndexesInterfaceMap.class).child(IfIndexInterface.class, new IfIndexInterfaceKey(ifIndex)).build();
183         IfIndexInterface ifIndexInterface = new IfIndexInterfaceBuilder().setIfIndex(ifIndex).setKey(new IfIndexInterfaceKey(ifIndex)).setInterfaceName(infName).build();
184         t.put(LogicalDatastoreType.OPERATIONAL, id, ifIndexInterface, true);
185     }
186
187     public static void removeLportTagInterfaceMap(WriteTransaction t, IdManagerService idManager, DataBroker broker, String infName, Integer ifIndex) {
188         InstanceIdentifier<IfIndexInterface> id = InstanceIdentifier.builder(IfIndexesInterfaceMap.class).child(IfIndexInterface.class, new IfIndexInterfaceKey(ifIndex)).build();
189         Optional<IfIndexInterface> ifIndexesInterface = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker);
190         if(ifIndexesInterface.isPresent()) {
191            LOG.debug("removing lport tag to interface map for {}",infName);
192            t.delete(LogicalDatastoreType.OPERATIONAL, id);
193         }
194         IfmUtil.releaseId(idManager, IfmConstants.IFM_IDPOOL_NAME, infName);
195     }
196
197     public static void createBridgeRefEntry(BigInteger dpnId, InstanceIdentifier<?> bridgeIid,
198                                             WriteTransaction tx){
199         LOG.debug("Creating bridge ref entry for dpn: {} bridge: {}",
200                 dpnId, bridgeIid);
201         BridgeRefEntryKey bridgeRefEntryKey = new BridgeRefEntryKey(dpnId);
202         InstanceIdentifier<BridgeRefEntry> bridgeEntryId =
203                 InterfaceMetaUtils.getBridgeRefEntryIdentifier(bridgeRefEntryKey);
204         BridgeRefEntryBuilder tunnelDpnBridgeEntryBuilder =
205                 new BridgeRefEntryBuilder().setKey(bridgeRefEntryKey).setDpid(dpnId)
206                         .setBridgeReference(new OvsdbBridgeRef(bridgeIid));
207         tx.put(LogicalDatastoreType.OPERATIONAL, bridgeEntryId, tunnelDpnBridgeEntryBuilder.build(), true);
208     }
209
210     public static void deleteBridgeRefEntry(BigInteger dpnId,
211                                             WriteTransaction tx) {
212         LOG.debug("Deleting bridge ref entry for dpn: {}",
213                 dpnId);
214         BridgeRefEntryKey bridgeRefEntryKey = new BridgeRefEntryKey(dpnId);
215         InstanceIdentifier<BridgeRefEntry> bridgeEntryId =
216                 InterfaceMetaUtils.getBridgeRefEntryIdentifier(bridgeRefEntryKey);
217         tx.delete(LogicalDatastoreType.OPERATIONAL, bridgeEntryId);
218     }
219 }