Merge "Added support to update flows for induvidual security rule add/remove , after...
[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.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;
53
54 public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
55
56     private static final Logger LOG = LoggerFactory.getLogger(OpenVSwitchUpdateCommand.class);
57
58     public OpenVSwitchUpdateCommand(OvsdbConnectionInstance key, TableUpdates updates,
59             DatabaseSchema dbSchema) {
60         super(key, updates, dbSchema);
61     }
62
63     @Override
64     public void execute(ReadWriteTransaction transaction) {
65         Map<UUID, OpenVSwitch> updatedOpenVSwitchRows = TyperUtils
66                 .extractRowsUpdated(OpenVSwitch.class, getUpdates(),
67                         getDbSchema());
68         Map<UUID, OpenVSwitch> deletedOpenVSwitchRows = TyperUtils
69                 .extractRowsOld(OpenVSwitch.class, getUpdates(),
70                         getDbSchema());
71
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);
76
77             OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
78
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());
85
86             NodeBuilder nodeBuilder = new NodeBuilder();
87             nodeBuilder.setNodeId(getNodeId(openVSwitch));
88             nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class,
89                     ovsdbNodeBuilder.build());
90             transaction.merge(LogicalDatastoreType.OPERATIONAL, nodePath,
91                     nodeBuilder.build());
92         }
93     }
94
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;
99
100         if (openVSwitch.getOtherConfigColumn() != null) {
101             otherConfigs = openVSwitch.getOtherConfigColumn().getData();
102         }
103         if (oldEntry != null && oldEntry.getOtherConfigColumn() != null) {
104             oldOtherConfigs = oldEntry.getOtherConfigColumn().getData();
105         }
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);
111         }
112     }
113
114     private void removeOldConfigs(ReadWriteTransaction transaction, Map<String, String> oldOtherConfigs,
115             OpenVSwitch ovs) {
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 =
123                     nodeAugmentataionIid
124                     .child(OpenvswitchOtherConfigs.class, new OpenvswitchOtherConfigsKey(otherConfigKey));
125             transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIid);
126         }
127     }
128
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());
139             }
140         }
141         ovsdbNodeBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
142     }
143
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;
148
149         if (openVSwitch.getExternalIdsColumn() != null) {
150             externalIds = openVSwitch.getExternalIdsColumn().getData();
151         }
152         if (oldEntry != null && oldEntry.getExternalIdsColumn() != null) {
153             oldExternalIds = oldEntry.getExternalIdsColumn().getData();
154         }
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);
160         }
161     }
162
163     private void removeExternalIds(ReadWriteTransaction transaction, Map<String, String> oldExternalIds,
164             OpenVSwitch ovs) {
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 =
172                     nodeAugmentataionIid
173                     .child(OpenvswitchExternalIds.class, new OpenvswitchExternalIdsKey(externalIdKey));
174             transaction.delete(LogicalDatastoreType.OPERATIONAL, externalIid);
175         }
176     }
177
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());
188             }
189         }
190         ovsdbNodeBuilder.setOpenvswitchExternalIds(externalIdsList);
191     }
192
193     private void setInterfaceTypes(
194             OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
195             OpenVSwitch openVSwitch) {
196         try {
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()
202                             .setInterfaceType(
203                                     SouthboundMapper.createInterfaceType(ifType))
204                             .build();
205                     ifEntryList.add(ifEntry);
206                 } else {
207                     LOG.warn("Interface type {} not present in model", ifType);
208                 }
209             }
210             ovsdbNodeBuilder.setInterfaceTypeEntry(ifEntryList);
211         } catch (SchemaVersionMismatchException e) {
212             // We don't care about the exception stack trace here
213             LOG.debug("Iface types  not supported by this version of ovsdb: {}", e.getMessage());
214         }
215     }
216
217     private void setDataPathTypes(
218             OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
219             OpenVSwitch openVSwitch) {
220         try {
221             Set<String> dptypes = openVSwitch.getDatapathTypesColumn()
222                     .getData();
223             List<DatapathTypeEntry> dpEntryList = new ArrayList<>();
224             for (String dpType : dptypes) {
225                 if (SouthboundMapper.createDatapathType(dpType) != null) {
226                     DatapathTypeEntry dpEntry = new DatapathTypeEntryBuilder()
227                             .setDatapathType(
228                                     SouthboundMapper.createDatapathType(dpType))
229                             .build();
230                     dpEntryList.add(dpEntry);
231                 } else {
232                     LOG.warn("Datapath type {} not present in model", dpType);
233                 }
234             }
235             ovsdbNodeBuilder.setDatapathTypeEntry(dpEntryList);
236         } catch (SchemaVersionMismatchException e) {
237             // We don't care about the exception stack trace here
238             LOG.debug("Datapath types not supported by this version of ovsdb: {}", e.getMessage());
239         }
240     }
241
242     private void setVersion(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder,
243             OpenVSwitch openVSwitch) {
244         try {
245             ovsdbNodeBuilder.setOvsVersion(openVSwitch.getOvsVersionColumn().getData().iterator().next());
246         } catch (NoSuchElementException e) {
247             LOG.debug("ovs_version is not set for this switch",e);
248         }
249     }
250
251     private InstanceIdentifier<Node> getInstanceIdentifier(OpenVSwitch ovs) {
252         if (ovs.getExternalIdsColumn() != null
253                 && ovs.getExternalIdsColumn().getData() != null
254                 && ovs.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
255             String iidString = ovs.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
256             InstanceIdentifier<Node> iid =
257                    (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
258             getOvsdbConnectionInstance().setInstanceIdentifier(iid);
259         } else {
260             String nodeString = SouthboundConstants.OVSDB_URI_PREFIX + "://" + SouthboundConstants.UUID + "/"
261                     + ovs.getUuid().toString();
262             NodeId nodeId = new NodeId(new Uri(nodeString));
263             NodeKey nodeKey = new NodeKey(nodeId);
264             InstanceIdentifier<Node> iid = InstanceIdentifier.builder(NetworkTopology.class)
265                     .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
266                     .child(Node.class,nodeKey)
267                     .build();
268             getOvsdbConnectionInstance().setInstanceIdentifier(iid);
269         }
270         return getOvsdbConnectionInstance().getInstanceIdentifier();
271     }
272
273     private NodeId getNodeId(OpenVSwitch ovs) {
274         NodeKey nodeKey = getInstanceIdentifier(ovs).firstKeyOf(Node.class, NodeKey.class);
275         return nodeKey.getNodeId();
276     }
277 }