2 * Copyright (c) 2015 Cisco Systems, 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 com.google.common.annotations.VisibleForTesting;
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.util.HashMap;
13 import java.util.List;
15 import java.util.Optional;
16 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.ovsdb.lib.message.TableUpdates;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
20 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
21 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
22 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
23 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
24 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
25 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
26 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
27 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntryKey;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
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.NodeKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 public class OvsdbControllerUpdateCommand extends AbstractTransactionCommand {
45 private static final Logger LOG = LoggerFactory.getLogger(OvsdbControllerUpdateCommand.class);
47 private final Map<UUID, Controller> updatedControllerRows;
48 private final Map<UUID, Bridge> updatedBridgeRows;
50 @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR", justification = "Non-final for mocking")
51 public OvsdbControllerUpdateCommand(final OvsdbConnectionInstance key,
52 final TableUpdates updates, final DatabaseSchema dbSchema) {
53 super(key, updates, dbSchema);
54 updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
55 updatedControllerRows = TyperUtils.extractRowsUpdated(Controller.class,
56 getUpdates(), getDbSchema());
60 public void execute(final ReadWriteTransaction transaction) {
61 if (updatedControllerRows != null && !updatedControllerRows.isEmpty()) {
62 if (updatedBridgeRows != null && !updatedBridgeRows.isEmpty()) {
63 updateController(transaction, updatedControllerRows, updatedBridgeRows);
65 updateController(transaction, updatedControllerRows);
71 * Update the ControllerEntry values for the given {@link Bridge} list.
74 * Controller and Bridge are independent tables in the Open_vSwitch schema
75 * but the OVSDB yang model includes the Controller fields in the
76 * Bridge data. In some cases the OVSDB will send Bridge and Controller
77 * updates together and in other cases independently. This method here
81 * @param transaction the {@link ReadWriteTransaction}
82 * @param newUpdatedControllerRows updated {@link Controller} rows
83 * @param newUpdatedBridgeRows updated {@link Bridge} rows
85 private void updateController(final ReadWriteTransaction transaction,
86 final Map<UUID, Controller> newUpdatedControllerRows,
87 final Map<UUID, Bridge> newUpdatedBridgeRows) {
89 for (Map.Entry<UUID, Bridge> bridgeEntry : newUpdatedBridgeRows.entrySet()) {
90 final List<ControllerEntry> controllerEntries =
91 SouthboundMapper.createControllerEntries(bridgeEntry.getValue(), newUpdatedControllerRows);
93 for (ControllerEntry controllerEntry : controllerEntries) {
94 transaction.merge(LogicalDatastoreType.OPERATIONAL,
95 getControllerEntryIid(controllerEntry, bridgeEntry.getValue().getNameColumn().getData()),
102 * Update the ControllerEntry values after finding the related {@Bridge} list.
105 * Controller and Bridge are independent tables in the Open_vSwitch schema
106 * but the OVSDB yang model includes the Controller fields in the
107 * Bridge data. In some cases the OVSDB will send Bridge and Controller
108 * updates together and in other cases independently. This method here
109 * assumes the latter.
112 * @param transaction the {@link ReadWriteTransaction}
113 * @param newUpdatedControllerRows updated {@link Controller} rows
116 void updateController(final ReadWriteTransaction transaction,
117 final Map<UUID, Controller> newUpdatedControllerRows) {
119 Map<InstanceIdentifier<Node>, Node> bridgeNodes = getBridgeNodes(transaction);
120 for (Map.Entry<InstanceIdentifier<Node>, Node> bridgeNodeEntry : bridgeNodes.entrySet()) {
121 final List<ControllerEntry> controllerEntries =
122 SouthboundMapper.createControllerEntries(bridgeNodeEntry.getValue(), newUpdatedControllerRows);
124 for (ControllerEntry controllerEntry : controllerEntries) {
125 final InstanceIdentifier<Node> bridgeIid = bridgeNodeEntry.getKey();
126 InstanceIdentifier<ControllerEntry> iid = bridgeIid
127 .augmentation(OvsdbBridgeAugmentation.class)
128 .child(ControllerEntry.class, controllerEntry.key());
129 transaction.merge(LogicalDatastoreType.OPERATIONAL,
130 iid, controllerEntry);
136 * Find all the {@link Node} bridge nodes for the given connection.
138 * @param transaction the {@link ReadWriteTransaction}
139 * @return map of nodes
141 private Map<InstanceIdentifier<Node>, Node> getBridgeNodes(final ReadWriteTransaction transaction) {
142 Map<InstanceIdentifier<Node>, Node> bridgeNodes = new HashMap<>();
143 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
144 final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, connectionIId);
145 if (ovsdbNode.isPresent()) {
146 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.get().augmentation(OvsdbNodeAugmentation.class);
147 if (ovsdbNodeAugmentation != null) {
148 final Map<ManagedNodeEntryKey, ManagedNodeEntry> managedNodeEntries
149 = ovsdbNodeAugmentation.getManagedNodeEntry();
150 for (ManagedNodeEntry managedNodeEntry : managedNodeEntries.values()) {
151 final InstanceIdentifier<Node> bridgeIid =
152 (InstanceIdentifier<Node>) managedNodeEntry.getBridgeRef().getValue();
153 final Optional<Node> bridgeNode = SouthboundUtil.readNode(transaction, bridgeIid);
154 if (bridgeNode.isPresent()) {
155 bridgeNodes.put(bridgeIid, bridgeNode.get());
157 LOG.warn("OVSDB bridge node was not found: {}", bridgeIid);
161 LOG.warn("OvsdbNodeAugmentation was not found: {}", connectionIId);
164 LOG.warn("OVSDB node was not found: {}", connectionIId);
171 * Create the {@link InstanceIdentifier} for the {@link ControllerEntry}.
173 * @param controllerEntry the {@link ControllerEntry}
174 * @param bridgeName the name of the bridge
175 * @return the {@link InstanceIdentifier}
178 InstanceIdentifier<ControllerEntry> getControllerEntryIid(
179 final ControllerEntry controllerEntry, final String bridgeName) {
181 OvsdbConnectionInstance client = getOvsdbConnectionInstance();
182 String nodeString = client.getNodeKey().getNodeId().getValue()
183 + "/bridge/" + bridgeName;
184 NodeId nodeId = new NodeId(new Uri(nodeString));
185 NodeKey nodeKey = new NodeKey(nodeId);
186 InstanceIdentifier<Node> bridgeIid = InstanceIdentifier.builder(NetworkTopology.class)
187 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
188 .child(Node.class,nodeKey)
192 .augmentation(OvsdbBridgeAugmentation.class)
193 .child(ControllerEntry.class, controllerEntry.key());