2 * Copyright (c) 2015 Inocybe inc. and others. All rights reserved.
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
8 package org.opendaylight.ovsdb.southbound.transactions.md;
10 import static org.opendaylight.ovsdb.southbound.SouthboundUtil.schemaMismatchLog;
12 import com.google.common.annotations.VisibleForTesting;
13 import java.util.ArrayList;
14 import java.util.List;
16 import java.util.Map.Entry;
17 import java.util.NoSuchElementException;
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;
56 public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
58 private static final Logger LOG = LoggerFactory.getLogger(OpenVSwitchUpdateCommand.class);
60 private final InstanceIdentifierCodec instanceIdentifierCodec;
62 public OpenVSwitchUpdateCommand(InstanceIdentifierCodec instanceIdentifierCodec, OvsdbConnectionInstance key,
63 TableUpdates updates, DatabaseSchema dbSchema) {
64 super(key, updates, dbSchema);
65 this.instanceIdentifierCodec = instanceIdentifierCodec;
69 public void execute(ReadWriteTransaction transaction) {
70 Map<UUID, OpenVSwitch> updatedOpenVSwitchRows = TyperUtils
71 .extractRowsUpdated(OpenVSwitch.class, getUpdates(),
73 Map<UUID, OpenVSwitch> deletedOpenVSwitchRows = TyperUtils
74 .extractRowsOld(OpenVSwitch.class, getUpdates(),
77 for (Entry<UUID, OpenVSwitch> entry : updatedOpenVSwitchRows.entrySet()) {
78 OpenVSwitch openVSwitch = entry.getValue();
79 final InstanceIdentifier<Node> nodePath = getInstanceIdentifier(openVSwitch);
81 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
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());
92 transaction.merge(LogicalDatastoreType.OPERATIONAL, nodePath, new NodeBuilder()
93 .setNodeId(getNodeId(openVSwitch))
94 .addAugmentation(ovsdbNodeBuilder.build())
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;
104 if (openVSwitch.getOtherConfigColumn() != null) {
105 otherConfigs = openVSwitch.getOtherConfigColumn().getData();
107 if (oldEntry != null && oldEntry.getOtherConfigColumn() != null) {
108 oldOtherConfigs = oldEntry.getOtherConfigColumn().getData();
111 if (oldOtherConfigs != null && !oldOtherConfigs.isEmpty()) {
112 removeOldConfigs(transaction, oldOtherConfigs, openVSwitch);
114 if (otherConfigs != null && !otherConfigs.isEmpty()) {
115 setNewOtherConfigs(ovsdbNodeBuilder, otherConfigs);
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 =
130 .child(OpenvswitchOtherConfigs.class, new OpenvswitchOtherConfigsKey(otherConfigKey));
131 transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIid);
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());
146 ovsdbNodeBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
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;
154 if (openVSwitch.getExternalIdsColumn() != null) {
155 externalIds = openVSwitch.getExternalIdsColumn().getData();
157 if (oldEntry != null && oldEntry.getExternalIdsColumn() != null) {
158 oldExternalIds = oldEntry.getExternalIdsColumn().getData();
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);
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 =
179 .child(OpenvswitchExternalIds.class, new OpenvswitchExternalIdsKey(externalIdKey));
180 transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIid);
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());
195 ovsdbNodeBuilder.setOpenvswitchExternalIds(externalIdsList);
198 private void setInterfaceTypes(
199 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
200 OpenVSwitch openVSwitch) {
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()
208 SouthboundMapper.createInterfaceType(ifType))
210 ifEntryList.add(ifEntry);
212 LOG.warn("Interface type {} not present in model", ifType);
215 ovsdbNodeBuilder.setInterfaceTypeEntry(ifEntryList);
216 } catch (SchemaVersionMismatchException e) {
217 schemaMismatchLog("iface_types", SouthboundConstants.OPEN_V_SWITCH, e);
221 private void setDataPathTypes(
222 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
223 OpenVSwitch openVSwitch) {
225 Set<String> dptypes = openVSwitch.getDatapathTypesColumn()
227 List<DatapathTypeEntry> dpEntryList = new ArrayList<>();
228 for (String dpType : dptypes) {
229 if (SouthboundMapper.createDatapathType(dpType) != null) {
230 DatapathTypeEntry dpEntry = new DatapathTypeEntryBuilder()
232 SouthboundMapper.createDatapathType(dpType))
234 dpEntryList.add(dpEntry);
236 LOG.warn("Datapath type {} not present in model", dpType);
239 ovsdbNodeBuilder.setDatapathTypeEntry(dpEntryList);
240 } catch (SchemaVersionMismatchException e) {
241 schemaMismatchLog("datapath_types", SouthboundConstants.OPEN_V_SWITCH, e);
245 private void setOvsVersion(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder, OpenVSwitch openVSwitch) {
247 ovsdbNodeBuilder.setOvsVersion(openVSwitch.getOvsVersionColumn().getData().iterator().next());
248 } catch (NoSuchElementException e) {
249 LOG.debug("ovs_version is not set for this switch",e);
253 private void setDbVersion(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder, OpenVSwitch openVSwitch) {
255 ovsdbNodeBuilder.setDbVersion(openVSwitch.getDbVersionColumn().getData().iterator().next());
256 } catch (NoSuchElementException e) {
257 LOG.debug("db_version is not set for this switch",e);
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);
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)
278 getOvsdbConnectionInstance().setInstanceIdentifier(iid);
280 return getOvsdbConnectionInstance().getInstanceIdentifier();
284 NodeId getNodeId(OpenVSwitch ovs) {
285 NodeKey nodeKey = getInstanceIdentifier(ovs).firstKeyOf(Node.class);
286 return nodeKey.getNodeId();