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.base.Optional;
11 import java.util.HashMap;
12 import java.util.List;
14 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.ovsdb.lib.message.TableUpdates;
17 import org.opendaylight.ovsdb.lib.notation.UUID;
18 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
19 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
20 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
21 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
22 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
23 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
24 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
25 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class OvsdbControllerUpdateCommand extends AbstractTransactionCommand {
42 private static final Logger LOG = LoggerFactory.getLogger(OvsdbControllerUpdateCommand.class);
43 private Map<UUID, Controller> updatedControllerRows;
44 private Map<UUID, Bridge> updatedBridgeRows;
46 public OvsdbControllerUpdateCommand(OvsdbConnectionInstance key,
47 TableUpdates updates, DatabaseSchema dbSchema) {
48 super(key, updates, dbSchema);
49 updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
50 updatedControllerRows = TyperUtils.extractRowsUpdated(Controller.class,
51 getUpdates(), getDbSchema());
55 public void execute(ReadWriteTransaction transaction) {
56 if (updatedControllerRows != null && !updatedControllerRows.isEmpty()) {
57 if (updatedBridgeRows != null && !updatedBridgeRows.isEmpty()) {
58 updateController(transaction, updatedControllerRows, updatedBridgeRows);
60 updateController(transaction, updatedControllerRows);
66 * Update the ControllerEntry values for the given {@link Bridge} list.
69 * Controller and Bridge are independent tables in the Open_vSwitch schema
70 * but the OVSDB yang model includes the Controller fields in the
71 * Bridge data. In some cases the OVSDB will send Bridge and Controller
72 * updates together and in other cases independently. This method here
76 * @param transaction the {@link ReadWriteTransaction}
77 * @param updatedControllerRows updated {@link Controller} rows
78 * @param updatedBridgeRows updated {@link Bridge} rows
80 private void updateController(ReadWriteTransaction transaction,
81 Map<UUID, Controller> updatedControllerRows,
82 Map<UUID, Bridge> updatedBridgeRows) {
84 for (Map.Entry<UUID, Bridge> bridgeEntry : updatedBridgeRows.entrySet()) {
85 final List<ControllerEntry> controllerEntries =
86 SouthboundMapper.createControllerEntries(bridgeEntry.getValue(), updatedControllerRows);
88 for (ControllerEntry controllerEntry : controllerEntries) {
89 transaction.merge(LogicalDatastoreType.OPERATIONAL,
90 getControllerEntryIid(controllerEntry, bridgeEntry.getValue().getNameColumn().getData()),
97 * Update the ControllerEntry values after finding the related {@Bridge} list.
100 * Controller and Bridge are independent tables in the Open_vSwitch schema
101 * but the OVSDB yang model includes the Controller fields in the
102 * Bridge data. In some cases the OVSDB will send Bridge and Controller
103 * updates together and in other cases independently. This method here
104 * assumes the latter.
107 * @param transaction the {@link ReadWriteTransaction}
108 * @param updatedControllerRows updated {@link Controller} rows
111 private void updateController(ReadWriteTransaction transaction,
112 Map<UUID, Controller> updatedControllerRows) {
114 Map<InstanceIdentifier<Node>, Node> bridgeNodes = getBridgeNodes(transaction);
115 for (Map.Entry<InstanceIdentifier<Node>, Node> bridgeNodeEntry : bridgeNodes.entrySet()) {
116 final List<ControllerEntry> controllerEntries =
117 SouthboundMapper.createControllerEntries(bridgeNodeEntry.getValue(), updatedControllerRows);
119 for (ControllerEntry controllerEntry : controllerEntries) {
120 final InstanceIdentifier<Node> bridgeIid = bridgeNodeEntry.getKey();
121 InstanceIdentifier<ControllerEntry> iid = bridgeIid
122 .augmentation(OvsdbBridgeAugmentation.class)
123 .child(ControllerEntry.class, controllerEntry.getKey());
124 transaction.merge(LogicalDatastoreType.OPERATIONAL,
125 iid, controllerEntry);
131 * Find all the {@link Node} bridge nodes for the given connection.
133 * @param transaction the {@link ReadWriteTransaction}
134 * @return map of nodes
136 private Map<InstanceIdentifier<Node>, Node> getBridgeNodes(ReadWriteTransaction transaction) {
137 Map<InstanceIdentifier<Node>, Node> bridgeNodes = new HashMap<>();
138 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
139 final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, connectionIId);
140 if (ovsdbNode.isPresent()) {
141 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.get().getAugmentation(OvsdbNodeAugmentation.class);
142 if (ovsdbNodeAugmentation != null) {
143 final List<ManagedNodeEntry> managedNodeEntries = ovsdbNodeAugmentation.getManagedNodeEntry();
144 for (ManagedNodeEntry managedNodeEntry : managedNodeEntries) {
145 final InstanceIdentifier<Node> bridgeIid =
146 (InstanceIdentifier<Node>) managedNodeEntry.getBridgeRef().getValue();
147 @SuppressWarnings("unchecked")
148 final Optional<Node> bridgeNode = SouthboundUtil.readNode(transaction, bridgeIid);
149 if (bridgeNode.isPresent()) {
150 bridgeNodes.put(bridgeIid, bridgeNode.get());
152 LOG.warn("OVSDB bridge node was not found: {}", bridgeIid);
156 LOG.warn("OvsdbNodeAugmentation was not found: {}", connectionIId);
159 LOG.warn("OVSDB node was not found: {}", connectionIId);
166 * Create the {@link InstanceIdentifier} for the {@link ControllerEntry}.
168 * @param controllerEntry the {@link ControllerEntry}
169 * @param bridgeName the name of the bridge
170 * @return the {@link InstanceIdentifier}
172 private InstanceIdentifier<ControllerEntry> getControllerEntryIid(
173 ControllerEntry controllerEntry, String bridgeName) {
175 OvsdbConnectionInstance client = getOvsdbConnectionInstance();
176 String nodeString = client.getNodeKey().getNodeId().getValue()
177 + "/bridge/" + bridgeName;
178 NodeId nodeId = new NodeId(new Uri(nodeString));
179 NodeKey nodeKey = new NodeKey(nodeId);
180 InstanceIdentifier<Node> bridgeIid = InstanceIdentifier.builder(NetworkTopology.class)
181 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
182 .child(Node.class,nodeKey)
186 .augmentation(OvsdbBridgeAugmentation.class)
187 .child(ControllerEntry.class, controllerEntry.getKey());