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 java.util.HashMap;
12 import java.util.List;
14 import java.util.Optional;
15 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.ovsdb.lib.message.TableUpdates;
18 import org.opendaylight.ovsdb.lib.notation.UUID;
19 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
20 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
21 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
22 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
23 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
24 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
25 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
26 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntryKey;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
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.NodeKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class OvsdbControllerUpdateCommand extends AbstractTransactionCommand {
44 private static final Logger LOG = LoggerFactory.getLogger(OvsdbControllerUpdateCommand.class);
46 private final Map<UUID, Controller> updatedControllerRows;
47 private final Map<UUID, Bridge> updatedBridgeRows;
49 public OvsdbControllerUpdateCommand(final OvsdbConnectionInstance key,
50 final TableUpdates updates, final DatabaseSchema dbSchema) {
51 super(key, updates, dbSchema);
52 updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
53 updatedControllerRows = TyperUtils.extractRowsUpdated(Controller.class,
54 getUpdates(), getDbSchema());
58 public void execute(final ReadWriteTransaction transaction) {
59 if (updatedControllerRows != null && !updatedControllerRows.isEmpty()) {
60 if (updatedBridgeRows != null && !updatedBridgeRows.isEmpty()) {
61 updateController(transaction, updatedControllerRows, updatedBridgeRows);
63 updateController(transaction, updatedControllerRows);
69 * Update the ControllerEntry values for the given {@link Bridge} list.
72 * Controller and Bridge are independent tables in the Open_vSwitch schema
73 * but the OVSDB yang model includes the Controller fields in the
74 * Bridge data. In some cases the OVSDB will send Bridge and Controller
75 * updates together and in other cases independently. This method here
79 * @param transaction the {@link ReadWriteTransaction}
80 * @param newUpdatedControllerRows updated {@link Controller} rows
81 * @param newUpdatedBridgeRows updated {@link Bridge} rows
83 private void updateController(final ReadWriteTransaction transaction,
84 final Map<UUID, Controller> newUpdatedControllerRows,
85 final Map<UUID, Bridge> newUpdatedBridgeRows) {
87 for (Map.Entry<UUID, Bridge> bridgeEntry : newUpdatedBridgeRows.entrySet()) {
88 final List<ControllerEntry> controllerEntries =
89 SouthboundMapper.createControllerEntries(bridgeEntry.getValue(), newUpdatedControllerRows);
91 for (ControllerEntry controllerEntry : controllerEntries) {
92 transaction.merge(LogicalDatastoreType.OPERATIONAL,
93 getControllerEntryIid(controllerEntry, bridgeEntry.getValue().getNameColumn().getData()),
100 * Update the ControllerEntry values after finding the related {@Bridge} list.
103 * Controller and Bridge are independent tables in the Open_vSwitch schema
104 * but the OVSDB yang model includes the Controller fields in the
105 * Bridge data. In some cases the OVSDB will send Bridge and Controller
106 * updates together and in other cases independently. This method here
107 * assumes the latter.
110 * @param transaction the {@link ReadWriteTransaction}
111 * @param newUpdatedControllerRows updated {@link Controller} rows
114 void updateController(final ReadWriteTransaction transaction,
115 final Map<UUID, Controller> newUpdatedControllerRows) {
117 Map<InstanceIdentifier<Node>, Node> bridgeNodes = getBridgeNodes(transaction);
118 for (Map.Entry<InstanceIdentifier<Node>, Node> bridgeNodeEntry : bridgeNodes.entrySet()) {
119 final List<ControllerEntry> controllerEntries =
120 SouthboundMapper.createControllerEntries(bridgeNodeEntry.getValue(), newUpdatedControllerRows);
122 for (ControllerEntry controllerEntry : controllerEntries) {
123 final InstanceIdentifier<Node> bridgeIid = bridgeNodeEntry.getKey();
124 InstanceIdentifier<ControllerEntry> iid = bridgeIid
125 .augmentation(OvsdbBridgeAugmentation.class)
126 .child(ControllerEntry.class, controllerEntry.key());
127 transaction.merge(LogicalDatastoreType.OPERATIONAL,
128 iid, controllerEntry);
134 * Find all the {@link Node} bridge nodes for the given connection.
136 * @param transaction the {@link ReadWriteTransaction}
137 * @return map of nodes
139 private Map<InstanceIdentifier<Node>, Node> getBridgeNodes(final ReadWriteTransaction transaction) {
140 Map<InstanceIdentifier<Node>, Node> bridgeNodes = new HashMap<>();
141 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
142 final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, connectionIId);
143 if (ovsdbNode.isPresent()) {
144 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.get().augmentation(OvsdbNodeAugmentation.class);
145 if (ovsdbNodeAugmentation != null) {
146 final Map<ManagedNodeEntryKey, ManagedNodeEntry> managedNodeEntries
147 = ovsdbNodeAugmentation.getManagedNodeEntry();
148 for (ManagedNodeEntry managedNodeEntry : managedNodeEntries.values()) {
149 final InstanceIdentifier<Node> bridgeIid =
150 (InstanceIdentifier<Node>) managedNodeEntry.getBridgeRef().getValue();
151 final Optional<Node> bridgeNode = SouthboundUtil.readNode(transaction, bridgeIid);
152 if (bridgeNode.isPresent()) {
153 bridgeNodes.put(bridgeIid, bridgeNode.get());
155 LOG.warn("OVSDB bridge node was not found: {}", bridgeIid);
159 LOG.warn("OvsdbNodeAugmentation was not found: {}", connectionIId);
162 LOG.warn("OVSDB node was not found: {}", connectionIId);
169 * Create the {@link InstanceIdentifier} for the {@link ControllerEntry}.
171 * @param controllerEntry the {@link ControllerEntry}
172 * @param bridgeName the name of the bridge
173 * @return the {@link InstanceIdentifier}
176 InstanceIdentifier<ControllerEntry> getControllerEntryIid(
177 final ControllerEntry controllerEntry, final String bridgeName) {
179 OvsdbConnectionInstance client = getOvsdbConnectionInstance();
180 String nodeString = client.getNodeKey().getNodeId().getValue()
181 + "/bridge/" + bridgeName;
182 NodeId nodeId = new NodeId(new Uri(nodeString));
183 NodeKey nodeKey = new NodeKey(nodeId);
184 InstanceIdentifier<Node> bridgeIid = InstanceIdentifier.builder(NetworkTopology.class)
185 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
186 .child(Node.class,nodeKey)
190 .augmentation(OvsdbBridgeAugmentation.class)
191 .child(ControllerEntry.class, controllerEntry.key());