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.operations.TransactionBuilder;
21 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
22 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
23 import org.opendaylight.ovsdb.schema.openvswitch.Port;
24 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceOtherConfigs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortOtherConfigs;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Trunks;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.common.collect.ImmutableMap;
39 import com.google.common.collect.Sets;
41 public class TerminationPointUpdateCommand extends AbstractTransactCommand {
43 private static final Logger LOG = LoggerFactory.getLogger(TerminationPointUpdateCommand.class);
45 public TerminationPointUpdateCommand(BridgeOperationalState state,
46 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
47 super(state, changes);
51 public void execute(TransactionBuilder transaction) {
52 LOG.trace("TerminationPointUpdateCommand called");
53 Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> created =
54 TransactUtils.extractCreated(getChanges(),OvsdbTerminationPointAugmentation.class);
55 for (Entry<InstanceIdentifier<OvsdbTerminationPointAugmentation>,
56 OvsdbTerminationPointAugmentation> terminationPointEntry : created.entrySet()) {
57 updateTerminationPoint(transaction, terminationPointEntry.getKey(), terminationPointEntry.getValue());
59 Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> updated =
60 TransactUtils.extractUpdated(getChanges(), OvsdbTerminationPointAugmentation.class);
61 for (Entry<InstanceIdentifier<OvsdbTerminationPointAugmentation>,
62 OvsdbTerminationPointAugmentation> terminationPointEntry : updated.entrySet()) {
63 updateTerminationPoint(transaction, terminationPointEntry.getKey(),
64 terminationPointEntry.getValue());
68 public void updateTerminationPoint(TransactionBuilder transaction,
69 InstanceIdentifier<OvsdbTerminationPointAugmentation> iid,
70 OvsdbTerminationPointAugmentation terminationPoint) {
71 if (terminationPoint instanceof OvsdbTerminationPointAugmentation) {
72 LOG.debug("Received request to update termination point {}",
73 terminationPoint.getName());
76 Interface ovsInterface =
77 TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
78 updateInterface(terminationPoint, ovsInterface);
79 Interface extraInterface = TyperUtils.getTypedRowWrapper(
80 transaction.getDatabaseSchema(), Interface.class);
81 extraInterface.setName("");
82 transaction.add(op.update(ovsInterface)
83 .where(extraInterface.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
87 Port port = TyperUtils.getTypedRowWrapper(
88 transaction.getDatabaseSchema(), Port.class);
89 updatePort(terminationPoint,port);
90 Port extraPort = TyperUtils.getTypedRowWrapper(
91 transaction.getDatabaseSchema(), Port.class);
92 extraPort.setName("");
93 transaction.add(op.update(port)
94 .where(extraPort.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
99 private void updateInterface(
100 final OvsdbTerminationPointAugmentation terminationPoint,
101 final Interface ovsInterface) {
102 updateOfPort(terminationPoint, ovsInterface);
103 updateOfPortRequest(terminationPoint, ovsInterface);
104 updateInterfaceOptions(terminationPoint, ovsInterface);
105 updateInterfaceOtherConfig(terminationPoint, ovsInterface);
106 updateInterfaceExternalIds(terminationPoint, ovsInterface);
109 private void updatePort(
110 final OvsdbTerminationPointAugmentation terminationPoint,
113 updatePortOtherConfig(terminationPoint, port);
114 updatePortVlanTag(terminationPoint, port);
115 updatePortVlanTrunk(terminationPoint, port);
116 updatePortVlanMode(terminationPoint, port);
117 updatePortExternalIds(terminationPoint, port);
120 private void updateOfPort(
121 final OvsdbTerminationPointAugmentation terminationPoint,
122 final Interface ovsInterface) {
124 Long ofPort = terminationPoint.getOfport();
125 if (ofPort != null) {
126 ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
130 private void updateOfPortRequest(
131 final OvsdbTerminationPointAugmentation terminationPoint,
132 final Interface ovsInterface) {
134 Integer ofPortRequest = terminationPoint.getOfportRequest();
135 if (ofPortRequest != null) {
136 ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest.longValue()));
140 private void updateInterfaceOptions(
141 final OvsdbTerminationPointAugmentation terminationPoint,
142 final Interface ovsInterface) {
144 //Configure optional input
145 if (terminationPoint.getOptions() != null) {
146 HashMap<String, String> optionsMap = new HashMap<String, String>();
147 for (Options option : terminationPoint.getOptions()) {
148 optionsMap.put(option.getOption(), option.getValue());
151 ovsInterface.setOptions(ImmutableMap.copyOf(optionsMap));
152 } catch (NullPointerException e) {
153 LOG.warn("Incomplete OVSDB interface options");
158 private void updateInterfaceExternalIds(
159 final OvsdbTerminationPointAugmentation terminationPoint,
160 final Interface ovsInterface) {
162 List<InterfaceExternalIds> interfaceExternalIds =
163 terminationPoint.getInterfaceExternalIds();
164 if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
165 HashMap<String, String> externalIdsMap = new HashMap<String, String>();
166 for (InterfaceExternalIds externalId: interfaceExternalIds) {
167 externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
170 ovsInterface.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
171 } catch (NullPointerException e) {
172 LOG.warn("Incomplete OVSDB interface external_ids");
177 private void updateInterfaceOtherConfig(
178 final OvsdbTerminationPointAugmentation terminationPoint,
179 final Interface ovsInterface) {
181 List<InterfaceOtherConfigs> interfaceOtherConfigs =
182 terminationPoint.getInterfaceOtherConfigs();
183 if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
184 HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
185 for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) {
186 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
187 interfaceOtherConfig.getOtherConfigValue());
190 ovsInterface.setOtherConfig(otherConfigsMap);
191 } catch (NullPointerException e) {
192 LOG.warn("Incomplete OVSDB interface other_config", e);
197 private void updatePortExternalIds(
198 final OvsdbTerminationPointAugmentation terminationPoint,
201 List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
202 if (portExternalIds != null && !portExternalIds.isEmpty()) {
203 HashMap<String, String> externalIdsMap = new HashMap<String, String>();
204 for (PortExternalIds externalId: portExternalIds) {
205 externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
208 port.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
209 } catch (NullPointerException e) {
210 LOG.warn("Incomplete OVSDB port external_ids");
215 private void updatePortVlanTag(
216 final OvsdbTerminationPointAugmentation terminationPoint,
219 if (terminationPoint.getVlanTag() != null) {
220 Set<Long> vlanTag = new HashSet<Long>();
221 vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
222 port.setTag(vlanTag);
226 private void updatePortVlanTrunk(
227 final OvsdbTerminationPointAugmentation terminationPoint,
230 if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
231 Set<Long> portTrunks = new HashSet<Long>();
232 List<Trunks> modelTrunks = terminationPoint.getTrunks();
233 for (Trunks trunk: modelTrunks) {
234 if (trunk.getTrunk() != null) {
235 portTrunks.add(trunk.getTrunk().getValue().longValue());
238 port.setTrunks(portTrunks);
242 private void updatePortVlanMode(
243 final OvsdbTerminationPointAugmentation terminationPoint,
245 if (terminationPoint.getVlanMode() != null) {
246 Set<String> portVlanMode = new HashSet<String>();
247 VlanMode modelVlanMode = terminationPoint.getVlanMode();
248 portVlanMode.add(SouthboundConstants.VLANMODES.values()[modelVlanMode.getIntValue() - 1].getMode());
249 port.setVlanMode(portVlanMode);
253 private void updatePortOtherConfig(
254 final OvsdbTerminationPointAugmentation terminationPoint,
255 final Port ovsPort) {
256 List<PortOtherConfigs> portOtherConfigs =
257 terminationPoint.getPortOtherConfigs();
258 if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
259 HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
260 for (PortOtherConfigs portOtherConfig : portOtherConfigs) {
261 otherConfigsMap.put(portOtherConfig.getOtherConfigKey(),
262 portOtherConfig.getOtherConfigValue());
265 ovsPort.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
266 } catch (NullPointerException e) {
267 LOG.warn("Incomplete OVSDB port other_config", e);