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