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 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.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.rev130715.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 public class OvsdbManagersUpdateCommand extends AbstractTransactionCommand {
40 private static final Logger LOG = LoggerFactory.getLogger(OvsdbManagersUpdateCommand.class);
42 private Map<UUID, Manager> updatedManagerRows;
43 private Map<UUID, OpenVSwitch> updatedOpenVSwitchRows;
45 public OvsdbManagersUpdateCommand(OvsdbConnectionInstance key,
46 TableUpdates updates, DatabaseSchema dbSchema) {
47 super(key, updates, dbSchema);
48 updatedOpenVSwitchRows = TyperUtils.extractRowsUpdated(OpenVSwitch.class, getUpdates(), getDbSchema());
49 updatedManagerRows = TyperUtils.extractRowsUpdated(Manager.class,getUpdates(), getDbSchema());
53 public void execute(ReadWriteTransaction transaction) {
54 if (updatedManagerRows != null && !updatedManagerRows.isEmpty()) {
55 Map<Uri, Manager> updatedManagerRowsWithUri = getUriManagerMap(updatedManagerRows);
56 if (updatedOpenVSwitchRows != null && !updatedOpenVSwitchRows.isEmpty()) {
57 updateManagers(transaction, updatedManagerRows, updatedOpenVSwitchRows);
59 updateManagers(transaction, updatedManagerRowsWithUri);
65 * Update the Manager values for the given {@link OpenVSwitch} list.
68 * Manager and OpenVSwitch are independent tables in the Open_vSwitch schema
69 * but the OVSDB yang model includes the Manager fields in the
70 * OVSDB Node data. In some cases the OVSDB will send OpenVSwitch and Manager
71 * updates together and in other cases independently. This method here
75 * @param transaction the {@link ReadWriteTransaction}
76 * @param newUpdatedManagerRows updated {@link Manager} rows
77 * @param newUpdatedOpenVSwitchRows updated {@link OpenVSwitch} rows
79 private void updateManagers(ReadWriteTransaction transaction,
80 Map<UUID, Manager> newUpdatedManagerRows,
81 Map<UUID, OpenVSwitch> newUpdatedOpenVSwitchRows) {
83 for (Map.Entry<UUID, OpenVSwitch> ovsdbNodeEntry : newUpdatedOpenVSwitchRows.entrySet()) {
84 final List<ManagerEntry> managerEntries =
85 SouthboundMapper.createManagerEntries(ovsdbNodeEntry.getValue(), newUpdatedManagerRows);
86 LOG.debug("Update Ovsdb Node : {} with manager entries : {}",
87 ovsdbNodeEntry.getValue(), managerEntries);
88 for (ManagerEntry managerEntry : managerEntries) {
89 transaction.merge(LogicalDatastoreType.OPERATIONAL,
90 getManagerEntryIid(managerEntry),
97 * Update the ManagerEntry values after finding the related {@OpenVSwitch} list.
100 * Manager and OpenVSwitch are independent tables in the Open_vSwitch schema
101 * but the OVSDB yang model includes the Manager fields in the
102 * OvsdbNode data. In some cases the OVSDB will send OpenVSwitch and Manager
103 * updates together and in other cases independently. This method here
104 * assumes the latter.
107 * @param transaction the {@link ReadWriteTransaction}
108 * @param newUpdatedManagerRows updated {@link Manager} rows
111 private void updateManagers(ReadWriteTransaction transaction,
112 Map<Uri, Manager> newUpdatedManagerRows) {
114 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
115 final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, connectionIId);
116 if (ovsdbNode.isPresent()) {
117 final List<ManagerEntry> managerEntries =
118 SouthboundMapper.createManagerEntries(ovsdbNode.get(), newUpdatedManagerRows);
120 LOG.debug("Update Ovsdb Node : {} with manager entries : {}",
121 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;