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