2 * Copyright (c) 2015 Inocybe inc. and others. All rights reserved.
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
8 package org.opendaylight.ovsdb.southbound.transactions.md;
10 import java.util.ArrayList;
11 import java.util.List;
13 import java.util.Map.Entry;
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;
44 import com.google.common.base.Optional;
46 public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand {
48 private static final Logger LOG = LoggerFactory
49 .getLogger(OpenVSwitchUpdateCommand.class);
51 public OpenVSwitchUpdateCommand(OvsdbClientKey key, TableUpdates updates,
52 DatabaseSchema dbSchema) {
53 super(key, updates, dbSchema);
57 public void execute(ReadWriteTransaction transaction) {
58 Map<UUID, OpenVSwitch> updatedOpenVSwitchRows = TyperUtils
59 .extractRowsUpdated(OpenVSwitch.class, getUpdates(),
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();
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);
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()
81 Set<String> dptypes = openVSwitch.getDatapathTypesColumn()
83 List<DatapathTypeEntry> dpEntryList = new ArrayList<DatapathTypeEntry>();
84 for (String dpType : dptypes) {
85 DatapathTypeEntry dpEntry = new DatapathTypeEntryBuilder()
87 SouthboundMapper.createDatapathType(dpType))
89 dpEntryList.add(dpEntry);
91 ovsdbNodeBuilder.setDatapathTypeEntry(dpEntryList);
92 } catch (SchemaVersionMismatchException e) {
93 LOG.debug("Datapath types not supported by this version of ovsdb",e);;
96 Set<String> iftypes = openVSwitch.getIfaceTypesColumn().getData();
97 List<InterfaceTypeEntry> ifEntryList = new ArrayList<InterfaceTypeEntry>();
98 for (String ifType : iftypes) {
99 InterfaceTypeEntry ifEntry = new InterfaceTypeEntryBuilder()
101 SouthboundMapper.createInterfaceType(ifType))
103 ifEntryList.add(ifEntry);
105 ovsdbNodeBuilder.setInterfaceTypeEntry(ifEntryList);
106 } catch (SchemaVersionMismatchException e) {
107 LOG.debug("Iface types not supported by this version of ovsdb",e);;
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)
124 ovsdbNodeBuilder.setOpenvswitchExternalIds(externalIdsList);
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)
141 ovsdbNodeBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
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());