Merge "Add OpenFlow node reference to the bridge."
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / TerminationPointUpdateCommand.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.operations.TransactionBuilder;
21 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
22 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
23 import org.opendaylight.ovsdb.schema.openvswitch.Port;
24 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceExternalIds;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.InterfaceOtherConfigs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortExternalIds;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.PortOtherConfigs;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Trunks;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import com.google.common.collect.ImmutableMap;
39 import com.google.common.collect.Sets;
40
41 public class TerminationPointUpdateCommand extends AbstractTransactCommand {
42
43     private static final Logger LOG = LoggerFactory.getLogger(TerminationPointUpdateCommand.class);
44
45     public TerminationPointUpdateCommand(BridgeOperationalState state,
46             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
47         super(state, changes);
48     }
49
50     @Override
51     public void execute(TransactionBuilder transaction) {
52         LOG.trace("TerminationPointUpdateCommand called");
53         Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> created =
54             TransactUtils.extractCreated(getChanges(),OvsdbTerminationPointAugmentation.class);
55         for (Entry<InstanceIdentifier<OvsdbTerminationPointAugmentation>,
56                  OvsdbTerminationPointAugmentation> terminationPointEntry : created.entrySet()) {
57             updateTerminationPoint(transaction, terminationPointEntry.getKey(), terminationPointEntry.getValue());
58         }
59         Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> updated =
60                 TransactUtils.extractUpdated(getChanges(), OvsdbTerminationPointAugmentation.class);
61         for (Entry<InstanceIdentifier<OvsdbTerminationPointAugmentation>,
62                  OvsdbTerminationPointAugmentation> terminationPointEntry : updated.entrySet()) {
63             updateTerminationPoint(transaction, terminationPointEntry.getKey(),
64                     terminationPointEntry.getValue());
65         }
66     }
67
68     public void updateTerminationPoint(TransactionBuilder transaction,
69             InstanceIdentifier<OvsdbTerminationPointAugmentation> iid,
70             OvsdbTerminationPointAugmentation terminationPoint) {
71         if (terminationPoint instanceof OvsdbTerminationPointAugmentation) {
72             LOG.debug("Received request to update termination point {}",
73                     terminationPoint.getName());
74
75             // Update interface
76             Interface ovsInterface =
77                     TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Interface.class);
78             updateInterface(terminationPoint, ovsInterface);
79             Interface extraInterface = TyperUtils.getTypedRowWrapper(
80                     transaction.getDatabaseSchema(), Interface.class);
81             extraInterface.setName("");
82             transaction.add(op.update(ovsInterface)
83                     .where(extraInterface.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
84                     .build());
85
86             // Update port
87             Port port = TyperUtils.getTypedRowWrapper(
88                     transaction.getDatabaseSchema(), Port.class);
89             updatePort(terminationPoint,port);
90             Port extraPort = TyperUtils.getTypedRowWrapper(
91                     transaction.getDatabaseSchema(), Port.class);
92             extraPort.setName("");
93             transaction.add(op.update(port)
94                     .where(extraPort.getNameColumn().getSchema().opEqual(terminationPoint.getName()))
95                     .build());
96         }
97     }
98
99     private void updateInterface(
100             final OvsdbTerminationPointAugmentation terminationPoint,
101             final Interface ovsInterface) {
102         updateOfPort(terminationPoint, ovsInterface);
103         updateOfPortRequest(terminationPoint, ovsInterface);
104         updateInterfaceOptions(terminationPoint, ovsInterface);
105         updateInterfaceOtherConfig(terminationPoint, ovsInterface);
106         updateInterfaceExternalIds(terminationPoint, ovsInterface);
107     }
108
109     private void updatePort(
110             final OvsdbTerminationPointAugmentation terminationPoint,
111             final Port port) {
112
113         updatePortOtherConfig(terminationPoint, port);
114         updatePortVlanTag(terminationPoint, port);
115         updatePortVlanTrunk(terminationPoint, port);
116         updatePortVlanMode(terminationPoint, port);
117         updatePortExternalIds(terminationPoint, port);
118     }
119
120     private void updateOfPort(
121             final OvsdbTerminationPointAugmentation terminationPoint,
122             final Interface ovsInterface) {
123
124         Long ofPort = terminationPoint.getOfport();
125         if (ofPort != null) {
126             ovsInterface.setOpenFlowPort(Sets.newHashSet(ofPort));
127         }
128     }
129
130     private void updateOfPortRequest(
131             final OvsdbTerminationPointAugmentation terminationPoint,
132             final Interface ovsInterface) {
133
134         Integer ofPortRequest = terminationPoint.getOfportRequest();
135         if (ofPortRequest != null) {
136             ovsInterface.setOpenFlowPortRequest(Sets.newHashSet(ofPortRequest.longValue()));
137         }
138     }
139
140     private void updateInterfaceOptions(
141             final OvsdbTerminationPointAugmentation terminationPoint,
142             final Interface ovsInterface) {
143
144         //Configure optional input
145         if (terminationPoint.getOptions() != null) {
146             HashMap<String, String> optionsMap = new HashMap<String, String>();
147             for (Options option : terminationPoint.getOptions()) {
148                 optionsMap.put(option.getOption(), option.getValue());
149             }
150             try {
151                 ovsInterface.setOptions(ImmutableMap.copyOf(optionsMap));
152             } catch (NullPointerException e) {
153                 LOG.warn("Incomplete OVSDB interface options");
154             }
155         }
156     }
157
158     private void updateInterfaceExternalIds(
159             final OvsdbTerminationPointAugmentation terminationPoint,
160             final Interface ovsInterface) {
161
162         List<InterfaceExternalIds> interfaceExternalIds =
163                 terminationPoint.getInterfaceExternalIds();
164         if (interfaceExternalIds != null && !interfaceExternalIds.isEmpty()) {
165             HashMap<String, String> externalIdsMap = new HashMap<String, String>();
166             for (InterfaceExternalIds externalId: interfaceExternalIds) {
167                 externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
168             }
169             try {
170                 ovsInterface.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
171             } catch (NullPointerException e) {
172                 LOG.warn("Incomplete OVSDB interface external_ids");
173             }
174         }
175     }
176
177     private void updateInterfaceOtherConfig(
178             final OvsdbTerminationPointAugmentation terminationPoint,
179             final Interface ovsInterface) {
180
181         List<InterfaceOtherConfigs> interfaceOtherConfigs =
182                 terminationPoint.getInterfaceOtherConfigs();
183         if (interfaceOtherConfigs != null && !interfaceOtherConfigs.isEmpty()) {
184             HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
185             for (InterfaceOtherConfigs interfaceOtherConfig : interfaceOtherConfigs) {
186                 otherConfigsMap.put(interfaceOtherConfig.getOtherConfigKey(),
187                         interfaceOtherConfig.getOtherConfigValue());
188             }
189             try {
190                 ovsInterface.setOtherConfig(otherConfigsMap);
191             } catch (NullPointerException e) {
192                 LOG.warn("Incomplete OVSDB interface other_config", e);
193             }
194         }
195     }
196
197     private void updatePortExternalIds(
198             final OvsdbTerminationPointAugmentation terminationPoint,
199             final Port port) {
200
201         List<PortExternalIds> portExternalIds = terminationPoint.getPortExternalIds();
202         if (portExternalIds != null && !portExternalIds.isEmpty()) {
203             HashMap<String, String> externalIdsMap = new HashMap<String, String>();
204             for (PortExternalIds externalId: portExternalIds) {
205                 externalIdsMap.put(externalId.getExternalIdKey(), externalId.getExternalIdValue());
206             }
207             try {
208                 port.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
209             } catch (NullPointerException e) {
210                 LOG.warn("Incomplete OVSDB port external_ids");
211             }
212         }
213     }
214
215     private void updatePortVlanTag(
216             final OvsdbTerminationPointAugmentation terminationPoint,
217             final Port port) {
218
219         if (terminationPoint.getVlanTag() != null) {
220             Set<Long> vlanTag = new HashSet<Long>();
221             vlanTag.add(terminationPoint.getVlanTag().getValue().longValue());
222             port.setTag(vlanTag);
223         }
224     }
225
226     private void updatePortVlanTrunk(
227             final OvsdbTerminationPointAugmentation terminationPoint,
228             final Port port) {
229
230         if (terminationPoint.getTrunks() != null && terminationPoint.getTrunks().size() > 0) {
231             Set<Long> portTrunks = new HashSet<Long>();
232             List<Trunks> modelTrunks = terminationPoint.getTrunks();
233             for (Trunks trunk: modelTrunks) {
234                 if (trunk.getTrunk() != null) {
235                     portTrunks.add(trunk.getTrunk().getValue().longValue());
236                 }
237             }
238             port.setTrunks(portTrunks);
239         }
240     }
241
242     private void updatePortVlanMode(
243             final OvsdbTerminationPointAugmentation terminationPoint,
244             final Port port) {
245         if (terminationPoint.getVlanMode() != null) {
246             Set<String> portVlanMode = new HashSet<String>();
247             VlanMode modelVlanMode = terminationPoint.getVlanMode();
248             portVlanMode.add(SouthboundConstants.VLANMODES.values()[modelVlanMode.getIntValue() - 1].getMode());
249             port.setVlanMode(portVlanMode);
250         }
251     }
252
253     private void updatePortOtherConfig(
254             final OvsdbTerminationPointAugmentation terminationPoint,
255             final Port ovsPort) {
256         List<PortOtherConfigs> portOtherConfigs =
257                 terminationPoint.getPortOtherConfigs();
258         if (portOtherConfigs != null && !portOtherConfigs.isEmpty()) {
259             HashMap<String, String> otherConfigsMap = new HashMap<String, String>();
260             for (PortOtherConfigs portOtherConfig : portOtherConfigs) {
261                 otherConfigsMap.put(portOtherConfig.getOtherConfigKey(),
262                         portOtherConfig.getOtherConfigValue());
263             }
264             try {
265                 ovsPort.setOtherConfig(ImmutableMap.copyOf(otherConfigsMap));
266             } catch (NullPointerException e) {
267                 LOG.warn("Incomplete OVSDB port other_config", e);
268             }
269         }
270     }
271
272 }