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