2 * Copyright (c) 2015 Brocade Communications 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.Manager;
21 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
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.OvsdbNodeAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntry;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.common.base.Optional;
41 public class OvsdbManagersUpdateCommand extends AbstractTransactionCommand {
42 private static final Logger LOG = LoggerFactory.getLogger(OvsdbManagersUpdateCommand.class);
44 private Map<UUID, Manager> updatedManagerRows;
45 private Map<UUID, OpenVSwitch> updatedOpenVSwitchRows;
47 public OvsdbManagersUpdateCommand(OvsdbConnectionInstance key,
48 TableUpdates updates, DatabaseSchema dbSchema) {
49 super(key, updates, dbSchema);
50 updatedOpenVSwitchRows = TyperUtils.extractRowsUpdated(OpenVSwitch.class, getUpdates(), getDbSchema());
51 updatedManagerRows = TyperUtils.extractRowsUpdated(Manager.class,getUpdates(), getDbSchema());
55 public void execute(ReadWriteTransaction transaction) {
56 if (updatedManagerRows != null && !updatedManagerRows.isEmpty()) {
57 Map<Uri, Manager> updatedManagerRowsWithUri = getUriManagerMap(updatedManagerRows);
58 if (updatedOpenVSwitchRows != null && !updatedOpenVSwitchRows.isEmpty()) {
59 updateManagers(transaction, updatedManagerRows, updatedOpenVSwitchRows);
61 updateManagers(transaction, updatedManagerRowsWithUri);
67 * Update the Manager values for the given {@link OpenVSwitch} list.
70 * Manager and OpenVSwitch are independent tables in the Open_vSwitch schema
71 * but the OVSDB yang model includes the Manager fields in the
72 * OVSDB Node data. In some cases the OVSDB will send OpenVSwitch and Manager
73 * updates together and in other cases independently. This method here
77 * @param transaction the {@link ReadWriteTransaction}
78 * @param updatedManagerRows updated {@link Manager} rows
79 * @param updatedOpenVSwitchRows updated {@link OpenVSwitch} rows
81 private void updateManagers(ReadWriteTransaction transaction,
82 Map<UUID, Manager> updatedManagerRows,
83 Map<UUID, OpenVSwitch> updatedOpenVSwitchRows) {
85 for (Map.Entry<UUID, OpenVSwitch> ovsdbNodeEntry : updatedOpenVSwitchRows.entrySet()) {
86 final List<ManagerEntry> managerEntries =
87 SouthboundMapper.createManagerEntries(ovsdbNodeEntry.getValue(), updatedManagerRows);
88 LOG.debug("Update Ovsdb Node {} with manager entries {}",ovsdbNodeEntry.getValue(), managerEntries);
89 for (ManagerEntry managerEntry : managerEntries) {
90 transaction.merge(LogicalDatastoreType.OPERATIONAL,
91 getManagerEntryIid(managerEntry),
98 * Update the ManagerEntry values after finding the related {@OpenVSwitch} list.
101 * Manager and OpenVSwitch are independent tables in the Open_vSwitch schema
102 * but the OVSDB yang model includes the Manager fields in the
103 * OvsdbNode data. In some cases the OVSDB will send OpenVSwitch and Manager
104 * updates together and in other cases independently. This method here
105 * assumes the latter.
108 * @param transaction the {@link ReadWriteTransaction}
109 * @param updatedManagerRows updated {@link Manager} rows
112 private void updateManagers(ReadWriteTransaction transaction,
113 Map<Uri, Manager> updatedManagerRows) {
115 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
116 final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, connectionIId);
117 if (ovsdbNode.isPresent()) {
118 final List<ManagerEntry> managerEntries =
119 SouthboundMapper.createManagerEntries(ovsdbNode.get(), updatedManagerRows);
121 LOG.debug("Update Ovsdb Node {} with manager entries {}",ovsdbNode.get(), managerEntries);
122 for (ManagerEntry managerEntry : managerEntries) {
123 InstanceIdentifier<ManagerEntry> iid = connectionIId
124 .augmentation(OvsdbNodeAugmentation.class)
125 .child(ManagerEntry.class, managerEntry.getKey());
126 transaction.merge(LogicalDatastoreType.OPERATIONAL,
133 * Create the {@link InstanceIdentifier} for the {@link ManagerEntry}.
135 * @param managerEntry the {@link ManagerEntry}
136 * @return the {@link InstanceIdentifier}
138 private InstanceIdentifier<ManagerEntry> getManagerEntryIid(ManagerEntry managerEntry) {
140 OvsdbConnectionInstance client = getOvsdbConnectionInstance();
141 String nodeString = client.getNodeKey().getNodeId().getValue();
142 NodeId nodeId = new NodeId(new Uri(nodeString));
143 NodeKey nodeKey = new NodeKey(nodeId);
144 InstanceIdentifier<Node> ovsdbNodeIid = InstanceIdentifier.builder(NetworkTopology.class)
145 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
146 .child(Node.class,nodeKey)
150 .augmentation(OvsdbNodeAugmentation.class)
151 .child(ManagerEntry.class, managerEntry.getKey());
154 private Map<Uri, Manager> getUriManagerMap(Map<UUID,Manager> uuidManagerMap) {
155 Map<Uri, Manager> uriManagerMap = new HashMap<>();
156 for (Map.Entry<UUID, Manager> uuidManagerMapEntry : uuidManagerMap.entrySet()) {
158 new Uri(uuidManagerMapEntry.getValue().getTargetColumn().getData()),
159 uuidManagerMapEntry.getValue());
161 return uriManagerMap;