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.rev130715.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);
44 private Map<UUID, Controller> updatedControllerRows;
45 private Map<UUID, Bridge> updatedBridgeRows;
47 public OvsdbControllerUpdateCommand(OvsdbConnectionInstance key,
48 TableUpdates updates, DatabaseSchema dbSchema) {
49 super(key, updates, dbSchema);
50 updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
51 updatedControllerRows = TyperUtils.extractRowsUpdated(Controller.class,
52 getUpdates(), getDbSchema());
56 public void execute(ReadWriteTransaction transaction) {
57 if (updatedControllerRows != null && !updatedControllerRows.isEmpty()) {
58 if (updatedBridgeRows != null && !updatedBridgeRows.isEmpty()) {
59 updateController(transaction, updatedControllerRows, updatedBridgeRows);
61 updateController(transaction, updatedControllerRows);
67 * Update the ControllerEntry values for the given {@link Bridge} list.
70 * Controller and Bridge are independent tables in the Open_vSwitch schema
71 * but the OVSDB yang model includes the Controller fields in the
72 * Bridge data. In some cases the OVSDB will send Bridge and Controller
73 * updates together and in other cases independently. This method here
77 * @param transaction the {@link ReadWriteTransaction}
78 * @param newUpdatedControllerRows updated {@link Controller} rows
79 * @param newUpdatedBridgeRows updated {@link Bridge} rows
81 private void updateController(ReadWriteTransaction transaction,
82 Map<UUID, Controller> newUpdatedControllerRows,
83 Map<UUID, Bridge> newUpdatedBridgeRows) {
85 for (Map.Entry<UUID, Bridge> bridgeEntry : newUpdatedBridgeRows.entrySet()) {
86 final List<ControllerEntry> controllerEntries =
87 SouthboundMapper.createControllerEntries(bridgeEntry.getValue(), newUpdatedControllerRows);
89 for (ControllerEntry controllerEntry : controllerEntries) {
90 transaction.merge(LogicalDatastoreType.OPERATIONAL,
91 getControllerEntryIid(controllerEntry, bridgeEntry.getValue().getNameColumn().getData()),
98 * Update the ControllerEntry values after finding the related {@Bridge} list.
101 * Controller and Bridge are independent tables in the Open_vSwitch schema
102 * but the OVSDB yang model includes the Controller fields in the
103 * Bridge data. In some cases the OVSDB will send Bridge and Controller
104 * updates together and in other cases independently. This method here
105 * assumes the latter.
108 * @param transaction the {@link ReadWriteTransaction}
109 * @param newUpdatedControllerRows updated {@link Controller} rows
112 private void updateController(ReadWriteTransaction transaction,
113 Map<UUID, Controller> newUpdatedControllerRows) {
115 Map<InstanceIdentifier<Node>, Node> bridgeNodes = getBridgeNodes(transaction);
116 for (Map.Entry<InstanceIdentifier<Node>, Node> bridgeNodeEntry : bridgeNodes.entrySet()) {
117 final List<ControllerEntry> controllerEntries =
118 SouthboundMapper.createControllerEntries(bridgeNodeEntry.getValue(), newUpdatedControllerRows);
120 for (ControllerEntry controllerEntry : controllerEntries) {
121 final InstanceIdentifier<Node> bridgeIid = bridgeNodeEntry.getKey();
122 InstanceIdentifier<ControllerEntry> iid = bridgeIid
123 .augmentation(OvsdbBridgeAugmentation.class)
124 .child(ControllerEntry.class, controllerEntry.getKey());
125 transaction.merge(LogicalDatastoreType.OPERATIONAL,
126 iid, controllerEntry);
132 * Find all the {@link Node} bridge nodes for the given connection.
134 * @param transaction the {@link ReadWriteTransaction}
135 * @return map of nodes
137 private Map<InstanceIdentifier<Node>, Node> getBridgeNodes(ReadWriteTransaction transaction) {
138 Map<InstanceIdentifier<Node>, Node> bridgeNodes = new HashMap<>();
139 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
140 final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, connectionIId);
141 if (ovsdbNode.isPresent()) {
142 OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.get().getAugmentation(OvsdbNodeAugmentation.class);
143 if (ovsdbNodeAugmentation != null) {
144 final List<ManagedNodeEntry> managedNodeEntries = ovsdbNodeAugmentation.getManagedNodeEntry();
145 for (ManagedNodeEntry managedNodeEntry : managedNodeEntries) {
146 final InstanceIdentifier<Node> bridgeIid =
147 (InstanceIdentifier<Node>) managedNodeEntry.getBridgeRef().getValue();
148 @SuppressWarnings("unchecked")
149 final Optional<Node> bridgeNode = SouthboundUtil.readNode(transaction, bridgeIid);
150 if (bridgeNode.isPresent()) {
151 bridgeNodes.put(bridgeIid, bridgeNode.get());
153 LOG.warn("OVSDB bridge node was not found: {}", bridgeIid);
157 LOG.warn("OvsdbNodeAugmentation was not found: {}", connectionIId);
160 LOG.warn("OVSDB node was not found: {}", connectionIId);
167 * Create the {@link InstanceIdentifier} for the {@link ControllerEntry}.
169 * @param controllerEntry the {@link ControllerEntry}
170 * @param bridgeName the name of the bridge
171 * @return the {@link InstanceIdentifier}
173 private InstanceIdentifier<ControllerEntry> getControllerEntryIid(
174 ControllerEntry controllerEntry, String bridgeName) {
176 OvsdbConnectionInstance client = getOvsdbConnectionInstance();
177 String nodeString = client.getNodeKey().getNodeId().getValue()
178 + "/bridge/" + bridgeName;
179 NodeId nodeId = new NodeId(new Uri(nodeString));
180 NodeKey nodeKey = new NodeKey(nodeId);
181 InstanceIdentifier<Node> bridgeIid = InstanceIdentifier.builder(NetworkTopology.class)
182 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
183 .child(Node.class,nodeKey)
187 .augmentation(OvsdbBridgeAugmentation.class)
188 .child(ControllerEntry.class, controllerEntry.getKey());