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 java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
20 import java.util.Map.Entry;
21 import java.util.Optional;
23 import org.opendaylight.mdsal.binding.api.DataTreeModification;
24 import org.opendaylight.mdsal.binding.api.ReadTransaction;
25 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
26 import org.opendaylight.ovsdb.lib.notation.UUID;
27 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
28 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
29 import org.opendaylight.ovsdb.schema.openvswitch.Port;
30 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
31 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
32 import org.opendaylight.ovsdb.southbound.SouthboundProvider;
33 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
34 import org.opendaylight.ovsdb.utils.yang.YangUtils;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbQosRef;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceBfd;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceBfdKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIdsKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceLldp;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceLldpKey;
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.InterfaceOtherConfigsKey;
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.PortOtherConfigsKey;
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(final TransactionBuilder transaction, final BridgeOperationalState state,
69 final DataChangeEvent events, final InstanceIdentifierCodec instanceIdentifierCodec) {
70 execute(transaction, state,
71 TransactUtils.extractCreatedOrUpdated(events, OvsdbTerminationPointAugmentation.class),
72 instanceIdentifierCodec);
76 public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
77 final Collection<DataTreeModification<Node>> modifications,
78 final InstanceIdentifierCodec instanceIdentifierCodec) {
79 execute(transaction, state,
80 TransactUtils.extractCreatedOrUpdated(modifications, OvsdbTerminationPointAugmentation.class),
81 instanceIdentifierCodec);
84 private void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
85 final Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation>
87 final InstanceIdentifierCodec instanceIdentifierCodec) {
88 for (Entry<InstanceIdentifier<OvsdbTerminationPointAugmentation>,
89 OvsdbTerminationPointAugmentation> terminationPointEntry : createdOrUpdated.entrySet()) {
90 updateTerminationPoint(transaction, state, terminationPointEntry.getKey(),
91 terminationPointEntry.getValue(), instanceIdentifierCodec);
95 public void updateTerminationPoint(final TransactionBuilder transaction, final BridgeOperationalState state,
96 final InstanceIdentifier<OvsdbTerminationPointAugmentation> iid,
97 final OvsdbTerminationPointAugmentation terminationPoint,
98 final InstanceIdentifierCodec instanceIdentifierCodec) {
100 if (terminationPoint != null) {
101 LOG.debug("Received request to update termination point {}",
102 terminationPoint.getName());
105 Interface ovsInterface = transaction.getTypedRowWrapper(Interface.class);
106 updateInterface(terminationPoint, ovsInterface);
107 Interface extraInterface = transaction.getTypedRowWrapper(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 = transaction.getTypedRowWrapper(Port.class);
124 updatePort(terminationPoint, port, operBridge, opendaylightIid);
125 Port extraPort = transaction.getTypedRowWrapper(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 static void updateInterface(final OvsdbTerminationPointAugmentation terminationPoint,
140 final Interface ovsInterface) {
141 updateOfPort(terminationPoint, ovsInterface);
142 updateOfPortRequest(terminationPoint, ovsInterface);
143 updateInterfaceOptions(terminationPoint, ovsInterface);
144 updateInterfaceOtherConfig(terminationPoint, ovsInterface);
145 updateInterfaceExternalIds(terminationPoint, ovsInterface);
146 updateInterfaceLldp(terminationPoint, ovsInterface);
147 updateInterfaceBfd(terminationPoint, ovsInterface);
148 updateInterfacePolicing(terminationPoint, ovsInterface);
151 private static void updatePort(final OvsdbTerminationPointAugmentation terminationPoint,
152 final Port port, final OvsdbBridgeAugmentation operBridge, final String opendaylightIid) {
153 updatePortOtherConfig(terminationPoint, port);
154 updatePortVlanTag(terminationPoint, port);
155 updatePortVlanTrunk(terminationPoint, port);
156 updatePortVlanMode(terminationPoint, port);
157 updatePortExternalIds(terminationPoint, port, opendaylightIid);
158 updatePortQos(terminationPoint, port, operBridge);
161 private static void updatePortQos(final OvsdbTerminationPointAugmentation terminationPoint,
162 final Port port, final OvsdbBridgeAugmentation operBridge) {
164 Set<UUID> uuidSet = new HashSet<>();
166 // First check if QosEntry is present and use that
167 if (terminationPoint.getQosEntry() != null && !terminationPoint.getQosEntry().isEmpty()) {
168 OvsdbQosRef qosRef = terminationPoint.getQosEntry().values().iterator().next().getQosRef();
169 Uri qosId = qosRef.getValue().firstKeyOf(QosEntries.class).getQosId();
170 OvsdbNodeAugmentation operNode = getOperNode(operBridge);
171 if (operNode != null) {
172 Map<QosEntriesKey, QosEntries> entries = operNode.getQosEntries();
173 if (entries != null) {
174 QosEntries qosEntry = entries.get(new QosEntriesKey(qosId));
175 if (qosEntry != null) {
176 uuidSet.add(new UUID(qosEntry.getQosUuid().getValue()));
180 if (uuidSet.size() == 0) {
181 uuidSet.add(new UUID(SouthboundConstants.QOS_NAMED_UUID_PREFIX
182 + TransactUtils.bytesToHexString(qosId.getValue().getBytes(UTF_8))));
185 port.setQos(uuidSet);
188 @SuppressWarnings("IllegalCatch")
189 private static OvsdbNodeAugmentation getOperNode(final OvsdbBridgeAugmentation operBridge) {
190 @SuppressWarnings("unchecked")
191 InstanceIdentifier<Node> iidNode = (InstanceIdentifier<Node>)operBridge.getManagedBy().getValue();
192 OvsdbNodeAugmentation operNode = null;
193 try (ReadTransaction transaction = SouthboundProvider.getDb().newReadOnlyTransaction()) {
194 Optional<Node> nodeOptional = SouthboundUtil.readNode(transaction, iidNode);
195 if (nodeOptional.isPresent()) {
196 operNode = nodeOptional.get().augmentation(OvsdbNodeAugmentation.class);
198 } catch (Exception exp) {
199 LOG.error("Error in getting the brideNode for {}", iidNode, exp);
204 private static void updateOfPort(final OvsdbTerminationPointAugmentation terminationPoint,
205 final Interface ovsInterface) {
207 Uint32 ofPort = terminationPoint.getOfport();
208 if (ofPort != null) {
209 ovsInterface.setOpenFlowPort(Collections.singleton(ofPort.toJava()));
213 private static void updateOfPortRequest(final OvsdbTerminationPointAugmentation terminationPoint,
214 final Interface ovsInterface) {
216 Uint16 ofPortRequest = terminationPoint.getOfportRequest();
217 if (ofPortRequest != null) {
218 ovsInterface.setOpenFlowPortRequest(Collections.singleton(ofPortRequest.longValue()));
222 private static void updateInterfaceOptions(final OvsdbTerminationPointAugmentation terminationPoint,
223 final Interface ovsInterface) {
225 //Configure optional input
226 if (terminationPoint.getOptions() != null) {
228 ovsInterface.setOptions(YangUtils.convertYangKeyValueListToMap(terminationPoint.getOptions(),
229 Options::getOption, Options::getValue));
230 } catch (NullPointerException e) {
231 LOG.warn("Incomplete OVSDB interface options", e);
236 private static void updateInterfaceExternalIds(final OvsdbTerminationPointAugmentation terminationPoint,
237 final Interface ovsInterface) {
239 Map<InterfaceExternalIdsKey, InterfaceExternalIds> interfaceExternalIds =
240 terminationPoint.getInterfaceExternalIds();
241 final InterfaceExternalIds odl = SouthboundUtil.interfaceCreatedByOpenDaylight();
243 if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
244 interfaceExternalIds.put(odl.key(), odl);
246 interfaceExternalIds = Map.of(odl.key(), odl);
249 ovsInterface.setExternalIds(YangUtils.convertYangKeyValueListToMap(interfaceExternalIds,
250 InterfaceExternalIds::getExternalIdKey, InterfaceExternalIds::getExternalIdValue));
251 } catch (NullPointerException e) {
252 LOG.warn("Incomplete OVSDB interface external_ids", e);
256 private static void updateInterfaceLldp(final OvsdbTerminationPointAugmentation terminationPoint,
257 final Interface ovsInterface) {
259 Map<InterfaceLldpKey, InterfaceLldp> interfaceLldpList =
260 terminationPoint.getInterfaceLldp();
261 if (interfaceLldpList != null && !interfaceLldpList.isEmpty()) {
263 ovsInterface.setLldp(YangUtils.convertYangKeyValueListToMap(interfaceLldpList,
264 InterfaceLldp::getLldpKey, InterfaceLldp::getLldpValue));
265 } catch (NullPointerException e) {
266 LOG.warn("Incomplete OVSDB interface lldp", e);
269 } catch (SchemaVersionMismatchException e) {
270 schemaMismatchLog("lldp", "Interface", e);
274 private static void updateInterfaceOtherConfig(final OvsdbTerminationPointAugmentation terminationPoint,
275 final Interface ovsInterface) {
277 Map<InterfaceOtherConfigsKey, InterfaceOtherConfigs> interfaceOtherConfigs =
278 terminationPoint.getInterfaceOtherConfigs();
279 if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
280 Map<String, String> otherConfigsMap = new HashMap<>();
281 for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs.values()) {
282 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
283 interfaceOtherConfig.getOtherConfigValue());
286 ovsInterface.setOtherConfig(otherConfigsMap);
287 } catch (NullPointerException e) {
288 LOG.warn("Incomplete OVSDB interface other_config", e);
293 private static void updateInterfaceBfd(final OvsdbTerminationPointAugmentation terminationPoint,
294 final Interface ovsInterface) {
297 Map<InterfaceBfdKey, InterfaceBfd> interfaceBfdList = terminationPoint.getInterfaceBfd();
298 if (interfaceBfdList != null && !interfaceBfdList.isEmpty()) {
300 ovsInterface.setBfd(YangUtils.convertYangKeyValueListToMap(interfaceBfdList,
301 InterfaceBfd::getBfdKey, InterfaceBfd::getBfdValue));
302 } catch (NullPointerException e) {
303 LOG.warn("Incomplete OVSDB interface bfd", e);
306 } catch (SchemaVersionMismatchException e) {
307 schemaMismatchLog("bfd", "Interface", e);
311 private static void updateInterfacePolicing(final OvsdbTerminationPointAugmentation terminationPoint,
312 final Interface ovsInterface) {
314 Uint32 ingressPolicingRate = terminationPoint.getIngressPolicingRate();
315 if (ingressPolicingRate != null) {
316 ovsInterface.setIngressPolicingRate(ingressPolicingRate.toJava());
318 Uint32 ingressPolicingBurst = terminationPoint.getIngressPolicingBurst();
319 if (ingressPolicingBurst != null) {
320 ovsInterface.setIngressPolicingBurst(ingressPolicingBurst.toJava());
324 private static void updatePortExternalIds(final OvsdbTerminationPointAugmentation terminationPoint,
325 final Port port, final String opendaylightIid) {
327 Map<String, String> externalIdMap = new HashMap<>();
328 externalIdMap.put(SouthboundConstants.IID_EXTERNAL_ID_KEY, opendaylightIid);
329 externalIdMap.put(SouthboundConstants.CREATED_BY, SouthboundConstants.ODL);
331 YangUtils.copyYangKeyValueListToMap(externalIdMap, terminationPoint.getPortExternalIds(),
332 PortExternalIds::getExternalIdKey, PortExternalIds::getExternalIdValue);
333 } catch (NullPointerException e) {
334 LOG.warn("Incomplete OVSDB port external_ids", e);
336 port.setExternalIds(externalIdMap);
339 private static void updatePortVlanTag(final OvsdbTerminationPointAugmentation terminationPoint, final Port port) {
340 if (terminationPoint.getVlanTag() != null) {
341 Set<Long> vlanTag = new HashSet<>();
342 vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
343 port.setTag(vlanTag);
347 private static void updatePortVlanTrunk(final OvsdbTerminationPointAugmentation terminationPoint, final Port port) {
348 if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
349 Set<Long> portTrunks = new HashSet<>();
350 List<Trunks> modelTrunks = terminationPoint.getTrunks();
351 for (Trunks trunk : modelTrunks) {
352 if (trunk.getTrunk() != null) {
353 portTrunks.add(trunk.getTrunk().getValue().longValue());
356 port.setTrunks(portTrunks);
360 private static void updatePortVlanMode(final OvsdbTerminationPointAugmentation terminationPoint, final Port port) {
361 if (terminationPoint.getVlanMode() != null) {
362 Set<String> portVlanMode = new HashSet<>();
363 VlanMode modelVlanMode = terminationPoint.getVlanMode();
364 portVlanMode.add(SouthboundConstants.VlanModes.values()[modelVlanMode.getIntValue() - 1].getMode());
365 port.setVlanMode(portVlanMode);
369 private static void updatePortOtherConfig(final OvsdbTerminationPointAugmentation terminationPoint,
370 final Port ovsPort) {
371 Map<PortOtherConfigsKey, PortOtherConfigs> portOtherConfigs =
372 terminationPoint.getPortOtherConfigs();
373 if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
375 ovsPort.setOtherConfig(YangUtils.convertYangKeyValueListToMap(portOtherConfigs,
376 PortOtherConfigs::getOtherConfigKey, PortOtherConfigs::getOtherConfigValue));
377 } catch (NullPointerException e) {
378 LOG.warn("Incomplete OVSDB port other_config", e);