Upgrade powermock to 2.0.0
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbControllerUpdateCommand.java
1 /*
2  * Copyright (c) 2015 Cisco 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 com.google.common.base.Optional;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.ovsdb.lib.message.TableUpdates;
18 import org.opendaylight.ovsdb.lib.notation.UUID;
19 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
20 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
21 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
22 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
23 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
24 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
25 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
26 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
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.bridge.attributes.ControllerEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class OvsdbControllerUpdateCommand extends AbstractTransactionCommand {
43     private static final Logger LOG = LoggerFactory.getLogger(OvsdbControllerUpdateCommand.class);
44
45     private final Map<UUID, Controller> updatedControllerRows;
46     private final Map<UUID, Bridge> updatedBridgeRows;
47
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());
54     }
55
56     @Override
57     public void execute(ReadWriteTransaction transaction) {
58         if (updatedControllerRows != null && !updatedControllerRows.isEmpty()) {
59             if (updatedBridgeRows != null && !updatedBridgeRows.isEmpty()) {
60                 updateController(transaction, updatedControllerRows, updatedBridgeRows);
61             } else {
62                 updateController(transaction, updatedControllerRows);
63             }
64         }
65     }
66
67     /**
68      * Update the ControllerEntry values for the given {@link Bridge} list.
69      *
70      * <p>
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
75      * assumes the former.
76      * </p>
77      *
78      * @param transaction the {@link ReadWriteTransaction}
79      * @param newUpdatedControllerRows updated {@link Controller} rows
80      * @param newUpdatedBridgeRows updated {@link Bridge} rows
81      */
82     private void updateController(ReadWriteTransaction transaction,
83                                   Map<UUID, Controller> newUpdatedControllerRows,
84                                   Map<UUID, Bridge> newUpdatedBridgeRows) {
85
86         for (Map.Entry<UUID, Bridge> bridgeEntry : newUpdatedBridgeRows.entrySet()) {
87             final List<ControllerEntry> controllerEntries =
88                     SouthboundMapper.createControllerEntries(bridgeEntry.getValue(), newUpdatedControllerRows);
89
90             for (ControllerEntry controllerEntry : controllerEntries) {
91                 transaction.merge(LogicalDatastoreType.OPERATIONAL,
92                         getControllerEntryIid(controllerEntry, bridgeEntry.getValue().getNameColumn().getData()),
93                         controllerEntry);
94             }
95         }
96     }
97
98     /**
99      * Update the ControllerEntry values after finding the related {@Bridge} list.
100      *
101      * <p>
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.
107      * </p>
108      *
109      * @param transaction the {@link ReadWriteTransaction}
110      * @param newUpdatedControllerRows updated {@link Controller} rows
111      */
112     @VisibleForTesting
113     void updateController(ReadWriteTransaction transaction,
114                                   Map<UUID, Controller> newUpdatedControllerRows) {
115
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(), newUpdatedControllerRows);
120
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.key());
126                 transaction.merge(LogicalDatastoreType.OPERATIONAL,
127                         iid, controllerEntry);
128             }
129         }
130     }
131
132     /**
133      * Find all the {@link Node} bridge nodes for the given connection.
134      *
135      * @param transaction the {@link ReadWriteTransaction}
136      * @return map of nodes
137      */
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().augmentation(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());
153                     } else {
154                         LOG.warn("OVSDB bridge node was not found: {}", bridgeIid);
155                     }
156                 }
157             } else {
158                 LOG.warn("OvsdbNodeAugmentation was not found: {}", connectionIId);
159             }
160         } else {
161             LOG.warn("OVSDB node was not found: {}", connectionIId);
162         }
163
164         return bridgeNodes;
165     }
166
167     /**
168      * Create the {@link InstanceIdentifier} for the {@link ControllerEntry}.
169      *
170      * @param controllerEntry the {@link ControllerEntry}
171      * @param bridgeName the name of the bridge
172      * @return the {@link InstanceIdentifier}
173      */
174     @VisibleForTesting
175     InstanceIdentifier<ControllerEntry> getControllerEntryIid(
176             ControllerEntry controllerEntry, String bridgeName) {
177
178         OvsdbConnectionInstance client = getOvsdbConnectionInstance();
179         String nodeString = client.getNodeKey().getNodeId().getValue()
180                 + "/bridge/" + bridgeName;
181         NodeId nodeId = new NodeId(new Uri(nodeString));
182         NodeKey nodeKey = new NodeKey(nodeId);
183         InstanceIdentifier<Node> bridgeIid = InstanceIdentifier.builder(NetworkTopology.class)
184                 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
185                 .child(Node.class,nodeKey)
186                 .build();
187
188         return bridgeIid
189                 .augmentation(OvsdbBridgeAugmentation.class)
190                 .child(ControllerEntry.class, controllerEntry.key());
191     }
192 }