Merge "Fix for TerminationPointCreateCommand to insert a termination point, if not...
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / TerminationPointCreateCommand.java
1 /*
2  * Copyright (c) 2015 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.notation.Mutator;
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.Bridge;
25 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
26 import org.opendaylight.ovsdb.schema.openvswitch.Port;
27 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
28 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
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.InterfaceOtherConfigs;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortOtherConfigs;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Trunks;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
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.base.Optional;
46 import com.google.common.collect.ImmutableMap;
47 import com.google.common.collect.Sets;
48
49 public class TerminationPointCreateCommand extends AbstractTransactCommand {
50
51     private static final Logger LOG = LoggerFactory.getLogger(TerminationPointCreateCommand.class);
52
53     public TerminationPointCreateCommand(BridgeOperationalState state,
54             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
55         super(state, changes);
56     }
57
58     @Override
59     public void execute(TransactionBuilder transaction) {
60         for (Entry<InstanceIdentifier<?>, DataObject> entry: getChanges().getCreatedData().entrySet()) {
61             DataObject dataObject = entry.getValue();
62             if (dataObject instanceof OvsdbTerminationPointAugmentation) {
63                 OvsdbTerminationPointAugmentation terminationPoint = (OvsdbTerminationPointAugmentation) dataObject;
64                 LOG.debug("Received request to create termination point {}",
65                         terminationPoint.getName());
66                 InstanceIdentifier terminationPointIid = entry.getKey();
67                 Optional<TerminationPoint> terminationPointOptional =
68                         getOperationalState().getBridgeTerminationPoint(terminationPointIid);
69                 if (!terminationPointOptional.isPresent()) {
70                     // Configure interface
71                     String interfaceUuid = "Interface_" + SouthboundMapper.getRandomUUID();;
72                     Interface ovsInterface =
73                             TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
74                     createInterface(terminationPoint, ovsInterface);
75                     transaction.add(op.insert(ovsInterface).withId(interfaceUuid));
76
77                     // Configure port with the above interface details
78                     String portUuid = "Port_" + SouthboundMapper.getRandomUUID();
79                     Port port = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Port.class);
80                     createPort(terminationPoint, port, interfaceUuid);
81                     transaction.add(op.insert(port).withId(portUuid));
82
83                     //Configure bridge with the above port details
84                     Bridge bridge = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Bridge.class);
85                     bridge.setName(getBridge(entry.getKey()).getBridgeName().getValue());
86                     bridge.setPorts(Sets.newHashSet(new UUID(portUuid)));
87
88                     transaction.add(op.mutate(bridge)
89                             .addMutation(bridge.getPortsColumn().getSchema(),
90                                     Mutator.INSERT,bridge.getPortsColumn().getData())
91                             .where(bridge.getNameColumn().getSchema()
92                                     .opEqual(bridge.getNameColumn().getData())).build());
93                 }
94             }
95         }
96
97     }
98
99     private void createInterface(
100             final OvsdbTerminationPointAugmentation terminationPoint,
101             final Interface ovsInterface) {
102         ovsInterface.setName(terminationPoint.getName());
103         ovsInterface.setType(SouthboundMapper.createOvsdbInterfaceType(terminationPoint.getInterfaceType()));
104
105         createOfPort(terminationPoint, ovsInterface);
106         createOfPortRequest(terminationPoint, ovsInterface);
107         createInterfaceOptions(terminationPoint, ovsInterface);
108         createInterfaceOtherConfig(terminationPoint, ovsInterface);
109         createInterfaceExternalIds(terminationPoint, ovsInterface);
110     }
111
112     private void createPort(
113             final OvsdbTerminationPointAugmentation terminationPoint,
114             final Port port, final String interfaceUuid) {
115
116         port.setName(terminationPoint.getName());
117         port.setInterfaces(Sets.newHashSet(new UUID(interfaceUuid)));
118         createPortOtherConfig(terminationPoint, port);
119         createPortVlanTag(terminationPoint, port);
120         createPortVlanTrunk(terminationPoint, port);
121         createPortVlanMode(terminationPoint, port);
122         createPortExternalIds(terminationPoint, port);
123     }
124
125     private void createOfPort(
126             final OvsdbTerminationPointAugmentation terminationPoint,
127             final Interface ovsInterface) {
128
129         Long ofPort = terminationPoint.getOfport();
130         if (ofPort != null) {
131             ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
132         }
133     }
134
135     private void createOfPortRequest(
136             final OvsdbTerminationPointAugmentation terminationPoint,
137             final Interface ovsInterface) {
138
139         Integer ofPortRequest = terminationPoint.getOfportRequest();
140         if (ofPortRequest != null) {
141             ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest.longValue()));
142         }
143     }
144
145     private void createInterfaceOptions(
146             final OvsdbTerminationPointAugmentation terminationPoint,
147             final Interface ovsInterface) {
148
149         //Configure optional input
150         if (terminationPoint.getOptions() != null) {
151             HashMap<String, String> optionsMap = new HashMap<String, String>();
152             for (Options option : terminationPoint.getOptions()) {
153                 optionsMap.put(option.getOption(), option.getValue());
154             }
155             try {
156                 ovsInterface.setOptions(ImmutableMap.copyOf(optionsMap));
157             } catch (NullPointerException e) {
158                 LOG.warn("Incomplete OVSDB interface options");
159             }
160         }
161     }
162
163     private void createInterfaceExternalIds(
164             final OvsdbTerminationPointAugmentation terminationPoint,
165             final Interface ovsInterface) {
166
167         List<InterfaceExternalIds> interfaceExternalIds =
168                 terminationPoint.getInterfaceExternalIds();
169         if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
170             HashMap<String, String> externalIdsMap = new HashMap<String, String>();
171             for (InterfaceExternalIds externalId: interfaceExternalIds) {
172                 externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
173             }
174             try {
175                 ovsInterface.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
176             } catch (NullPointerException e) {
177                 LOG.warn("Incomplete OVSDB interface external_ids");
178             }
179         }
180     }
181
182     private void createInterfaceOtherConfig(
183             final OvsdbTerminationPointAugmentation terminationPoint,
184             final Interface ovsInterface) {
185
186         List<InterfaceOtherConfigs> interfaceOtherConfigs =
187                 terminationPoint.getInterfaceOtherConfigs();
188         if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
189             HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
190             for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) {
191                 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
192                         interfaceOtherConfig.getOtherConfigValue());
193             }
194             try {
195                 ovsInterface.setOtherConfig(otherConfigsMap);
196             } catch (NullPointerException e) {
197                 LOG.warn("Incomplete OVSDB interface other_config", e);
198             }
199         }
200     }
201
202     private void createPortExternalIds(
203             final OvsdbTerminationPointAugmentation terminationPoint,
204             final Port port) {
205
206         List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
207         if (portExternalIds != null && !portExternalIds.isEmpty()) {
208             HashMap<String, String> externalIdsMap = new HashMap<String, String>();
209             for (PortExternalIds externalId: portExternalIds) {
210                 externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
211             }
212             try {
213                 port.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
214             } catch (NullPointerException e) {
215                 LOG.warn("Incomplete OVSDB port external_ids");
216             }
217         }
218     }
219
220     private void createPortVlanTag(
221             final OvsdbTerminationPointAugmentation terminationPoint,
222             final Port port) {
223
224         if (terminationPoint.getVlanTag() != null) {
225             Set<Long> vlanTag = new HashSet<Long>();
226             vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
227             port.setTag(vlanTag);
228         }
229     }
230
231     private void createPortVlanTrunk(
232             final OvsdbTerminationPointAugmentation terminationPoint,
233             final Port port) {
234
235         if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
236             Set<Long> portTrunks = new HashSet<Long>();
237             List<Trunks> modelTrunks = terminationPoint.getTrunks();
238             for (Trunks trunk: modelTrunks) {
239                 if (trunk.getTrunk() != null) {
240                     portTrunks.add(trunk.getTrunk().getValue().longValue());
241                 }
242             }
243             port.setTrunks(portTrunks);
244         }
245     }
246
247     private void createPortVlanMode(
248             final OvsdbTerminationPointAugmentation terminationPoint,
249             final Port port) {
250         if (terminationPoint.getVlanMode() != null) {
251             Set<String> portVlanMode = new HashSet<String>();
252             VlanMode modelVlanMode = terminationPoint.getVlanMode();
253             portVlanMode.add(SouthboundConstants.VLANMODES.values()[modelVlanMode.getIntValue() - 1].getMode());
254             port.setVlanMode(portVlanMode);
255         }
256     }
257
258     private void createPortOtherConfig(
259             final OvsdbTerminationPointAugmentation terminationPoint,
260             final Port ovsPort) {
261         List<PortOtherConfigs> portOtherConfigs =
262                 terminationPoint.getPortOtherConfigs();
263         if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
264             HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
265             for (PortOtherConfigs portOtherConfig : portOtherConfigs) {
266                 otherConfigsMap.put(portOtherConfig.getOtherConfigKey(),
267                         portOtherConfig.getOtherConfigValue());
268             }
269             try {
270                 ovsPort.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
271             } catch (NullPointerException e) {
272                 LOG.warn("Incomplete OVSDB port other_config", e);
273             }
274         }
275     }
276
277     private OvsdbBridgeAugmentation getBridge(InstanceIdentifier<?> key) {
278         InstanceIdentifier<Node> nodeIid = key.firstIdentifierOf(Node.class);
279         Map<InstanceIdentifier<Node>, Node> nodes =
280                 TransactUtils.extractCreatedOrUpdated(getChanges(),Node.class);
281         if (nodes != null && nodes.get(nodeIid) != null) {
282             Node node = nodes.get(nodeIid);
283             OvsdbBridgeAugmentation bridge = node.getAugmentation(OvsdbBridgeAugmentation.class);
284             if (bridge != null) {
285                 return bridge;
286             }
287         }
288         return null;
289     }
290
291 }