2d56395cffebb98534455a1dcdb940f1a341ce05
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbManagersUpdateCommand.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.ovsdb.southbound.transactions.md;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Optional;
16 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.ovsdb.lib.message.TableUpdates;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
20 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
21 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
22 import org.opendaylight.ovsdb.schema.openvswitch.Manager;
23 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
24 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
25 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
26 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
27 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntry;
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;
40
41 public class OvsdbManagersUpdateCommand extends AbstractTransactionCommand {
42     private static final Logger LOG = LoggerFactory.getLogger(OvsdbManagersUpdateCommand.class);
43
44     private Map<UUID, Manager> updatedManagerRows;
45     private Map<UUID, OpenVSwitch> updatedOpenVSwitchRows;
46
47     @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR", justification = "Non-final for mocking")
48     public OvsdbManagersUpdateCommand(OvsdbConnectionInstance key, 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());
52     }
53
54     @Override
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);
60             } else {
61                 updateManagers(transaction, updatedManagerRowsWithUri);
62             }
63         }
64     }
65
66     /**
67      * Update the Manager values for the given {@link OpenVSwitch} list.
68      *
69      * <p>
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
74      * assumes the former.
75      * </p>
76      *
77      * @param transaction the {@link ReadWriteTransaction}
78      * @param newUpdatedManagerRows updated {@link Manager} rows
79      * @param newUpdatedOpenVSwitchRows updated {@link OpenVSwitch} rows
80      */
81     private void updateManagers(ReadWriteTransaction transaction,
82                                   Map<UUID, Manager> newUpdatedManagerRows,
83                                   Map<UUID, OpenVSwitch> newUpdatedOpenVSwitchRows) {
84
85         for (Map.Entry<UUID, OpenVSwitch> ovsdbNodeEntry : newUpdatedOpenVSwitchRows.entrySet()) {
86             final List<ManagerEntry> managerEntries =
87                     SouthboundMapper.createManagerEntries(ovsdbNodeEntry.getValue(), newUpdatedManagerRows);
88             LOG.debug("Update Ovsdb Node : {} with manager entries : {}",
89                     ovsdbNodeEntry.getValue(), managerEntries);
90             for (ManagerEntry managerEntry : managerEntries) {
91                 transaction.merge(LogicalDatastoreType.OPERATIONAL,
92                         getManagerEntryIid(managerEntry),
93                         managerEntry);
94             }
95         }
96     }
97
98     /**
99      * Update the ManagerEntry values after finding the related {@OpenVSwitch} list.
100      *
101      * <p>
102      * Manager and OpenVSwitch are independent tables in the Open_vSwitch schema
103      * but the OVSDB yang model includes the Manager fields in the
104      * OvsdbNode data. In some cases the OVSDB will send OpenVSwitch and Manager
105      * updates together and in other cases independently. This method here
106      * assumes the latter.
107      * </p>
108      *
109      * @param transaction the {@link ReadWriteTransaction}
110      * @param newUpdatedManagerRows updated {@link Manager} rows
111
112      */
113     private void updateManagers(ReadWriteTransaction transaction,
114                                   Map<Uri, Manager> newUpdatedManagerRows) {
115
116         final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
117         final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, connectionIId);
118         if (ovsdbNode.isPresent()) {
119             final List<ManagerEntry> managerEntries =
120                     SouthboundMapper.createManagerEntries(ovsdbNode.get(), newUpdatedManagerRows);
121
122             LOG.debug("Update Ovsdb Node : {} with manager entries : {}",
123                     ovsdbNode.get(), managerEntries);
124             for (ManagerEntry managerEntry : managerEntries) {
125                 InstanceIdentifier<ManagerEntry> iid = connectionIId
126                         .augmentation(OvsdbNodeAugmentation.class)
127                         .child(ManagerEntry.class, managerEntry.key());
128                 transaction.merge(LogicalDatastoreType.OPERATIONAL,
129                         iid, managerEntry);
130             }
131         }
132     }
133
134     /**
135      * Create the {@link InstanceIdentifier} for the {@link ManagerEntry}.
136      *
137      * @param managerEntry the {@link ManagerEntry}
138      * @return the {@link InstanceIdentifier}
139      */
140     @VisibleForTesting
141     final InstanceIdentifier<ManagerEntry> getManagerEntryIid(ManagerEntry managerEntry) {
142
143         OvsdbConnectionInstance client = getOvsdbConnectionInstance();
144         String nodeString = client.getNodeKey().getNodeId().getValue();
145         NodeId nodeId = new NodeId(new Uri(nodeString));
146         NodeKey nodeKey = new NodeKey(nodeId);
147         InstanceIdentifier<Node> ovsdbNodeIid = InstanceIdentifier.builder(NetworkTopology.class)
148                 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
149                 .child(Node.class,nodeKey)
150                 .build();
151
152         return ovsdbNodeIid
153                 .augmentation(OvsdbNodeAugmentation.class)
154                 .child(ManagerEntry.class, managerEntry.key());
155     }
156
157     private Map<Uri, Manager> getUriManagerMap(Map<UUID,Manager> uuidManagerMap) {
158         Map<Uri, Manager> uriManagerMap = new HashMap<>();
159         for (Map.Entry<UUID, Manager> uuidManagerMapEntry : uuidManagerMap.entrySet()) {
160             uriManagerMap.put(
161                     new Uri(uuidManagerMapEntry.getValue().getTargetColumn().getData()),
162                     uuidManagerMapEntry.getValue());
163         }
164         return uriManagerMap;
165
166     }
167 }