2c83b22cf01828e89e8efdd67e4d08dda211f7dc
[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 import static org.opendaylight.ovsdb.southbound.SouthboundUtil.schemaMismatchLog;
12
13 import com.google.common.base.Optional;
14 import com.google.common.collect.Sets;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.Set;
23 import java.util.concurrent.ExecutionException;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
25 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
29 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
30 import org.opendaylight.ovsdb.lib.notation.UUID;
31 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
32 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
33 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
34 import org.opendaylight.ovsdb.schema.openvswitch.Port;
35 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
36 import org.opendaylight.ovsdb.southbound.SouthboundProvider;
37 import org.opendaylight.ovsdb.utils.yang.YangUtils;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbQosRef;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceBfd;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceLldp;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceOtherConfigs;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortOtherConfigs;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Trunks;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
55 import org.opendaylight.yangtools.yang.binding.DataObject;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 public class TerminationPointUpdateCommand implements TransactCommand {
61
62     private static final Logger LOG = LoggerFactory.getLogger(TerminationPointUpdateCommand.class);
63
64     @Override
65     public void execute(TransactionBuilder transaction, BridgeOperationalState state,
66                         AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> events) {
67         execute(transaction, state,
68                 TransactUtils.extractCreatedOrUpdated(events, OvsdbTerminationPointAugmentation.class));
69     }
70
71     @Override
72     public void execute(TransactionBuilder transaction, BridgeOperationalState state,
73                         Collection<DataTreeModification<Node>> modifications) {
74         execute(transaction, state,
75                 TransactUtils.extractCreatedOrUpdated(modifications, OvsdbTerminationPointAugmentation.class));
76     }
77
78     private void execute(TransactionBuilder transaction, BridgeOperationalState state,
79                          Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>,
80                                  OvsdbTerminationPointAugmentation> createdOrUpdated) {
81         for (Entry<InstanceIdentifier<OvsdbTerminationPointAugmentation>,
82                  OvsdbTerminationPointAugmentation> terminationPointEntry : createdOrUpdated.entrySet()) {
83             updateTerminationPoint(transaction, state, terminationPointEntry.getKey(),
84                     terminationPointEntry.getValue());
85         }
86     }
87
88     public void updateTerminationPoint(TransactionBuilder transaction, BridgeOperationalState state,
89                                        InstanceIdentifier<OvsdbTerminationPointAugmentation> iid,
90                                        OvsdbTerminationPointAugmentation terminationPoint) {
91
92         if (terminationPoint != null) {
93             LOG.debug("Received request to update termination point {}",
94                    terminationPoint.getName());
95
96             // Update interface
97             Interface ovsInterface =
98                     TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
99             updateInterface(terminationPoint, ovsInterface);
100             Interface extraInterface = TyperUtils.getTypedRowWrapper(
101                     transaction.getDatabaseSchema(), Interface.class);
102             extraInterface.setName("");
103             transaction.add(op.update(ovsInterface)
104                     .where(extraInterface.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
105                     .build());
106
107             TerminationPointCreateCommand.stampInstanceIdentifier(transaction,
108                     iid.firstIdentifierOf(OvsdbTerminationPointAugmentation.class), terminationPoint.getName());
109
110             // Update port
111             OvsdbBridgeAugmentation operBridge =
112                     state.getBridgeNode(iid).get().getAugmentation(OvsdbBridgeAugmentation.class);
113             Port port = TyperUtils.getTypedRowWrapper(
114                     transaction.getDatabaseSchema(), Port.class);
115             updatePort(terminationPoint, port, operBridge);
116             Port extraPort = TyperUtils.getTypedRowWrapper(
117                     transaction.getDatabaseSchema(), Port.class);
118             extraPort.setName("");
119             transaction.add(op.update(port)
120                     .where(extraPort.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
121                     .build());
122             LOG.info("Updated Termination Point : {}  with Uuid : {}",
123                     terminationPoint.getName(), terminationPoint.getPortUuid());
124         }
125     }
126
127     private void updateInterface(
128             final OvsdbTerminationPointAugmentation terminationPoint,
129             final Interface ovsInterface) {
130         updateOfPort(terminationPoint, ovsInterface);
131         updateOfPortRequest(terminationPoint, ovsInterface);
132         updateInterfaceOptions(terminationPoint, ovsInterface);
133         updateInterfaceOtherConfig(terminationPoint, ovsInterface);
134         updateInterfaceExternalIds(terminationPoint, ovsInterface);
135         updateInterfaceLldp(terminationPoint, ovsInterface);
136         updateInterfaceBfd(terminationPoint, ovsInterface);
137         updateInterfacePolicing(terminationPoint, ovsInterface);
138     }
139
140     private void updatePort(
141             final OvsdbTerminationPointAugmentation terminationPoint,
142             final Port port,
143             final OvsdbBridgeAugmentation operBridge) {
144
145         updatePortOtherConfig(terminationPoint, port);
146         updatePortVlanTag(terminationPoint, port);
147         updatePortVlanTrunk(terminationPoint, port);
148         updatePortVlanMode(terminationPoint, port);
149         updatePortExternalIds(terminationPoint, port);
150         updatePortQos(terminationPoint, port, operBridge);
151     }
152
153     private void updatePortQos(
154             final OvsdbTerminationPointAugmentation terminationPoint,
155             final Port port,
156             final OvsdbBridgeAugmentation operBridge) {
157
158         Set<UUID> uuidSet = Sets.newHashSet();
159
160         // First check if QosEntry is present and use that
161         if (terminationPoint.getQosEntry() != null && !terminationPoint.getQosEntry().isEmpty()) {
162             OvsdbQosRef qosRef = terminationPoint.getQosEntry().iterator().next().getQosRef();
163             Uri qosId = qosRef.getValue().firstKeyOf(QosEntries.class).getQosId();
164             OvsdbNodeAugmentation operNode = getOperNode(operBridge);
165             if (operNode != null && operNode.getQosEntries() != null
166                     && !operNode.getQosEntries().isEmpty()) {
167                 for (QosEntries qosEntry : operNode.getQosEntries()) {
168                     if (qosEntry.getQosId().equals(qosId)) {
169                         uuidSet.add(new UUID(qosEntry.getQosUuid().getValue()));
170                     }
171                 }
172             }
173             if (uuidSet.size() == 0) {
174                 uuidSet.add(new UUID(SouthboundConstants.QOS_NAMED_UUID_PREFIX
175                         + TransactUtils.bytesToHexString(qosId.getValue().getBytes())));
176             }
177         } else {
178             // Second check if Qos is present and use that (deprecated)
179             // Do not bother to check if QosEntry and Qos are consistent if both are present
180             Uuid qosUuid = terminationPoint.getQos();
181             if (qosUuid != null) {
182                 uuidSet.add(new UUID(qosUuid.getValue()));
183             }
184         }
185         port.setQos(uuidSet);
186     }
187
188     private OvsdbNodeAugmentation getOperNode(final OvsdbBridgeAugmentation operBridge) {
189         @SuppressWarnings("unchecked")
190         InstanceIdentifier<Node> iidNode = (InstanceIdentifier<Node>)operBridge.getManagedBy().getValue();
191         OvsdbNodeAugmentation operNode = null;
192         ReadOnlyTransaction transaction = SouthboundProvider.getDb().newReadOnlyTransaction();
193         CheckedFuture<Optional<Node>, ReadFailedException> future =
194                 transaction.read(LogicalDatastoreType.OPERATIONAL, iidNode);
195         try {
196             Optional<Node> nodeOptional = future.get();
197             if (nodeOptional.isPresent()) {
198                 operNode = nodeOptional.get().getAugmentation(OvsdbNodeAugmentation.class);
199             }
200         } catch (InterruptedException | ExecutionException e) {
201             LOG.warn("Error reading from datastore", e);
202         }
203         return operNode;
204     }
205
206     private void updateOfPort(
207             final OvsdbTerminationPointAugmentation terminationPoint,
208             final Interface ovsInterface) {
209
210         Long ofPort = terminationPoint.getOfport();
211         if (ofPort != null) {
212             ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
213         }
214     }
215
216     private void updateOfPortRequest(
217             final OvsdbTerminationPointAugmentation terminationPoint,
218             final Interface ovsInterface) {
219
220         Integer ofPortRequest = terminationPoint.getOfportRequest();
221         if (ofPortRequest != null) {
222             ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest.longValue()));
223         }
224     }
225
226     private void updateInterfaceOptions(
227             final OvsdbTerminationPointAugmentation terminationPoint,
228             final Interface ovsInterface) {
229
230         //Configure optional input
231         if (terminationPoint.getOptions() != null) {
232             try {
233                 ovsInterface.setOptions(YangUtils.convertYangKeyValueListToMap(terminationPoint.getOptions(),
234                         Options::getOption, Options::getValue));
235             } catch (NullPointerException e) {
236                 LOG.warn("Incomplete OVSDB interface options", e);
237             }
238         }
239     }
240
241     private void updateInterfaceExternalIds(
242             final OvsdbTerminationPointAugmentation terminationPoint,
243             final Interface ovsInterface) {
244
245         List<InterfaceExternalIds> interfaceExternalIds =
246                 terminationPoint.getInterfaceExternalIds();
247         if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
248             try {
249                 ovsInterface.setExternalIds(YangUtils.convertYangKeyValueListToMap(interfaceExternalIds,
250                         InterfaceExternalIds::getExternalIdKey, InterfaceExternalIds::getExternalIdValue));
251             } catch (NullPointerException e) {
252                 LOG.warn("Incomplete OVSDB interface external_ids", e);
253             }
254         }
255     }
256
257     private void updateInterfaceLldp(
258             final OvsdbTerminationPointAugmentation terminationPoint,
259             final Interface ovsInterface) {
260
261         try {
262             List<InterfaceLldp> interfaceLldpList =
263                     terminationPoint.getInterfaceLldp();
264             if (interfaceLldpList != null && !interfaceLldpList.isEmpty()) {
265                 try {
266                     ovsInterface.setLldp(YangUtils.convertYangKeyValueListToMap(interfaceLldpList,
267                             InterfaceLldp::getLldpKey, InterfaceLldp::getLldpValue));
268                 } catch (NullPointerException e) {
269                     LOG.warn("Incomplete OVSDB interface lldp", e);
270                 }
271             }
272         } catch (SchemaVersionMismatchException e) {
273             schemaMismatchLog("lldp", "Interface", e);
274         }
275     }
276
277     private void updateInterfaceOtherConfig(
278             final OvsdbTerminationPointAugmentation terminationPoint,
279             final Interface ovsInterface) {
280
281         List<InterfaceOtherConfigs> interfaceOtherConfigs =
282                 terminationPoint.getInterfaceOtherConfigs();
283         if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
284             Map<String, String> otherConfigsMap = new HashMap<>();
285             for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) {
286                 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
287                         interfaceOtherConfig.getOtherConfigValue());
288             }
289             try {
290                 ovsInterface.setOtherConfig(otherConfigsMap);
291             } catch (NullPointerException e) {
292                 LOG.warn("Incomplete OVSDB interface other_config", e);
293             }
294         }
295     }
296
297     private void updateInterfaceBfd(
298             final OvsdbTerminationPointAugmentation terminationPoint,
299             final Interface ovsInterface) {
300
301         try {
302             List<InterfaceBfd> interfaceBfdList =
303                     terminationPoint.getInterfaceBfd();
304             if (interfaceBfdList != null && !interfaceBfdList.isEmpty()) {
305                 try {
306                     ovsInterface.setBfd(YangUtils.convertYangKeyValueListToMap(interfaceBfdList,
307                             InterfaceBfd::getBfdKey, InterfaceBfd::getBfdValue));
308                 } catch (NullPointerException e) {
309                     LOG.warn("Incomplete OVSDB interface bfd", e);
310                 }
311             }
312         } catch (SchemaVersionMismatchException e) {
313             schemaMismatchLog("bfd", "Interface", e);
314         }
315     }
316
317     private void updateInterfacePolicing(
318             final OvsdbTerminationPointAugmentation terminationPoint,
319             final Interface ovsInterface) {
320
321         Long ingressPolicingRate = terminationPoint.getIngressPolicingRate();
322         if (ingressPolicingRate != null) {
323             ovsInterface.setIngressPolicingRate(ingressPolicingRate);
324         }
325         Long ingressPolicingBurst = terminationPoint.getIngressPolicingBurst();
326         if (ingressPolicingBurst != null) {
327             ovsInterface.setIngressPolicingBurst(ingressPolicingBurst);
328         }
329     }
330
331     private void updatePortExternalIds(
332             final OvsdbTerminationPointAugmentation terminationPoint,
333             final Port port) {
334
335         List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
336         if (portExternalIds != null && !portExternalIds.isEmpty()) {
337             try {
338                 port.setExternalIds(YangUtils.convertYangKeyValueListToMap(portExternalIds,
339                         PortExternalIds::getExternalIdKey, PortExternalIds::getExternalIdValue));
340             } catch (NullPointerException e) {
341                 LOG.warn("Incomplete OVSDB port external_ids", e);
342             }
343         }
344     }
345
346     private void updatePortVlanTag(
347             final OvsdbTerminationPointAugmentation terminationPoint,
348             final Port port) {
349
350         if (terminationPoint.getVlanTag() != null) {
351             Set<Long> vlanTag = new HashSet<>();
352             vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
353             port.setTag(vlanTag);
354         }
355     }
356
357     private void updatePortVlanTrunk(
358             final OvsdbTerminationPointAugmentation terminationPoint,
359             final Port port) {
360
361         if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
362             Set<Long> portTrunks = new HashSet<>();
363             List<Trunks> modelTrunks = terminationPoint.getTrunks();
364             for (Trunks trunk : modelTrunks) {
365                 if (trunk.getTrunk() != null) {
366                     portTrunks.add(trunk.getTrunk().getValue().longValue());
367                 }
368             }
369             port.setTrunks(portTrunks);
370         }
371     }
372
373     private void updatePortVlanMode(
374             final OvsdbTerminationPointAugmentation terminationPoint,
375             final Port port) {
376         if (terminationPoint.getVlanMode() != null) {
377             Set<String> portVlanMode = new HashSet<>();
378             VlanMode modelVlanMode = terminationPoint.getVlanMode();
379             portVlanMode.add(SouthboundConstants.VlanModes.values()[modelVlanMode.getIntValue() - 1].getMode());
380             port.setVlanMode(portVlanMode);
381         }
382     }
383
384     private void updatePortOtherConfig(
385             final OvsdbTerminationPointAugmentation terminationPoint,
386             final Port ovsPort) {
387         List<PortOtherConfigs> portOtherConfigs =
388                 terminationPoint.getPortOtherConfigs();
389         if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
390             try {
391                 ovsPort.setOtherConfig(YangUtils.convertYangKeyValueListToMap(portOtherConfigs,
392                         PortOtherConfigs::getOtherConfigKey, PortOtherConfigs::getOtherConfigValue));
393             } catch (NullPointerException e) {
394                 LOG.warn("Incomplete OVSDB port other_config", e);
395             }
396         }
397     }
398
399 }