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;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.List;
18 import java.util.Map.Entry;
19 import java.util.concurrent.ExecutionException;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
23 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
27 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
28 import org.opendaylight.ovsdb.lib.notation.Mutator;
29 import org.opendaylight.ovsdb.lib.notation.UUID;
30 import org.opendaylight.ovsdb.lib.operations.Mutate;
31 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
32 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
33 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
34 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
35 import org.opendaylight.ovsdb.schema.openvswitch.Port;
36 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
37 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
38 import org.opendaylight.ovsdb.southbound.SouthboundProvider;
39 import org.opendaylight.ovsdb.utils.yang.YangUtils;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase;
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.OvsdbPortInterfaceAttributes.VlanMode;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceBfd;
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.InterfaceLldp;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceOtherConfigs;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortOtherConfigs;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Trunks;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
54 import org.opendaylight.yangtools.yang.binding.DataObject;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 import com.google.common.base.Optional;
60 import com.google.common.collect.Sets;
61 import com.google.common.util.concurrent.CheckedFuture;
63 public class TerminationPointCreateCommand implements TransactCommand {
65 private static final Logger LOG = LoggerFactory.getLogger(TerminationPointCreateCommand.class);
68 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
69 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> events) {
70 execute(transaction, state, TransactUtils.extractCreated(events, OvsdbTerminationPointAugmentation.class),
71 TransactUtils.extractCreatedOrUpdated(events, Node.class));
75 public void execute(TransactionBuilder transaction, BridgeOperationalState state,
76 Collection<DataTreeModification<Node>> modifications) {
77 execute(transaction, state, 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));
109 //Configure bridge with the above port details
110 Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
111 if (getBridge(entry.getKey(), nodes) != null) {
112 bridge.setName(getBridge(entry.getKey(), nodes).getBridgeName().getValue());
113 bridge.setPorts(Sets.newHashSet(new UUID(portUuid)));
115 transaction.add(op.mutate(bridge)
116 .addMutation(bridge.getPortsColumn().getSchema(),
117 Mutator.INSERT, bridge.getPortsColumn().getData())
118 .where(bridge.getNameColumn().getSchema()
119 .opEqual(bridge.getNameColumn().getData())).build());
126 private void createInterface(
127 final OvsdbTerminationPointAugmentation terminationPoint,
128 final Interface ovsInterface) {
129 ovsInterface.setName(terminationPoint.getName());
131 createInterfaceType(terminationPoint, ovsInterface);
132 createOfPort(terminationPoint, ovsInterface);
133 createOfPortRequest(terminationPoint, ovsInterface);
134 createInterfaceOptions(terminationPoint, ovsInterface);
135 createInterfaceOtherConfig(terminationPoint, ovsInterface);
136 createInterfaceExternalIds(terminationPoint, ovsInterface);
137 createInterfaceLldp(terminationPoint, ovsInterface);
138 createInterfaceBfd(terminationPoint, ovsInterface);
141 private void createInterfaceType(final OvsdbTerminationPointAugmentation terminationPoint,
142 final Interface ovsInterface) {
144 Class<? extends InterfaceTypeBase> mdsaltype = terminationPoint.getInterfaceType();
145 if (mdsaltype != null) {
146 ovsInterface.setType(SouthboundMapper.createOvsdbInterfaceType(mdsaltype));
150 private void createPort(
151 final OvsdbTerminationPointAugmentation terminationPoint,
152 final Port port, final String interfaceUuid) {
154 port.setName(terminationPoint.getName());
155 port.setInterfaces(Sets.newHashSet(new UUID(interfaceUuid)));
156 createPortOtherConfig(terminationPoint, port);
157 createPortVlanTag(terminationPoint, port);
158 createPortVlanTrunk(terminationPoint, port);
159 createPortVlanMode(terminationPoint, port);
160 createPortExternalIds(terminationPoint, port);
163 private void createOfPort(
164 final OvsdbTerminationPointAugmentation terminationPoint,
165 final Interface ovsInterface) {
167 Long ofPort = terminationPoint.getOfport();
168 if (ofPort != null) {
169 ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
173 private void createOfPortRequest(
174 final OvsdbTerminationPointAugmentation terminationPoint,
175 final Interface ovsInterface) {
177 Integer ofPortRequest = terminationPoint.getOfportRequest();
178 if (ofPortRequest != null) {
179 ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest.longValue()));
183 private void createInterfaceOptions(
184 final OvsdbTerminationPointAugmentation terminationPoint,
185 final Interface ovsInterface) {
187 //Configure optional input
188 if (terminationPoint.getOptions() != null) {
190 ovsInterface.setOptions(YangUtils.convertYangKeyValueListToMap(terminationPoint.getOptions(),
191 Options::getOption, Options::getValue));
192 } catch (NullPointerException e) {
193 LOG.warn("Incomplete OVSDB interface options", e);
198 private void createInterfaceExternalIds(
199 final OvsdbTerminationPointAugmentation terminationPoint,
200 final Interface ovsInterface) {
202 List<InterfaceExternalIds> interfaceExternalIds =
203 terminationPoint.getInterfaceExternalIds();
204 if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
206 ovsInterface.setExternalIds(YangUtils.convertYangKeyValueListToMap(interfaceExternalIds,
207 InterfaceExternalIds::getExternalIdKey, InterfaceExternalIds::getExternalIdValue));
208 } catch (NullPointerException e) {
209 LOG.warn("Incomplete OVSDB interface external_ids", e);
214 private void createInterfaceOtherConfig(
215 final OvsdbTerminationPointAugmentation terminationPoint,
216 final Interface ovsInterface) {
218 List<InterfaceOtherConfigs> interfaceOtherConfigs =
219 terminationPoint.getInterfaceOtherConfigs();
220 if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
221 Map<String, String> otherConfigsMap = new HashMap<>();
222 for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) {
223 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
224 interfaceOtherConfig.getOtherConfigValue());
227 ovsInterface.setOtherConfig(otherConfigsMap);
228 } catch (NullPointerException e) {
229 LOG.warn("Incomplete OVSDB interface other_config", e);
234 private void createInterfaceLldp(
235 final OvsdbTerminationPointAugmentation terminationPoint,
236 final Interface ovsInterface) {
239 List<InterfaceLldp> interfaceLldpList =
240 terminationPoint.getInterfaceLldp();
241 if (interfaceLldpList != null && !interfaceLldpList.isEmpty()) {
243 ovsInterface.setLldp(YangUtils.convertYangKeyValueListToMap(interfaceLldpList,
244 InterfaceLldp::getLldpKey, InterfaceLldp::getLldpValue));
245 } catch (NullPointerException e) {
246 LOG.warn("Incomplete OVSDB interface lldp", e);
249 } catch (SchemaVersionMismatchException e) {
250 LOG.debug("lldp column for Interface Table unsupported for this version of ovsdb schema", e);
254 private void createInterfaceBfd(final OvsdbTerminationPointAugmentation terminationPoint,
255 final Interface ovsInterface) {
258 List<InterfaceBfd> interfaceBfdList = terminationPoint.getInterfaceBfd();
259 if (interfaceBfdList != null && !interfaceBfdList.isEmpty()) {
261 ovsInterface.setBfd(YangUtils.convertYangKeyValueListToMap(interfaceBfdList,
262 InterfaceBfd::getBfdKey, InterfaceBfd::getBfdValue));
263 } catch (NullPointerException e) {
264 LOG.warn("Incomplete OVSDB interface bfd", e);
267 } catch (SchemaVersionMismatchException e) {
268 LOG.debug("bfd column for Interface Table unsupported for this version of ovsdb schema", e);
272 private void createPortExternalIds(
273 final OvsdbTerminationPointAugmentation terminationPoint,
276 List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
277 if (portExternalIds != null && !portExternalIds.isEmpty()) {
279 port.setExternalIds(YangUtils.convertYangKeyValueListToMap(portExternalIds,
280 PortExternalIds::getExternalIdKey, PortExternalIds::getExternalIdValue));
281 } catch (NullPointerException e) {
282 LOG.warn("Incomplete OVSDB port external_ids", e);
287 private void createPortVlanTag(
288 final OvsdbTerminationPointAugmentation terminationPoint,
291 if (terminationPoint.getVlanTag() != null) {
292 Set<Long> vlanTag = new HashSet<>();
293 vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
294 port.setTag(vlanTag);
298 private void createPortVlanTrunk(
299 final OvsdbTerminationPointAugmentation terminationPoint,
302 if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
303 Set<Long> portTrunks = new HashSet<>();
304 List<Trunks> modelTrunks = terminationPoint.getTrunks();
305 for (Trunks trunk: modelTrunks) {
306 if (trunk.getTrunk() != null) {
307 portTrunks.add(trunk.getTrunk().getValue().longValue());
310 port.setTrunks(portTrunks);
314 private void createPortVlanMode(
315 final OvsdbTerminationPointAugmentation terminationPoint,
317 if (terminationPoint.getVlanMode() != null) {
318 Set<String> portVlanMode = new HashSet<>();
319 VlanMode modelVlanMode = terminationPoint.getVlanMode();
320 portVlanMode.add(SouthboundConstants.VLANMODES.values()[modelVlanMode.getIntValue() - 1].getMode());
321 port.setVlanMode(portVlanMode);
325 private void createPortOtherConfig(
326 final OvsdbTerminationPointAugmentation terminationPoint,
327 final Port ovsPort) {
328 List<PortOtherConfigs> portOtherConfigs =
329 terminationPoint.getPortOtherConfigs();
330 if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
332 ovsPort.setOtherConfig(YangUtils.convertYangKeyValueListToMap(portOtherConfigs,
333 PortOtherConfigs::getOtherConfigKey, PortOtherConfigs::getOtherConfigValue));
334 } catch (NullPointerException e) {
335 LOG.warn("Incomplete OVSDB port other_config", e);
340 private OvsdbBridgeAugmentation getBridge(InstanceIdentifier<?> key, Map<InstanceIdentifier<Node>, Node> nodes) {
341 OvsdbBridgeAugmentation bridge = null;
342 InstanceIdentifier<Node> nodeIid = key.firstIdentifierOf(Node.class);
343 if (nodes != null && nodes.get(nodeIid) != null) {
344 Node node = nodes.get(nodeIid);
345 bridge = node.getAugmentation(OvsdbBridgeAugmentation.class);
346 if (bridge == null) {
347 ReadOnlyTransaction transaction = SouthboundProvider.getDb().newReadOnlyTransaction();
348 CheckedFuture<Optional<Node>, ReadFailedException> future =
349 transaction.read(LogicalDatastoreType.OPERATIONAL, nodeIid);
351 Optional<Node> nodeOptional = future.get();
352 if (nodeOptional.isPresent()) {
353 bridge = nodeOptional.get().getAugmentation(OvsdbBridgeAugmentation.class);
355 } catch (InterruptedException | ExecutionException e) {
356 LOG.warn("Error reading from datastore",e);
364 public static void stampInstanceIdentifier(TransactionBuilder transaction, InstanceIdentifier
365 <OvsdbTerminationPointAugmentation> iid, String interfaceName) {
366 Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
367 port.setName(interfaceName);
368 port.setExternalIds(Collections.<String,String>emptyMap());
369 Mutate mutate = TransactUtils.stampInstanceIdentifierMutation(transaction,
372 port.getExternalIdsColumn().getSchema());
373 transaction.add(mutate
374 .where(port.getNameColumn().getSchema().opEqual(interfaceName))