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.InterfaceTypeBase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceOtherConfigs;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortOtherConfigs;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Trunks;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 import com.google.common.base.Optional;
47 import com.google.common.collect.ImmutableMap;
48 import com.google.common.collect.Sets;
50 public class TerminationPointCreateCommand extends AbstractTransactCommand {
52 private static final Logger LOG = LoggerFactory.getLogger(TerminationPointCreateCommand.class);
54 public TerminationPointCreateCommand(BridgeOperationalState state,
55 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
56 super(state, changes);
60 public void execute(TransactionBuilder transaction) {
61 for (Entry<InstanceIdentifier<?>, DataObject> entry: getChanges().getCreatedData().entrySet()) {
62 DataObject dataObject = entry.getValue();
63 if (dataObject instanceof OvsdbTerminationPointAugmentation) {
64 OvsdbTerminationPointAugmentation terminationPoint = (OvsdbTerminationPointAugmentation) dataObject;
65 LOG.debug("Received request to create termination point {}",
66 terminationPoint.getName());
67 InstanceIdentifier terminationPointIid = entry.getKey();
68 Optional<TerminationPoint> terminationPointOptional =
69 getOperationalState().getBridgeTerminationPoint(terminationPointIid);
70 if (!terminationPointOptional.isPresent()) {
71 // Configure interface
72 String interfaceUuid = "Interface_" + SouthboundMapper.getRandomUUID();;
73 Interface ovsInterface =
74 TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
75 createInterface(terminationPoint, ovsInterface);
76 transaction.add(op.insert(ovsInterface).withId(interfaceUuid));
78 // Configure port with the above interface details
79 String portUuid = "Port_" + SouthboundMapper.getRandomUUID();
80 Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
81 createPort(terminationPoint, port, interfaceUuid);
82 transaction.add(op.insert(port).withId(portUuid));
84 //Configure bridge with the above port details
85 Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
86 bridge.setName(getBridge(entry.getKey()).getBridgeName().getValue());
87 bridge.setPorts(Sets.newHashSet(new UUID(portUuid)));
89 transaction.add(op.mutate(bridge)
90 .addMutation(bridge.getPortsColumn().getSchema(),
91 Mutator.INSERT,bridge.getPortsColumn().getData())
92 .where(bridge.getNameColumn().getSchema()
93 .opEqual(bridge.getNameColumn().getData())).build());
100 private void createInterface(
101 final OvsdbTerminationPointAugmentation terminationPoint,
102 final Interface ovsInterface) {
103 ovsInterface.setName(terminationPoint.getName());
105 createInterfaceType(terminationPoint, ovsInterface);
106 createOfPort(terminationPoint, ovsInterface);
107 createOfPortRequest(terminationPoint, ovsInterface);
108 createInterfaceOptions(terminationPoint, ovsInterface);
109 createInterfaceOtherConfig(terminationPoint, ovsInterface);
110 createInterfaceExternalIds(terminationPoint, ovsInterface);
113 private void createInterfaceType(final OvsdbTerminationPointAugmentation terminationPoint,
114 final Interface ovsInterface) {
116 Class<? extends InterfaceTypeBase> mdsaltype = terminationPoint.getInterfaceType();
117 if (mdsaltype != null) {
118 ovsInterface.setType(SouthboundMapper.createOvsdbInterfaceType(mdsaltype));
122 private void createPort(
123 final OvsdbTerminationPointAugmentation terminationPoint,
124 final Port port, final String interfaceUuid) {
126 port.setName(terminationPoint.getName());
127 port.setInterfaces(Sets.newHashSet(new UUID(interfaceUuid)));
128 createPortOtherConfig(terminationPoint, port);
129 createPortVlanTag(terminationPoint, port);
130 createPortVlanTrunk(terminationPoint, port);
131 createPortVlanMode(terminationPoint, port);
132 createPortExternalIds(terminationPoint, port);
135 private void createOfPort(
136 final OvsdbTerminationPointAugmentation terminationPoint,
137 final Interface ovsInterface) {
139 Long ofPort = terminationPoint.getOfport();
140 if (ofPort != null) {
141 ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
145 private void createOfPortRequest(
146 final OvsdbTerminationPointAugmentation terminationPoint,
147 final Interface ovsInterface) {
149 Integer ofPortRequest = terminationPoint.getOfportRequest();
150 if (ofPortRequest != null) {
151 ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest.longValue()));
155 private void createInterfaceOptions(
156 final OvsdbTerminationPointAugmentation terminationPoint,
157 final Interface ovsInterface) {
159 //Configure optional input
160 if (terminationPoint.getOptions() != null) {
161 HashMap<String, String> optionsMap = new HashMap<String, String>();
162 for (Options option : terminationPoint.getOptions()) {
163 optionsMap.put(option.getOption(), option.getValue());
166 ovsInterface.setOptions(ImmutableMap.copyOf(optionsMap));
167 } catch (NullPointerException e) {
168 LOG.warn("Incomplete OVSDB interface options");
173 private void createInterfaceExternalIds(
174 final OvsdbTerminationPointAugmentation terminationPoint,
175 final Interface ovsInterface) {
177 List<InterfaceExternalIds> interfaceExternalIds =
178 terminationPoint.getInterfaceExternalIds();
179 if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
180 HashMap<String, String> externalIdsMap = new HashMap<String, String>();
181 for (InterfaceExternalIds externalId: interfaceExternalIds) {
182 externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
185 ovsInterface.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
186 } catch (NullPointerException e) {
187 LOG.warn("Incomplete OVSDB interface external_ids");
192 private void createInterfaceOtherConfig(
193 final OvsdbTerminationPointAugmentation terminationPoint,
194 final Interface ovsInterface) {
196 List<InterfaceOtherConfigs> interfaceOtherConfigs =
197 terminationPoint.getInterfaceOtherConfigs();
198 if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
199 HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
200 for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) {
201 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
202 interfaceOtherConfig.getOtherConfigValue());
205 ovsInterface.setOtherConfig(otherConfigsMap);
206 } catch (NullPointerException e) {
207 LOG.warn("Incomplete OVSDB interface other_config", e);
212 private void createPortExternalIds(
213 final OvsdbTerminationPointAugmentation terminationPoint,
216 List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
217 if (portExternalIds != null && !portExternalIds.isEmpty()) {
218 HashMap<String, String> externalIdsMap = new HashMap<String, String>();
219 for (PortExternalIds externalId: portExternalIds) {
220 externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
223 port.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
224 } catch (NullPointerException e) {
225 LOG.warn("Incomplete OVSDB port external_ids");
230 private void createPortVlanTag(
231 final OvsdbTerminationPointAugmentation terminationPoint,
234 if (terminationPoint.getVlanTag() != null) {
235 Set<Long> vlanTag = new HashSet<Long>();
236 vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
237 port.setTag(vlanTag);
241 private void createPortVlanTrunk(
242 final OvsdbTerminationPointAugmentation terminationPoint,
245 if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
246 Set<Long> portTrunks = new HashSet<Long>();
247 List<Trunks> modelTrunks = terminationPoint.getTrunks();
248 for (Trunks trunk: modelTrunks) {
249 if (trunk.getTrunk() != null) {
250 portTrunks.add(trunk.getTrunk().getValue().longValue());
253 port.setTrunks(portTrunks);
257 private void createPortVlanMode(
258 final OvsdbTerminationPointAugmentation terminationPoint,
260 if (terminationPoint.getVlanMode() != null) {
261 Set<String> portVlanMode = new HashSet<String>();
262 VlanMode modelVlanMode = terminationPoint.getVlanMode();
263 portVlanMode.add(SouthboundConstants.VLANMODES.values()[modelVlanMode.getIntValue() - 1].getMode());
264 port.setVlanMode(portVlanMode);
268 private void createPortOtherConfig(
269 final OvsdbTerminationPointAugmentation terminationPoint,
270 final Port ovsPort) {
271 List<PortOtherConfigs> portOtherConfigs =
272 terminationPoint.getPortOtherConfigs();
273 if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
274 HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
275 for (PortOtherConfigs portOtherConfig : portOtherConfigs) {
276 otherConfigsMap.put(portOtherConfig.getOtherConfigKey(),
277 portOtherConfig.getOtherConfigValue());
280 ovsPort.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
281 } catch (NullPointerException e) {
282 LOG.warn("Incomplete OVSDB port other_config", e);
287 private OvsdbBridgeAugmentation getBridge(InstanceIdentifier<?> key) {
288 InstanceIdentifier<Node> nodeIid = key.firstIdentifierOf(Node.class);
289 Map<InstanceIdentifier<Node>, Node> nodes =
290 TransactUtils.extractCreatedOrUpdated(getChanges(),Node.class);
291 if (nodes != null && nodes.get(nodeIid) != null) {
292 Node node = nodes.get(nodeIid);
293 OvsdbBridgeAugmentation bridge = node.getAugmentation(OvsdbBridgeAugmentation.class);
294 if (bridge != null) {