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 java.util.ArrayList;
13 import java.util.List;
15 import java.util.Map.Entry;
16 import java.util.NoSuchElementException;
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;
55 public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
57 private static final Logger LOG = LoggerFactory.getLogger(OpenVSwitchUpdateCommand.class);
59 public OpenVSwitchUpdateCommand(OvsdbConnectionInstance key, TableUpdates updates,
60 DatabaseSchema dbSchema) {
61 super(key, updates, dbSchema);
65 public void execute(ReadWriteTransaction transaction) {
66 Map<UUID, OpenVSwitch> updatedOpenVSwitchRows = TyperUtils
67 .extractRowsUpdated(OpenVSwitch.class, getUpdates(),
69 Map<UUID, OpenVSwitch> deletedOpenVSwitchRows = TyperUtils
70 .extractRowsOld(OpenVSwitch.class, getUpdates(),
73 for (Entry<UUID, OpenVSwitch> entry : updatedOpenVSwitchRows.entrySet()) {
74 OpenVSwitch openVSwitch = entry.getValue();
75 final InstanceIdentifier<Node> nodePath = getInstanceIdentifier(openVSwitch);
77 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
79 setDbVersion(ovsdbNodeBuilder, openVSwitch);
80 setOvsVersion(ovsdbNodeBuilder, openVSwitch);
81 setDataPathTypes(ovsdbNodeBuilder, openVSwitch);
82 setInterfaceTypes(ovsdbNodeBuilder, openVSwitch);
83 OpenVSwitch oldEntry = deletedOpenVSwitchRows.get(entry.getKey());
84 setExternalIds(transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
85 setOtherConfig(transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
86 ovsdbNodeBuilder.setConnectionInfo(getConnectionInfo());
88 NodeBuilder nodeBuilder = new NodeBuilder();
89 nodeBuilder.setNodeId(getNodeId(openVSwitch));
90 nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class,
91 ovsdbNodeBuilder.build());
92 transaction.merge(LogicalDatastoreType.OPERATIONAL, nodePath,
97 private void setOtherConfig(ReadWriteTransaction transaction,
98 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder, OpenVSwitch oldEntry, OpenVSwitch openVSwitch) {
99 Map<String, String> oldOtherConfigs = null;
100 Map<String, String> otherConfigs = null;
102 if (openVSwitch.getOtherConfigColumn() != null) {
103 otherConfigs = openVSwitch.getOtherConfigColumn().getData();
105 if (oldEntry != null && oldEntry.getOtherConfigColumn() != null) {
106 oldOtherConfigs = oldEntry.getOtherConfigColumn().getData();
108 if ((oldOtherConfigs == null) || oldOtherConfigs.isEmpty()) {
109 setNewOtherConfigs(ovsdbNodeBuilder, otherConfigs);
110 } else if (otherConfigs != null && !otherConfigs.isEmpty()) {
111 removeOldConfigs(transaction, oldOtherConfigs, openVSwitch);
112 setNewOtherConfigs(ovsdbNodeBuilder, otherConfigs);
116 private void removeOldConfigs(ReadWriteTransaction transaction, Map<String, String> oldOtherConfigs,
118 InstanceIdentifier<OvsdbNodeAugmentation> nodeAugmentataionIid = InstanceIdentifier
119 .create(NetworkTopology.class)
120 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
121 .child(Node.class, new NodeKey(getNodeId(ovs))).augmentation(OvsdbNodeAugmentation.class);
122 Set<String> otherConfigKeys = oldOtherConfigs.keySet();
123 for (String otherConfigKey : otherConfigKeys) {
124 KeyedInstanceIdentifier<OpenvswitchOtherConfigs, OpenvswitchOtherConfigsKey> externalIid =
126 .child(OpenvswitchOtherConfigs.class, new OpenvswitchOtherConfigsKey(otherConfigKey));
127 transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIid);
131 private void setNewOtherConfigs(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
132 Map<String, String> otherConfigs) {
133 Set<String> otherConfigKeys = otherConfigs.keySet();
134 List<OpenvswitchOtherConfigs> otherConfigsList = new ArrayList<>();
135 String otherConfigValue;
136 for (String otherConfigKey : otherConfigKeys) {
137 otherConfigValue = otherConfigs.get(otherConfigKey);
138 if (otherConfigKey != null && otherConfigValue != null) {
139 otherConfigsList.add(new OpenvswitchOtherConfigsBuilder().setOtherConfigKey(otherConfigKey)
140 .setOtherConfigValue(otherConfigValue).build());
143 ovsdbNodeBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
146 private void setExternalIds(ReadWriteTransaction transaction,
147 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder, OpenVSwitch oldEntry, OpenVSwitch openVSwitch) {
148 Map<String, String> oldExternalIds = null;
149 Map<String, String> externalIds = null;
151 if (openVSwitch.getExternalIdsColumn() != null) {
152 externalIds = openVSwitch.getExternalIdsColumn().getData();
154 if (oldEntry != null && oldEntry.getExternalIdsColumn() != null) {
155 oldExternalIds = oldEntry.getExternalIdsColumn().getData();
157 if ((oldExternalIds == null) || oldExternalIds.isEmpty()) {
158 setNewExternalIds(ovsdbNodeBuilder, externalIds);
159 } else if (externalIds != null && !externalIds.isEmpty()) {
160 removeExternalIds(transaction, oldExternalIds, openVSwitch);
161 setNewExternalIds(ovsdbNodeBuilder, externalIds);
165 private void removeExternalIds(ReadWriteTransaction transaction, Map<String, String> oldExternalIds,
167 InstanceIdentifier<OvsdbNodeAugmentation> nodeAugmentataionIid = InstanceIdentifier
168 .create(NetworkTopology.class)
169 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
170 .child(Node.class, new NodeKey(getNodeId(ovs))).augmentation(OvsdbNodeAugmentation.class);
171 Set<String> externalIdKeys = oldExternalIds.keySet();
172 for (String externalIdKey : externalIdKeys) {
173 KeyedInstanceIdentifier<OpenvswitchExternalIds, OpenvswitchExternalIdsKey> externalIid =
175 .child(OpenvswitchExternalIds.class, new OpenvswitchExternalIdsKey(externalIdKey));
176 transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIid);
180 private void setNewExternalIds(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
181 Map<String, String> externalIds) {
182 Set<String> externalIdKeys = externalIds.keySet();
183 List<OpenvswitchExternalIds> externalIdsList = new ArrayList<>();
184 String externalIdValue;
185 for (String externalIdKey : externalIdKeys) {
186 externalIdValue = externalIds.get(externalIdKey);
187 if (externalIdKey != null && externalIdValue != null) {
188 externalIdsList.add(new OpenvswitchExternalIdsBuilder().setExternalIdKey(externalIdKey)
189 .setExternalIdValue(externalIdValue).build());
192 ovsdbNodeBuilder.setOpenvswitchExternalIds(externalIdsList);
195 private void setInterfaceTypes(
196 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
197 OpenVSwitch openVSwitch) {
199 Set<String> iftypes = openVSwitch.getIfaceTypesColumn().getData();
200 List<InterfaceTypeEntry> ifEntryList = new ArrayList<>();
201 for (String ifType : iftypes) {
202 if (SouthboundMapper.createInterfaceType(ifType) != null) {
203 InterfaceTypeEntry ifEntry = new InterfaceTypeEntryBuilder()
205 SouthboundMapper.createInterfaceType(ifType))
207 ifEntryList.add(ifEntry);
209 LOG.warn("Interface type {} not present in model", ifType);
212 ovsdbNodeBuilder.setInterfaceTypeEntry(ifEntryList);
213 } catch (SchemaVersionMismatchException e) {
214 schemaMismatchLog("iface_types", SouthboundConstants.OPEN_V_SWITCH, e);
218 private void setDataPathTypes(
219 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
220 OpenVSwitch openVSwitch) {
222 Set<String> dptypes = openVSwitch.getDatapathTypesColumn()
224 List<DatapathTypeEntry> dpEntryList = new ArrayList<>();
225 for (String dpType : dptypes) {
226 if (SouthboundMapper.createDatapathType(dpType) != null) {
227 DatapathTypeEntry dpEntry = new DatapathTypeEntryBuilder()
229 SouthboundMapper.createDatapathType(dpType))
231 dpEntryList.add(dpEntry);
233 LOG.warn("Datapath type {} not present in model", dpType);
236 ovsdbNodeBuilder.setDatapathTypeEntry(dpEntryList);
237 } catch (SchemaVersionMismatchException e) {
238 schemaMismatchLog("datapath_types", SouthboundConstants.OPEN_V_SWITCH, e);
242 private void setOvsVersion(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder, OpenVSwitch openVSwitch) {
244 ovsdbNodeBuilder.setOvsVersion(openVSwitch.getOvsVersionColumn().getData().iterator().next());
245 } catch (NoSuchElementException e) {
246 LOG.debug("ovs_version is not set for this switch",e);
250 private void setDbVersion(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder, OpenVSwitch openVSwitch) {
252 ovsdbNodeBuilder.setDbVersion(openVSwitch.getDbVersionColumn().getData().iterator().next());
253 } catch (NoSuchElementException e) {
254 LOG.debug("db_version is not set for this switch",e);
258 private InstanceIdentifier<Node> getInstanceIdentifier(OpenVSwitch ovs) {
259 if (ovs.getExternalIdsColumn() != null
260 && ovs.getExternalIdsColumn().getData() != null
261 && ovs.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
262 String iidString = ovs.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
263 InstanceIdentifier<Node> iid =
264 (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
265 getOvsdbConnectionInstance().setInstanceIdentifier(iid);
267 String nodeString = SouthboundConstants.OVSDB_URI_PREFIX + "://" + SouthboundConstants.UUID + "/"
268 + ovs.getUuid().toString();
269 NodeId nodeId = new NodeId(new Uri(nodeString));
270 NodeKey nodeKey = new NodeKey(nodeId);
271 InstanceIdentifier<Node> iid = InstanceIdentifier.builder(NetworkTopology.class)
272 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
273 .child(Node.class,nodeKey)
275 getOvsdbConnectionInstance().setInstanceIdentifier(iid);
277 return getOvsdbConnectionInstance().getInstanceIdentifier();
280 private NodeId getNodeId(OpenVSwitch ovs) {
281 NodeKey nodeKey = getInstanceIdentifier(ovs).firstKeyOf(Node.class, NodeKey.class);
282 return nodeKey.getNodeId();