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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.google.common.base.Optional;
46 import com.google.common.collect.ImmutableMap;
47 import com.google.common.collect.Sets;
49 public class TerminationPointCreateCommand extends AbstractTransactCommand {
51 private static final Logger LOG = LoggerFactory.getLogger(TerminationPointCreateCommand.class);
53 public TerminationPointCreateCommand(BridgeOperationalState state,
54 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
55 super(state, changes);
59 public void execute(TransactionBuilder transaction) {
60 for (Entry<InstanceIdentifier<?>, DataObject> entry: getChanges().getCreatedData().entrySet()) {
61 DataObject dataObject = entry.getValue();
62 if (dataObject instanceof OvsdbTerminationPointAugmentation) {
63 OvsdbTerminationPointAugmentation terminationPoint = (OvsdbTerminationPointAugmentation) dataObject;
64 LOG.debug("Received request to create termination point {}",
65 terminationPoint.getName());
66 InstanceIdentifier terminationPointIid = entry.getKey();
67 Optional<TerminationPoint> terminationPointOptional =
68 getOperationalState().getBridgeTerminationPoint(terminationPointIid);
69 if (!terminationPointOptional.isPresent()) {
70 // Configure interface
71 String interfaceUuid = "Interface_" + SouthboundMapper.getRandomUUID();;
72 Interface ovsInterface =
73 TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
74 createInterface(terminationPoint, ovsInterface);
75 transaction.add(op.insert(ovsInterface).withId(interfaceUuid));
77 // Configure port with the above interface details
78 String portUuid = "Port_" + SouthboundMapper.getRandomUUID();
79 Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
80 createPort(terminationPoint, port, interfaceUuid);
81 transaction.add(op.insert(port).withId(portUuid));
83 //Configure bridge with the above port details
84 Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
85 bridge.setName(getBridge(entry.getKey()).getBridgeName().getValue());
86 bridge.setPorts(Sets.newHashSet(new UUID(portUuid)));
88 transaction.add(op.mutate(bridge)
89 .addMutation(bridge.getPortsColumn().getSchema(),
90 Mutator.INSERT,bridge.getPortsColumn().getData())
91 .where(bridge.getNameColumn().getSchema()
92 .opEqual(bridge.getNameColumn().getData())).build());
99 private void createInterface(
100 final OvsdbTerminationPointAugmentation terminationPoint,
101 final Interface ovsInterface) {
102 ovsInterface.setName(terminationPoint.getName());
103 ovsInterface.setType(SouthboundMapper.createOvsdbInterfaceType(terminationPoint.getInterfaceType()));
105 createOfPort(terminationPoint, ovsInterface);
106 createOfPortRequest(terminationPoint, ovsInterface);
107 createInterfaceOptions(terminationPoint, ovsInterface);
108 createInterfaceOtherConfig(terminationPoint, ovsInterface);
109 createInterfaceExternalIds(terminationPoint, ovsInterface);
112 private void createPort(
113 final OvsdbTerminationPointAugmentation terminationPoint,
114 final Port port, final String interfaceUuid) {
116 port.setName(terminationPoint.getName());
117 port.setInterfaces(Sets.newHashSet(new UUID(interfaceUuid)));
118 createPortOtherConfig(terminationPoint, port);
119 createPortVlanTag(terminationPoint, port);
120 createPortVlanTrunk(terminationPoint, port);
121 createPortVlanMode(terminationPoint, port);
122 createPortExternalIds(terminationPoint, port);
125 private void createOfPort(
126 final OvsdbTerminationPointAugmentation terminationPoint,
127 final Interface ovsInterface) {
129 Long ofPort = terminationPoint.getOfport();
130 if (ofPort != null) {
131 ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
135 private void createOfPortRequest(
136 final OvsdbTerminationPointAugmentation terminationPoint,
137 final Interface ovsInterface) {
139 Integer ofPortRequest = terminationPoint.getOfportRequest();
140 if (ofPortRequest != null) {
141 ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest.longValue()));
145 private void createInterfaceOptions(
146 final OvsdbTerminationPointAugmentation terminationPoint,
147 final Interface ovsInterface) {
149 //Configure optional input
150 if (terminationPoint.getOptions() != null) {
151 HashMap<String, String> optionsMap = new HashMap<String, String>();
152 for (Options option : terminationPoint.getOptions()) {
153 optionsMap.put(option.getOption(), option.getValue());
156 ovsInterface.setOptions(ImmutableMap.copyOf(optionsMap));
157 } catch (NullPointerException e) {
158 LOG.warn("Incomplete OVSDB interface options");
163 private void createInterfaceExternalIds(
164 final OvsdbTerminationPointAugmentation terminationPoint,
165 final Interface ovsInterface) {
167 List<InterfaceExternalIds> interfaceExternalIds =
168 terminationPoint.getInterfaceExternalIds();
169 if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
170 HashMap<String, String> externalIdsMap = new HashMap<String, String>();
171 for (InterfaceExternalIds externalId: interfaceExternalIds) {
172 externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
175 ovsInterface.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
176 } catch (NullPointerException e) {
177 LOG.warn("Incomplete OVSDB interface external_ids");
182 private void createInterfaceOtherConfig(
183 final OvsdbTerminationPointAugmentation terminationPoint,
184 final Interface ovsInterface) {
186 List<InterfaceOtherConfigs> interfaceOtherConfigs =
187 terminationPoint.getInterfaceOtherConfigs();
188 if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
189 HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
190 for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) {
191 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
192 interfaceOtherConfig.getOtherConfigValue());
195 ovsInterface.setOtherConfig(otherConfigsMap);
196 } catch (NullPointerException e) {
197 LOG.warn("Incomplete OVSDB interface other_config", e);
202 private void createPortExternalIds(
203 final OvsdbTerminationPointAugmentation terminationPoint,
206 List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
207 if (portExternalIds != null && !portExternalIds.isEmpty()) {
208 HashMap<String, String> externalIdsMap = new HashMap<String, String>();
209 for (PortExternalIds externalId: portExternalIds) {
210 externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
213 port.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
214 } catch (NullPointerException e) {
215 LOG.warn("Incomplete OVSDB port external_ids");
220 private void createPortVlanTag(
221 final OvsdbTerminationPointAugmentation terminationPoint,
224 if (terminationPoint.getVlanTag() != null) {
225 Set<Long> vlanTag = new HashSet<Long>();
226 vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
227 port.setTag(vlanTag);
231 private void createPortVlanTrunk(
232 final OvsdbTerminationPointAugmentation terminationPoint,
235 if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
236 Set<Long> portTrunks = new HashSet<Long>();
237 List<Trunks> modelTrunks = terminationPoint.getTrunks();
238 for (Trunks trunk: modelTrunks) {
239 if (trunk.getTrunk() != null) {
240 portTrunks.add(trunk.getTrunk().getValue().longValue());
243 port.setTrunks(portTrunks);
247 private void createPortVlanMode(
248 final OvsdbTerminationPointAugmentation terminationPoint,
250 if (terminationPoint.getVlanMode() != null) {
251 Set<String> portVlanMode = new HashSet<String>();
252 VlanMode modelVlanMode = terminationPoint.getVlanMode();
253 portVlanMode.add(SouthboundConstants.VLANMODES.values()[modelVlanMode.getIntValue() - 1].getMode());
254 port.setVlanMode(portVlanMode);
258 private void createPortOtherConfig(
259 final OvsdbTerminationPointAugmentation terminationPoint,
260 final Port ovsPort) {
261 List<PortOtherConfigs> portOtherConfigs =
262 terminationPoint.getPortOtherConfigs();
263 if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
264 HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
265 for (PortOtherConfigs portOtherConfig : portOtherConfigs) {
266 otherConfigsMap.put(portOtherConfig.getOtherConfigKey(),
267 portOtherConfig.getOtherConfigValue());
270 ovsPort.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
271 } catch (NullPointerException e) {
272 LOG.warn("Incomplete OVSDB port other_config", e);
277 private OvsdbBridgeAugmentation getBridge(InstanceIdentifier<?> key) {
278 InstanceIdentifier<Node> nodeIid = key.firstIdentifierOf(Node.class);
279 Map<InstanceIdentifier<Node>, Node> nodes =
280 TransactUtils.extractCreatedOrUpdated(getChanges(),Node.class);
281 if (nodes != null && nodes.get(nodeIid) != null) {
282 Node node = nodes.get(nodeIid);
283 OvsdbBridgeAugmentation bridge = node.getAugmentation(OvsdbBridgeAugmentation.class);
284 if (bridge != null) {