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 java.nio.charset.StandardCharsets.UTF_8;
11 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12 import static org.opendaylight.ovsdb.southbound.SouthboundUtil.schemaMismatchLog;
14 import com.google.common.base.Optional;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
23 import java.util.Map.Entry;
25 import java.util.concurrent.ExecutionException;
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
27 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
30 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
31 import org.opendaylight.ovsdb.lib.notation.UUID;
32 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
33 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
34 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
35 import org.opendaylight.ovsdb.schema.openvswitch.Port;
36 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
37 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
38 import org.opendaylight.ovsdb.southbound.SouthboundProvider;
39 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
40 import org.opendaylight.ovsdb.utils.yang.YangUtils;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbQosRef;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceBfd;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceLldp;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceOtherConfigs;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortOtherConfigs;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Trunks;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import org.opendaylight.yangtools.yang.common.Uint16;
59 import org.opendaylight.yangtools.yang.common.Uint32;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
63 public class TerminationPointUpdateCommand implements TransactCommand {
65 private static final Logger LOG = LoggerFactory.getLogger(TerminationPointUpdateCommand.class);
68 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
69 DataChangeEvent events, InstanceIdentifierCodec instanceIdentifierCodec) {
70 execute(transaction, state,
71 TransactUtils.extractCreatedOrUpdated(events, OvsdbTerminationPointAugmentation.class),
72 instanceIdentifierCodec);
76 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
77 Collection<DataTreeModification<Node>> modifications, InstanceIdentifierCodec instanceIdentifierCodec) {
78 execute(transaction, state,
79 TransactUtils.extractCreatedOrUpdated(modifications, OvsdbTerminationPointAugmentation.class),
80 instanceIdentifierCodec);
83 private void execute(TransactionBuilder transaction, BridgeOperationalState state,
84 Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation>
86 InstanceIdentifierCodec instanceIdentifierCodec) {
87 for (Entry<InstanceIdentifier<OvsdbTerminationPointAugmentation>,
88 OvsdbTerminationPointAugmentation> terminationPointEntry : createdOrUpdated.entrySet()) {
89 updateTerminationPoint(transaction, state, terminationPointEntry.getKey(),
90 terminationPointEntry.getValue(), instanceIdentifierCodec);
94 public void updateTerminationPoint(TransactionBuilder transaction, BridgeOperationalState state,
95 InstanceIdentifier<OvsdbTerminationPointAugmentation> iid,
96 OvsdbTerminationPointAugmentation terminationPoint, InstanceIdentifierCodec instanceIdentifierCodec) {
98 if (terminationPoint != null) {
99 LOG.debug("Received request to update termination point {}",
100 terminationPoint.getName());
103 Interface ovsInterface =
104 TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
105 updateInterface(terminationPoint, ovsInterface);
106 Interface extraInterface = TyperUtils.getTypedRowWrapper(
107 transaction.getDatabaseSchema(), Interface.class);
108 extraInterface.setName("");
109 transaction.add(op.update(ovsInterface)
110 .where(extraInterface.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
113 TerminationPointCreateCommand.stampInstanceIdentifier(transaction,
114 iid.firstIdentifierOf(OvsdbTerminationPointAugmentation.class), terminationPoint.getName(),
115 instanceIdentifierCodec);
116 final String opendaylightIid = instanceIdentifierCodec.serialize(iid);
119 Optional<OvsdbBridgeAugmentation> ovsdbBridgeOptional = state.getOvsdbBridgeAugmentation(iid);
120 if (ovsdbBridgeOptional != null && ovsdbBridgeOptional.isPresent()) {
121 OvsdbBridgeAugmentation operBridge = ovsdbBridgeOptional.get();
122 if (operBridge != null) {
123 Port port = TyperUtils.getTypedRowWrapper(
124 transaction.getDatabaseSchema(), Port.class);
125 updatePort(terminationPoint, port, operBridge, opendaylightIid);
126 Port extraPort = TyperUtils.getTypedRowWrapper(
127 transaction.getDatabaseSchema(), Port.class);
128 extraPort.setName("");
129 transaction.add(op.update(port)
130 .where(extraPort.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
132 LOG.info("Updated Termination Point : {} with Uuid : {}",
133 terminationPoint.getName(), terminationPoint.getPortUuid());
136 LOG.warn("OVSDB bridge node was not found: {}", iid);
141 private void updateInterface(
142 final OvsdbTerminationPointAugmentation terminationPoint,
143 final Interface ovsInterface) {
144 updateOfPort(terminationPoint, ovsInterface);
145 updateOfPortRequest(terminationPoint, ovsInterface);
146 updateInterfaceOptions(terminationPoint, ovsInterface);
147 updateInterfaceOtherConfig(terminationPoint, ovsInterface);
148 updateInterfaceExternalIds(terminationPoint, ovsInterface);
149 updateInterfaceLldp(terminationPoint, ovsInterface);
150 updateInterfaceBfd(terminationPoint, ovsInterface);
151 updateInterfacePolicing(terminationPoint, ovsInterface);
154 private void updatePort(
155 final OvsdbTerminationPointAugmentation terminationPoint,
157 final OvsdbBridgeAugmentation operBridge,
158 final String opendaylightIid) {
160 updatePortOtherConfig(terminationPoint, port);
161 updatePortVlanTag(terminationPoint, port);
162 updatePortVlanTrunk(terminationPoint, port);
163 updatePortVlanMode(terminationPoint, port);
164 updatePortExternalIds(terminationPoint, port, opendaylightIid);
165 updatePortQos(terminationPoint, port, operBridge);
168 private void updatePortQos(
169 final OvsdbTerminationPointAugmentation terminationPoint,
171 final OvsdbBridgeAugmentation operBridge) {
173 Set<UUID> uuidSet = new HashSet<>();
175 // First check if QosEntry is present and use that
176 if (terminationPoint.getQosEntry() != null && !terminationPoint.getQosEntry().isEmpty()) {
177 OvsdbQosRef qosRef = terminationPoint.getQosEntry().iterator().next().getQosRef();
178 Uri qosId = qosRef.getValue().firstKeyOf(QosEntries.class).getQosId();
179 OvsdbNodeAugmentation operNode = getOperNode(operBridge);
180 if (operNode != null && operNode.getQosEntries() != null
181 && !operNode.getQosEntries().isEmpty()) {
182 for (QosEntries qosEntry : operNode.getQosEntries()) {
183 if (qosEntry.getQosId().equals(qosId)) {
184 uuidSet.add(new UUID(qosEntry.getQosUuid().getValue()));
188 if (uuidSet.size() == 0) {
189 uuidSet.add(new UUID(SouthboundConstants.QOS_NAMED_UUID_PREFIX
190 + TransactUtils.bytesToHexString(qosId.getValue().getBytes(UTF_8))));
193 port.setQos(uuidSet);
196 private OvsdbNodeAugmentation getOperNode(final OvsdbBridgeAugmentation operBridge) {
197 @SuppressWarnings("unchecked")
198 InstanceIdentifier<Node> iidNode = (InstanceIdentifier<Node>)operBridge.getManagedBy().getValue();
199 OvsdbNodeAugmentation operNode = null;
200 ReadOnlyTransaction transaction = SouthboundProvider.getDb().newReadOnlyTransaction();
201 CheckedFuture<Optional<Node>, ReadFailedException> future =
202 transaction.read(LogicalDatastoreType.OPERATIONAL, iidNode);
204 Optional<Node> nodeOptional = future.get();
205 if (nodeOptional.isPresent()) {
206 operNode = nodeOptional.get().augmentation(OvsdbNodeAugmentation.class);
208 } catch (InterruptedException | ExecutionException e) {
209 LOG.warn("Error reading from datastore", e);
214 private void updateOfPort(
215 final OvsdbTerminationPointAugmentation terminationPoint,
216 final Interface ovsInterface) {
218 Uint32 ofPort = terminationPoint.getOfport();
219 if (ofPort != null) {
220 ovsInterface.setOpenFlowPort(Collections.singleton(ofPort.toJava()));
224 private void updateOfPortRequest(
225 final OvsdbTerminationPointAugmentation terminationPoint,
226 final Interface ovsInterface) {
228 Uint16 ofPortRequest = terminationPoint.getOfportRequest();
229 if (ofPortRequest != null) {
230 ovsInterface.setOpenFlowPortRequest(Collections.singleton(ofPortRequest.longValue()));
234 private void updateInterfaceOptions(
235 final OvsdbTerminationPointAugmentation terminationPoint,
236 final Interface ovsInterface) {
238 //Configure optional input
239 if (terminationPoint.getOptions() != null) {
241 ovsInterface.setOptions(YangUtils.convertYangKeyValueListToMap(terminationPoint.getOptions(),
242 Options::getOption, Options::getValue));
243 } catch (NullPointerException e) {
244 LOG.warn("Incomplete OVSDB interface options", e);
249 private void updateInterfaceExternalIds(
250 final OvsdbTerminationPointAugmentation terminationPoint,
251 final Interface ovsInterface) {
253 List<InterfaceExternalIds> interfaceExternalIds =
254 terminationPoint.getInterfaceExternalIds();
255 if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
256 interfaceExternalIds.add(SouthboundUtil.createExternalIdsForInterface(
257 SouthboundConstants.CREATED_BY, SouthboundConstants.ODL));
259 interfaceExternalIds = Arrays.asList(SouthboundUtil.createExternalIdsForInterface(
260 SouthboundConstants.CREATED_BY, SouthboundConstants.ODL));
263 ovsInterface.setExternalIds(YangUtils.convertYangKeyValueListToMap(interfaceExternalIds,
264 InterfaceExternalIds::getExternalIdKey, InterfaceExternalIds::getExternalIdValue));
265 } catch (NullPointerException e) {
266 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 Uint32 ingressPolicingRate = terminationPoint.getIngressPolicingRate();
335 if (ingressPolicingRate != null) {
336 ovsInterface.setIngressPolicingRate(ingressPolicingRate.toJava());
338 Uint32 ingressPolicingBurst = terminationPoint.getIngressPolicingBurst();
339 if (ingressPolicingBurst != null) {
340 ovsInterface.setIngressPolicingBurst(ingressPolicingBurst.toJava());
344 private void updatePortExternalIds(
345 final OvsdbTerminationPointAugmentation terminationPoint,
347 final String opendaylightIid) {
349 Map<String, String> externalIdMap = new HashMap<>();
350 externalIdMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY, opendaylightIid);
351 externalIdMap.put(SouthboundConstants.CREATED_BY, SouthboundConstants.ODL);
353 YangUtils.copyYangKeyValueListToMap(externalIdMap, terminationPoint.getPortExternalIds(),
354 PortExternalIds::getExternalIdKey, PortExternalIds::getExternalIdValue);
355 } catch (NullPointerException e) {
356 LOG.warn("Incomplete OVSDB port external_ids", e);
358 port.setExternalIds(externalIdMap);
361 private void updatePortVlanTag(
362 final OvsdbTerminationPointAugmentation terminationPoint,
365 if (terminationPoint.getVlanTag() != null) {
366 Set<Long> vlanTag = new HashSet<>();
367 vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
368 port.setTag(vlanTag);
372 private void updatePortVlanTrunk(
373 final OvsdbTerminationPointAugmentation terminationPoint,
376 if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
377 Set<Long> portTrunks = new HashSet<>();
378 List<Trunks> modelTrunks = terminationPoint.getTrunks();
379 for (Trunks trunk : modelTrunks) {
380 if (trunk.getTrunk() != null) {
381 portTrunks.add(trunk.getTrunk().getValue().longValue());
384 port.setTrunks(portTrunks);
388 private void updatePortVlanMode(
389 final OvsdbTerminationPointAugmentation terminationPoint,
391 if (terminationPoint.getVlanMode() != null) {
392 Set<String> portVlanMode = new HashSet<>();
393 VlanMode modelVlanMode = terminationPoint.getVlanMode();
394 portVlanMode.add(SouthboundConstants.VlanModes.values()[modelVlanMode.getIntValue() - 1].getMode());
395 port.setVlanMode(portVlanMode);
399 private void updatePortOtherConfig(
400 final OvsdbTerminationPointAugmentation terminationPoint,
401 final Port ovsPort) {
402 List<PortOtherConfigs> portOtherConfigs =
403 terminationPoint.getPortOtherConfigs();
404 if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
406 ovsPort.setOtherConfig(YangUtils.convertYangKeyValueListToMap(portOtherConfigs,
407 PortOtherConfigs::getOtherConfigKey, PortOtherConfigs::getOtherConfigValue));
408 } catch (NullPointerException e) {
409 LOG.warn("Incomplete OVSDB port other_config", e);