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