Introducing ConnectionInfo
[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.Set;
15
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
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.OvsdbClientKey;
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(OvsdbClientKey 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 = getKey()
65                     .toInstanceIndentifier();
66             Optional<Node> node = Optional.absent();
67             try {
68                 node = transaction.read(LogicalDatastoreType.OPERATIONAL,
69                         nodePath).checkedGet();
70             } catch (final ReadFailedException e) {
71                 LOG.debug("Read Operational/DS for Node fail! {}", nodePath, e);
72             }
73             if (node.isPresent()) {
74                 LOG.debug("Node {} is present", node);
75                 OvsdbNodeAugmentation ovsdbNode = SouthboundMapper
76                         .createOvsdbAugmentation(getKey());
77                 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
78                 ovsdbNodeBuilder.setOvsVersion(openVSwitch.getVersion()
79                         .toString());
80                 try {
81                     Set<String> dptypes = openVSwitch.getDatapathTypesColumn()
82                             .getData();
83                     List<DatapathTypeEntry> dpEntryList = new ArrayList<DatapathTypeEntry>();
84                     for (String dpType : dptypes) {
85                         DatapathTypeEntry dpEntry = new DatapathTypeEntryBuilder()
86                                 .setDatapathType(
87                                         SouthboundMapper.createDatapathType(dpType))
88                                 .build();
89                         dpEntryList.add(dpEntry);
90                     }
91                     ovsdbNodeBuilder.setDatapathTypeEntry(dpEntryList);
92                 } catch (SchemaVersionMismatchException e) {
93                     LOG.debug("Datapath types not supported by this version of ovsdb",e);;
94                 }
95                 try {
96                     Set<String> iftypes = openVSwitch.getIfaceTypesColumn().getData();
97                     List<InterfaceTypeEntry> ifEntryList = new ArrayList<InterfaceTypeEntry>();
98                     for (String ifType : iftypes) {
99                         InterfaceTypeEntry ifEntry = new InterfaceTypeEntryBuilder()
100                                 .setInterfaceType(
101                                         SouthboundMapper.createInterfaceType(ifType))
102                                 .build();
103                         ifEntryList.add(ifEntry);
104                     }
105                     ovsdbNodeBuilder.setInterfaceTypeEntry(ifEntryList);
106                 } catch (SchemaVersionMismatchException e) {
107                     LOG.debug("Iface types  not supported by this version of ovsdb",e);;
108                 }
109
110                 Map<String, String> externalIds = openVSwitch.getExternalIdsColumn().getData();
111                 if (externalIds != null && !externalIds.isEmpty()) {
112                     Set<String> externalIdKeys = externalIds.keySet();
113                     List<OpenvswitchExternalIds> externalIdsList = new ArrayList<OpenvswitchExternalIds>();
114                     String externalIdValue;
115                     for (String externalIdKey : externalIdKeys) {
116                         externalIdValue = externalIds.get(externalIdKey);
117                         if (externalIdKey != null && externalIdValue != null) {
118                             externalIdsList.add(new OpenvswitchExternalIdsBuilder()
119                                     .setExternalIdKey(externalIdKey)
120                                     .setExternalIdValue(externalIdValue)
121                                     .build());
122                         }
123                     }
124                     ovsdbNodeBuilder.setOpenvswitchExternalIds(externalIdsList);
125                 }
126
127                 Map<String, String> otherConfigs = openVSwitch.getOtherConfigColumn().getData();
128                 if (otherConfigs != null && !otherConfigs.isEmpty()) {
129                     Set<String> otherConfigKeys = otherConfigs.keySet();
130                     List<OpenvswitchOtherConfigs> otherConfigsList = new ArrayList<OpenvswitchOtherConfigs>();
131                     String otherConfigValue;
132                     for (String otherConfigKey : otherConfigKeys) {
133                         otherConfigValue = otherConfigs.get(otherConfigKey);
134                         if (otherConfigKey != null && otherConfigValue != null) {
135                             otherConfigsList.add(new OpenvswitchOtherConfigsBuilder()
136                                     .setOtherConfigKey(otherConfigKey)
137                                     .setOtherConfigValue(otherConfigValue)
138                                     .build());
139                         }
140                     }
141                     ovsdbNodeBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
142                 }
143
144                 NodeBuilder nodeBuilder = new NodeBuilder();
145                 ConnectionInfo connectionInfo = ovsdbNode.getConnectionInfo();
146                 nodeBuilder.setNodeId(SouthboundMapper.createNodeId(
147                         connectionInfo.getRemoteIp(), connectionInfo.getRemotePort()));
148                 nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class,
149                         ovsdbNodeBuilder.build());
150                 transaction.merge(LogicalDatastoreType.OPERATIONAL, nodePath,
151                         nodeBuilder.build());
152             }
153         }
154     }
155 }