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