2 * Copyright © 2015, 2017 Brocade Communications Systems, Inc. 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.southbound.ovsdb.transact;
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
11 import static org.opendaylight.ovsdb.southbound.SouthboundUtil.schemaMismatchLog;
13 import com.google.common.base.Optional;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
21 import java.util.Map.Entry;
23 import java.util.concurrent.ExecutionException;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
25 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
29 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
30 import org.opendaylight.ovsdb.lib.notation.UUID;
31 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
32 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
33 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
34 import org.opendaylight.ovsdb.schema.openvswitch.Port;
35 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
36 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
37 import org.opendaylight.ovsdb.southbound.SouthboundProvider;
38 import org.opendaylight.ovsdb.utils.yang.YangUtils;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbQosRef;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceBfd;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceLldp;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceOtherConfigs;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortOtherConfigs;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Trunks;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
56 import org.opendaylight.yangtools.yang.binding.DataObject;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
61 public class TerminationPointUpdateCommand implements TransactCommand {
63 private static final Logger LOG = LoggerFactory.getLogger(TerminationPointUpdateCommand.class);
66 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
67 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> events,
68 InstanceIdentifierCodec instanceIdentifierCodec) {
69 execute(transaction, state,
70 TransactUtils.extractCreatedOrUpdated(events, OvsdbTerminationPointAugmentation.class),
71 instanceIdentifierCodec);
75 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
76 Collection<DataTreeModification<Node>> modifications, InstanceIdentifierCodec instanceIdentifierCodec) {
77 execute(transaction, state,
78 TransactUtils.extractCreatedOrUpdated(modifications, OvsdbTerminationPointAugmentation.class),
79 instanceIdentifierCodec);
82 private void execute(TransactionBuilder transaction, BridgeOperationalState state,
83 Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation>
85 InstanceIdentifierCodec instanceIdentifierCodec) {
86 for (Entry<InstanceIdentifier<OvsdbTerminationPointAugmentation>,
87 OvsdbTerminationPointAugmentation> terminationPointEntry : createdOrUpdated.entrySet()) {
88 updateTerminationPoint(transaction, state, terminationPointEntry.getKey(),
89 terminationPointEntry.getValue(), instanceIdentifierCodec);
93 public void updateTerminationPoint(TransactionBuilder transaction, BridgeOperationalState state,
94 InstanceIdentifier<OvsdbTerminationPointAugmentation> iid,
95 OvsdbTerminationPointAugmentation terminationPoint, InstanceIdentifierCodec instanceIdentifierCodec) {
97 if (terminationPoint != null) {
98 LOG.debug("Received request to update termination point {}",
99 terminationPoint.getName());
102 Interface ovsInterface =
103 TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
104 updateInterface(terminationPoint, ovsInterface);
105 Interface extraInterface = TyperUtils.getTypedRowWrapper(
106 transaction.getDatabaseSchema(), Interface.class);
107 extraInterface.setName("");
108 transaction.add(op.update(ovsInterface)
109 .where(extraInterface.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
112 TerminationPointCreateCommand.stampInstanceIdentifier(transaction,
113 iid.firstIdentifierOf(OvsdbTerminationPointAugmentation.class), terminationPoint.getName(),
114 instanceIdentifierCodec);
118 Optional<OvsdbBridgeAugmentation> ovsdbBridgeOptional = state.getOvsdbBridgeAugmentation(iid);
119 if (ovsdbBridgeOptional != null && ovsdbBridgeOptional.isPresent()) {
120 OvsdbBridgeAugmentation operBridge = ovsdbBridgeOptional.get();
121 if (operBridge != null) {
122 Port port = TyperUtils.getTypedRowWrapper(
123 transaction.getDatabaseSchema(), Port.class);
124 updatePort(terminationPoint, port, operBridge);
125 Port extraPort = TyperUtils.getTypedRowWrapper(
126 transaction.getDatabaseSchema(), Port.class);
127 extraPort.setName("");
128 transaction.add(op.update(port)
129 .where(extraPort.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
131 LOG.info("Updated Termination Point : {} with Uuid : {}",
132 terminationPoint.getName(), terminationPoint.getPortUuid());
135 LOG.warn("OVSDB bridge node was not found: {}", iid);
140 private void updateInterface(
141 final OvsdbTerminationPointAugmentation terminationPoint,
142 final Interface ovsInterface) {
143 updateOfPort(terminationPoint, ovsInterface);
144 updateOfPortRequest(terminationPoint, ovsInterface);
145 updateInterfaceOptions(terminationPoint, ovsInterface);
146 updateInterfaceOtherConfig(terminationPoint, ovsInterface);
147 updateInterfaceExternalIds(terminationPoint, ovsInterface);
148 updateInterfaceLldp(terminationPoint, ovsInterface);
149 updateInterfaceBfd(terminationPoint, ovsInterface);
150 updateInterfacePolicing(terminationPoint, ovsInterface);
153 private void updatePort(
154 final OvsdbTerminationPointAugmentation terminationPoint,
156 final OvsdbBridgeAugmentation operBridge) {
158 updatePortOtherConfig(terminationPoint, port);
159 updatePortVlanTag(terminationPoint, port);
160 updatePortVlanTrunk(terminationPoint, port);
161 updatePortVlanMode(terminationPoint, port);
162 updatePortExternalIds(terminationPoint, port);
163 updatePortQos(terminationPoint, port, operBridge);
166 private void updatePortQos(
167 final OvsdbTerminationPointAugmentation terminationPoint,
169 final OvsdbBridgeAugmentation operBridge) {
171 Set<UUID> uuidSet = new HashSet<>();
173 // First check if QosEntry is present and use that
174 if (terminationPoint.getQosEntry() != null && !terminationPoint.getQosEntry().isEmpty()) {
175 OvsdbQosRef qosRef = terminationPoint.getQosEntry().iterator().next().getQosRef();
176 Uri qosId = qosRef.getValue().firstKeyOf(QosEntries.class).getQosId();
177 OvsdbNodeAugmentation operNode = getOperNode(operBridge);
178 if (operNode != null && operNode.getQosEntries() != null
179 && !operNode.getQosEntries().isEmpty()) {
180 for (QosEntries qosEntry : operNode.getQosEntries()) {
181 if (qosEntry.getQosId().equals(qosId)) {
182 uuidSet.add(new UUID(qosEntry.getQosUuid().getValue()));
186 if (uuidSet.size() == 0) {
187 uuidSet.add(new UUID(SouthboundConstants.QOS_NAMED_UUID_PREFIX
188 + TransactUtils.bytesToHexString(qosId.getValue().getBytes())));
191 // Second check if Qos is present and use that (deprecated)
192 // Do not bother to check if QosEntry and Qos are consistent if both are present
193 Uuid qosUuid = terminationPoint.getQos();
194 if (qosUuid != null) {
195 uuidSet.add(new UUID(qosUuid.getValue()));
198 port.setQos(uuidSet);
201 private OvsdbNodeAugmentation getOperNode(final OvsdbBridgeAugmentation operBridge) {
202 @SuppressWarnings("unchecked")
203 InstanceIdentifier<Node> iidNode = (InstanceIdentifier<Node>)operBridge.getManagedBy().getValue();
204 OvsdbNodeAugmentation operNode = null;
205 ReadOnlyTransaction transaction = SouthboundProvider.getDb().newReadOnlyTransaction();
206 CheckedFuture<Optional<Node>, ReadFailedException> future =
207 transaction.read(LogicalDatastoreType.OPERATIONAL, iidNode);
209 Optional<Node> nodeOptional = future.get();
210 if (nodeOptional.isPresent()) {
211 operNode = nodeOptional.get().getAugmentation(OvsdbNodeAugmentation.class);
213 } catch (InterruptedException | ExecutionException e) {
214 LOG.warn("Error reading from datastore", e);
219 private void updateOfPort(
220 final OvsdbTerminationPointAugmentation terminationPoint,
221 final Interface ovsInterface) {
223 Long ofPort = terminationPoint.getOfport();
224 if (ofPort != null) {
225 ovsInterface.setOpenFlowPort(Collections.singleton(ofPort));
229 private void updateOfPortRequest(
230 final OvsdbTerminationPointAugmentation terminationPoint,
231 final Interface ovsInterface) {
233 Integer ofPortRequest = terminationPoint.getOfportRequest();
234 if (ofPortRequest != null) {
235 ovsInterface.setOpenFlowPortRequest(Collections.singleton(ofPortRequest.longValue()));
239 private void updateInterfaceOptions(
240 final OvsdbTerminationPointAugmentation terminationPoint,
241 final Interface ovsInterface) {
243 //Configure optional input
244 if (terminationPoint.getOptions() != null) {
246 ovsInterface.setOptions(YangUtils.convertYangKeyValueListToMap(terminationPoint.getOptions(),
247 Options::getOption, Options::getValue));
248 } catch (NullPointerException e) {
249 LOG.warn("Incomplete OVSDB interface options", e);
254 private void updateInterfaceExternalIds(
255 final OvsdbTerminationPointAugmentation terminationPoint,
256 final Interface ovsInterface) {
258 List<InterfaceExternalIds> interfaceExternalIds =
259 terminationPoint.getInterfaceExternalIds();
260 if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
262 ovsInterface.setExternalIds(YangUtils.convertYangKeyValueListToMap(interfaceExternalIds,
263 InterfaceExternalIds::getExternalIdKey, InterfaceExternalIds::getExternalIdValue));
264 } catch (NullPointerException e) {
265 LOG.warn("Incomplete OVSDB interface external_ids", e);
270 private void updateInterfaceLldp(
271 final OvsdbTerminationPointAugmentation terminationPoint,
272 final Interface ovsInterface) {
275 List<InterfaceLldp> interfaceLldpList =
276 terminationPoint.getInterfaceLldp();
277 if (interfaceLldpList != null && !interfaceLldpList.isEmpty()) {
279 ovsInterface.setLldp(YangUtils.convertYangKeyValueListToMap(interfaceLldpList,
280 InterfaceLldp::getLldpKey, InterfaceLldp::getLldpValue));
281 } catch (NullPointerException e) {
282 LOG.warn("Incomplete OVSDB interface lldp", e);
285 } catch (SchemaVersionMismatchException e) {
286 schemaMismatchLog("lldp", "Interface", e);
290 private void updateInterfaceOtherConfig(
291 final OvsdbTerminationPointAugmentation terminationPoint,
292 final Interface ovsInterface) {
294 List<InterfaceOtherConfigs> interfaceOtherConfigs =
295 terminationPoint.getInterfaceOtherConfigs();
296 if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
297 Map<String, String> otherConfigsMap = new HashMap<>();
298 for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) {
299 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
300 interfaceOtherConfig.getOtherConfigValue());
303 ovsInterface.setOtherConfig(otherConfigsMap);
304 } catch (NullPointerException e) {
305 LOG.warn("Incomplete OVSDB interface other_config", e);
310 private void updateInterfaceBfd(
311 final OvsdbTerminationPointAugmentation terminationPoint,
312 final Interface ovsInterface) {
315 List<InterfaceBfd> interfaceBfdList =
316 terminationPoint.getInterfaceBfd();
317 if (interfaceBfdList != null && !interfaceBfdList.isEmpty()) {
319 ovsInterface.setBfd(YangUtils.convertYangKeyValueListToMap(interfaceBfdList,
320 InterfaceBfd::getBfdKey, InterfaceBfd::getBfdValue));
321 } catch (NullPointerException e) {
322 LOG.warn("Incomplete OVSDB interface bfd", e);
325 } catch (SchemaVersionMismatchException e) {
326 schemaMismatchLog("bfd", "Interface", e);
330 private void updateInterfacePolicing(
331 final OvsdbTerminationPointAugmentation terminationPoint,
332 final Interface ovsInterface) {
334 Long ingressPolicingRate = terminationPoint.getIngressPolicingRate();
335 if (ingressPolicingRate != null) {
336 ovsInterface.setIngressPolicingRate(ingressPolicingRate);
338 Long ingressPolicingBurst = terminationPoint.getIngressPolicingBurst();
339 if (ingressPolicingBurst != null) {
340 ovsInterface.setIngressPolicingBurst(ingressPolicingBurst);
344 private void updatePortExternalIds(
345 final OvsdbTerminationPointAugmentation terminationPoint,
348 List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
349 if (portExternalIds != null && !portExternalIds.isEmpty()) {
351 port.setExternalIds(YangUtils.convertYangKeyValueListToMap(portExternalIds,
352 PortExternalIds::getExternalIdKey, PortExternalIds::getExternalIdValue));
353 } catch (NullPointerException e) {
354 LOG.warn("Incomplete OVSDB port external_ids", e);
359 private void updatePortVlanTag(
360 final OvsdbTerminationPointAugmentation terminationPoint,
363 if (terminationPoint.getVlanTag() != null) {
364 Set<Long> vlanTag = new HashSet<>();
365 vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
366 port.setTag(vlanTag);
370 private void updatePortVlanTrunk(
371 final OvsdbTerminationPointAugmentation terminationPoint,
374 if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
375 Set<Long> portTrunks = new HashSet<>();
376 List<Trunks> modelTrunks = terminationPoint.getTrunks();
377 for (Trunks trunk : modelTrunks) {
378 if (trunk.getTrunk() != null) {
379 portTrunks.add(trunk.getTrunk().getValue().longValue());
382 port.setTrunks(portTrunks);
386 private void updatePortVlanMode(
387 final OvsdbTerminationPointAugmentation terminationPoint,
389 if (terminationPoint.getVlanMode() != null) {
390 Set<String> portVlanMode = new HashSet<>();
391 VlanMode modelVlanMode = terminationPoint.getVlanMode();
392 portVlanMode.add(SouthboundConstants.VlanModes.values()[modelVlanMode.getIntValue() - 1].getMode());
393 port.setVlanMode(portVlanMode);
397 private void updatePortOtherConfig(
398 final OvsdbTerminationPointAugmentation terminationPoint,
399 final Port ovsPort) {
400 List<PortOtherConfigs> portOtherConfigs =
401 terminationPoint.getPortOtherConfigs();
402 if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
404 ovsPort.setOtherConfig(YangUtils.convertYangKeyValueListToMap(portOtherConfigs,
405 PortOtherConfigs::getOtherConfigKey, PortOtherConfigs::getOtherConfigValue));
406 } catch (NullPointerException e) {
407 LOG.warn("Incomplete OVSDB port other_config", e);