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
57 .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 OpenVSwitch oldEntry = deletedOpenVSwitchRows.get(entry.getKey());
76 final InstanceIdentifier<Node> nodePath = getInstanceIdentifier(openVSwitch);
78 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
80 setVersion(ovsdbNodeBuilder, openVSwitch);
81 setDataPathTypes(ovsdbNodeBuilder, openVSwitch);
82 setInterfaceTypes(ovsdbNodeBuilder, openVSwitch);
83 setExternalIds(transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
84 setOtherConfig(transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
85 ovsdbNodeBuilder.setConnectionInfo(getConnectionInfo());
87 NodeBuilder nodeBuilder = new NodeBuilder();
88 nodeBuilder.setNodeId(getNodeId(openVSwitch));
89 nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class,
90 ovsdbNodeBuilder.build());
91 transaction.merge(LogicalDatastoreType.OPERATIONAL, nodePath,
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;
101 if (openVSwitch.getOtherConfigColumn() != null) {
102 otherConfigs = openVSwitch.getOtherConfigColumn().getData();
104 if (oldEntry != null && oldEntry.getOtherConfigColumn() != null) {
105 oldOtherConfigs = oldEntry.getOtherConfigColumn().getData();
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);
115 private void removeOldConfigs(ReadWriteTransaction transaction, Map<String, String> oldOtherConfigs,
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 =
125 .child(OpenvswitchOtherConfigs.class, new OpenvswitchOtherConfigsKey(otherConfigKey));
126 transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIid);
130 private void setNewOtherConfigs(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
131 Map<String, String> otherConfigs) {
132 Set<String> otherConfigKeys = otherConfigs.keySet();
133 List<OpenvswitchOtherConfigs> otherConfigsList = new ArrayList<OpenvswitchOtherConfigs>();
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());
142 ovsdbNodeBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
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;
150 if (openVSwitch.getExternalIdsColumn() != null) {
151 externalIds = openVSwitch.getExternalIdsColumn().getData();
153 if (oldEntry != null && oldEntry.getExternalIdsColumn() != null) {
154 oldExternalIds = oldEntry.getExternalIdsColumn().getData();
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);
164 private void removeExternalIds(ReadWriteTransaction transaction, Map<String, String> oldExternalIds,
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 =
174 .child(OpenvswitchExternalIds.class, new OpenvswitchExternalIdsKey(externalIdKey));
175 transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIid);
179 private void setNewExternalIds(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
180 Map<String, String> externalIds) {
181 Set<String> externalIdKeys = externalIds.keySet();
182 List<OpenvswitchExternalIds> externalIdsList = new ArrayList<OpenvswitchExternalIds>();
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());
191 ovsdbNodeBuilder.setOpenvswitchExternalIds(externalIdsList);
194 private void setInterfaceTypes(
195 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
196 OpenVSwitch openVSwitch) {
198 Set<String> iftypes = openVSwitch.getIfaceTypesColumn().getData();
199 List<InterfaceTypeEntry> ifEntryList = new ArrayList<InterfaceTypeEntry>();
200 for (String ifType : iftypes) {
201 if (SouthboundMapper.createInterfaceType(ifType) != null) {
202 InterfaceTypeEntry ifEntry = new InterfaceTypeEntryBuilder()
204 SouthboundMapper.createInterfaceType(ifType))
206 ifEntryList.add(ifEntry);
208 LOG.warn("Interface type {} not present in model", ifType);
211 ovsdbNodeBuilder.setInterfaceTypeEntry(ifEntryList);
212 } catch (SchemaVersionMismatchException e) {
213 LOG.debug("Iface types not supported by this version of ovsdb",e);;
217 private void setDataPathTypes(
218 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
219 OpenVSwitch openVSwitch) {
221 Set<String> dptypes = openVSwitch.getDatapathTypesColumn()
223 List<DatapathTypeEntry> dpEntryList = new ArrayList<DatapathTypeEntry>();
224 for (String dpType : dptypes) {
225 if (SouthboundMapper.createDatapathType(dpType) != null) {
226 DatapathTypeEntry dpEntry = new DatapathTypeEntryBuilder()
228 SouthboundMapper.createDatapathType(dpType))
230 dpEntryList.add(dpEntry);
232 LOG.warn("Datapath type {} not present in model", dpType);
235 ovsdbNodeBuilder.setDatapathTypeEntry(dpEntryList);
236 } catch (SchemaVersionMismatchException e) {
237 LOG.debug("Datapath types not supported by this version of ovsdb",e);
241 private void setVersion(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
242 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 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);
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)
267 getOvsdbConnectionInstance().setInstanceIdentifier(iid);
269 return getOvsdbConnectionInstance().getInstanceIdentifier();
272 private NodeId getNodeId(OpenVSwitch ovs) {
273 NodeKey nodeKey = getInstanceIdentifier(ovs).firstKeyOf(Node.class, NodeKey.class);
274 return nodeKey.getNodeId();