2 * Copyright © 2015, 2017 China Telecom Beijing Research Institute and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
11 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
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;
19 import java.util.Map.Entry;
20 import java.util.Objects;
22 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
23 import org.opendaylight.controller.md.sal.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;
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();
45 public PhysicalPortUpdateCommand(final HwvtepOperationalState state,
46 final Collection<DataTreeModification<Node>> changes) {
47 super(state, changes);
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());
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());
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);
87 public void onConfigUpdate(TransactionBuilder transaction, InstanceIdentifier psNodeiid,
88 TerminationPoint port, InstanceIdentifier tpIId, Object... extraData) {
89 doDeviceTransaction(transaction, psNodeiid, port, tpIId);
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);
100 LOG.info("No port augmentation found for port {}", data);
103 if (port.getHwvtepNodeName() == null) {
104 LOG.info("No port hwvtep node name found for port {}", data);
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(),
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);
120 if (deviceData == null || deviceData.getData() == null) {
121 LOG.warn("Port not present in opdata store {}", key);
123 if (deviceData.getData() == null || !(deviceData.getData() instanceof PhysicalPort)) {
124 LOG.error("Failed to get the device data for port {}", key);
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());
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
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))
154 transaction.add(op.comment("Physical Port: Updating " + existingPhysicalPortName));
155 updateControllerTxHistory(TransactionType.UPDATE, physicalPort);
156 LOG.info("CONTROLLER - {} {}", TransactionType.UPDATE, physicalPort);
162 private void setName(PhysicalPort physicalPort, HwvtepPhysicalPortAugmentation inputPhysicalPort) {
163 if (inputPhysicalPort.getHwvtepNodeName() != null) {
164 physicalPort.setName(inputPhysicalPort.getHwvtepNodeName().getValue());
168 private static void setDescription(final PhysicalPort physicalPort,
169 final HwvtepPhysicalPortAugmentation inputPhysicalPort) {
170 if (inputPhysicalPort.getHwvtepNodeDescription() != null) {
171 physicalPort.setDescription(inputPhysicalPort.getHwvtepNodeDescription());
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();
190 Map inTransitDependencies = DEPENDENCY_GETTER.getInTransitDependencies(
191 getOperationalState(), vlanBinding);
192 Map configDependencies = DEPENDENCY_GETTER.getUnMetConfigDependencies(
193 getOperationalState(), vlanBinding);
195 if (!HwvtepSouthboundUtil.isEmptyMap(configDependencies)) {
196 createConfigWaitJob(psNodeiid, inputPhysicalPort, key, configDependencies);
199 if (!HwvtepSouthboundUtil.isEmptyMap(inTransitDependencies)) {
200 createOperWaitingJob(psNodeiid, inputPhysicalPort, key, inTransitDependencies);
204 UUID lsUUid = TransactUtils.getLogicalSwitchUUID(transaction, getOperationalState(), lswitchIid);
205 if (lsUUid == null) {
206 LOG.error("Could not get the logical switch uuid for {}", vlanBinding);
209 bindingMap.put(vlanBinding.getVlanIdKey().getValue().longValue(), lsUUid);
212 physicalPort.setVlanBindings(bindingMap);
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)) {
223 DependentJob<VlanBindings> opWaitingJob = new DependentJob.OpWaitingJob(
224 key, inputPhysicalPort, inTransitDependencies, getOperationalState().getTransactionId()) {
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();
236 doDeviceTransaction(transactionBuilder, psNodeiid, port, key);
239 LOG.info("Added the port to oper wait queue {}", key);//TODO delete
240 getDeviceInfo().addJobToQueue(opWaitingJob);
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)) {
250 DependentJob<TerminationPoint> configWaitingJob = new DependentJob.ConfigWaitingJob(
251 key, inputPhysicalPort, configDependencies) {
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();
263 doDeviceTransaction(transactionBuilder, psNodeiid, port, key);
266 LOG.info("Added the port to config wait queue {}", key);//TODO delete
267 getDeviceInfo().addJobToQueue(configWaitingJob);
270 static class VlanBindingsUnMetDependencyGetter extends UnMetDependencyGetter<VlanBindings> {
273 public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(final VlanBindings data) {
275 return Collections.emptyList();
277 return Collections.singletonList(data.getLogicalSwitchRef().getValue());
281 public List<InstanceIdentifier<?>> getTerminationPointDependencies(final VlanBindings data) {
282 return Collections.emptyList();
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);
305 result.put(key, portListUpdated);
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);
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);
342 portListUpdated.removeAll(portListBefore);
343 result.put(key, portListUpdated);
350 protected String getKeyStr(InstanceIdentifier iid) {
352 return ((TerminationPoint)iid.firstKeyOf(TerminationPoint.class)).getTpId().getValue();
353 } catch (ClassCastException exp) {
354 LOG.error("Error in getting the TerminationPoint id ", exp);
356 return super.getKeyStr(iid);