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.ConnectionInfo;
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.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.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
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.network.topology.Topology;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
54 private static final Logger LOG = LoggerFactory
55 .getLogger(OpenVSwitchUpdateCommand.class);
57 public OpenVSwitchUpdateCommand(OvsdbConnectionInstance key, TableUpdates updates,
58 DatabaseSchema dbSchema) {
59 super(key, updates, dbSchema);
63 public void execute(ReadWriteTransaction transaction) {
64 Map<UUID, OpenVSwitch> updatedOpenVSwitchRows = TyperUtils
65 .extractRowsUpdated(OpenVSwitch.class, getUpdates(),
68 for (Entry<UUID, OpenVSwitch> entry : updatedOpenVSwitchRows.entrySet()) {
69 OpenVSwitch openVSwitch = entry.getValue();
70 final InstanceIdentifier<Node> nodePath = getInstanceIdentifier(openVSwitch);
72 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
74 setVersion(ovsdbNodeBuilder, openVSwitch);
75 setDataPathTypes(ovsdbNodeBuilder, openVSwitch);
76 setInterfaceTypes(ovsdbNodeBuilder, openVSwitch);
77 setExternalIds(ovsdbNodeBuilder, openVSwitch);
78 setOtherConfig(ovsdbNodeBuilder, openVSwitch);
79 ovsdbNodeBuilder.setConnectionInfo(getConnectionInfo());
81 NodeBuilder nodeBuilder = new NodeBuilder();
82 ConnectionInfo connectionInfo = getConnectionInfo();
83 nodeBuilder.setNodeId(getNodeId(openVSwitch));
84 nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class,
85 ovsdbNodeBuilder.build());
86 transaction.merge(LogicalDatastoreType.OPERATIONAL, nodePath,
91 private void setOtherConfig(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
92 OpenVSwitch openVSwitch) {
93 Map<String, String> otherConfigs = openVSwitch.getOtherConfigColumn().getData();
94 if (otherConfigs != null && !otherConfigs.isEmpty()) {
95 Set<String> otherConfigKeys = otherConfigs.keySet();
96 List<OpenvswitchOtherConfigs> otherConfigsList = new ArrayList<OpenvswitchOtherConfigs>();
97 String otherConfigValue;
98 for (String otherConfigKey : otherConfigKeys) {
99 otherConfigValue = otherConfigs.get(otherConfigKey);
100 if (otherConfigKey != null && otherConfigValue != null) {
101 otherConfigsList.add(new OpenvswitchOtherConfigsBuilder()
102 .setOtherConfigKey(otherConfigKey)
103 .setOtherConfigValue(otherConfigValue)
107 ovsdbNodeBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
111 private void setExternalIds(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
112 OpenVSwitch openVSwitch) {
113 Map<String, String> externalIds = openVSwitch.getExternalIdsColumn().getData();
114 if (externalIds != null && !externalIds.isEmpty()) {
115 Set<String> externalIdKeys = externalIds.keySet();
116 List<OpenvswitchExternalIds> externalIdsList = new ArrayList<OpenvswitchExternalIds>();
117 String externalIdValue;
118 for (String externalIdKey : externalIdKeys) {
119 externalIdValue = externalIds.get(externalIdKey);
120 if (externalIdKey != null && externalIdValue != null) {
121 externalIdsList.add(new OpenvswitchExternalIdsBuilder()
122 .setExternalIdKey(externalIdKey)
123 .setExternalIdValue(externalIdValue)
127 ovsdbNodeBuilder.setOpenvswitchExternalIds(externalIdsList);
131 private void setInterfaceTypes(
132 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
133 OpenVSwitch openVSwitch) {
135 Set<String> iftypes = openVSwitch.getIfaceTypesColumn().getData();
136 List<InterfaceTypeEntry> ifEntryList = new ArrayList<InterfaceTypeEntry>();
137 for (String ifType : iftypes) {
138 InterfaceTypeEntry ifEntry = new InterfaceTypeEntryBuilder()
140 SouthboundMapper.createInterfaceType(ifType))
142 ifEntryList.add(ifEntry);
144 ovsdbNodeBuilder.setInterfaceTypeEntry(ifEntryList);
145 } catch (SchemaVersionMismatchException e) {
146 LOG.debug("Iface types not supported by this version of ovsdb",e);;
150 private void setDataPathTypes(
151 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
152 OpenVSwitch openVSwitch) {
154 Set<String> dptypes = openVSwitch.getDatapathTypesColumn()
156 List<DatapathTypeEntry> dpEntryList = new ArrayList<DatapathTypeEntry>();
157 for (String dpType : dptypes) {
158 DatapathTypeEntry dpEntry = new DatapathTypeEntryBuilder()
160 SouthboundMapper.createDatapathType(dpType))
162 dpEntryList.add(dpEntry);
164 ovsdbNodeBuilder.setDatapathTypeEntry(dpEntryList);
165 } catch (SchemaVersionMismatchException e) {
166 LOG.debug("Datapath types not supported by this version of ovsdb",e);
170 private void setVersion(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
171 OpenVSwitch openVSwitch) {
173 ovsdbNodeBuilder.setOvsVersion(openVSwitch.getOvsVersionColumn().getData().iterator().next());
174 } catch (NoSuchElementException e) {
175 LOG.debug("ovs_version is not set for this switch",e);
179 private InstanceIdentifier<Node> getInstanceIdentifier(OpenVSwitch ovs) {
180 if (ovs.getExternalIdsColumn() != null
181 && ovs.getExternalIdsColumn().getData() != null
182 && ovs.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
183 String iidString = ovs.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
184 InstanceIdentifier<Node> iid =
185 (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
186 getOvsdbConnectionInstance().setInstanceIdentifier(iid);
188 String nodeString = SouthboundConstants.OVSDB_URI_PREFIX + "://" + SouthboundConstants.UUID + "/"
189 + ovs.getUuid().toString();
190 NodeId nodeId = new NodeId(new Uri(nodeString));
191 NodeKey nodeKey = new NodeKey(nodeId);
192 InstanceIdentifier<Node> iid = InstanceIdentifier.builder(NetworkTopology.class)
193 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
194 .child(Node.class,nodeKey)
196 getOvsdbConnectionInstance().setInstanceIdentifier(iid);
198 return getOvsdbConnectionInstance().getInstanceIdentifier();
201 private NodeId getNodeId(OpenVSwitch ovs) {
202 NodeKey nodeKey = getInstanceIdentifier(ovs).firstKeyOf(Node.class, NodeKey.class);
203 return nodeKey.getNodeId();