Upgrade ietf-{inet,yang}-types to 2013-07-15
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OpenVSwitchUpdateCommand.java
1 /*
2  * Copyright (c) 2015 Inocybe 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.transactions.md;
9
10 import static org.opendaylight.ovsdb.southbound.SouthboundUtil.schemaMismatchLog;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.NoSuchElementException;
17 import java.util.Set;
18 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
21 import org.opendaylight.ovsdb.lib.message.TableUpdates;
22 import org.opendaylight.ovsdb.lib.notation.UUID;
23 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
24 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
25 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
26 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
27 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
28 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
29 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.DatapathTypeEntry;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.DatapathTypeEntryBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntry;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntryBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIdsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIdsKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigsKey;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
56
57     private static final Logger LOG = LoggerFactory.getLogger(OpenVSwitchUpdateCommand.class);
58
59     public OpenVSwitchUpdateCommand(OvsdbConnectionInstance key, TableUpdates updates,
60             DatabaseSchema dbSchema) {
61         super(key, updates, dbSchema);
62     }
63
64     @Override
65     public void execute(ReadWriteTransaction transaction) {
66         Map<UUID, OpenVSwitch> updatedOpenVSwitchRows = TyperUtils
67                 .extractRowsUpdated(OpenVSwitch.class, getUpdates(),
68                         getDbSchema());
69         Map<UUID, OpenVSwitch> deletedOpenVSwitchRows = TyperUtils
70                 .extractRowsOld(OpenVSwitch.class, getUpdates(),
71                         getDbSchema());
72
73         for (Entry<UUID, OpenVSwitch> entry : updatedOpenVSwitchRows.entrySet()) {
74             OpenVSwitch openVSwitch = entry.getValue();
75             final InstanceIdentifier<Node> nodePath = getInstanceIdentifier(openVSwitch);
76
77             OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
78
79             setVersion(ovsdbNodeBuilder, openVSwitch);
80             setDataPathTypes(ovsdbNodeBuilder, openVSwitch);
81             setInterfaceTypes(ovsdbNodeBuilder, openVSwitch);
82             OpenVSwitch oldEntry = deletedOpenVSwitchRows.get(entry.getKey());
83             setExternalIds(transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
84             setOtherConfig(transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
85             ovsdbNodeBuilder.setConnectionInfo(getConnectionInfo());
86
87             NodeBuilder nodeBuilder = new NodeBuilder();
88             nodeBuilder.setNodeId(getNodeId(openVSwitch));
89             nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class,
90                     ovsdbNodeBuilder.build());
91             transaction.merge(LogicalDatastoreType.OPERATIONAL, nodePath,
92                     nodeBuilder.build());
93         }
94     }
95
96     private void setOtherConfig(ReadWriteTransaction transaction,
97             OvsdbNodeAugmentationBuilder ovsdbNodeBuilder, OpenVSwitch oldEntry, OpenVSwitch openVSwitch) {
98         Map<String, String> oldOtherConfigs = null;
99         Map<String, String> otherConfigs = null;
100
101         if (openVSwitch.getOtherConfigColumn() != null) {
102             otherConfigs = openVSwitch.getOtherConfigColumn().getData();
103         }
104         if (oldEntry != null && oldEntry.getOtherConfigColumn() != null) {
105             oldOtherConfigs = oldEntry.getOtherConfigColumn().getData();
106         }
107         if ((oldOtherConfigs == null) || oldOtherConfigs.isEmpty()) {
108             setNewOtherConfigs(ovsdbNodeBuilder, otherConfigs);
109         } else if (otherConfigs != null && !otherConfigs.isEmpty()) {
110             removeOldConfigs(transaction, oldOtherConfigs, openVSwitch);
111             setNewOtherConfigs(ovsdbNodeBuilder, otherConfigs);
112         }
113     }
114
115     private void removeOldConfigs(ReadWriteTransaction transaction, Map<String, String> oldOtherConfigs,
116             OpenVSwitch ovs) {
117         InstanceIdentifier<OvsdbNodeAugmentation> nodeAugmentataionIid = InstanceIdentifier
118                 .create(NetworkTopology.class)
119                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
120                 .child(Node.class, new NodeKey(getNodeId(ovs))).augmentation(OvsdbNodeAugmentation.class);
121         Set<String> otherConfigKeys = oldOtherConfigs.keySet();
122         for (String otherConfigKey : otherConfigKeys) {
123             KeyedInstanceIdentifier<OpenvswitchOtherConfigs, OpenvswitchOtherConfigsKey> externalIid =
124                     nodeAugmentataionIid
125                     .child(OpenvswitchOtherConfigs.class, new OpenvswitchOtherConfigsKey(otherConfigKey));
126             transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIid);
127         }
128     }
129
130     private void setNewOtherConfigs(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
131             Map<String, String> otherConfigs) {
132         Set<String> otherConfigKeys = otherConfigs.keySet();
133         List<OpenvswitchOtherConfigs> otherConfigsList = new ArrayList<>();
134         String otherConfigValue;
135         for (String otherConfigKey : otherConfigKeys) {
136             otherConfigValue = otherConfigs.get(otherConfigKey);
137             if (otherConfigKey != null && otherConfigValue != null) {
138                 otherConfigsList.add(new OpenvswitchOtherConfigsBuilder().setOtherConfigKey(otherConfigKey)
139                         .setOtherConfigValue(otherConfigValue).build());
140             }
141         }
142         ovsdbNodeBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
143     }
144
145     private void setExternalIds(ReadWriteTransaction transaction,
146             OvsdbNodeAugmentationBuilder ovsdbNodeBuilder, OpenVSwitch oldEntry, OpenVSwitch openVSwitch) {
147         Map<String, String> oldExternalIds = null;
148         Map<String, String> externalIds = null;
149
150         if (openVSwitch.getExternalIdsColumn() != null) {
151             externalIds = openVSwitch.getExternalIdsColumn().getData();
152         }
153         if (oldEntry != null && oldEntry.getExternalIdsColumn() != null) {
154             oldExternalIds = oldEntry.getExternalIdsColumn().getData();
155         }
156         if ((oldExternalIds == null) || oldExternalIds.isEmpty()) {
157             setNewExternalIds(ovsdbNodeBuilder, externalIds);
158         } else if (externalIds != null && !externalIds.isEmpty()) {
159             removeExternalIds(transaction, oldExternalIds, openVSwitch);
160             setNewExternalIds(ovsdbNodeBuilder, externalIds);
161         }
162     }
163
164     private void removeExternalIds(ReadWriteTransaction transaction, Map<String, String> oldExternalIds,
165             OpenVSwitch ovs) {
166         InstanceIdentifier<OvsdbNodeAugmentation> nodeAugmentataionIid = InstanceIdentifier
167                 .create(NetworkTopology.class)
168                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
169                 .child(Node.class, new NodeKey(getNodeId(ovs))).augmentation(OvsdbNodeAugmentation.class);
170         Set<String> externalIdKeys = oldExternalIds.keySet();
171         for (String externalIdKey : externalIdKeys) {
172             KeyedInstanceIdentifier<OpenvswitchExternalIds, OpenvswitchExternalIdsKey> externalIid =
173                     nodeAugmentataionIid
174                     .child(OpenvswitchExternalIds.class, new OpenvswitchExternalIdsKey(externalIdKey));
175             transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIid);
176         }
177     }
178
179     private void setNewExternalIds(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
180             Map<String, String> externalIds) {
181         Set<String> externalIdKeys = externalIds.keySet();
182         List<OpenvswitchExternalIds> externalIdsList = new ArrayList<>();
183         String externalIdValue;
184         for (String externalIdKey : externalIdKeys) {
185             externalIdValue = externalIds.get(externalIdKey);
186             if (externalIdKey != null && externalIdValue != null) {
187                 externalIdsList.add(new OpenvswitchExternalIdsBuilder().setExternalIdKey(externalIdKey)
188                         .setExternalIdValue(externalIdValue).build());
189             }
190         }
191         ovsdbNodeBuilder.setOpenvswitchExternalIds(externalIdsList);
192     }
193
194     private void setInterfaceTypes(
195             OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
196             OpenVSwitch openVSwitch) {
197         try {
198             Set<String> iftypes = openVSwitch.getIfaceTypesColumn().getData();
199             List<InterfaceTypeEntry> ifEntryList = new ArrayList<>();
200             for (String ifType : iftypes) {
201                 if (SouthboundMapper.createInterfaceType(ifType) != null) {
202                     InterfaceTypeEntry ifEntry = new InterfaceTypeEntryBuilder()
203                             .setInterfaceType(
204                                     SouthboundMapper.createInterfaceType(ifType))
205                             .build();
206                     ifEntryList.add(ifEntry);
207                 } else {
208                     LOG.warn("Interface type {} not present in model", ifType);
209                 }
210             }
211             ovsdbNodeBuilder.setInterfaceTypeEntry(ifEntryList);
212         } catch (SchemaVersionMismatchException e) {
213             schemaMismatchLog("iface_types", SouthboundConstants.OPEN_V_SWITCH, e);
214         }
215     }
216
217     private void setDataPathTypes(
218             OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
219             OpenVSwitch openVSwitch) {
220         try {
221             Set<String> dptypes = openVSwitch.getDatapathTypesColumn()
222                     .getData();
223             List<DatapathTypeEntry> dpEntryList = new ArrayList<>();
224             for (String dpType : dptypes) {
225                 if (SouthboundMapper.createDatapathType(dpType) != null) {
226                     DatapathTypeEntry dpEntry = new DatapathTypeEntryBuilder()
227                             .setDatapathType(
228                                     SouthboundMapper.createDatapathType(dpType))
229                             .build();
230                     dpEntryList.add(dpEntry);
231                 } else {
232                     LOG.warn("Datapath type {} not present in model", dpType);
233                 }
234             }
235             ovsdbNodeBuilder.setDatapathTypeEntry(dpEntryList);
236         } catch (SchemaVersionMismatchException e) {
237             schemaMismatchLog("datapath_types", SouthboundConstants.OPEN_V_SWITCH, e);
238         }
239     }
240
241     private void setVersion(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
242             OpenVSwitch openVSwitch) {
243         try {
244             ovsdbNodeBuilder.setOvsVersion(openVSwitch.getOvsVersionColumn().getData().iterator().next());
245         } catch (NoSuchElementException e) {
246             LOG.debug("ovs_version is not set for this switch",e);
247         }
248     }
249
250     private InstanceIdentifier<Node> getInstanceIdentifier(OpenVSwitch ovs) {
251         if (ovs.getExternalIdsColumn() != null
252                 && ovs.getExternalIdsColumn().getData() != null
253                 && ovs.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
254             String iidString = ovs.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
255             InstanceIdentifier<Node> iid =
256                    (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
257             getOvsdbConnectionInstance().setInstanceIdentifier(iid);
258         } else {
259             String nodeString = SouthboundConstants.OVSDB_URI_PREFIX + "://" + SouthboundConstants.UUID + "/"
260                     + ovs.getUuid().toString();
261             NodeId nodeId = new NodeId(new Uri(nodeString));
262             NodeKey nodeKey = new NodeKey(nodeId);
263             InstanceIdentifier<Node> iid = InstanceIdentifier.builder(NetworkTopology.class)
264                     .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
265                     .child(Node.class,nodeKey)
266                     .build();
267             getOvsdbConnectionInstance().setInstanceIdentifier(iid);
268         }
269         return getOvsdbConnectionInstance().getInstanceIdentifier();
270     }
271
272     private NodeId getNodeId(OpenVSwitch ovs) {
273         NodeKey nodeKey = getInstanceIdentifier(ovs).firstKeyOf(Node.class, NodeKey.class);
274         return nodeKey.getNodeId();
275     }
276 }