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