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