efecef985d66dfc4b8912550575eea8ed7bc2fe2
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / HwvtepOperationalState.java
1 /*
2  * Copyright (c) 2015 China Telecom Beijing Research Institute 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
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
18 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
20 import org.opendaylight.ovsdb.lib.notation.UUID;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacsKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacsKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelsKey;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
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.Identifiable;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import java.util.Collection;
49 import java.util.Collections;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Map;
53 import java.util.Map.Entry;
54 import java.util.Set;
55 import java.util.concurrent.ConcurrentHashMap;
56
57 //TODO: need to be optimized, get entry by iid not name
58 public class HwvtepOperationalState {
59
60     private static final Logger LOG = LoggerFactory.getLogger(HwvtepOperationalState.class);
61
62     private Map<InstanceIdentifier<Node>, Node> operationalNodes = new HashMap<>();
63     private ReadWriteTransaction transaction;
64     HashMap<InstanceIdentifier<TerminationPoint>, UUID> inflightLocators = Maps.newHashMap();
65     private HwvtepDeviceInfo deviceInfo;
66     private HwvtepConnectionInstance connectionInstance;
67     private Map<Class<? extends Identifiable>, Map<InstanceIdentifier, UUID>> currentTxUUIDs = new ConcurrentHashMap<>();
68     private Map<Class<? extends Identifiable>, Map<InstanceIdentifier, Boolean>> currentTxDeletedKeys = new ConcurrentHashMap<>();
69
70     public HwvtepOperationalState(DataBroker db, Collection<DataTreeModification<Node>> changes) {
71         Map<InstanceIdentifier<Node>, Node> nodeCreateOrUpdate =
72             TransactUtils.extractCreatedOrUpdatedOrRemoved(changes, Node.class);
73         if (nodeCreateOrUpdate != null) {
74             transaction = db.newReadWriteTransaction();
75             for (Entry<InstanceIdentifier<Node>, Node> entry: nodeCreateOrUpdate.entrySet()) {
76                 Optional<Node> readNode = HwvtepSouthboundUtil.readNode(transaction, entry.getKey());
77                 //add related globalNode or physicalSwitchNode to operationalNodes map
78                 //for example, when creating physical port, logical switch is needed
79                 //but logical switch is in HwvtepGlobalAugmentation rather than PhysicalSwitchAugmentation
80                 if (readNode.isPresent()) {
81                     operationalNodes.put(entry.getKey(), readNode.get());
82                     HwvtepGlobalAugmentation hgAugmentation = readNode.get().getAugmentation(HwvtepGlobalAugmentation.class);
83                     PhysicalSwitchAugmentation psAugmentation = readNode.get().getAugmentation(PhysicalSwitchAugmentation.class);
84                     if (hgAugmentation != null && hgAugmentation.getSwitches() != null) {
85                         for (Switches pswitch : hgAugmentation.getSwitches()) {
86                             @SuppressWarnings("unchecked")
87                             InstanceIdentifier<Node> psNodeIid = (InstanceIdentifier<Node>) pswitch.getSwitchRef().getValue();
88                             Optional<Node> psNode = HwvtepSouthboundUtil.readNode(transaction, psNodeIid);
89                             if (psNode.isPresent()) {
90                                 operationalNodes.put(psNodeIid, psNode.get());
91                             }
92                         }
93                     }
94                     if (psAugmentation != null) {
95                         @SuppressWarnings("unchecked")
96                         InstanceIdentifier<Node> hgNodeIid = (InstanceIdentifier<Node>) psAugmentation.getManagedBy().getValue();
97                         Optional<Node> hgNode = HwvtepSouthboundUtil.readNode(transaction, hgNodeIid);
98                         if (hgNode.isPresent()) {
99                             operationalNodes.put(hgNodeIid, hgNode.get());
100                         }
101                     }
102                 }
103             }
104         }
105     }
106
107     public HwvtepOperationalState(HwvtepConnectionInstance connectionInstance) {
108         this.connectionInstance = connectionInstance;
109         this.deviceInfo = connectionInstance.getDeviceInfo();
110         transaction = connectionInstance.getDataBroker().newReadWriteTransaction();
111         Optional<Node> readNode = HwvtepSouthboundUtil.readNode(transaction, connectionInstance.getInstanceIdentifier());
112         if (readNode.isPresent()) {
113             operationalNodes.put(connectionInstance.getInstanceIdentifier(), readNode.get());
114         }
115     }
116
117     public Optional<Node> getGlobalNode(InstanceIdentifier<?> iid) {
118         InstanceIdentifier<Node> nodeIid = iid.firstIdentifierOf(Node.class);
119         return Optional.fromNullable(operationalNodes.get(nodeIid));
120     }
121
122     public Optional<HwvtepGlobalAugmentation> getHwvtepGlobalAugmentation(InstanceIdentifier<?> iid) {
123         Preconditions.checkNotNull(iid);
124         Optional<Node> nodeOptional = getGlobalNode(iid);
125         if (nodeOptional.isPresent()) {
126             return Optional.fromNullable(nodeOptional.get().getAugmentation(HwvtepGlobalAugmentation.class));
127         }
128         return Optional.absent();
129     }
130
131     public Optional<PhysicalSwitchAugmentation> getPhysicalSwitchAugmentation(InstanceIdentifier<?> iid) {
132         Preconditions.checkNotNull(iid);
133         Optional<Node> nodeOptional = getGlobalNode(iid);
134         if (nodeOptional.isPresent()) {
135             return Optional.fromNullable(nodeOptional.get().getAugmentation(PhysicalSwitchAugmentation.class));
136         }
137         return Optional.absent();
138     }
139
140     public Optional<List<TerminationPoint>> getTerminationPointList(InstanceIdentifier<?> iid) {
141         Preconditions.checkNotNull(iid);
142         Optional<Node> nodeOptional = getGlobalNode(iid);
143         if (nodeOptional.isPresent() && nodeOptional.get().getTerminationPoint() != null) {
144             return Optional.fromNullable(nodeOptional.get().getTerminationPoint());
145         }
146         return Optional.absent();
147     }
148
149     public Optional<LogicalSwitches> getLogicalSwitches(InstanceIdentifier<?> iid, LogicalSwitchesKey logicalSwitchesKey) {
150         Preconditions.checkNotNull(iid);
151         Optional<HwvtepGlobalAugmentation> nodeOptional = getHwvtepGlobalAugmentation(iid);
152         if (nodeOptional.isPresent()) {
153             HwvtepGlobalAugmentation hgAugmentation = nodeOptional.get();
154             List<LogicalSwitches> lswitchList = null;
155             if (hgAugmentation != null) {
156                 lswitchList = hgAugmentation.getLogicalSwitches();
157             }
158             if (lswitchList != null) {
159                 for (LogicalSwitches lswitch: lswitchList) {
160                     if (lswitch.getKey().equals(logicalSwitchesKey)) {
161                         return Optional.fromNullable(lswitch);
162                     }
163                 }
164             }
165         }
166         return Optional.absent();
167     }
168
169     public Optional<Tunnels> getTunnels(InstanceIdentifier<?> iid, TunnelsKey tunnelsKey) {
170         Preconditions.checkNotNull(iid);
171         Optional<PhysicalSwitchAugmentation> psOptional = getPhysicalSwitchAugmentation(iid);
172         if (psOptional.isPresent()) {
173             PhysicalSwitchAugmentation psAugmentation = psOptional.get();
174             List<Tunnels> tunnelList = null;
175             if (psAugmentation != null) {
176                 tunnelList = psAugmentation.getTunnels();
177             }
178             if (tunnelList != null) {
179                 for (Tunnels tunnel: tunnelList) {
180                     if (tunnel.getKey().equals(tunnelsKey)) {
181                         return Optional.fromNullable(tunnel);
182                     }
183                 }
184             }
185         }
186         return Optional.absent();
187     }
188
189     public Optional<HwvtepPhysicalPortAugmentation> getPhysicalPortAugmentation(InstanceIdentifier<?> iid,
190             HwvtepNodeName hwvtepNodeName) {
191         Preconditions.checkNotNull(iid);
192         Optional<List<TerminationPoint>> nodeOptional = getTerminationPointList(iid);
193         if (nodeOptional.isPresent()) {
194             List<TerminationPoint> tpList = nodeOptional.get();
195             for (TerminationPoint tp : tpList) {
196                 HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
197                 if (hppAugmentation != null && hppAugmentation.getHwvtepNodeName().equals(hwvtepNodeName)) {
198                     return Optional.fromNullable(hppAugmentation);
199                 }
200             }
201         }
202         return Optional.absent();
203     }
204
205     public Optional<HwvtepPhysicalLocatorAugmentation> getPhysicalLocatorAugmentation(InstanceIdentifier<?> iid,
206             IpAddress dstIp, Class<? extends EncapsulationTypeBase> encapType) {
207         Preconditions.checkNotNull(iid);
208         Optional<List<TerminationPoint>> nodeOptional = getTerminationPointList(iid);
209         if (nodeOptional.isPresent()) {
210             List<TerminationPoint> tpList = nodeOptional.get();
211             for (TerminationPoint tp : tpList) {
212                 HwvtepPhysicalLocatorAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalLocatorAugmentation.class);
213                 if (hppAugmentation != null && hppAugmentation.getDstIp().equals(dstIp)
214                         && hppAugmentation.getEncapsulationType().equals(encapType)) {
215                     return Optional.fromNullable(hppAugmentation);
216                 }
217             }
218         }
219         return Optional.absent();
220     }
221
222     public Optional<LocalMcastMacs> getLocalMcastMacs(InstanceIdentifier<?> iid, LocalMcastMacsKey key) {
223         Preconditions.checkNotNull(iid);
224         Optional<HwvtepGlobalAugmentation> nodeOptional = getHwvtepGlobalAugmentation(iid);
225         if (nodeOptional.isPresent()) {
226             HwvtepGlobalAugmentation hgAugmentation = nodeOptional.get();
227             List<LocalMcastMacs> macList = null;
228             if (hgAugmentation != null) {
229                 macList = hgAugmentation.getLocalMcastMacs();
230             }
231             if (macList != null) {
232                 for (LocalMcastMacs mac: macList) {
233                     if (mac.getKey().equals(key)) {
234                         return Optional.fromNullable(mac);
235                     }
236                 }
237             }
238         }
239         return Optional.absent();
240     }
241
242     public Optional<RemoteMcastMacs> getRemoteMcastMacs(InstanceIdentifier<?> iid, RemoteMcastMacsKey key) {
243         Preconditions.checkNotNull(iid);
244         Optional<HwvtepGlobalAugmentation> nodeOptional = getHwvtepGlobalAugmentation(iid);
245         if (nodeOptional.isPresent()) {
246             HwvtepGlobalAugmentation hgAugmentation = nodeOptional.get();
247             List<RemoteMcastMacs> macList = null;
248             if (hgAugmentation != null) {
249                 macList = hgAugmentation.getRemoteMcastMacs();
250             }
251             if (macList != null) {
252                 for (RemoteMcastMacs mac: macList) {
253                     if (mac.getKey().equals(key)) {
254                         return Optional.fromNullable(mac);
255                     }
256                 }
257             }
258         }
259         return Optional.absent();
260     }
261
262     public Optional<LocalUcastMacs> getLocalUcastMacs(InstanceIdentifier<?> iid, LocalUcastMacsKey key) {
263         Preconditions.checkNotNull(iid);
264         Optional<HwvtepGlobalAugmentation> nodeOptional = getHwvtepGlobalAugmentation(iid);
265         if (nodeOptional.isPresent()) {
266             HwvtepGlobalAugmentation hgAugmentation = nodeOptional.get();
267             List<LocalUcastMacs> macList = null;
268             if (hgAugmentation != null) {
269                 macList = hgAugmentation.getLocalUcastMacs();
270             }
271             if (macList != null) {
272                 for (LocalUcastMacs mac: macList) {
273                     if (mac.getKey().equals(key)) {
274                         return Optional.fromNullable(mac);
275                     }
276                 }
277             }
278         }
279         return Optional.absent();
280     }
281
282     public Optional<RemoteUcastMacs> getRemoteUcastMacs(InstanceIdentifier<?> iid, RemoteUcastMacsKey key) {
283         Preconditions.checkNotNull(iid);
284         Optional<HwvtepGlobalAugmentation> nodeOptional = getHwvtepGlobalAugmentation(iid);
285         if (nodeOptional.isPresent()) {
286             HwvtepGlobalAugmentation hgAugmentation = nodeOptional.get();
287             List<RemoteUcastMacs> macList = null;
288             if (hgAugmentation != null) {
289                 macList = hgAugmentation.getRemoteUcastMacs();
290             }
291             if (macList != null) {
292                 for (RemoteUcastMacs mac: macList) {
293                     if (mac.getKey().equals(key)) {
294                         return Optional.fromNullable(mac);
295                     }
296                 }
297             }
298         }
299         return Optional.absent();
300     }
301
302     public Optional<HwvtepPhysicalLocatorAugmentation> getPhysicalLocatorAugmentation(InstanceIdentifier<TerminationPoint> iid) {
303         Optional<TerminationPoint> tp = HwvtepSouthboundUtil.readNode(transaction, iid);
304         if (tp.isPresent()) {
305             return Optional.fromNullable(tp.get().getAugmentation(HwvtepPhysicalLocatorAugmentation.class));
306         }
307         return Optional.absent();
308     }
309
310     public Optional<LogicalSwitches> getLogicalSwitches(InstanceIdentifier<LogicalSwitches> iid) {
311         Optional<LogicalSwitches> lswitch = HwvtepSouthboundUtil.readNode(transaction, iid);
312         return lswitch;
313     }
314
315     public Optional<Tunnels> getTunnels(InstanceIdentifier<Tunnels> iid) {
316         Optional<Tunnels> tunnels = HwvtepSouthboundUtil.readNode(transaction, iid);
317         return tunnels;
318     }
319
320     public ReadWriteTransaction getReadWriteTransaction() {
321         return transaction;
322     }
323
324     public void setPhysicalLocatorInFlight(InstanceIdentifier<TerminationPoint> iid,
325                                            UUID uuid) {
326         inflightLocators.put(iid, uuid);
327     }
328
329     public UUID getPhysicalLocatorInFlight(InstanceIdentifier<TerminationPoint> iid) {
330         return inflightLocators.get(iid);
331     }
332
333     public HwvtepConnectionInstance getConnectionInstance() {
334         return connectionInstance;
335     }
336
337     public HwvtepDeviceInfo getDeviceInfo() {
338         return deviceInfo;
339     }
340
341     public void updateCurrentTxData(Class<? extends Identifiable> cls, InstanceIdentifier key, UUID uuid) {
342         HwvtepSouthboundUtil.updateData(currentTxUUIDs, cls, key, uuid);
343         deviceInfo.markKeyAsInTransit(cls, key);
344     }
345
346     public void updateCurrentTxDeleteData(Class<? extends Identifiable> cls, InstanceIdentifier key) {
347         HwvtepSouthboundUtil.updateData(currentTxDeletedKeys, cls, key, Boolean.TRUE);
348     }
349
350     public UUID getUUIDFromCurrentTx(Class<? extends Identifiable> cls, InstanceIdentifier key) {
351         return HwvtepSouthboundUtil.getData(currentTxUUIDs, cls, key);
352     }
353
354     public boolean isKeyPartOfCurrentTx(Class<? extends Identifiable> cls, InstanceIdentifier key) {
355         return HwvtepSouthboundUtil.containsKey(currentTxUUIDs, cls, key);
356     }
357
358     public Set<InstanceIdentifier> getDeletedKeysInCurrentTx(Class<? extends Identifiable> cls) {
359         if (currentTxDeletedKeys.containsKey(cls)) {
360             return currentTxDeletedKeys.get(cls).keySet();
361         }
362         return Collections.EMPTY_SET;
363     }
364
365 }