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 java.util.ArrayList;
11 import java.util.List;
13 import java.util.Map.Entry;
14 import java.util.NoSuchElementException;
17 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
20 import org.opendaylight.ovsdb.lib.message.TableUpdates;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
23 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
24 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
25 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
26 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
27 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
28 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.DatapathTypeEntry;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.DatapathTypeEntryBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntry;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntryBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIdsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIdsKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigsKey;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
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.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.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
56 private static final Logger LOG = LoggerFactory.getLogger(OpenVSwitchUpdateCommand.class);
58 public OpenVSwitchUpdateCommand(OvsdbConnectionInstance key, TableUpdates updates,
59 DatabaseSchema dbSchema) {
60 super(key, updates, dbSchema);
64 public void execute(ReadWriteTransaction transaction) {
65 Map<UUID, OpenVSwitch> updatedOpenVSwitchRows = TyperUtils
66 .extractRowsUpdated(OpenVSwitch.class, getUpdates(),
68 Map<UUID, OpenVSwitch> deletedOpenVSwitchRows = TyperUtils
69 .extractRowsOld(OpenVSwitch.class, getUpdates(),
72 for (Entry<UUID, OpenVSwitch> entry : updatedOpenVSwitchRows.entrySet()) {
73 OpenVSwitch openVSwitch = entry.getValue();
74 OpenVSwitch oldEntry = deletedOpenVSwitchRows.get(entry.getKey());
75 final InstanceIdentifier<Node> nodePath = getInstanceIdentifier(openVSwitch);
77 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
79 setVersion(ovsdbNodeBuilder, openVSwitch);
80 setDataPathTypes(ovsdbNodeBuilder, openVSwitch);
81 setInterfaceTypes(ovsdbNodeBuilder, openVSwitch);
82 setExternalIds(transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
83 setOtherConfig(transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
84 ovsdbNodeBuilder.setConnectionInfo(getConnectionInfo());
86 NodeBuilder nodeBuilder = new NodeBuilder();
87 nodeBuilder.setNodeId(getNodeId(openVSwitch));
88 nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class,
89 ovsdbNodeBuilder.build());
90 transaction.merge(LogicalDatastoreType.OPERATIONAL, nodePath,
95 private void setOtherConfig(ReadWriteTransaction transaction,
96 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder, OpenVSwitch oldEntry, OpenVSwitch openVSwitch) {
97 Map<String, String> oldOtherConfigs = null;
98 Map<String, String> otherConfigs = null;
100 if (openVSwitch.getOtherConfigColumn() != null) {
101 otherConfigs = openVSwitch.getOtherConfigColumn().getData();
103 if (oldEntry != null && oldEntry.getOtherConfigColumn() != null) {
104 oldOtherConfigs = oldEntry.getOtherConfigColumn().getData();
106 if ((oldOtherConfigs == null) || oldOtherConfigs.isEmpty()) {
107 setNewOtherConfigs(ovsdbNodeBuilder, otherConfigs);
108 } else if (otherConfigs != null && !otherConfigs.isEmpty()) {
109 removeOldConfigs(transaction, oldOtherConfigs, openVSwitch);
110 setNewOtherConfigs(ovsdbNodeBuilder, otherConfigs);
114 private void removeOldConfigs(ReadWriteTransaction transaction, Map<String, String> oldOtherConfigs,
116 InstanceIdentifier<OvsdbNodeAugmentation> nodeAugmentataionIid = InstanceIdentifier
117 .create(NetworkTopology.class)
118 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
119 .child(Node.class, new NodeKey(getNodeId(ovs))).augmentation(OvsdbNodeAugmentation.class);
120 Set<String> otherConfigKeys = oldOtherConfigs.keySet();
121 for (String otherConfigKey : otherConfigKeys) {
122 KeyedInstanceIdentifier<OpenvswitchOtherConfigs, OpenvswitchOtherConfigsKey> externalIid =
124 .child(OpenvswitchOtherConfigs.class, new OpenvswitchOtherConfigsKey(otherConfigKey));
125 transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIid);
129 private void setNewOtherConfigs(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
130 Map<String, String> otherConfigs) {
131 Set<String> otherConfigKeys = otherConfigs.keySet();
132 List<OpenvswitchOtherConfigs> otherConfigsList = new ArrayList<>();
133 String otherConfigValue;
134 for (String otherConfigKey : otherConfigKeys) {
135 otherConfigValue = otherConfigs.get(otherConfigKey);
136 if (otherConfigKey != null && otherConfigValue != null) {
137 otherConfigsList.add(new OpenvswitchOtherConfigsBuilder().setOtherConfigKey(otherConfigKey)
138 .setOtherConfigValue(otherConfigValue).build());
141 ovsdbNodeBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
144 private void setExternalIds(ReadWriteTransaction transaction,
145 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder, OpenVSwitch oldEntry, OpenVSwitch openVSwitch) {
146 Map<String, String> oldExternalIds = null;
147 Map<String, String> externalIds = null;
149 if (openVSwitch.getExternalIdsColumn() != null) {
150 externalIds = openVSwitch.getExternalIdsColumn().getData();
152 if (oldEntry != null && oldEntry.getExternalIdsColumn() != null) {
153 oldExternalIds = oldEntry.getExternalIdsColumn().getData();
155 if ((oldExternalIds == null) || oldExternalIds.isEmpty()) {
156 setNewExternalIds(ovsdbNodeBuilder, externalIds);
157 } else if (externalIds != null && !externalIds.isEmpty()) {
158 removeExternalIds(transaction, oldExternalIds, openVSwitch);
159 setNewExternalIds(ovsdbNodeBuilder, externalIds);
163 private void removeExternalIds(ReadWriteTransaction transaction, Map<String, String> oldExternalIds,
165 InstanceIdentifier<OvsdbNodeAugmentation> nodeAugmentataionIid = InstanceIdentifier
166 .create(NetworkTopology.class)
167 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
168 .child(Node.class, new NodeKey(getNodeId(ovs))).augmentation(OvsdbNodeAugmentation.class);
169 Set<String> externalIdKeys = oldExternalIds.keySet();
170 for (String externalIdKey : externalIdKeys) {
171 KeyedInstanceIdentifier<OpenvswitchExternalIds, OpenvswitchExternalIdsKey> externalIid =
173 .child(OpenvswitchExternalIds.class, new OpenvswitchExternalIdsKey(externalIdKey));
174 transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIid);
178 private void setNewExternalIds(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
179 Map<String, String> externalIds) {
180 Set<String> externalIdKeys = externalIds.keySet();
181 List<OpenvswitchExternalIds> externalIdsList = new ArrayList<>();
182 String externalIdValue;
183 for (String externalIdKey : externalIdKeys) {
184 externalIdValue = externalIds.get(externalIdKey);
185 if (externalIdKey != null && externalIdValue != null) {
186 externalIdsList.add(new OpenvswitchExternalIdsBuilder().setExternalIdKey(externalIdKey)
187 .setExternalIdValue(externalIdValue).build());
190 ovsdbNodeBuilder.setOpenvswitchExternalIds(externalIdsList);
193 private void setInterfaceTypes(
194 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
195 OpenVSwitch openVSwitch) {
197 Set<String> iftypes = openVSwitch.getIfaceTypesColumn().getData();
198 List<InterfaceTypeEntry> ifEntryList = new ArrayList<>();
199 for (String ifType : iftypes) {
200 if (SouthboundMapper.createInterfaceType(ifType) != null) {
201 InterfaceTypeEntry ifEntry = new InterfaceTypeEntryBuilder()
203 SouthboundMapper.createInterfaceType(ifType))
205 ifEntryList.add(ifEntry);
207 LOG.warn("Interface type {} not present in model", ifType);
210 ovsdbNodeBuilder.setInterfaceTypeEntry(ifEntryList);
211 } catch (SchemaVersionMismatchException e) {
212 LOG.debug("Iface types not supported by this version of ovsdb",e);
216 private void setDataPathTypes(
217 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
218 OpenVSwitch openVSwitch) {
220 Set<String> dptypes = openVSwitch.getDatapathTypesColumn()
222 List<DatapathTypeEntry> dpEntryList = new ArrayList<>();
223 for (String dpType : dptypes) {
224 if (SouthboundMapper.createDatapathType(dpType) != null) {
225 DatapathTypeEntry dpEntry = new DatapathTypeEntryBuilder()
227 SouthboundMapper.createDatapathType(dpType))
229 dpEntryList.add(dpEntry);
231 LOG.warn("Datapath type {} not present in model", dpType);
234 ovsdbNodeBuilder.setDatapathTypeEntry(dpEntryList);
235 } catch (SchemaVersionMismatchException e) {
236 LOG.debug("Datapath types not supported by this version of ovsdb",e);
240 private void setVersion(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
241 OpenVSwitch openVSwitch) {
243 ovsdbNodeBuilder.setOvsVersion(openVSwitch.getOvsVersionColumn().getData().iterator().next());
244 } catch (NoSuchElementException e) {
245 LOG.debug("ovs_version is not set for this switch",e);
249 private InstanceIdentifier<Node> getInstanceIdentifier(OpenVSwitch ovs) {
250 if (ovs.getExternalIdsColumn() != null
251 && ovs.getExternalIdsColumn().getData() != null
252 && ovs.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
253 String iidString = ovs.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
254 InstanceIdentifier<Node> iid =
255 (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
256 getOvsdbConnectionInstance().setInstanceIdentifier(iid);
258 String nodeString = SouthboundConstants.OVSDB_URI_PREFIX + "://" + SouthboundConstants.UUID + "/"
259 + ovs.getUuid().toString();
260 NodeId nodeId = new NodeId(new Uri(nodeString));
261 NodeKey nodeKey = new NodeKey(nodeId);
262 InstanceIdentifier<Node> iid = InstanceIdentifier.builder(NetworkTopology.class)
263 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
264 .child(Node.class,nodeKey)
266 getOvsdbConnectionInstance().setInstanceIdentifier(iid);
268 return getOvsdbConnectionInstance().getInstanceIdentifier();
271 private NodeId getNodeId(OpenVSwitch ovs) {
272 NodeKey nodeKey = getInstanceIdentifier(ovs).firstKeyOf(Node.class);
273 return nodeKey.getNodeId();