Bug 3310 Bug 3316 Use iid stamp if we configured a connection, otherwise systemid
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OpenVSwitchUpdateCommand.java
1 /*
2  * Copyright (c) 2015 Inocybe inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.ovsdb.southbound.transactions.md;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import java.util.NoSuchElementException;
15 import java.util.Set;
16
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;
51
52 public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
53
54     private static final Logger LOG = LoggerFactory
55             .getLogger(OpenVSwitchUpdateCommand.class);
56
57     public OpenVSwitchUpdateCommand(OvsdbConnectionInstance key, TableUpdates updates,
58             DatabaseSchema dbSchema) {
59         super(key, updates, dbSchema);
60     }
61
62     @Override
63     public void execute(ReadWriteTransaction transaction) {
64         Map<UUID, OpenVSwitch> updatedOpenVSwitchRows = TyperUtils
65                 .extractRowsUpdated(OpenVSwitch.class, getUpdates(),
66                         getDbSchema());
67
68         for (Entry<UUID, OpenVSwitch> entry : updatedOpenVSwitchRows.entrySet()) {
69             OpenVSwitch openVSwitch = entry.getValue();
70             final InstanceIdentifier<Node> nodePath = getInstanceIdentifier(openVSwitch);
71
72             OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
73
74             setVersion(ovsdbNodeBuilder, openVSwitch);
75             setDataPathTypes(ovsdbNodeBuilder, openVSwitch);
76             setInterfaceTypes(ovsdbNodeBuilder, openVSwitch);
77             setExternalIds(ovsdbNodeBuilder, openVSwitch);
78             setOtherConfig(ovsdbNodeBuilder, openVSwitch);
79             ovsdbNodeBuilder.setConnectionInfo(getConnectionInfo());
80
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,
87                     nodeBuilder.build());
88         }
89     }
90
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)
104                             .build());
105                 }
106             }
107             ovsdbNodeBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
108         }
109     }
110
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)
124                             .build());
125                 }
126             }
127             ovsdbNodeBuilder.setOpenvswitchExternalIds(externalIdsList);
128         }
129     }
130
131     private void setInterfaceTypes(
132             OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
133             OpenVSwitch openVSwitch) {
134         try {
135             Set<String> iftypes = openVSwitch.getIfaceTypesColumn().getData();
136             List<InterfaceTypeEntry> ifEntryList = new ArrayList<InterfaceTypeEntry>();
137             for (String ifType : iftypes) {
138                 InterfaceTypeEntry ifEntry = new InterfaceTypeEntryBuilder()
139                         .setInterfaceType(
140                                 SouthboundMapper.createInterfaceType(ifType))
141                         .build();
142                 ifEntryList.add(ifEntry);
143             }
144             ovsdbNodeBuilder.setInterfaceTypeEntry(ifEntryList);
145         } catch (SchemaVersionMismatchException e) {
146             LOG.debug("Iface types  not supported by this version of ovsdb",e);;
147         }
148     }
149
150     private void setDataPathTypes(
151             OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
152             OpenVSwitch openVSwitch) {
153         try {
154             Set<String> dptypes = openVSwitch.getDatapathTypesColumn()
155                     .getData();
156             List<DatapathTypeEntry> dpEntryList = new ArrayList<DatapathTypeEntry>();
157             for (String dpType : dptypes) {
158                 DatapathTypeEntry dpEntry = new DatapathTypeEntryBuilder()
159                         .setDatapathType(
160                                 SouthboundMapper.createDatapathType(dpType))
161                         .build();
162                 dpEntryList.add(dpEntry);
163             }
164             ovsdbNodeBuilder.setDatapathTypeEntry(dpEntryList);
165         } catch (SchemaVersionMismatchException e) {
166             LOG.debug("Datapath types not supported by this version of ovsdb",e);
167         }
168     }
169
170     private void setVersion(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
171             OpenVSwitch openVSwitch) {
172         try {
173             ovsdbNodeBuilder.setOvsVersion(openVSwitch.getOvsVersionColumn().getData().iterator().next());
174         } catch (NoSuchElementException e) {
175             LOG.debug("ovs_version is not set for this switch",e);
176         }
177     }
178
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);
187         } else {
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)
195                     .build();
196             getOvsdbConnectionInstance().setInstanceIdentifier(iid);
197         }
198         return getOvsdbConnectionInstance().getInstanceIdentifier();
199     }
200
201     private NodeId getNodeId(OpenVSwitch ovs) {
202         NodeKey nodeKey = getInstanceIdentifier(ovs).firstKeyOf(Node.class, NodeKey.class);
203         return nodeKey.getNodeId();
204     }
205 }