2 * Copyright (c) 2015 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.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
16 import java.util.Map.Entry;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
20 import org.opendaylight.ovsdb.lib.notation.Mutator;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
23 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
24 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
25 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
26 import org.opendaylight.ovsdb.schema.openvswitch.Port;
27 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
28 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceOtherConfigs;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortOtherConfigs;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Trunks;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
39 import org.opendaylight.yangtools.yang.binding.DataObject;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 import com.google.common.collect.ImmutableMap;
45 import com.google.common.collect.Sets;
47 public class TerminationPointCreateCommand extends AbstractTransactCommand {
49 private static final Logger LOG = LoggerFactory.getLogger(TerminationPointCreateCommand.class);
51 public TerminationPointCreateCommand(BridgeOperationalState state,
52 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
53 super(state, changes);
57 public void execute(TransactionBuilder transaction) {
58 for (Entry<InstanceIdentifier<?>, DataObject> entry: getChanges().getCreatedData().entrySet()) {
59 DataObject dataObject = entry.getValue();
60 if (dataObject instanceof OvsdbTerminationPointAugmentation) {
61 OvsdbTerminationPointAugmentation terminationPoint = (OvsdbTerminationPointAugmentation) dataObject;
62 LOG.debug("Received request to create termination point {}",
63 terminationPoint.getName());
65 // Configure interface
66 String interfaceUuid = "Interface_" + SouthboundMapper.getRandomUUID();;
67 Interface ovsInterface =
68 TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
69 createInterface(terminationPoint, ovsInterface);
70 transaction.add(op.insert(ovsInterface).withId(interfaceUuid));
72 // Configure port with the above interface details
73 String portUuid = "Port_" + SouthboundMapper.getRandomUUID();
74 Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
75 createPort(terminationPoint, port, interfaceUuid);
76 transaction.add(op.insert(port).withId(portUuid));
78 //Configure bridge with the above port details
79 Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
80 bridge.setName(getBridge(entry.getKey()).getBridgeName().getValue());
81 bridge.setPorts(Sets.newHashSet(new UUID(portUuid)));
83 transaction.add(op.mutate(bridge)
84 .addMutation(bridge.getPortsColumn().getSchema(),
85 Mutator.INSERT,bridge.getPortsColumn().getData())
86 .where(bridge.getNameColumn().getSchema()
87 .opEqual(bridge.getNameColumn().getData())).build());
93 private void createInterface(
94 final OvsdbTerminationPointAugmentation terminationPoint,
95 final Interface ovsInterface) {
96 ovsInterface.setName(terminationPoint.getName());
97 ovsInterface.setType(SouthboundMapper.createOvsdbInterfaceType(terminationPoint.getInterfaceType()));
99 createOfPort(terminationPoint, ovsInterface);
100 createOfPortRequest(terminationPoint, ovsInterface);
101 createInterfaceOptions(terminationPoint, ovsInterface);
102 createInterfaceOtherConfig(terminationPoint, ovsInterface);
103 createInterfaceExternalIds(terminationPoint, ovsInterface);
106 private void createPort(
107 final OvsdbTerminationPointAugmentation terminationPoint,
108 final Port port, final String interfaceUuid) {
110 port.setName(terminationPoint.getName());
111 port.setInterfaces(Sets.newHashSet(new UUID(interfaceUuid)));
112 createPortOtherConfig(terminationPoint, port);
113 createPortVlanTag(terminationPoint, port);
114 createPortVlanTrunk(terminationPoint, port);
115 createPortVlanMode(terminationPoint, port);
116 createPortExternalIds(terminationPoint, port);
119 private void createOfPort(
120 final OvsdbTerminationPointAugmentation terminationPoint,
121 final Interface ovsInterface) {
123 Long ofPort = terminationPoint.getOfport();
124 if (ofPort != null) {
125 ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
129 private void createOfPortRequest(
130 final OvsdbTerminationPointAugmentation terminationPoint,
131 final Interface ovsInterface) {
133 Integer ofPortRequest = terminationPoint.getOfportRequest();
134 if (ofPortRequest != null) {
135 ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest.longValue()));
139 private void createInterfaceOptions(
140 final OvsdbTerminationPointAugmentation terminationPoint,
141 final Interface ovsInterface) {
143 //Configure optional input
144 if (terminationPoint.getOptions() != null) {
145 HashMap<String, String> optionsMap = new HashMap<String, String>();
146 for (Options option : terminationPoint.getOptions()) {
147 optionsMap.put(option.getOption(), option.getValue());
150 ovsInterface.setOptions(ImmutableMap.copyOf(optionsMap));
151 } catch (NullPointerException e) {
152 LOG.warn("Incomplete OVSDB interface options");
157 private void createInterfaceExternalIds(
158 final OvsdbTerminationPointAugmentation terminationPoint,
159 final Interface ovsInterface) {
161 List<InterfaceExternalIds> interfaceExternalIds =
162 terminationPoint.getInterfaceExternalIds();
163 if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
164 HashMap<String, String> externalIdsMap = new HashMap<String, String>();
165 for (InterfaceExternalIds externalId: interfaceExternalIds) {
166 externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
169 ovsInterface.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
170 } catch (NullPointerException e) {
171 LOG.warn("Incomplete OVSDB interface external_ids");
176 private void createInterfaceOtherConfig(
177 final OvsdbTerminationPointAugmentation terminationPoint,
178 final Interface ovsInterface) {
180 List<InterfaceOtherConfigs> interfaceOtherConfigs =
181 terminationPoint.getInterfaceOtherConfigs();
182 if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
183 HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
184 for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) {
185 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
186 interfaceOtherConfig.getOtherConfigValue());
189 ovsInterface.setOtherConfig(otherConfigsMap);
190 } catch (NullPointerException e) {
191 LOG.warn("Incomplete OVSDB interface other_config", e);
196 private void createPortExternalIds(
197 final OvsdbTerminationPointAugmentation terminationPoint,
200 List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
201 if (portExternalIds != null && !portExternalIds.isEmpty()) {
202 HashMap<String, String> externalIdsMap = new HashMap<String, String>();
203 for (PortExternalIds externalId: portExternalIds) {
204 externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
207 port.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
208 } catch (NullPointerException e) {
209 LOG.warn("Incomplete OVSDB port external_ids");
214 private void createPortVlanTag(
215 final OvsdbTerminationPointAugmentation terminationPoint,
218 if (terminationPoint.getVlanTag() != null) {
219 Set<Long> vlanTag = new HashSet<Long>();
220 vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
221 port.setTag(vlanTag);
225 private void createPortVlanTrunk(
226 final OvsdbTerminationPointAugmentation terminationPoint,
229 if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
230 Set<Long> portTrunks = new HashSet<Long>();
231 List<Trunks> modelTrunks = terminationPoint.getTrunks();
232 for (Trunks trunk: modelTrunks) {
233 if (trunk.getTrunk() != null) {
234 portTrunks.add(trunk.getTrunk().getValue().longValue());
237 port.setTrunks(portTrunks);
241 private void createPortVlanMode(
242 final OvsdbTerminationPointAugmentation terminationPoint,
244 if (terminationPoint.getVlanMode() != null) {
245 Set<String> portVlanMode = new HashSet<String>();
246 VlanMode modelVlanMode = terminationPoint.getVlanMode();
247 portVlanMode.add(SouthboundConstants.VLANMODES.values()[modelVlanMode.getIntValue() - 1].getMode());
248 port.setVlanMode(portVlanMode);
252 private void createPortOtherConfig(
253 final OvsdbTerminationPointAugmentation terminationPoint,
254 final Port ovsPort) {
255 List<PortOtherConfigs> portOtherConfigs =
256 terminationPoint.getPortOtherConfigs();
257 if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
258 HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
259 for (PortOtherConfigs portOtherConfig : portOtherConfigs) {
260 otherConfigsMap.put(portOtherConfig.getOtherConfigKey(),
261 portOtherConfig.getOtherConfigValue());
264 ovsPort.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
265 } catch (NullPointerException e) {
266 LOG.warn("Incomplete OVSDB port other_config", e);
271 private OvsdbBridgeAugmentation getBridge(InstanceIdentifier<?> key) {
272 InstanceIdentifier<Node> nodeIid = key.firstIdentifierOf(Node.class);
273 Map<InstanceIdentifier<Node>, Node> nodes =
274 TransactUtils.extractCreatedOrUpdated(getChanges(),Node.class);
275 if (nodes != null && nodes.get(nodeIid) != null) {
276 Node node = nodes.get(nodeIid);
277 OvsdbBridgeAugmentation bridge = node.getAugmentation(OvsdbBridgeAugmentation.class);
278 if (bridge != null) {