Use a utility function for key-value to map conversions
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / TerminationPointCreateCommand.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.Collections;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.concurrent.ExecutionException;
19 import java.util.Set;
20
21 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
26 import org.opendaylight.ovsdb.lib.notation.Mutator;
27 import org.opendaylight.ovsdb.lib.notation.UUID;
28 import org.opendaylight.ovsdb.lib.operations.Mutate;
29 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
30 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
31 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
32 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
33 import org.opendaylight.ovsdb.schema.openvswitch.Port;
34 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
35 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
36 import org.opendaylight.ovsdb.southbound.SouthboundProvider;
37 import org.opendaylight.ovsdb.utils.yang.YangUtils;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
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.port._interface.attributes.InterfaceExternalIds;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceLldp;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceOtherConfigs;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortOtherConfigs;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Trunks;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
51 import org.opendaylight.yangtools.yang.binding.DataObject;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 import com.google.common.base.Optional;
57 import com.google.common.collect.Sets;
58 import com.google.common.util.concurrent.CheckedFuture;
59
60 public class TerminationPointCreateCommand extends AbstractTransactCommand {
61
62     private static final Logger LOG = LoggerFactory.getLogger(TerminationPointCreateCommand.class);
63
64     public TerminationPointCreateCommand(BridgeOperationalState state,
65             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
66         super(state, changes);
67     }
68
69     @Override
70     public void execute(TransactionBuilder transaction) {
71         for (Entry<InstanceIdentifier<?>, DataObject> entry: getChanges().getCreatedData().entrySet()) {
72             DataObject dataObject = entry.getValue();
73             if (dataObject instanceof OvsdbTerminationPointAugmentation) {
74                 OvsdbTerminationPointAugmentation terminationPoint = (OvsdbTerminationPointAugmentation) dataObject;
75                 LOG.debug("Received request to create termination point {}",
76                         terminationPoint.getName());
77                 InstanceIdentifier terminationPointIid = entry.getKey();
78                 Optional<TerminationPoint> terminationPointOptional =
79                         getOperationalState().getBridgeTerminationPoint(terminationPointIid);
80                 if (!terminationPointOptional.isPresent()) {
81                     // Configure interface
82                     String interfaceUuid = "Interface_" + SouthboundMapper.getRandomUUID();
83                     Interface ovsInterface =
84                             TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
85                     createInterface(terminationPoint, ovsInterface);
86                     transaction.add(op.insert(ovsInterface).withId(interfaceUuid));
87
88                     stampInstanceIdentifier(transaction, (InstanceIdentifier<TerminationPoint>) entry.getKey(),
89                             ovsInterface.getName());
90
91                     // Configure port with the above interface details
92                     String portUuid = "Port_" + SouthboundMapper.getRandomUUID();
93                     Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
94                     createPort(terminationPoint, port, interfaceUuid);
95                     transaction.add(op.insert(port).withId(portUuid));
96
97                     //Configure bridge with the above port details
98                     Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
99                     if (getBridge(entry.getKey()) != null) {
100                         bridge.setName(getBridge(entry.getKey()).getBridgeName().getValue());
101                         bridge.setPorts(Sets.newHashSet(new UUID(portUuid)));
102
103                         transaction.add(op.mutate(bridge)
104                                 .addMutation(bridge.getPortsColumn().getSchema(),
105                                         Mutator.INSERT,bridge.getPortsColumn().getData())
106                                 .where(bridge.getNameColumn().getSchema()
107                                         .opEqual(bridge.getNameColumn().getData())).build());
108                     }
109                 }
110             }
111         }
112
113     }
114
115     private void createInterface(
116             final OvsdbTerminationPointAugmentation terminationPoint,
117             final Interface ovsInterface) {
118         ovsInterface.setName(terminationPoint.getName());
119
120         createInterfaceType(terminationPoint, ovsInterface);
121         createOfPort(terminationPoint, ovsInterface);
122         createOfPortRequest(terminationPoint, ovsInterface);
123         createInterfaceOptions(terminationPoint, ovsInterface);
124         createInterfaceOtherConfig(terminationPoint, ovsInterface);
125         createInterfaceExternalIds(terminationPoint, ovsInterface);
126         createInterfaceLldp(terminationPoint, ovsInterface);
127     }
128
129     private void createInterfaceType(final OvsdbTerminationPointAugmentation terminationPoint,
130                                      final Interface ovsInterface) {
131
132         Class<? extends InterfaceTypeBase> mdsaltype = terminationPoint.getInterfaceType();
133         if (mdsaltype != null) {
134             ovsInterface.setType(SouthboundMapper.createOvsdbInterfaceType(mdsaltype));
135         }
136     }
137
138     private void createPort(
139             final OvsdbTerminationPointAugmentation terminationPoint,
140             final Port port, final String interfaceUuid) {
141
142         port.setName(terminationPoint.getName());
143         port.setInterfaces(Sets.newHashSet(new UUID(interfaceUuid)));
144         createPortOtherConfig(terminationPoint, port);
145         createPortVlanTag(terminationPoint, port);
146         createPortVlanTrunk(terminationPoint, port);
147         createPortVlanMode(terminationPoint, port);
148         createPortExternalIds(terminationPoint, port);
149     }
150
151     private void createOfPort(
152             final OvsdbTerminationPointAugmentation terminationPoint,
153             final Interface ovsInterface) {
154
155         Long ofPort = terminationPoint.getOfport();
156         if (ofPort != null) {
157             ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
158         }
159     }
160
161     private void createOfPortRequest(
162             final OvsdbTerminationPointAugmentation terminationPoint,
163             final Interface ovsInterface) {
164
165         Integer ofPortRequest = terminationPoint.getOfportRequest();
166         if (ofPortRequest != null) {
167             ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest.longValue()));
168         }
169     }
170
171     private void createInterfaceOptions(
172             final OvsdbTerminationPointAugmentation terminationPoint,
173             final Interface ovsInterface) {
174
175         //Configure optional input
176         if (terminationPoint.getOptions() != null) {
177             try {
178                 ovsInterface.setOptions(YangUtils.convertYangKeyValueListToMap(terminationPoint.getOptions(),
179                         Options::getOption, Options::getValue));
180             } catch (NullPointerException e) {
181                 LOG.warn("Incomplete OVSDB interface options", e);
182             }
183         }
184     }
185
186     private void createInterfaceExternalIds(
187             final OvsdbTerminationPointAugmentation terminationPoint,
188             final Interface ovsInterface) {
189
190         List<InterfaceExternalIds> interfaceExternalIds =
191                 terminationPoint.getInterfaceExternalIds();
192         if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
193             try {
194                 ovsInterface.setExternalIds(YangUtils.convertYangKeyValueListToMap(interfaceExternalIds,
195                         InterfaceExternalIds::getExternalIdKey, InterfaceExternalIds::getExternalIdValue));
196             } catch (NullPointerException e) {
197                 LOG.warn("Incomplete OVSDB interface external_ids", e);
198             }
199         }
200     }
201
202     private void createInterfaceOtherConfig(
203             final OvsdbTerminationPointAugmentation terminationPoint,
204             final Interface ovsInterface) {
205
206         List<InterfaceOtherConfigs> interfaceOtherConfigs =
207                 terminationPoint.getInterfaceOtherConfigs();
208         if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
209             Map<String, String> otherConfigsMap = new HashMap<>();
210             for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) {
211                 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
212                         interfaceOtherConfig.getOtherConfigValue());
213             }
214             try {
215                 ovsInterface.setOtherConfig(otherConfigsMap);
216             } catch (NullPointerException e) {
217                 LOG.warn("Incomplete OVSDB interface other_config", e);
218             }
219         }
220     }
221
222     private void createInterfaceLldp(
223             final OvsdbTerminationPointAugmentation terminationPoint,
224             final Interface ovsInterface) {
225
226         try {
227             List<InterfaceLldp> interfaceLldpList =
228                     terminationPoint.getInterfaceLldp();
229             if (interfaceLldpList != null && !interfaceLldpList.isEmpty()) {
230                 try {
231                     ovsInterface.setLldp(YangUtils.convertYangKeyValueListToMap(interfaceLldpList,
232                             InterfaceLldp::getLldpKey, InterfaceLldp::getLldpValue));
233                 } catch (NullPointerException e) {
234                     LOG.warn("Incomplete OVSDB interface lldp", e);
235                 }
236             }
237         } catch (SchemaVersionMismatchException e) {
238             LOG.debug("lldp column for Interface Table unsupported for this version of ovsdb schema", e);
239         }
240     }
241
242     private void createPortExternalIds(
243             final OvsdbTerminationPointAugmentation terminationPoint,
244             final Port port) {
245
246         List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
247         if (portExternalIds != null && !portExternalIds.isEmpty()) {
248             try {
249                 port.setExternalIds(YangUtils.convertYangKeyValueListToMap(portExternalIds,
250                         PortExternalIds::getExternalIdKey, PortExternalIds::getExternalIdValue));
251             } catch (NullPointerException e) {
252                 LOG.warn("Incomplete OVSDB port external_ids", e);
253             }
254         }
255     }
256
257     private void createPortVlanTag(
258             final OvsdbTerminationPointAugmentation terminationPoint,
259             final Port port) {
260
261         if (terminationPoint.getVlanTag() != null) {
262             Set<Long> vlanTag = new HashSet<>();
263             vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
264             port.setTag(vlanTag);
265         }
266     }
267
268     private void createPortVlanTrunk(
269             final OvsdbTerminationPointAugmentation terminationPoint,
270             final Port port) {
271
272         if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
273             Set<Long> portTrunks = new HashSet<>();
274             List<Trunks> modelTrunks = terminationPoint.getTrunks();
275             for (Trunks trunk: modelTrunks) {
276                 if (trunk.getTrunk() != null) {
277                     portTrunks.add(trunk.getTrunk().getValue().longValue());
278                 }
279             }
280             port.setTrunks(portTrunks);
281         }
282     }
283
284     private void createPortVlanMode(
285             final OvsdbTerminationPointAugmentation terminationPoint,
286             final Port port) {
287         if (terminationPoint.getVlanMode() != null) {
288             Set<String> portVlanMode = new HashSet<>();
289             VlanMode modelVlanMode = terminationPoint.getVlanMode();
290             portVlanMode.add(SouthboundConstants.VLANMODES.values()[modelVlanMode.getIntValue() - 1].getMode());
291             port.setVlanMode(portVlanMode);
292         }
293     }
294
295     private void createPortOtherConfig(
296             final OvsdbTerminationPointAugmentation terminationPoint,
297             final Port ovsPort) {
298         List<PortOtherConfigs> portOtherConfigs =
299                 terminationPoint.getPortOtherConfigs();
300         if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
301             try {
302                 ovsPort.setOtherConfig(YangUtils.convertYangKeyValueListToMap(portOtherConfigs,
303                         PortOtherConfigs::getOtherConfigKey, PortOtherConfigs::getOtherConfigValue));
304             } catch (NullPointerException e) {
305                 LOG.warn("Incomplete OVSDB port other_config", e);
306             }
307         }
308     }
309
310     private OvsdbBridgeAugmentation getBridge(InstanceIdentifier<?> key) {
311         OvsdbBridgeAugmentation bridge = null;
312         InstanceIdentifier<Node> nodeIid = key.firstIdentifierOf(Node.class);
313         Map<InstanceIdentifier<Node>, Node> nodes =
314                 TransactUtils.extractCreatedOrUpdated(getChanges(),Node.class);
315         if (nodes != null && nodes.get(nodeIid) != null) {
316             Node node = nodes.get(nodeIid);
317             bridge = node.getAugmentation(OvsdbBridgeAugmentation.class);
318             if (bridge == null) {
319                 ReadOnlyTransaction transaction = SouthboundProvider.getDb().newReadOnlyTransaction();
320                 CheckedFuture<Optional<Node>, ReadFailedException> future =
321                         transaction.read(LogicalDatastoreType.OPERATIONAL, nodeIid);
322                 try {
323                     Optional<Node> nodeOptional = future.get();
324                     if (nodeOptional.isPresent()) {
325                         bridge = nodeOptional.get().getAugmentation(OvsdbBridgeAugmentation.class);
326                     }
327                 } catch (InterruptedException | ExecutionException e) {
328                     LOG.warn("Error reading from datastore",e);
329                 }
330                 transaction.close();
331             }
332         }
333         return bridge;
334     }
335
336     public static void stampInstanceIdentifier(TransactionBuilder transaction,InstanceIdentifier<TerminationPoint> iid,
337             String interfaceName) {
338         Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
339         port.setName(interfaceName);
340         port.setExternalIds(Collections.<String,String>emptyMap());
341         Mutate mutate = TransactUtils.stampInstanceIdentifierMutation(transaction,
342                 iid,
343                 port.getSchema(),
344                 port.getExternalIdsColumn().getSchema());
345         transaction.add(mutate
346                 .where(port.getNameColumn().getSchema().opEqual(interfaceName))
347                 .build());
348     }
349
350 }