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