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 java.util.HashMap;
11 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.NodeKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
41 import com.google.common.base.Optional;
43 public class OvsdbControllerUpdateCommand extends AbstractTransactionCommand {
44 private static final Logger LOG = LoggerFactory.getLogger(OvsdbControllerUpdateCommand.class);
45 private Map<UUID, Controller> updatedControllerRows;
46 private Map<UUID, Bridge> updatedBridgeRows;
48 public OvsdbControllerUpdateCommand(OvsdbConnectionInstance key,
49 TableUpdates updates, DatabaseSchema dbSchema) {
50 super(key, updates, dbSchema);
51 updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
52 updatedControllerRows = TyperUtils.extractRowsUpdated(Controller.class,
53 getUpdates(), getDbSchema());
57 public void execute(ReadWriteTransaction transaction) {
58 if (updatedControllerRows != null && !updatedControllerRows.isEmpty()) {
59 if (updatedBridgeRows != null && !updatedBridgeRows.isEmpty()) {
60 updateController(transaction, updatedControllerRows, updatedBridgeRows);
62 updateController(transaction, updatedControllerRows);
68 * Update the ControllerEntry values for the given {@link Bridge} list.
71 * Controller and Bridge are independent tables in the Open_vSwitch schema
72 * but the OVSDB yang model includes the Controller fields in the
73 * Bridge data. In some cases the OVSDB will send Bridge and Controller
74 * updates together and in other cases independently. This method here
78 * @param transaction the {@link ReadWriteTransaction}
79 * @param updatedControllerRows updated {@link Controller} rows
80 * @param updatedBridgeRows updated {@link Bridge} rows
82 private void updateController(ReadWriteTransaction transaction,
83 Map<UUID, Controller> updatedControllerRows,
84 Map<UUID, Bridge> updatedBridgeRows) {
86 for (Map.Entry<UUID, Bridge> bridgeEntry : updatedBridgeRows.entrySet()) {
87 final List<ControllerEntry> controllerEntries =
88 SouthboundMapper.createControllerEntries(bridgeEntry.getValue(), updatedControllerRows);
90 for (ControllerEntry controllerEntry : controllerEntries) {
91 transaction.merge(LogicalDatastoreType.OPERATIONAL,
92 getControllerEntryIid(controllerEntry, bridgeEntry.getValue().getNameColumn().getData()),
99 * Update the ControllerEntry values after finding the related {@Bridge} list.
102 * Controller and Bridge are independent tables in the Open_vSwitch schema
103 * but the OVSDB yang model includes the Controller fields in the
104 * Bridge data. In some cases the OVSDB will send Bridge and Controller
105 * updates together and in other cases independently. This method here
106 * assumes the latter.
109 * @param transaction the {@link ReadWriteTransaction}
110 * @param updatedControllerRows updated {@link Controller} rows
113 private void updateController(ReadWriteTransaction transaction,
114 Map<UUID, Controller> updatedControllerRows) {
116 Map<InstanceIdentifier<Node>, Node> bridgeNodes = getBridgeNodes(transaction);
117 for (Map.Entry<InstanceIdentifier<Node>, Node> bridgeNodeEntry : bridgeNodes.entrySet()) {
118 final List<ControllerEntry> controllerEntries =
119 SouthboundMapper.createControllerEntries(bridgeNodeEntry.getValue(), updatedControllerRows);
121 for (ControllerEntry controllerEntry : controllerEntries) {
122 final InstanceIdentifier<Node> bridgeIid = bridgeNodeEntry.getKey();
123 InstanceIdentifier<ControllerEntry> iid = bridgeIid
124 .augmentation(OvsdbBridgeAugmentation.class)
125 .child(ControllerEntry.class, controllerEntry.getKey());
126 transaction.merge(LogicalDatastoreType.OPERATIONAL,
127 iid, controllerEntry);
133 * Find all the {@link Node} bridge nodes for the given connection.
135 * @param transaction the {@link ReadWriteTransaction}
138 private Map<InstanceIdentifier<Node>, Node> getBridgeNodes(ReadWriteTransaction transaction) {
139 Map<InstanceIdentifier<Node>, Node> bridgeNodes = new HashMap<>();
140 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
141 final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, connectionIId);
142 if (ovsdbNode.isPresent()) {
143 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.get().getAugmentation(OvsdbNodeAugmentation.class);
144 if (ovsdbNodeAugmentation != null) {
145 final List<ManagedNodeEntry> managedNodeEntries = ovsdbNodeAugmentation.getManagedNodeEntry();
146 for (ManagedNodeEntry managedNodeEntry : managedNodeEntries) {
147 final InstanceIdentifier<Node> bridgeIid =
148 (InstanceIdentifier<Node>) managedNodeEntry.getBridgeRef().getValue();
149 @SuppressWarnings("unchecked")
150 final Optional<Node> bridgeNode = SouthboundUtil.readNode(transaction, bridgeIid);
151 if (bridgeNode.isPresent()) {
152 bridgeNodes.put(bridgeIid, bridgeNode.get());
154 LOG.warn("OVSDB bridge node was not found: {}", bridgeIid);
158 LOG.warn("OvsdbNodeAugmentation was not found: {}", connectionIId);
161 LOG.warn("OVSDB node was not found: {}", connectionIId);
168 * Create the {@link InstanceIdentifier} for the {@link ControllerEntry}.
170 * @param controllerEntry the {@link ControllerEntry}
171 * @param bridgeName the name of the bridge
172 * @return the {@link InstanceIdentifier}
174 private InstanceIdentifier<ControllerEntry> getControllerEntryIid(
175 ControllerEntry controllerEntry, String bridgeName) {
177 OvsdbConnectionInstance client = getOvsdbConnectionInstance();
178 String nodeString = client.getNodeKey().getNodeId().getValue()
179 + "/bridge/" + bridgeName;
180 NodeId nodeId = new NodeId(new Uri(nodeString));
181 NodeKey nodeKey = new NodeKey(nodeId);
182 InstanceIdentifier<Node> bridgeIid = InstanceIdentifier.builder(NetworkTopology.class)
183 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
184 .child(Node.class,nodeKey)
187 InstanceIdentifier<ControllerEntry> iid = bridgeIid
188 .augmentation(OvsdbBridgeAugmentation.class)
189 .child(ControllerEntry.class, controllerEntry.getKey());