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 com.google.common.collect.Lists;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.List;
20 import java.util.Map.Entry;
21 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
23 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
24 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
25 import org.opendaylight.ovsdb.lib.notation.UUID;
26 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
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.hwvtep.global.attributes.LogicalSwitches;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsKey;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class PhysicalPortUpdateCommand extends AbstractTransactCommand {
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<HwvtepPhysicalPortAugmentation>> createds =
53 extractCreated(getChanges(),HwvtepPhysicalPortAugmentation.class);
54 if (!createds.isEmpty()) {
55 for (Entry<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> created:
56 createds.entrySet()) {
57 updatePhysicalPort(transaction, created.getKey(), created.getValue());
60 Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> updateds =
61 extractUpdatedPorts(getChanges(), HwvtepPhysicalPortAugmentation.class);
62 if (!updateds.isEmpty()) {
63 for (Entry<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> 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<HwvtepPhysicalPortAugmentation> listPort) {
73 //Get physical switch which the port belong to: in operation DS or new created
74 for (HwvtepPhysicalPortAugmentation port : listPort) {
75 LOG.debug("Creating a physical port named: {}", port.getHwvtepNodeName().getValue());
76 HwvtepDeviceInfo.DeviceData deviceOperdata = getDeviceInfo().getDeviceOperData(TerminationPoint.class,
77 getTpIid(psNodeiid, port.getHwvtepNodeName().getValue()));
78 if (deviceOperdata == null) {
79 //create a physical port always happens from device
80 LOG.error("Physical port {} not present in oper datastore", port.getHwvtepNodeName().getValue());
82 PhysicalPort physicalPort = transaction.getTypedRowWrapper(PhysicalPort.class);
83 physicalPort.setName(port.getHwvtepNodeName().getValue());
84 setVlanBindings(psNodeiid, physicalPort, port, transaction);
85 setDescription(physicalPort, port);
86 String existingPhysicalPortName = port.getHwvtepNodeName().getValue();
87 PhysicalPort extraPhyscialPort = transaction.getTypedRowWrapper(PhysicalPort.class);
88 extraPhyscialPort.setName("");
89 LOG.trace("execute: updating physical port: {}", physicalPort);
90 transaction.add(op.update(physicalPort)
91 .where(extraPhyscialPort.getNameColumn().getSchema().opEqual(existingPhysicalPortName))
93 transaction.add(op.comment("Physical Port: Updating " + existingPhysicalPortName));
94 updateControllerTxHistory(TransactionType.UPDATE, physicalPort);
99 private static void setDescription(final PhysicalPort physicalPort,
100 final HwvtepPhysicalPortAugmentation inputPhysicalPort) {
101 if (inputPhysicalPort.getHwvtepNodeDescription() != null) {
102 physicalPort.setDescription(inputPhysicalPort.getHwvtepNodeDescription());
106 private void setVlanBindings(final InstanceIdentifier<Node> psNodeiid,
107 final PhysicalPort physicalPort,
108 final HwvtepPhysicalPortAugmentation inputPhysicalPort,
109 final TransactionBuilder transaction) {
110 if (inputPhysicalPort.getVlanBindings() != null) {
111 //get UUID by LogicalSwitchRef
112 Map<Long, UUID> bindingMap = new HashMap<>();
113 for (VlanBindings vlanBinding: inputPhysicalPort.getVlanBindings()) {
114 InstanceIdentifier<VlanBindings> vlanIid = getVlanBindingIid(psNodeiid, physicalPort, vlanBinding);
115 @SuppressWarnings("unchecked")
116 InstanceIdentifier<LogicalSwitches> lswitchIid =
117 (InstanceIdentifier<LogicalSwitches>) vlanBinding.getLogicalSwitchRef().getValue();
119 Map inTransitDependencies = DEPENDENCY_GETTER.getInTransitDependencies(
120 getOperationalState(), vlanBinding);
121 Map configDependencies = DEPENDENCY_GETTER.getUnMetConfigDependencies(
122 getOperationalState(), vlanBinding);
124 if (!HwvtepSouthboundUtil.isEmptyMap(configDependencies)) {
125 createConfigWaitJob(psNodeiid, inputPhysicalPort,
126 vlanBinding, configDependencies, vlanIid);
129 if (!HwvtepSouthboundUtil.isEmptyMap(inTransitDependencies)) {
130 createOperWaitingJob(psNodeiid, inputPhysicalPort,
131 vlanBinding, inTransitDependencies, vlanIid);
135 bindingMap.put(vlanBinding.getVlanIdKey().getValue().longValue(),
136 TransactUtils.getLogicalSwitchUUID(transaction, getOperationalState(), lswitchIid));
138 physicalPort.setVlanBindings(bindingMap);
142 private void createOperWaitingJob(final InstanceIdentifier<Node> psNodeiid,
143 final HwvtepPhysicalPortAugmentation inputPhysicalPort,
144 final VlanBindings vlanBinding,
145 final Map inTransitDependencies,
146 final InstanceIdentifier<VlanBindings> vlanIid) {
148 DependentJob<VlanBindings> opWaitingJob = new DependentJob.OpWaitingJob<VlanBindings>(
149 vlanIid, vlanBinding, inTransitDependencies) {
151 public void onDependencyResolved(final HwvtepOperationalState operationalState,
152 final TransactionBuilder transactionBuilder) {
153 hwvtepOperationalState = operationalState;
154 deviceTransaction = transactionBuilder;
155 updatePhysicalPort(transactionBuilder, psNodeiid, Lists.newArrayList(inputPhysicalPort));
158 getDeviceInfo().addJobToQueue(opWaitingJob);
161 private void createConfigWaitJob(final InstanceIdentifier<Node> psNodeiid,
162 final HwvtepPhysicalPortAugmentation inputPhysicalPort,
163 final VlanBindings vlanBinding,
164 final Map configDependencies,
165 final InstanceIdentifier<VlanBindings> vlanIid) {
167 DependentJob<VlanBindings> configWaitingJob = new DependentJob.ConfigWaitingJob<VlanBindings>(
168 vlanIid, vlanBinding, configDependencies) {
170 public void onDependencyResolved(final HwvtepOperationalState operationalState,
171 final TransactionBuilder transactionBuilder) {
172 hwvtepOperationalState = operationalState;
173 deviceTransaction = transactionBuilder;
174 updatePhysicalPort(transactionBuilder, psNodeiid, Lists.newArrayList(inputPhysicalPort));
177 getDeviceInfo().addJobToQueue(configWaitingJob);
180 private static InstanceIdentifier<TerminationPoint> getTpIid(final InstanceIdentifier<Node> psNodeiid,
181 final String portName) {
182 return psNodeiid.child(TerminationPoint.class, new TerminationPointKey(new TpId(portName)));
185 private static InstanceIdentifier<VlanBindings> getVlanBindingIid(final InstanceIdentifier<Node> psNodeiid,
186 final PhysicalPort physicalPort,
187 final VlanBindings vlanBinding) {
188 return getTpIid(psNodeiid, physicalPort.getName())
189 .augmentation(HwvtepPhysicalPortAugmentation.class)
190 .child(VlanBindings.class, new VlanBindingsKey(vlanBinding.getVlanIdKey()));
193 static class VlanBindingsUnMetDependencyGetter extends UnMetDependencyGetter<VlanBindings> {
196 public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(final VlanBindings data) {
198 return Collections.emptyList();
200 return Collections.singletonList(data.getLogicalSwitchRef().getValue());
204 public List<InstanceIdentifier<?>> getTerminationPointDependencies(final VlanBindings data) {
205 return Collections.emptyList();
209 private static Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> extractCreated(
210 final Collection<DataTreeModification<Node>> changes, final Class<HwvtepPhysicalPortAugmentation> class1) {
211 Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> result = new HashMap<>();
212 if (changes != null && !changes.isEmpty()) {
213 for (DataTreeModification<Node> change : changes) {
214 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
215 final DataObjectModification<Node> mod = change.getRootNode();
216 Node created = TransactUtils.getCreated(mod);
217 if (created != null) {
218 List<HwvtepPhysicalPortAugmentation> portListUpdated = new ArrayList<>();
219 if (created.getTerminationPoint() != null) {
220 for (TerminationPoint tp : created.getTerminationPoint()) {
221 HwvtepPhysicalPortAugmentation hppAugmentation =
222 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
223 if (hppAugmentation != null) {
224 portListUpdated.add(hppAugmentation);
228 result.put(key, portListUpdated);
235 private static Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> extractUpdatedPorts(
236 final Collection<DataTreeModification<Node>> changes, final Class<HwvtepPhysicalPortAugmentation> class1) {
237 Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> result = new HashMap<>();
238 if (changes != null && !changes.isEmpty()) {
239 for (DataTreeModification<Node> change : changes) {
240 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
241 final DataObjectModification<Node> mod = change.getRootNode();
242 Node updated = TransactUtils.getUpdated(mod);
243 Node before = mod.getDataBefore();
244 if (updated != null && before != null) {
245 List<HwvtepPhysicalPortAugmentation> portListUpdated = new ArrayList<>();
246 List<HwvtepPhysicalPortAugmentation> portListBefore = new ArrayList<>();
247 if (updated.getTerminationPoint() != null) {
248 for (TerminationPoint tp : updated.getTerminationPoint()) {
249 HwvtepPhysicalPortAugmentation hppAugmentation =
250 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
251 if (hppAugmentation != null) {
252 portListUpdated.add(hppAugmentation);
256 if (before.getTerminationPoint() != null) {
257 for (TerminationPoint tp : before.getTerminationPoint()) {
258 HwvtepPhysicalPortAugmentation hppAugmentation =
259 tp.augmentation(HwvtepPhysicalPortAugmentation.class);
260 if (hppAugmentation != null) {
261 portListBefore.add(hppAugmentation);
265 portListUpdated.removeAll(portListBefore);
266 result.put(key, portListUpdated);