Use a utility function for key-value to map conversions
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / TerminationPointUpdateCommand.java
1 /*
2  * Copyright (c) 2015, 2016 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.ovsdb.southbound.ovsdb.transact;
9
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
11
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
18
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
20 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
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.Interface;
25 import org.opendaylight.ovsdb.schema.openvswitch.Port;
26 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
27 import org.opendaylight.ovsdb.utils.yang.YangUtils;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceLldp;
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.TerminationPoint;
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;
43
44 import com.google.common.collect.Sets;
45
46 public class TerminationPointUpdateCommand extends AbstractTransactCommand {
47
48     private static final Logger LOG = LoggerFactory.getLogger(TerminationPointUpdateCommand.class);
49
50     public TerminationPointUpdateCommand(BridgeOperationalState state,
51             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
52         super(state, changes);
53     }
54
55     @Override
56     public void execute(TransactionBuilder transaction) {
57         LOG.trace("TerminationPointUpdateCommand called");
58         Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> created =
59             TransactUtils.extractCreated(getChanges(),OvsdbTerminationPointAugmentation.class);
60         for (Entry<InstanceIdentifier<OvsdbTerminationPointAugmentation>,
61                  OvsdbTerminationPointAugmentation> terminationPointEntry : created.entrySet()) {
62             updateTerminationPoint(transaction, terminationPointEntry.getKey(), terminationPointEntry.getValue());
63         }
64         Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> updated =
65                 TransactUtils.extractUpdated(getChanges(), OvsdbTerminationPointAugmentation.class);
66         for (Entry<InstanceIdentifier<OvsdbTerminationPointAugmentation>,
67                  OvsdbTerminationPointAugmentation> terminationPointEntry : updated.entrySet()) {
68             updateTerminationPoint(transaction, terminationPointEntry.getKey(),
69                     terminationPointEntry.getValue());
70         }
71     }
72
73     public void updateTerminationPoint(TransactionBuilder transaction,
74             InstanceIdentifier<OvsdbTerminationPointAugmentation> iid,
75             OvsdbTerminationPointAugmentation terminationPoint) {
76         if (terminationPoint instanceof OvsdbTerminationPointAugmentation) {
77             LOG.debug("Received request to update termination point {}",
78                     terminationPoint.getName());
79
80             // Update interface
81             Interface ovsInterface =
82                     TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
83             updateInterface(terminationPoint, ovsInterface);
84             Interface extraInterface = TyperUtils.getTypedRowWrapper(
85                     transaction.getDatabaseSchema(), Interface.class);
86             extraInterface.setName("");
87             transaction.add(op.update(ovsInterface)
88                     .where(extraInterface.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
89                     .build());
90
91             TerminationPointCreateCommand.stampInstanceIdentifier(transaction,
92                     iid.firstIdentifierOf(TerminationPoint.class), terminationPoint.getName());
93
94             // Update port
95             Port port = TyperUtils.getTypedRowWrapper(
96                     transaction.getDatabaseSchema(), Port.class);
97             updatePort(terminationPoint,port);
98             Port extraPort = TyperUtils.getTypedRowWrapper(
99                     transaction.getDatabaseSchema(), Port.class);
100             extraPort.setName("");
101             transaction.add(op.update(port)
102                     .where(extraPort.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
103                     .build());
104         }
105     }
106
107     private void updateInterface(
108             final OvsdbTerminationPointAugmentation terminationPoint,
109             final Interface ovsInterface) {
110         updateOfPort(terminationPoint, ovsInterface);
111         updateOfPortRequest(terminationPoint, ovsInterface);
112         updateInterfaceOptions(terminationPoint, ovsInterface);
113         updateInterfaceOtherConfig(terminationPoint, ovsInterface);
114         updateInterfaceExternalIds(terminationPoint, ovsInterface);
115         updateInterfaceLldp(terminationPoint, ovsInterface);
116     }
117
118     private void updatePort(
119             final OvsdbTerminationPointAugmentation terminationPoint,
120             final Port port) {
121
122         updatePortOtherConfig(terminationPoint, port);
123         updatePortVlanTag(terminationPoint, port);
124         updatePortVlanTrunk(terminationPoint, port);
125         updatePortVlanMode(terminationPoint, port);
126         updatePortExternalIds(terminationPoint, port);
127         updatePortQos(terminationPoint, port);
128     }
129
130     private void updatePortQos(
131             final OvsdbTerminationPointAugmentation terminationPoint,
132             final Port port) {
133
134         Set<UUID> uuidSet = Sets.newHashSet();
135         Uuid qosUuid = terminationPoint.getQos();
136         if (qosUuid != null) {
137             uuidSet.add(new UUID(qosUuid.getValue()));
138         }
139         port.setQos(uuidSet);
140     }
141
142
143     private void updateOfPort(
144             final OvsdbTerminationPointAugmentation terminationPoint,
145             final Interface ovsInterface) {
146
147         Long ofPort = terminationPoint.getOfport();
148         if (ofPort != null) {
149             ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
150         }
151     }
152
153     private void updateOfPortRequest(
154             final OvsdbTerminationPointAugmentation terminationPoint,
155             final Interface ovsInterface) {
156
157         Integer ofPortRequest = terminationPoint.getOfportRequest();
158         if (ofPortRequest != null) {
159             ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest.longValue()));
160         }
161     }
162
163     private void updateInterfaceOptions(
164             final OvsdbTerminationPointAugmentation terminationPoint,
165             final Interface ovsInterface) {
166
167         //Configure optional input
168         if (terminationPoint.getOptions() != null) {
169             try {
170                 ovsInterface.setOptions(YangUtils.convertYangKeyValueListToMap(terminationPoint.getOptions(),
171                         Options::getOption, Options::getValue));
172             } catch (NullPointerException e) {
173                 LOG.warn("Incomplete OVSDB interface options", e);
174             }
175         }
176     }
177
178     private void updateInterfaceExternalIds(
179             final OvsdbTerminationPointAugmentation terminationPoint,
180             final Interface ovsInterface) {
181
182         List<InterfaceExternalIds> interfaceExternalIds =
183                 terminationPoint.getInterfaceExternalIds();
184         if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
185             try {
186                 ovsInterface.setExternalIds(YangUtils.convertYangKeyValueListToMap(interfaceExternalIds,
187                         InterfaceExternalIds::getExternalIdKey, InterfaceExternalIds::getExternalIdValue));
188             } catch (NullPointerException e) {
189                 LOG.warn("Incomplete OVSDB interface external_ids", e);
190             }
191         }
192     }
193
194     private void updateInterfaceLldp(
195             final OvsdbTerminationPointAugmentation terminationPoint,
196             final Interface ovsInterface) {
197
198         try {
199             List<InterfaceLldp> interfaceLldpList =
200                     terminationPoint.getInterfaceLldp();
201             if (interfaceLldpList != null && !interfaceLldpList.isEmpty()) {
202                 try {
203                     ovsInterface.setLldp(YangUtils.convertYangKeyValueListToMap(interfaceLldpList,
204                             InterfaceLldp::getLldpKey, InterfaceLldp::getLldpValue));
205                 } catch (NullPointerException e) {
206                     LOG.warn("Incomplete OVSDB interface lldp", e);
207                 }
208             }
209         } catch (SchemaVersionMismatchException e) {
210             LOG.debug("lldp column for Interface Table unsupported for this version of ovsdb schema", e);
211         }
212     }
213
214     private void updateInterfaceOtherConfig(
215             final OvsdbTerminationPointAugmentation terminationPoint,
216             final Interface ovsInterface) {
217
218         List<InterfaceOtherConfigs> interfaceOtherConfigs =
219                 terminationPoint.getInterfaceOtherConfigs();
220         if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
221             Map<String, String> otherConfigsMap = new HashMap<>();
222             for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) {
223                 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
224                         interfaceOtherConfig.getOtherConfigValue());
225             }
226             try {
227                 ovsInterface.setOtherConfig(otherConfigsMap);
228             } catch (NullPointerException e) {
229                 LOG.warn("Incomplete OVSDB interface other_config", e);
230             }
231         }
232     }
233
234     private void updatePortExternalIds(
235             final OvsdbTerminationPointAugmentation terminationPoint,
236             final Port port) {
237
238         List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
239         if (portExternalIds != null && !portExternalIds.isEmpty()) {
240             try {
241                 port.setExternalIds(YangUtils.convertYangKeyValueListToMap(portExternalIds,
242                         PortExternalIds::getExternalIdKey, PortExternalIds::getExternalIdValue));
243             } catch (NullPointerException e) {
244                 LOG.warn("Incomplete OVSDB port external_ids", e);
245             }
246         }
247     }
248
249     private void updatePortVlanTag(
250             final OvsdbTerminationPointAugmentation terminationPoint,
251             final Port port) {
252
253         if (terminationPoint.getVlanTag() != null) {
254             Set<Long> vlanTag = new HashSet<>();
255             vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
256             port.setTag(vlanTag);
257         }
258     }
259
260     private void updatePortVlanTrunk(
261             final OvsdbTerminationPointAugmentation terminationPoint,
262             final Port port) {
263
264         if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
265             Set<Long> portTrunks = new HashSet<>();
266             List<Trunks> modelTrunks = terminationPoint.getTrunks();
267             for (Trunks trunk: modelTrunks) {
268                 if (trunk.getTrunk() != null) {
269                     portTrunks.add(trunk.getTrunk().getValue().longValue());
270                 }
271             }
272             port.setTrunks(portTrunks);
273         }
274     }
275
276     private void updatePortVlanMode(
277             final OvsdbTerminationPointAugmentation terminationPoint,
278             final Port port) {
279         if (terminationPoint.getVlanMode() != null) {
280             Set<String> portVlanMode = new HashSet<>();
281             VlanMode modelVlanMode = terminationPoint.getVlanMode();
282             portVlanMode.add(SouthboundConstants.VLANMODES.values()[modelVlanMode.getIntValue() - 1].getMode());
283             port.setVlanMode(portVlanMode);
284         }
285     }
286
287     private void updatePortOtherConfig(
288             final OvsdbTerminationPointAugmentation terminationPoint,
289             final Port ovsPort) {
290         List<PortOtherConfigs> portOtherConfigs =
291                 terminationPoint.getPortOtherConfigs();
292         if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
293             try {
294                 ovsPort.setOtherConfig(YangUtils.convertYangKeyValueListToMap(portOtherConfigs,
295                         PortOtherConfigs::getOtherConfigKey, PortOtherConfigs::getOtherConfigValue));
296             } catch (NullPointerException e) {
297                 LOG.warn("Incomplete OVSDB port other_config", e);
298             }
299         }
300     }
301
302 }