ovsdb enable checkstyle on error
[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.base.Optional;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
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.rev100924.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;
40
41 public class OvsdbControllerUpdateCommand extends AbstractTransactionCommand {
42     private static final Logger LOG = LoggerFactory.getLogger(OvsdbControllerUpdateCommand.class);
43     private Map<UUID, Controller> updatedControllerRows;
44     private Map<UUID, Bridge> updatedBridgeRows;
45
46     public OvsdbControllerUpdateCommand(OvsdbConnectionInstance key,
47             TableUpdates updates, DatabaseSchema dbSchema) {
48         super(key, updates, dbSchema);
49         updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
50         updatedControllerRows = TyperUtils.extractRowsUpdated(Controller.class,
51                 getUpdates(), getDbSchema());
52     }
53
54     @Override
55     public void execute(ReadWriteTransaction transaction) {
56         if (updatedControllerRows != null && !updatedControllerRows.isEmpty()) {
57             if (updatedBridgeRows != null && !updatedBridgeRows.isEmpty()) {
58                 updateController(transaction, updatedControllerRows, updatedBridgeRows);
59             } else {
60                 updateController(transaction, updatedControllerRows);
61             }
62         }
63     }
64
65     /**
66      * Update the ControllerEntry values for the given {@link Bridge} list.
67      *
68      * <p>
69      * Controller and Bridge are independent tables in the Open_vSwitch schema
70      * but the OVSDB yang model includes the Controller fields in the
71      * Bridge data. In some cases the OVSDB will send Bridge and Controller
72      * updates together and in other cases independently. This method here
73      * assumes the former.
74      * </p>
75      *
76      * @param transaction the {@link ReadWriteTransaction}
77      * @param updatedControllerRows updated {@link Controller} rows
78      * @param updatedBridgeRows updated {@link Bridge} rows
79      */
80     private void updateController(ReadWriteTransaction transaction,
81                                   Map<UUID, Controller> updatedControllerRows,
82                                   Map<UUID, Bridge> updatedBridgeRows) {
83
84         for (Map.Entry<UUID, Bridge> bridgeEntry : updatedBridgeRows.entrySet()) {
85             final List<ControllerEntry> controllerEntries =
86                     SouthboundMapper.createControllerEntries(bridgeEntry.getValue(), updatedControllerRows);
87
88             for (ControllerEntry controllerEntry : controllerEntries) {
89                 transaction.merge(LogicalDatastoreType.OPERATIONAL,
90                         getControllerEntryIid(controllerEntry, bridgeEntry.getValue().getNameColumn().getData()),
91                         controllerEntry);
92             }
93         }
94     }
95
96     /**
97      * Update the ControllerEntry values after finding the related {@Bridge} list.
98      *
99      * <p>
100      * Controller and Bridge are independent tables in the Open_vSwitch schema
101      * but the OVSDB yang model includes the Controller fields in the
102      * Bridge data. In some cases the OVSDB will send Bridge and Controller
103      * updates together and in other cases independently. This method here
104      * assumes the latter.
105      * </p>
106      *
107      * @param transaction the {@link ReadWriteTransaction}
108      * @param updatedControllerRows updated {@link Controller} rows
109
110      */
111     private void updateController(ReadWriteTransaction transaction,
112                                   Map<UUID, Controller> updatedControllerRows) {
113
114         Map<InstanceIdentifier<Node>, Node> bridgeNodes = getBridgeNodes(transaction);
115         for (Map.Entry<InstanceIdentifier<Node>, Node> bridgeNodeEntry : bridgeNodes.entrySet()) {
116             final List<ControllerEntry> controllerEntries =
117                     SouthboundMapper.createControllerEntries(bridgeNodeEntry.getValue(), updatedControllerRows);
118
119             for (ControllerEntry controllerEntry : controllerEntries) {
120                 final InstanceIdentifier<Node> bridgeIid = bridgeNodeEntry.getKey();
121                 InstanceIdentifier<ControllerEntry> iid = bridgeIid
122                         .augmentation(OvsdbBridgeAugmentation.class)
123                         .child(ControllerEntry.class, controllerEntry.getKey());
124                 transaction.merge(LogicalDatastoreType.OPERATIONAL,
125                         iid, controllerEntry);
126             }
127         }
128     }
129
130     /**
131      * Find all the {@link Node} bridge nodes for the given connection.
132      *
133      * @param transaction the {@link ReadWriteTransaction}
134      * @return map of nodes
135      */
136     private Map<InstanceIdentifier<Node>, Node> getBridgeNodes(ReadWriteTransaction transaction) {
137         Map<InstanceIdentifier<Node>, Node> bridgeNodes = new HashMap<>();
138         final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
139         final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, connectionIId);
140         if (ovsdbNode.isPresent()) {
141             OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.get().getAugmentation(OvsdbNodeAugmentation.class);
142             if (ovsdbNodeAugmentation != null) {
143                 final List<ManagedNodeEntry> managedNodeEntries = ovsdbNodeAugmentation.getManagedNodeEntry();
144                 for (ManagedNodeEntry managedNodeEntry : managedNodeEntries) {
145                     final InstanceIdentifier<Node> bridgeIid =
146                             (InstanceIdentifier<Node>) managedNodeEntry.getBridgeRef().getValue();
147                     @SuppressWarnings("unchecked")
148                     final Optional<Node> bridgeNode = SouthboundUtil.readNode(transaction, bridgeIid);
149                     if (bridgeNode.isPresent()) {
150                         bridgeNodes.put(bridgeIid, bridgeNode.get());
151                     } else {
152                         LOG.warn("OVSDB bridge node was not found: {}", bridgeIid);
153                     }
154                 }
155             } else {
156                 LOG.warn("OvsdbNodeAugmentation was not found: {}", connectionIId);
157             }
158         } else {
159             LOG.warn("OVSDB node was not found: {}", connectionIId);
160         }
161
162         return bridgeNodes;
163     }
164
165     /**
166      * Create the {@link InstanceIdentifier} for the {@link ControllerEntry}.
167      *
168      * @param controllerEntry the {@link ControllerEntry}
169      * @param bridgeName the name of the bridge
170      * @return the {@link InstanceIdentifier}
171      */
172     private InstanceIdentifier<ControllerEntry> getControllerEntryIid(
173             ControllerEntry controllerEntry, String bridgeName) {
174
175         OvsdbConnectionInstance client = getOvsdbConnectionInstance();
176         String nodeString = client.getNodeKey().getNodeId().getValue()
177                 + "/bridge/" + bridgeName;
178         NodeId nodeId = new NodeId(new Uri(nodeString));
179         NodeKey nodeKey = new NodeKey(nodeId);
180         InstanceIdentifier<Node> bridgeIid = InstanceIdentifier.builder(NetworkTopology.class)
181                 .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
182                 .child(Node.class,nodeKey)
183                 .build();
184
185         return bridgeIid
186                 .augmentation(OvsdbBridgeAugmentation.class)
187                 .child(ControllerEntry.class, controllerEntry.getKey());
188     }
189 }