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.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbQosRef;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceBfd;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceLldp;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceOtherConfigs;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortOtherConfigs;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Trunks;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
55 import org.opendaylight.yangtools.yang.binding.DataObject;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
60 public class TerminationPointUpdateCommand implements TransactCommand {
62 private static final Logger LOG = LoggerFactory.getLogger(TerminationPointUpdateCommand.class);
65 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
66 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> events,
67 InstanceIdentifierCodec instanceIdentifierCodec) {
68 execute(transaction, state,
69 TransactUtils.extractCreatedOrUpdated(events, OvsdbTerminationPointAugmentation.class),
70 instanceIdentifierCodec);
74 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
75 Collection<DataTreeModification<Node>> modifications, InstanceIdentifierCodec instanceIdentifierCodec) {
76 execute(transaction, state,
77 TransactUtils.extractCreatedOrUpdated(modifications, OvsdbTerminationPointAugmentation.class),
78 instanceIdentifierCodec);
81 private void execute(TransactionBuilder transaction, BridgeOperationalState state,
82 Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation>
84 InstanceIdentifierCodec instanceIdentifierCodec) {
85 for (Entry<InstanceIdentifier<OvsdbTerminationPointAugmentation>,
86 OvsdbTerminationPointAugmentation> terminationPointEntry : createdOrUpdated.entrySet()) {
87 updateTerminationPoint(transaction, state, terminationPointEntry.getKey(),
88 terminationPointEntry.getValue(), instanceIdentifierCodec);
92 public void updateTerminationPoint(TransactionBuilder transaction, BridgeOperationalState state,
93 InstanceIdentifier<OvsdbTerminationPointAugmentation> iid,
94 OvsdbTerminationPointAugmentation terminationPoint, InstanceIdentifierCodec instanceIdentifierCodec) {
96 if (terminationPoint != null) {
97 LOG.debug("Received request to update termination point {}",
98 terminationPoint.getName());
101 Interface ovsInterface =
102 TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
103 updateInterface(terminationPoint, ovsInterface);
104 Interface extraInterface = TyperUtils.getTypedRowWrapper(
105 transaction.getDatabaseSchema(), Interface.class);
106 extraInterface.setName("");
107 transaction.add(op.update(ovsInterface)
108 .where(extraInterface.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
111 TerminationPointCreateCommand.stampInstanceIdentifier(transaction,
112 iid.firstIdentifierOf(OvsdbTerminationPointAugmentation.class), terminationPoint.getName(),
113 instanceIdentifierCodec);
117 Optional<OvsdbBridgeAugmentation> ovsdbBridgeOptional = state.getOvsdbBridgeAugmentation(iid);
118 if (ovsdbBridgeOptional != null && ovsdbBridgeOptional.isPresent()) {
119 OvsdbBridgeAugmentation operBridge = ovsdbBridgeOptional.get();
120 if (operBridge != null) {
121 Port port = TyperUtils.getTypedRowWrapper(
122 transaction.getDatabaseSchema(), Port.class);
123 updatePort(terminationPoint, port, operBridge);
124 Port extraPort = TyperUtils.getTypedRowWrapper(
125 transaction.getDatabaseSchema(), Port.class);
126 extraPort.setName("");
127 transaction.add(op.update(port)
128 .where(extraPort.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
130 LOG.info("Updated Termination Point : {} with Uuid : {}",
131 terminationPoint.getName(), terminationPoint.getPortUuid());
134 LOG.warn("OVSDB bridge node was not found: {}", iid);
139 private void updateInterface(
140 final OvsdbTerminationPointAugmentation terminationPoint,
141 final Interface ovsInterface) {
142 updateOfPort(terminationPoint, ovsInterface);
143 updateOfPortRequest(terminationPoint, ovsInterface);
144 updateInterfaceOptions(terminationPoint, ovsInterface);
145 updateInterfaceOtherConfig(terminationPoint, ovsInterface);
146 updateInterfaceExternalIds(terminationPoint, ovsInterface);
147 updateInterfaceLldp(terminationPoint, ovsInterface);
148 updateInterfaceBfd(terminationPoint, ovsInterface);
149 updateInterfacePolicing(terminationPoint, ovsInterface);
152 private void updatePort(
153 final OvsdbTerminationPointAugmentation terminationPoint,
155 final OvsdbBridgeAugmentation operBridge) {
157 updatePortOtherConfig(terminationPoint, port);
158 updatePortVlanTag(terminationPoint, port);
159 updatePortVlanTrunk(terminationPoint, port);
160 updatePortVlanMode(terminationPoint, port);
161 updatePortExternalIds(terminationPoint, port);
162 updatePortQos(terminationPoint, port, operBridge);
165 private void updatePortQos(
166 final OvsdbTerminationPointAugmentation terminationPoint,
168 final OvsdbBridgeAugmentation operBridge) {
170 Set<UUID> uuidSet = new HashSet<>();
172 // First check if QosEntry is present and use that
173 if (terminationPoint.getQosEntry() != null && !terminationPoint.getQosEntry().isEmpty()) {
174 OvsdbQosRef qosRef = terminationPoint.getQosEntry().iterator().next().getQosRef();
175 Uri qosId = qosRef.getValue().firstKeyOf(QosEntries.class).getQosId();
176 OvsdbNodeAugmentation operNode = getOperNode(operBridge);
177 if (operNode != null && operNode.getQosEntries() != null
178 && !operNode.getQosEntries().isEmpty()) {
179 for (QosEntries qosEntry : operNode.getQosEntries()) {
180 if (qosEntry.getQosId().equals(qosId)) {
181 uuidSet.add(new UUID(qosEntry.getQosUuid().getValue()));
185 if (uuidSet.size() == 0) {
186 uuidSet.add(new UUID(SouthboundConstants.QOS_NAMED_UUID_PREFIX
187 + TransactUtils.bytesToHexString(qosId.getValue().getBytes())));
190 port.setQos(uuidSet);
193 private OvsdbNodeAugmentation getOperNode(final OvsdbBridgeAugmentation operBridge) {
194 @SuppressWarnings("unchecked")
195 InstanceIdentifier<Node> iidNode = (InstanceIdentifier<Node>)operBridge.getManagedBy().getValue();
196 OvsdbNodeAugmentation operNode = null;
197 ReadOnlyTransaction transaction = SouthboundProvider.getDb().newReadOnlyTransaction();
198 CheckedFuture<Optional<Node>, ReadFailedException> future =
199 transaction.read(LogicalDatastoreType.OPERATIONAL, iidNode);
201 Optional<Node> nodeOptional = future.get();
202 if (nodeOptional.isPresent()) {
203 operNode = nodeOptional.get().getAugmentation(OvsdbNodeAugmentation.class);
205 } catch (InterruptedException | ExecutionException e) {
206 LOG.warn("Error reading from datastore", e);
211 private void updateOfPort(
212 final OvsdbTerminationPointAugmentation terminationPoint,
213 final Interface ovsInterface) {
215 Long ofPort = terminationPoint.getOfport();
216 if (ofPort != null) {
217 ovsInterface.setOpenFlowPort(Collections.singleton(ofPort));
221 private void updateOfPortRequest(
222 final OvsdbTerminationPointAugmentation terminationPoint,
223 final Interface ovsInterface) {
225 Integer ofPortRequest = terminationPoint.getOfportRequest();
226 if (ofPortRequest != null) {
227 ovsInterface.setOpenFlowPortRequest(Collections.singleton(ofPortRequest.longValue()));
231 private void updateInterfaceOptions(
232 final OvsdbTerminationPointAugmentation terminationPoint,
233 final Interface ovsInterface) {
235 //Configure optional input
236 if (terminationPoint.getOptions() != null) {
238 ovsInterface.setOptions(YangUtils.convertYangKeyValueListToMap(terminationPoint.getOptions(),
239 Options::getOption, Options::getValue));
240 } catch (NullPointerException e) {
241 LOG.warn("Incomplete OVSDB interface options", e);
246 private void updateInterfaceExternalIds(
247 final OvsdbTerminationPointAugmentation terminationPoint,
248 final Interface ovsInterface) {
250 List<InterfaceExternalIds> interfaceExternalIds =
251 terminationPoint.getInterfaceExternalIds();
252 if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
254 ovsInterface.setExternalIds(YangUtils.convertYangKeyValueListToMap(interfaceExternalIds,
255 InterfaceExternalIds::getExternalIdKey, InterfaceExternalIds::getExternalIdValue));
256 } catch (NullPointerException e) {
257 LOG.warn("Incomplete OVSDB interface external_ids", e);
262 private void updateInterfaceLldp(
263 final OvsdbTerminationPointAugmentation terminationPoint,
264 final Interface ovsInterface) {
267 List<InterfaceLldp> interfaceLldpList =
268 terminationPoint.getInterfaceLldp();
269 if (interfaceLldpList != null && !interfaceLldpList.isEmpty()) {
271 ovsInterface.setLldp(YangUtils.convertYangKeyValueListToMap(interfaceLldpList,
272 InterfaceLldp::getLldpKey, InterfaceLldp::getLldpValue));
273 } catch (NullPointerException e) {
274 LOG.warn("Incomplete OVSDB interface lldp", e);
277 } catch (SchemaVersionMismatchException e) {
278 schemaMismatchLog("lldp", "Interface", e);
282 private void updateInterfaceOtherConfig(
283 final OvsdbTerminationPointAugmentation terminationPoint,
284 final Interface ovsInterface) {
286 List<InterfaceOtherConfigs> interfaceOtherConfigs =
287 terminationPoint.getInterfaceOtherConfigs();
288 if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
289 Map<String, String> otherConfigsMap = new HashMap<>();
290 for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) {
291 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
292 interfaceOtherConfig.getOtherConfigValue());
295 ovsInterface.setOtherConfig(otherConfigsMap);
296 } catch (NullPointerException e) {
297 LOG.warn("Incomplete OVSDB interface other_config", e);
302 private void updateInterfaceBfd(
303 final OvsdbTerminationPointAugmentation terminationPoint,
304 final Interface ovsInterface) {
307 List<InterfaceBfd> interfaceBfdList =
308 terminationPoint.getInterfaceBfd();
309 if (interfaceBfdList != null && !interfaceBfdList.isEmpty()) {
311 ovsInterface.setBfd(YangUtils.convertYangKeyValueListToMap(interfaceBfdList,
312 InterfaceBfd::getBfdKey, InterfaceBfd::getBfdValue));
313 } catch (NullPointerException e) {
314 LOG.warn("Incomplete OVSDB interface bfd", e);
317 } catch (SchemaVersionMismatchException e) {
318 schemaMismatchLog("bfd", "Interface", e);
322 private void updateInterfacePolicing(
323 final OvsdbTerminationPointAugmentation terminationPoint,
324 final Interface ovsInterface) {
326 Long ingressPolicingRate = terminationPoint.getIngressPolicingRate();
327 if (ingressPolicingRate != null) {
328 ovsInterface.setIngressPolicingRate(ingressPolicingRate);
330 Long ingressPolicingBurst = terminationPoint.getIngressPolicingBurst();
331 if (ingressPolicingBurst != null) {
332 ovsInterface.setIngressPolicingBurst(ingressPolicingBurst);
336 private void updatePortExternalIds(
337 final OvsdbTerminationPointAugmentation terminationPoint,
340 List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
341 if (portExternalIds != null && !portExternalIds.isEmpty()) {
343 port.setExternalIds(YangUtils.convertYangKeyValueListToMap(portExternalIds,
344 PortExternalIds::getExternalIdKey, PortExternalIds::getExternalIdValue));
345 } catch (NullPointerException e) {
346 LOG.warn("Incomplete OVSDB port external_ids", e);
351 private void updatePortVlanTag(
352 final OvsdbTerminationPointAugmentation terminationPoint,
355 if (terminationPoint.getVlanTag() != null) {
356 Set<Long> vlanTag = new HashSet<>();
357 vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
358 port.setTag(vlanTag);
362 private void updatePortVlanTrunk(
363 final OvsdbTerminationPointAugmentation terminationPoint,
366 if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
367 Set<Long> portTrunks = new HashSet<>();
368 List<Trunks> modelTrunks = terminationPoint.getTrunks();
369 for (Trunks trunk : modelTrunks) {
370 if (trunk.getTrunk() != null) {
371 portTrunks.add(trunk.getTrunk().getValue().longValue());
374 port.setTrunks(portTrunks);
378 private void updatePortVlanMode(
379 final OvsdbTerminationPointAugmentation terminationPoint,
381 if (terminationPoint.getVlanMode() != null) {
382 Set<String> portVlanMode = new HashSet<>();
383 VlanMode modelVlanMode = terminationPoint.getVlanMode();
384 portVlanMode.add(SouthboundConstants.VlanModes.values()[modelVlanMode.getIntValue() - 1].getMode());
385 port.setVlanMode(portVlanMode);
389 private void updatePortOtherConfig(
390 final OvsdbTerminationPointAugmentation terminationPoint,
391 final Port ovsPort) {
392 List<PortOtherConfigs> portOtherConfigs =
393 terminationPoint.getPortOtherConfigs();
394 if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
396 ovsPort.setOtherConfig(YangUtils.convertYangKeyValueListToMap(portOtherConfigs,
397 PortOtherConfigs::getOtherConfigKey, PortOtherConfigs::getOtherConfigValue));
398 } catch (NullPointerException e) {
399 LOG.warn("Incomplete OVSDB port other_config", e);