2 * Copyright (c) 2015, 2016 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.collect.Sets;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
22 import java.util.Map.Entry;
24 import java.util.concurrent.ExecutionException;
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
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.Mutator;
32 import org.opendaylight.ovsdb.lib.notation.UUID;
33 import org.opendaylight.ovsdb.lib.operations.Mutate;
34 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
35 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
36 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
37 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
38 import org.opendaylight.ovsdb.schema.openvswitch.Port;
39 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
40 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
41 import org.opendaylight.ovsdb.southbound.SouthboundProvider;
42 import org.opendaylight.ovsdb.utils.yang.YangUtils;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
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.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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
57 import org.opendaylight.yangtools.yang.binding.DataObject;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
62 public class TerminationPointCreateCommand implements TransactCommand {
64 private static final Logger LOG = LoggerFactory.getLogger(TerminationPointCreateCommand.class);
67 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
68 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> events) {
69 execute(transaction, state, TransactUtils.extractCreated(events, OvsdbTerminationPointAugmentation.class),
70 TransactUtils.extractCreatedOrUpdated(events, Node.class));
74 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
75 Collection<DataTreeModification<Node>> modifications) {
76 execute(transaction, state,
77 TransactUtils.extractCreated(modifications, OvsdbTerminationPointAugmentation.class),
78 TransactUtils.extractCreatedOrUpdated(modifications, Node.class));
81 private void execute(TransactionBuilder transaction, BridgeOperationalState state,
82 Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>,
83 OvsdbTerminationPointAugmentation>
84 createdTerminationPoints, Map<InstanceIdentifier<Node>, Node> nodes) {
85 for (Entry<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> entry :
86 createdTerminationPoints.entrySet()) {
87 OvsdbTerminationPointAugmentation terminationPoint = entry.getValue();
88 LOG.debug("Received request to create termination point {}",
89 terminationPoint.getName());
90 InstanceIdentifier terminationPointIid = entry.getKey();
91 Optional<TerminationPoint> terminationPointOptional =
92 state.getBridgeTerminationPoint(terminationPointIid);
93 if (!terminationPointOptional.isPresent()) {
94 // Configure interface
95 String interfaceUuid = "Interface_" + SouthboundMapper.getRandomUuid();
96 Interface ovsInterface =
97 TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
98 createInterface(terminationPoint, ovsInterface);
99 transaction.add(op.insert(ovsInterface).withId(interfaceUuid));
101 stampInstanceIdentifier(transaction, entry.getKey(), ovsInterface.getName());
103 // Configure port with the above interface details
104 String portUuid = "Port_" + SouthboundMapper.getRandomUuid();
105 Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
106 createPort(terminationPoint, port, interfaceUuid);
107 transaction.add(op.insert(port).withId(portUuid));
108 LOG.info("Created Termination Point : {} with Uuid : {}",
109 terminationPoint.getName(),
110 terminationPoint.getPortUuid());
111 //Configure bridge with the above port details
112 Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
113 if (getBridge(entry.getKey(), nodes) != null) {
114 bridge.setName(getBridge(entry.getKey(), nodes).getBridgeName().getValue());
115 bridge.setPorts(Sets.newHashSet(new UUID(portUuid)));
117 transaction.add(op.mutate(bridge)
118 .addMutation(bridge.getPortsColumn().getSchema(),
119 Mutator.INSERT, bridge.getPortsColumn().getData())
120 .where(bridge.getNameColumn().getSchema()
121 .opEqual(bridge.getNameColumn().getData())).build());
128 private void createInterface(
129 final OvsdbTerminationPointAugmentation terminationPoint,
130 final Interface ovsInterface) {
131 ovsInterface.setName(terminationPoint.getName());
133 createInterfaceType(terminationPoint, ovsInterface);
134 createOfPort(terminationPoint, ovsInterface);
135 createOfPortRequest(terminationPoint, ovsInterface);
136 createInterfaceOptions(terminationPoint, ovsInterface);
137 createInterfaceOtherConfig(terminationPoint, ovsInterface);
138 createInterfaceExternalIds(terminationPoint, ovsInterface);
139 createInterfaceLldp(terminationPoint, ovsInterface);
140 createInterfaceBfd(terminationPoint, ovsInterface);
143 private void createInterfaceType(final OvsdbTerminationPointAugmentation terminationPoint,
144 final Interface ovsInterface) {
146 Class<? extends InterfaceTypeBase> mdsaltype = terminationPoint.getInterfaceType();
147 if (mdsaltype != null) {
148 ovsInterface.setType(SouthboundMapper.createOvsdbInterfaceType(mdsaltype));
152 private void createPort(
153 final OvsdbTerminationPointAugmentation terminationPoint,
154 final Port port, final String interfaceUuid) {
156 port.setName(terminationPoint.getName());
157 port.setInterfaces(Sets.newHashSet(new UUID(interfaceUuid)));
158 createPortOtherConfig(terminationPoint, port);
159 createPortVlanTag(terminationPoint, port);
160 createPortVlanTrunk(terminationPoint, port);
161 createPortVlanMode(terminationPoint, port);
162 createPortExternalIds(terminationPoint, port);
165 private void createOfPort(
166 final OvsdbTerminationPointAugmentation terminationPoint,
167 final Interface ovsInterface) {
169 Long ofPort = terminationPoint.getOfport();
170 if (ofPort != null) {
171 ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
175 private void createOfPortRequest(
176 final OvsdbTerminationPointAugmentation terminationPoint,
177 final Interface ovsInterface) {
179 Integer ofPortRequest = terminationPoint.getOfportRequest();
180 if (ofPortRequest != null) {
181 ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest.longValue()));
185 private void createInterfaceOptions(
186 final OvsdbTerminationPointAugmentation terminationPoint,
187 final Interface ovsInterface) {
189 //Configure optional input
190 if (terminationPoint.getOptions() != null) {
192 ovsInterface.setOptions(YangUtils.convertYangKeyValueListToMap(terminationPoint.getOptions(),
193 Options::getOption, Options::getValue));
194 } catch (NullPointerException e) {
195 LOG.warn("Incomplete OVSDB interface options", e);
200 private void createInterfaceExternalIds(
201 final OvsdbTerminationPointAugmentation terminationPoint,
202 final Interface ovsInterface) {
204 List<InterfaceExternalIds> interfaceExternalIds =
205 terminationPoint.getInterfaceExternalIds();
206 if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
208 ovsInterface.setExternalIds(YangUtils.convertYangKeyValueListToMap(interfaceExternalIds,
209 InterfaceExternalIds::getExternalIdKey, InterfaceExternalIds::getExternalIdValue));
210 } catch (NullPointerException e) {
211 LOG.warn("Incomplete OVSDB interface external_ids", e);
216 private void createInterfaceOtherConfig(
217 final OvsdbTerminationPointAugmentation terminationPoint,
218 final Interface ovsInterface) {
220 List<InterfaceOtherConfigs> interfaceOtherConfigs =
221 terminationPoint.getInterfaceOtherConfigs();
222 if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
223 Map<String, String> otherConfigsMap = new HashMap<>();
224 for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) {
225 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
226 interfaceOtherConfig.getOtherConfigValue());
229 ovsInterface.setOtherConfig(otherConfigsMap);
230 } catch (NullPointerException e) {
231 LOG.warn("Incomplete OVSDB interface other_config", e);
236 private void createInterfaceLldp(
237 final OvsdbTerminationPointAugmentation terminationPoint,
238 final Interface ovsInterface) {
241 List<InterfaceLldp> interfaceLldpList =
242 terminationPoint.getInterfaceLldp();
243 if (interfaceLldpList != null && !interfaceLldpList.isEmpty()) {
245 ovsInterface.setLldp(YangUtils.convertYangKeyValueListToMap(interfaceLldpList,
246 InterfaceLldp::getLldpKey, InterfaceLldp::getLldpValue));
247 } catch (NullPointerException e) {
248 LOG.warn("Incomplete OVSDB interface lldp", e);
251 } catch (SchemaVersionMismatchException e) {
252 schemaMismatchLog("lldp", "Interface", e);
256 private void createInterfaceBfd(final OvsdbTerminationPointAugmentation terminationPoint,
257 final Interface ovsInterface) {
260 List<InterfaceBfd> interfaceBfdList = terminationPoint.getInterfaceBfd();
261 if (interfaceBfdList != null && !interfaceBfdList.isEmpty()) {
263 ovsInterface.setBfd(YangUtils.convertYangKeyValueListToMap(interfaceBfdList,
264 InterfaceBfd::getBfdKey, InterfaceBfd::getBfdValue));
265 } catch (NullPointerException e) {
266 LOG.warn("Incomplete OVSDB interface bfd", e);
269 } catch (SchemaVersionMismatchException e) {
270 schemaMismatchLog("bfd", "Interface", e);
274 private void createPortExternalIds(
275 final OvsdbTerminationPointAugmentation terminationPoint,
278 List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
279 if (portExternalIds != null && !portExternalIds.isEmpty()) {
281 port.setExternalIds(YangUtils.convertYangKeyValueListToMap(portExternalIds,
282 PortExternalIds::getExternalIdKey, PortExternalIds::getExternalIdValue));
283 } catch (NullPointerException e) {
284 LOG.warn("Incomplete OVSDB port external_ids", e);
289 private void createPortVlanTag(
290 final OvsdbTerminationPointAugmentation terminationPoint,
293 if (terminationPoint.getVlanTag() != null) {
294 Set<Long> vlanTag = new HashSet<>();
295 vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
296 port.setTag(vlanTag);
300 private void createPortVlanTrunk(
301 final OvsdbTerminationPointAugmentation terminationPoint,
304 if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
305 Set<Long> portTrunks = new HashSet<>();
306 List<Trunks> modelTrunks = terminationPoint.getTrunks();
307 for (Trunks trunk: modelTrunks) {
308 if (trunk.getTrunk() != null) {
309 portTrunks.add(trunk.getTrunk().getValue().longValue());
312 port.setTrunks(portTrunks);
316 private void createPortVlanMode(
317 final OvsdbTerminationPointAugmentation terminationPoint,
319 if (terminationPoint.getVlanMode() != null) {
320 Set<String> portVlanMode = new HashSet<>();
321 VlanMode modelVlanMode = terminationPoint.getVlanMode();
322 portVlanMode.add(SouthboundConstants.VlanModes.values()[modelVlanMode.getIntValue() - 1].getMode());
323 port.setVlanMode(portVlanMode);
327 private void createPortOtherConfig(
328 final OvsdbTerminationPointAugmentation terminationPoint,
329 final Port ovsPort) {
330 List<PortOtherConfigs> portOtherConfigs =
331 terminationPoint.getPortOtherConfigs();
332 if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
334 ovsPort.setOtherConfig(YangUtils.convertYangKeyValueListToMap(portOtherConfigs,
335 PortOtherConfigs::getOtherConfigKey, PortOtherConfigs::getOtherConfigValue));
336 } catch (NullPointerException e) {
337 LOG.warn("Incomplete OVSDB port other_config", e);
342 private OvsdbBridgeAugmentation getBridge(InstanceIdentifier<?> key, Map<InstanceIdentifier<Node>, Node> nodes) {
343 OvsdbBridgeAugmentation bridge = null;
344 InstanceIdentifier<Node> nodeIid = key.firstIdentifierOf(Node.class);
345 if (nodes != null && nodes.get(nodeIid) != null) {
346 Node node = nodes.get(nodeIid);
347 bridge = node.getAugmentation(OvsdbBridgeAugmentation.class);
348 if (bridge == null) {
349 ReadOnlyTransaction transaction = SouthboundProvider.getDb().newReadOnlyTransaction();
350 CheckedFuture<Optional<Node>, ReadFailedException> future =
351 transaction.read(LogicalDatastoreType.OPERATIONAL, nodeIid);
353 Optional<Node> nodeOptional = future.get();
354 if (nodeOptional.isPresent()) {
355 bridge = nodeOptional.get().getAugmentation(OvsdbBridgeAugmentation.class);
357 } catch (InterruptedException | ExecutionException e) {
358 LOG.warn("Error reading from datastore",e);
366 public static void stampInstanceIdentifier(TransactionBuilder transaction, InstanceIdentifier
367 <OvsdbTerminationPointAugmentation> iid, String interfaceName) {
368 Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
369 port.setName(interfaceName);
370 port.setExternalIds(Collections.<String,String>emptyMap());
371 Mutate mutate = TransactUtils.stampInstanceIdentifierMutation(transaction,
374 port.getExternalIdsColumn().getSchema());
375 transaction.add(mutate
376 .where(port.getNameColumn().getSchema().opEqual(interfaceName))