Merge "Fix for exception thrown when no ovs_version set."
[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.controller.md.sal.common.api.data.ReadFailedException;
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.SouthboundMapper;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.DatapathTypeEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.DatapathTypeEntryBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntry;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntryBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIdsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import com.google.common.base.Optional;
45
46 public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
47
48     private static final Logger LOG = LoggerFactory
49             .getLogger(OpenVSwitchUpdateCommand.class);
50
51     public OpenVSwitchUpdateCommand(ConnectionInfo key, TableUpdates updates,
52             DatabaseSchema dbSchema) {
53         super(key, updates, dbSchema);
54     }
55
56     @Override
57     public void execute(ReadWriteTransaction transaction) {
58         Map<UUID, OpenVSwitch> updatedOpenVSwitchRows = TyperUtils
59                 .extractRowsUpdated(OpenVSwitch.class, getUpdates(),
60                         getDbSchema());
61
62         for (Entry<UUID, OpenVSwitch> entry : updatedOpenVSwitchRows.entrySet()) {
63             OpenVSwitch openVSwitch = entry.getValue();
64             final InstanceIdentifier<Node> nodePath = SouthboundMapper.createInstanceIdentifier(getConnectionInfo());
65             Optional<Node> node = Optional.absent();
66             try {
67                 node = transaction.read(LogicalDatastoreType.OPERATIONAL,
68                         nodePath).checkedGet();
69             } catch (final ReadFailedException e) {
70                 LOG.debug("Read Operational/DS for Node fail! {}", nodePath, e);
71             }
72             if (node.isPresent()) {
73                 LOG.debug("Node {} is present", node);
74                 OvsdbNodeAugmentation ovsdbNode = SouthboundMapper
75                         .createOvsdbAugmentation(getConnectionInfo());
76                 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
77                 try {
78                     ovsdbNodeBuilder.setOvsVersion(openVSwitch.getOvsVersionColumn().getData().iterator().next());
79                 } catch (NoSuchElementException e) {
80                     LOG.debug("ovs_version is not set for this switch",e);
81                 }
82                 try {
83                     Set<String> dptypes = openVSwitch.getDatapathTypesColumn()
84                             .getData();
85                     List<DatapathTypeEntry> dpEntryList = new ArrayList<DatapathTypeEntry>();
86                     for (String dpType : dptypes) {
87                         DatapathTypeEntry dpEntry = new DatapathTypeEntryBuilder()
88                                 .setDatapathType(
89                                         SouthboundMapper.createDatapathType(dpType))
90                                 .build();
91                         dpEntryList.add(dpEntry);
92                     }
93                     ovsdbNodeBuilder.setDatapathTypeEntry(dpEntryList);
94                 } catch (SchemaVersionMismatchException e) {
95                     LOG.debug("Datapath types not supported by this version of ovsdb",e);
96                 }
97                 try {
98                     Set<String> iftypes = openVSwitch.getIfaceTypesColumn().getData();
99                     List<InterfaceTypeEntry> ifEntryList = new ArrayList<InterfaceTypeEntry>();
100                     for (String ifType : iftypes) {
101                         InterfaceTypeEntry ifEntry = new InterfaceTypeEntryBuilder()
102                                 .setInterfaceType(
103                                         SouthboundMapper.createInterfaceType(ifType))
104                                 .build();
105                         ifEntryList.add(ifEntry);
106                     }
107                     ovsdbNodeBuilder.setInterfaceTypeEntry(ifEntryList);
108                 } catch (SchemaVersionMismatchException e) {
109                     LOG.debug("Iface types  not supported by this version of ovsdb",e);;
110                 }
111
112                 Map<String, String> externalIds = openVSwitch.getExternalIdsColumn().getData();
113                 if (externalIds != null && !externalIds.isEmpty()) {
114                     Set<String> externalIdKeys = externalIds.keySet();
115                     List<OpenvswitchExternalIds> externalIdsList = new ArrayList<OpenvswitchExternalIds>();
116                     String externalIdValue;
117                     for (String externalIdKey : externalIdKeys) {
118                         externalIdValue = externalIds.get(externalIdKey);
119                         if (externalIdKey != null && externalIdValue != null) {
120                             externalIdsList.add(new OpenvswitchExternalIdsBuilder()
121                                     .setExternalIdKey(externalIdKey)
122                                     .setExternalIdValue(externalIdValue)
123                                     .build());
124                         }
125                     }
126                     ovsdbNodeBuilder.setOpenvswitchExternalIds(externalIdsList);
127                 }
128
129                 Map<String, String> otherConfigs = openVSwitch.getOtherConfigColumn().getData();
130                 if (otherConfigs != null && !otherConfigs.isEmpty()) {
131                     Set<String> otherConfigKeys = otherConfigs.keySet();
132                     List<OpenvswitchOtherConfigs> otherConfigsList = new ArrayList<OpenvswitchOtherConfigs>();
133                     String otherConfigValue;
134                     for (String otherConfigKey : otherConfigKeys) {
135                         otherConfigValue = otherConfigs.get(otherConfigKey);
136                         if (otherConfigKey != null && otherConfigValue != null) {
137                             otherConfigsList.add(new OpenvswitchOtherConfigsBuilder()
138                                     .setOtherConfigKey(otherConfigKey)
139                                     .setOtherConfigValue(otherConfigValue)
140                                     .build());
141                         }
142                     }
143                     ovsdbNodeBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
144                 }
145
146                 NodeBuilder nodeBuilder = new NodeBuilder();
147                 ConnectionInfo connectionInfo = ovsdbNode.getConnectionInfo();
148                 nodeBuilder.setNodeId(SouthboundMapper.createNodeId(
149                         connectionInfo.getRemoteIp(), connectionInfo.getRemotePort()));
150                 nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class,
151                         ovsdbNodeBuilder.build());
152                 transaction.merge(LogicalDatastoreType.OPERATIONAL, nodePath,
153                         nodeBuilder.build());
154             }
155         }
156     }
157 }