Update MRI projects for Aluminium
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / PhysicalPortUpdateCommand.java
1 /*
2  * Copyright © 2015, 2017 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 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
9
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import java.util.Objects;
20 import org.opendaylight.mdsal.binding.api.DataObjectModification;
21 import org.opendaylight.mdsal.binding.api.DataTreeModification;
22 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
23 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
24 import org.opendaylight.ovsdb.lib.notation.UUID;
25 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
26 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
27 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalPort;
28 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class PhysicalPortUpdateCommand
41         extends AbstractTransactCommand<TerminationPoint, TerminationPointKey, PhysicalSwitchAugmentation> {
42     private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortUpdateCommand.class);
43     private static final VlanBindingsUnMetDependencyGetter DEPENDENCY_GETTER = new VlanBindingsUnMetDependencyGetter();
44
45     public PhysicalPortUpdateCommand(final HwvtepOperationalState state,
46             final Collection<DataTreeModification<Node>> changes) {
47         super(state, changes);
48     }
49
50     @Override
51     public void execute(final TransactionBuilder transaction) {
52         Map<InstanceIdentifier<Node>, List<TerminationPoint>> createds =
53                 extractCreated(getChanges(),TerminationPoint.class);
54         if (!createds.isEmpty()) {
55             for (Entry<InstanceIdentifier<Node>, List<TerminationPoint>> created:
56                 createds.entrySet()) {
57                 updatePhysicalPort(transaction,  created.getKey(), created.getValue());
58             }
59         }
60         Map<InstanceIdentifier<Node>, List<TerminationPoint>> updateds =
61                 extractUpdatedPorts(getChanges(), TerminationPoint.class);
62         if (!updateds.isEmpty()) {
63             for (Entry<InstanceIdentifier<Node>, List<TerminationPoint>> updated:
64                 updateds.entrySet()) {
65                 updatePhysicalPort(transaction,  updated.getKey(), updated.getValue());
66             }
67         }
68     }
69
70     public void updatePhysicalPort(final TransactionBuilder transaction,
71                                    final InstanceIdentifier<Node> psNodeiid,
72                                    final List<TerminationPoint> listPort) {
73         if (listPort != null) {
74             for (TerminationPoint port : listPort) {
75                 LOG.debug("Processing port {}", port);
76                 InstanceIdentifier<TerminationPoint> tpIId = psNodeiid.child(TerminationPoint.class, port.key());
77                 HwvtepPhysicalPortAugmentation hwvtepPhysicalPortAugmentation =
78                         port.augmentation(HwvtepPhysicalPortAugmentation.class);
79                 if (hwvtepPhysicalPortAugmentation != null) {
80                     onConfigUpdate(transaction, psNodeiid, port, tpIId);
81                 }
82             }
83         }
84     }
85
86     @Override
87     public void onConfigUpdate(final TransactionBuilder transaction, final InstanceIdentifier psNodeiid,
88                                final TerminationPoint port, final InstanceIdentifier tpIId, final Object... extraData) {
89         doDeviceTransaction(transaction, psNodeiid, port, tpIId);
90     }
91
92     @Override
93     public void doDeviceTransaction(final TransactionBuilder transaction, final InstanceIdentifier nodeIid,
94                                     final TerminationPoint data, final InstanceIdentifier key,
95                                     final Object... extraData) {
96         LOG.debug("Processing port doDeviceTransaction {}", data);
97         InstanceIdentifier<Node> psNodeiid = nodeIid;
98         HwvtepPhysicalPortAugmentation port = data.augmentation(
99                 HwvtepPhysicalPortAugmentation.class);
100         if (port == null) {
101             LOG.info("No port augmentation found for port {}", data);
102             return;
103         }
104         if (port.getHwvtepNodeName() == null) {
105             LOG.info("No port hwvtep node name found for port {}", data);
106             return;
107         }
108         LOG.debug("Creating a physical port named: {}", port.getHwvtepNodeName().getValue());
109         getOperationalState().getDeviceInfo().updateConfigData(VlanBindings.class, key, data);
110         HwvtepDeviceInfo.DeviceData deviceData = getOperationalState().getDeviceInfo()
111                 .getDeviceOperData(VlanBindings.class, key);
112         PhysicalPort physicalPort = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
113                 PhysicalPort.class);
114         //get managing global node of physicalSwitchBelong
115         setName(physicalPort, port);
116         setDescription(physicalPort, port);
117         if (deviceData == null || deviceData.getData() == null) {
118             LOG.error("Updated the device oper cache for port from actual device {}", key);
119             deviceData = super.fetchDeviceData(VlanBindings.class, key);
120         }
121         if (deviceData == null || deviceData.getData() == null) {
122             LOG.warn("Port not present in opdata store {}", key);
123         } else {
124             if (deviceData.getData() == null || !(deviceData.getData() instanceof PhysicalPort)) {
125                 LOG.error("Failed to get the device data for port {}", key);
126             }
127             Map<Long, UUID> bindingMap = setVlanBindings(nodeIid, physicalPort, data, key, transaction);
128             PhysicalPort tp = (PhysicalPort) deviceData.getData();
129             if (getOperationalState().isInReconciliation()) {
130                 if (tp.getVlanBindingsColumn() != null && tp.getVlanBindingsColumn().getData() != null) {
131                     Map<Long, UUID> existing = new HashMap<>(tp.getVlanBindingsColumn().getData());
132                     if (existing.size() == bindingMap.size()) {
133                         boolean allMatched = bindingMap.entrySet().stream().allMatch(newEntry -> {
134                             return Objects.equals(existing.get(newEntry.getKey()), newEntry.getValue());
135                         });
136                         if (allMatched) {
137                             return;
138                         }
139                     }
140                 }
141             }
142             String nodeId = psNodeiid.firstKeyOf(Node.class).getNodeId().getValue();
143             getOperationalState().getDeviceInfo().updateDeviceOperData(VlanBindings.class, key,
144                     deviceData.getUuid(), deviceData.getData());
145             //updated physical port only
146
147             String existingPhysicalPortName = tp.getName();
148             PhysicalPort extraPhyscialPort =
149                     TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalPort.class);
150             extraPhyscialPort.setName("");
151             LOG.trace("execute: updating physical port: {} {}", nodeId, physicalPort);
152             transaction.add(op.update(physicalPort)
153                     .where(extraPhyscialPort.getNameColumn().getSchema().opEqual(existingPhysicalPortName))
154                     .build());
155             transaction.add(op.comment("Physical Port: Updating " + existingPhysicalPortName));
156             updateControllerTxHistory(TransactionType.UPDATE, physicalPort);
157             LOG.info("CONTROLLER - {} {}", TransactionType.UPDATE, physicalPort);
158
159         }
160         return;
161     }
162
163     private void setName(final PhysicalPort physicalPort, final HwvtepPhysicalPortAugmentation inputPhysicalPort) {
164         if (inputPhysicalPort.getHwvtepNodeName() != null) {
165             physicalPort.setName(inputPhysicalPort.getHwvtepNodeName().getValue());
166         }
167     }
168
169     private static void setDescription(final PhysicalPort physicalPort,
170             final HwvtepPhysicalPortAugmentation inputPhysicalPort) {
171         if (inputPhysicalPort.getHwvtepNodeDescription() != null) {
172             physicalPort.setDescription(inputPhysicalPort.getHwvtepNodeDescription());
173         }
174     }
175
176     private Map<Long, UUID> setVlanBindings(final InstanceIdentifier<Node> psNodeiid,
177                                             final PhysicalPort physicalPort,
178                                             final TerminationPoint inputPhysicalPort,
179                                             final InstanceIdentifier key,
180                                             final TransactionBuilder transaction) {
181         HwvtepPhysicalPortAugmentation portAugmentation = inputPhysicalPort.augmentation(
182                 HwvtepPhysicalPortAugmentation.class);
183         Map<Long, UUID> bindingMap = new HashMap<>();
184         //get UUID by LogicalSwitchRef
185         for (VlanBindings vlanBinding : portAugmentation.nonnullVlanBindings().values()) {
186             @SuppressWarnings("unchecked")
187             InstanceIdentifier<LogicalSwitches> lswitchIid =
188                 (InstanceIdentifier<LogicalSwitches>) vlanBinding.getLogicalSwitchRef().getValue();
189
190             Map inTransitDependencies = DEPENDENCY_GETTER.getInTransitDependencies(
191                 getOperationalState(), vlanBinding);
192             Map configDependencies = DEPENDENCY_GETTER.getUnMetConfigDependencies(
193                 getOperationalState(), vlanBinding);
194
195             if (!HwvtepSouthboundUtil.isEmptyMap(configDependencies)) {
196                 createConfigWaitJob(psNodeiid, inputPhysicalPort, key, configDependencies);
197                 continue;
198             }
199             if (!HwvtepSouthboundUtil.isEmptyMap(inTransitDependencies)) {
200                 createOperWaitingJob(psNodeiid, inputPhysicalPort, key, inTransitDependencies);
201                 continue;
202             }
203
204             UUID lsUUid = TransactUtils.getLogicalSwitchUUID(transaction, getOperationalState(), lswitchIid);
205             if (lsUUid == null) {
206                 LOG.error("Could not get the logical switch uuid for {}", vlanBinding);
207                 continue;
208             }
209             bindingMap.put(vlanBinding.getVlanIdKey().getValue().longValue(), lsUUid);
210         }
211         physicalPort.setVlanBindings(bindingMap);
212         return bindingMap;
213     }
214
215     private void createOperWaitingJob(final InstanceIdentifier<Node> psNodeiid,
216                                       final TerminationPoint inputPhysicalPort,
217                                       final InstanceIdentifier<TerminationPoint> key,
218                                       final Map inTransitDependencies) {
219         if (getDeviceInfo().isKeyInDependencyQueue(key)) {
220             return;
221         }
222         DependentJob<VlanBindings> opWaitingJob = new DependentJob.OpWaitingJob(
223                 key, inputPhysicalPort, inTransitDependencies, getOperationalState().getTransactionId()) {
224             @Override
225             public void onDependencyResolved(final HwvtepOperationalState operationalState,
226                                              final TransactionBuilder transactionBuilder) {
227                 LOG.info("physical port oper dependency resolved {}", key);//TODO delete
228                 PhysicalPortUpdateCommand.this.hwvtepOperationalState = operationalState;
229                 HwvtepDeviceInfo.DeviceData deviceData = getOperationalState().getDeviceInfo().getConfigData(
230                         VlanBindings.class, key);
231                 TerminationPoint port = inputPhysicalPort;
232                 if (deviceData != null && deviceData.getData() != null) {
233                     port = (TerminationPoint) deviceData.getData();
234                 }
235                 doDeviceTransaction(transactionBuilder, psNodeiid, port, key);
236             }
237         };
238         LOG.info("Added the port to oper wait queue {}", key);//TODO delete
239         getDeviceInfo().addJobToQueue(opWaitingJob);
240     }
241
242     private void createConfigWaitJob(final InstanceIdentifier<Node> psNodeiid,
243                                      final TerminationPoint inputPhysicalPort,
244                                      final InstanceIdentifier<TerminationPoint> key,
245                                      final Map configDependencies) {
246         if (getDeviceInfo().isKeyInDependencyQueue(key)) {
247             return;
248         }
249         DependentJob<TerminationPoint> configWaitingJob = new DependentJob.ConfigWaitingJob(
250                 key, inputPhysicalPort, configDependencies) {
251             @Override
252             public void onDependencyResolved(final HwvtepOperationalState operationalState,
253                                              final TransactionBuilder transactionBuilder) {
254                 LOG.info("physical port config dependency resolved {}", key);//TODO delete
255                 PhysicalPortUpdateCommand.this.hwvtepOperationalState = operationalState;
256                 HwvtepDeviceInfo.DeviceData deviceData = getOperationalState().getDeviceInfo().getConfigData(
257                         VlanBindings.class, key);
258                 TerminationPoint port = inputPhysicalPort;
259                 if (deviceData != null && deviceData.getData() != null) {
260                     port = (TerminationPoint) deviceData.getData();
261                 }
262                 doDeviceTransaction(transactionBuilder, psNodeiid, port, key);
263             }
264         };
265         LOG.info("Added the port to config wait queue {}", key);//TODO delete
266         getDeviceInfo().addJobToQueue(configWaitingJob);
267     }
268
269     static class VlanBindingsUnMetDependencyGetter extends UnMetDependencyGetter<VlanBindings> {
270
271         @Override
272         public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(final VlanBindings data) {
273             if (data == null) {
274                 return Collections.emptyList();
275             }
276             return Collections.singletonList(data.getLogicalSwitchRef().getValue());
277         }
278
279         @Override
280         public List<InstanceIdentifier<?>> getTerminationPointDependencies(final VlanBindings data) {
281             return Collections.emptyList();
282         }
283     }
284
285     private static Map<InstanceIdentifier<Node>, List<TerminationPoint>> extractCreated(
286             final Collection<DataTreeModification<Node>> changes, final Class<TerminationPoint> class1) {
287         Map<InstanceIdentifier<Node>, List<TerminationPoint>> result = new HashMap<>();
288         if (changes != null && !changes.isEmpty()) {
289             for (DataTreeModification<Node> change : changes) {
290                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
291                 final DataObjectModification<Node> mod = change.getRootNode();
292                 Node created = TransactUtils.getCreated(mod);
293                 if (created != null) {
294                     List<TerminationPoint> portListUpdated = new ArrayList<>();
295                     for (TerminationPoint tp : created.nonnullTerminationPoint().values()) {
296                         HwvtepPhysicalPortAugmentation hppAugmentation =
297                                 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
298                         if (hppAugmentation != null) {
299                             portListUpdated.add(tp);
300                         }
301                     }
302                     result.put(key, portListUpdated);
303                 }
304             }
305         }
306         return result;
307     }
308
309     private static Map<InstanceIdentifier<Node>, List<TerminationPoint>> extractUpdatedPorts(
310             final Collection<DataTreeModification<Node>> changes, final Class<TerminationPoint> class1) {
311         Map<InstanceIdentifier<Node>, List<TerminationPoint>> result = new HashMap<>();
312         if (changes != null && !changes.isEmpty()) {
313             for (DataTreeModification<Node> change : changes) {
314                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
315                 final DataObjectModification<Node> mod = change.getRootNode();
316                 Node updated = TransactUtils.getUpdated(mod);
317                 Node before = mod.getDataBefore();
318                 if (updated != null && before != null) {
319                     List<TerminationPoint> portListUpdated = new ArrayList<>();
320                     List<TerminationPoint> portListBefore = new ArrayList<>();
321                     for (TerminationPoint tp : updated.nonnullTerminationPoint().values()) {
322                         HwvtepPhysicalPortAugmentation hppAugmentation =
323                                 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
324                         if (hppAugmentation != null) {
325                             portListUpdated.add(tp);
326                         }
327                     }
328                     for (TerminationPoint tp : before.nonnullTerminationPoint().values()) {
329                         HwvtepPhysicalPortAugmentation hppAugmentation =
330                                 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
331                         if (hppAugmentation != null) {
332                             portListBefore.add(tp);
333                         }
334                     }
335                     portListUpdated.removeAll(portListBefore);
336                     result.put(key, portListUpdated);
337                 }
338             }
339         }
340         return result;
341     }
342
343     @Override
344     protected String getKeyStr(final InstanceIdentifier iid) {
345         try {
346             return ((TerminationPoint)iid.firstKeyOf(TerminationPoint.class)).getTpId().getValue();
347         } catch (ClassCastException exp) {
348             LOG.error("Error in getting the TerminationPoint id ", exp);
349         }
350         return super.getKeyStr(iid);
351     }
352
353 }