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