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