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 ovsInterface.setName(terminationPoint.getName());
70 ovsInterface.setType(SouthboundMapper.createOvsdbInterfaceType(terminationPoint.getInterfaceType()));
71 Long ofPort = terminationPoint.getOfport();
73 ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
75 Integer ofPortRequest = terminationPoint.getOfportRequest();
76 if (ofPortRequest != null) {
77 ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest.longValue()));
80 //Configure optional input
81 if (terminationPoint.getOptions() != null) {
82 HashMap<String, String> optionsMap = new HashMap<String, String>();
83 for (Options option : terminationPoint.getOptions()) {
84 optionsMap.put(option.getOption(), option.getValue());
87 ovsInterface.setOptions(ImmutableMap.copyOf(optionsMap));
88 } catch (NullPointerException e) {
89 LOG.warn("Incomplete OVSDB interface options");
93 createInterfaceOtherConfig(terminationPoint, ovsInterface);
95 List<InterfaceExternalIds> interfaceExternalIds =
96 terminationPoint.getInterfaceExternalIds();
97 if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
98 HashMap<String, String> externalIdsMap = new HashMap<String, String>();
99 for (InterfaceExternalIds externalId: interfaceExternalIds) {
100 externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
103 ovsInterface.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
104 } catch (NullPointerException e) {
105 LOG.warn("Incomplete OVSDB interface external_ids");
108 transaction.add(op.insert(ovsInterface).withId(interfaceUuid));
110 // Configure port with the above interface details
111 String portUuid = "Port_" + SouthboundMapper.getRandomUUID();;
112 Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
113 port.setName(terminationPoint.getName());
114 port.setInterfaces(Sets.newHashSet(new UUID(interfaceUuid)));
115 createPortOtherConfig(terminationPoint, port);
116 if (terminationPoint.getVlanTag() != null) {
117 Set<Long> vlanTag = new HashSet<Long>();
118 vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
119 port.setTag(vlanTag);
121 if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
122 Set<Long> portTrunks = new HashSet<Long>();
123 List<Trunks> modelTrunks = terminationPoint.getTrunks();
124 for (Trunks trunk: modelTrunks) {
125 if (trunk.getTrunk() != null) {
126 portTrunks.add(trunk.getTrunk().getValue().longValue());
129 port.setTrunks(portTrunks);
131 if (terminationPoint.getVlanMode() != null) {
132 Set<String> portVlanMode = new HashSet<String>();
133 VlanMode modelVlanMode = terminationPoint.getVlanMode();
134 portVlanMode.add(SouthboundConstants.VLANMODES.values()[modelVlanMode.getIntValue() - 1].getMode());
135 port.setVlanMode(portVlanMode);
138 List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
139 if (portExternalIds != null && !portExternalIds.isEmpty()) {
140 HashMap<String, String> externalIdsMap = new HashMap<String, String>();
141 for (PortExternalIds externalId: portExternalIds) {
142 externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
145 port.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
146 } catch (NullPointerException e) {
147 LOG.warn("Incomplete OVSDB port external_ids");
150 transaction.add(op.insert(port).withId(portUuid));
152 //Configure bridge with the above port details
153 Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
154 bridge.setName(getBridge(entry.getKey()).getBridgeName().getValue());
155 bridge.setPorts(Sets.newHashSet(new UUID(portUuid)));
157 transaction.add(op.mutate(bridge)
158 .addMutation(bridge.getPortsColumn().getSchema(),
159 Mutator.INSERT,bridge.getPortsColumn().getData())
160 .where(bridge.getNameColumn().getSchema()
161 .opEqual(bridge.getNameColumn().getData())).build());
167 private void createInterfaceOtherConfig(
168 OvsdbTerminationPointAugmentation terminationPoint,
169 Interface ovsInterface) {
170 List<InterfaceOtherConfigs> interfaceOtherConfigs =
171 terminationPoint.getInterfaceOtherConfigs();
172 if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
173 HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
174 for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) {
175 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
176 interfaceOtherConfig.getOtherConfigValue());
179 ovsInterface.setOtherConfig(otherConfigsMap);
180 } catch (NullPointerException e) {
181 LOG.warn("Incomplete OVSDB interface other_config", e);
186 private void createPortOtherConfig(
187 OvsdbTerminationPointAugmentation terminationPoint,
189 List<PortOtherConfigs> portOtherConfigs =
190 terminationPoint.getPortOtherConfigs();
191 if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
192 HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
193 for (PortOtherConfigs portOtherConfig : portOtherConfigs) {
194 otherConfigsMap.put(portOtherConfig.getOtherConfigKey(),
195 portOtherConfig.getOtherConfigValue());
198 ovsPort.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
199 } catch (NullPointerException e) {
200 LOG.warn("Incomplete OVSDB port other_config", e);
204 private OvsdbBridgeAugmentation getBridge(InstanceIdentifier<?> key) {
205 InstanceIdentifier<Node> nodeIid = key.firstIdentifierOf(Node.class);
206 Map<InstanceIdentifier<Node>, Node> nodes =
207 TransactUtils.extractCreatedOrUpdated(getChanges(),Node.class);
208 if (nodes != null && nodes.get(nodeIid) != null) {
209 Node node = nodes.get(nodeIid);
210 OvsdbBridgeAugmentation bridge = node.getAugmentation(OvsdbBridgeAugmentation.class);
211 if (bridge != null) {