2 * Copyright © 2016, 2017 Inocybe Technologies 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.ovsdb.transact;
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12 import com.google.common.base.Optional;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.List;
19 import java.util.Map.Entry;
20 import java.util.concurrent.ExecutionException;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.ovsdb.lib.notation.Mutator;
25 import org.opendaylight.ovsdb.lib.notation.UUID;
26 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
27 import org.opendaylight.ovsdb.schema.openvswitch.AutoAttach;
28 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
29 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
30 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
31 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
32 import org.opendaylight.ovsdb.southbound.SouthboundProvider;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Autoattach;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.autoattach.AutoattachExternalIds;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.autoattach.Mappings;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 @SuppressFBWarnings("UPM_UNCALLED_PRIVATE_METHOD")
51 public class AutoAttachUpdateCommand implements TransactCommand {
52 private static final Logger LOG = LoggerFactory.getLogger(AutoAttachUpdateCommand.class);
55 public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
56 final DataChangeEvent events, final InstanceIdentifierCodec instanceIdentifierCodec) {
57 execute(transaction, state, TransactUtils.extractCreatedOrUpdated(events, OvsdbNodeAugmentation.class));
61 public void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
62 final Collection<DataTreeModification<Node>> modifications,
63 final InstanceIdentifierCodec instanceIdentifierCodec) {
64 execute(transaction, state, TransactUtils.extractCreatedOrUpdated(modifications, OvsdbNodeAugmentation.class));
67 private void execute(final TransactionBuilder transaction, final BridgeOperationalState state,
68 final Map<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> createdOrUpdated) {
70 for (final Entry<InstanceIdentifier<OvsdbNodeAugmentation>, OvsdbNodeAugmentation> ovsdbNodeEntry
71 : createdOrUpdated.entrySet()) {
72 updateAutoAttach(transaction, state, ovsdbNodeEntry.getKey(), ovsdbNodeEntry.getValue());
76 private void updateAutoAttach(final TransactionBuilder transaction, final BridgeOperationalState state,
77 final InstanceIdentifier<OvsdbNodeAugmentation> iid,
78 final OvsdbNodeAugmentation ovsdbNode) {
80 if (!state.getBridgeNode(iid).isPresent()) {
83 final List<Autoattach> autoAttachList = ovsdbNode.getAutoattach();
84 if (autoAttachList != null) {
86 // FIXME: Remove if loop after ovs community supports external_ids column in AutoAttach Table
87 LOG.info("UNSUPPORTED FUNCTIONALITY: Auto Attach related CRUD operations are not supported for"
88 + " this version of OVSDB schema due to missing external_ids column.");
92 final OvsdbNodeAugmentation currentOvsdbNode =
93 state.getBridgeNode(iid).get().augmentation(OvsdbNodeAugmentation.class);
94 final List<Autoattach> currentAutoAttach = currentOvsdbNode.getAutoattach();
95 for (final Autoattach autoAttach : autoAttachList) {
96 final AutoAttach autoAttachWrapper = transaction.getTypedRowWrapper(AutoAttach.class);
97 if (autoAttach.getSystemName() != null) {
98 autoAttachWrapper.setSystemName(autoAttach.getSystemName());
100 if (autoAttach.getSystemDescription() != null) {
101 autoAttachWrapper.setSystemDescription(autoAttach.getSystemDescription());
104 final List<Mappings> mappingsList = autoAttach.getMappings();
105 if (mappingsList != null && !mappingsList.isEmpty()) {
106 final Map<Long, Long> newMappings = new HashMap<>();
107 for (final Mappings mappings : mappingsList) {
108 final Long mappingsValue = new Long(mappings.getMappingsValue().toString());
109 newMappings.put(mappings.getMappingsKey().toJava(), mappingsValue);
111 autoAttachWrapper.setMappings(newMappings);
114 final List<AutoattachExternalIds> externalIds = autoAttach.getAutoattachExternalIds();
115 final Map<String, String> externalIdsMap = new HashMap<>();
116 if (externalIds != null) {
117 for (final AutoattachExternalIds externalId : externalIds) {
118 externalIdsMap.put(externalId.getAutoattachExternalIdKey(),
119 externalId.getAutoattachExternalIdValue());
122 externalIdsMap.put(SouthboundConstants.AUTOATTACH_ID_EXTERNAL_ID_KEY,
123 autoAttach.getAutoattachId().getValue());
124 // FIXME: To be uncommented when Open vSwitch supports external_ids column
126 // autoAttachWrapper.setExternalIds(ImmutableMap.copyOf(externalIdsMap));
127 // } catch (NullPointerException e) {
128 // LOG.warn("Incomplete AutoAttach external IDs");
131 final Uuid aaUuid = getAutoAttachUuid(currentAutoAttach, autoAttach.getAutoattachId());
132 if (aaUuid != null) {
133 final UUID uuid = new UUID(aaUuid.getValue());
134 final AutoAttach newAutoAttach = transaction.getTypedRowSchema(AutoAttach.class);
135 newAutoAttach.getUuidColumn().setData(uuid);
136 LOG.trace("Updating autoattach table entries {}", uuid);
137 transaction.add(op.update(autoAttachWrapper)
138 .where(newAutoAttach.getUuidColumn().getSchema().opEqual(uuid)).build());
139 transaction.add(op.comment("Updating AutoAttach table: " + uuid));
141 final Uri bridgeUri = autoAttach.getBridgeId();
142 final String namedUuid = SouthboundMapper.getRandomUuid();
143 final Bridge bridge = transaction.getTypedRowWrapper(Bridge.class);
144 transaction.add(op.insert(autoAttachWrapper).withId(namedUuid));
145 final OvsdbBridgeAugmentation ovsdbBridgeAugmentation = getBridge(iid, bridgeUri);
146 if (ovsdbBridgeAugmentation != null) {
147 bridge.setName(ovsdbBridgeAugmentation.getBridgeName().getValue());
148 bridge.setAutoAttach(Collections.singleton(new UUID(namedUuid)));
149 LOG.trace("Create Autoattach table {}, and mutate the bridge {}",
150 autoAttach.getAutoattachId(), getBridge(iid, bridgeUri).getBridgeName().getValue());
151 transaction.add(op.mutate(bridge)
152 .addMutation(bridge.getAutoAttachColumn().getSchema(),
153 Mutator.INSERT,bridge.getAutoAttachColumn().getData())
154 .where(bridge.getNameColumn().getSchema()
155 .opEqual(bridge.getNameColumn().getData())).build());
157 op.comment("Bridge: Mutating " + ovsdbBridgeAugmentation.getBridgeName().getValue()
158 + " to add autoattach column " + namedUuid));
165 private OvsdbBridgeAugmentation getBridge(final InstanceIdentifier<OvsdbNodeAugmentation> key,
166 final Uri bridgeUri) {
167 final InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid = InstanceIdentifier
168 .create(NetworkTopology.class)
169 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
170 .child(Node.class, new NodeKey(new NodeId(bridgeUri)))
171 .augmentation(OvsdbBridgeAugmentation.class);
173 OvsdbBridgeAugmentation bridge = null;
174 try (ReadOnlyTransaction transaction = SouthboundProvider.getDb().newReadOnlyTransaction()) {
175 final Optional<OvsdbBridgeAugmentation> bridgeOptional =
176 transaction.read(LogicalDatastoreType.OPERATIONAL, bridgeIid).get();
177 if (bridgeOptional.isPresent()) {
178 bridge = bridgeOptional.get();
180 } catch (InterruptedException | ExecutionException e) {
181 LOG.warn("Error reading from datastore", e);
186 private Uuid getAutoAttachUuid(final List<Autoattach> currentAutoAttach, final Uri autoattachId) {
187 if (currentAutoAttach != null && !currentAutoAttach.isEmpty()) {
188 for (final Autoattach autoAttach : currentAutoAttach) {
189 if (autoAttach.getAutoattachId().equals(autoattachId)) {
190 return autoAttach.getAutoattachUuid();