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
8 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
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;
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;
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();
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(final TransactionBuilder transaction, final InstanceIdentifier psNodeiid,
88 final TerminationPoint port, final InstanceIdentifier tpIId, final Object... extraData) {
89 doDeviceTransaction(transaction, psNodeiid, port, tpIId);
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);
101 LOG.info("No port augmentation found for port {}", data);
104 if (port.getHwvtepNodeName() == null) {
105 LOG.info("No port hwvtep node name found for port {}", data);
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(),
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);
121 if (deviceData == null || deviceData.getData() == null) {
122 LOG.warn("Port not present in opdata store {}", key);
124 if (deviceData.getData() == null || !(deviceData.getData() instanceof PhysicalPort)) {
125 LOG.error("Failed to get the device data for port {}", getKeyStr(key));
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());
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
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))
155 transaction.add(op.comment("Physical Port: Updating " + existingPhysicalPortName));
156 updateControllerTxHistory(TransactionType.UPDATE, physicalPort);
157 LOG.info("CONTROLLER - {} {}", TransactionType.UPDATE, physicalPort);
163 private static void setName(final PhysicalPort physicalPort,
164 final HwvtepPhysicalPortAugmentation inputPhysicalPort) {
165 if (inputPhysicalPort.getHwvtepNodeName() != null) {
166 physicalPort.setName(inputPhysicalPort.getHwvtepNodeName().getValue());
170 private static void setDescription(final PhysicalPort physicalPort,
171 final HwvtepPhysicalPortAugmentation inputPhysicalPort) {
172 if (inputPhysicalPort.getHwvtepNodeDescription() != null) {
173 physicalPort.setDescription(inputPhysicalPort.getHwvtepNodeDescription());
177 private Map<Long, UUID> setVlanBindings(final InstanceIdentifier<Node> psNodeiid,
178 final PhysicalPort physicalPort,
179 final TerminationPoint inputPhysicalPort,
180 final InstanceIdentifier key,
181 final TransactionBuilder transaction) {
182 HwvtepPhysicalPortAugmentation portAugmentation = inputPhysicalPort.augmentation(
183 HwvtepPhysicalPortAugmentation.class);
184 Map<Long, UUID> bindingMap = new HashMap<>();
185 //get UUID by LogicalSwitchRef
186 for (VlanBindings vlanBinding : portAugmentation.nonnullVlanBindings().values()) {
187 @SuppressWarnings("unchecked")
188 InstanceIdentifier<LogicalSwitches> lswitchIid =
189 (InstanceIdentifier<LogicalSwitches>) vlanBinding.getLogicalSwitchRef().getValue();
191 Map inTransitDependencies = DEPENDENCY_GETTER.getInTransitDependencies(
192 getOperationalState(), vlanBinding);
193 Map configDependencies = DEPENDENCY_GETTER.getUnMetConfigDependencies(
194 getOperationalState(), vlanBinding);
196 if (!HwvtepSouthboundUtil.isEmptyMap(configDependencies)) {
197 createConfigWaitJob(psNodeiid, inputPhysicalPort, key, configDependencies);
200 if (!HwvtepSouthboundUtil.isEmptyMap(inTransitDependencies)) {
201 createOperWaitingJob(psNodeiid, inputPhysicalPort, key, inTransitDependencies);
205 UUID lsUUid = TransactUtils.getLogicalSwitchUUID(transaction, getOperationalState(), lswitchIid);
206 if (lsUUid == null) {
207 LOG.error("Could not get the logical switch uuid for {}", vlanBinding);
210 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 for (TerminationPoint tp : created.nonnullTerminationPoint().values()) {
297 HwvtepPhysicalPortAugmentation hppAugmentation =
298 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
299 if (hppAugmentation != null) {
300 portListUpdated.add(tp);
303 result.put(key, portListUpdated);
310 private static Map<InstanceIdentifier<Node>, List<TerminationPoint>> extractUpdatedPorts(
311 final Collection<DataTreeModification<Node>> changes, final Class<TerminationPoint> class1) {
312 Map<InstanceIdentifier<Node>, List<TerminationPoint>> result = new HashMap<>();
313 if (changes != null && !changes.isEmpty()) {
314 for (DataTreeModification<Node> change : changes) {
315 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
316 final DataObjectModification<Node> mod = change.getRootNode();
317 Node updated = TransactUtils.getUpdated(mod);
318 Node before = mod.getDataBefore();
319 if (updated != null && before != null) {
320 List<TerminationPoint> portListUpdated = new ArrayList<>();
321 List<TerminationPoint> portListBefore = new ArrayList<>();
322 for (TerminationPoint tp : updated.nonnullTerminationPoint().values()) {
323 HwvtepPhysicalPortAugmentation hppAugmentation =
324 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
325 if (hppAugmentation != null) {
326 portListUpdated.add(tp);
329 for (TerminationPoint tp : before.nonnullTerminationPoint().values()) {
330 HwvtepPhysicalPortAugmentation hppAugmentation =
331 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
332 if (hppAugmentation != null) {
333 portListBefore.add(tp);
336 portListUpdated.removeAll(portListBefore);
337 result.put(key, portListUpdated);
345 protected String getKeyStr(final InstanceIdentifier iid) {
347 return ((TerminationPoint)iid.firstKeyOf(TerminationPoint.class)).getTpId().getValue();
348 } catch (ClassCastException exp) {
349 LOG.error("Error in getting the TerminationPoint id ", exp);
351 return super.getKeyStr(iid);