3ad92c2d931347d49deed39a4f9192e2e715cba3
[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 java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
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;
38
39 import com.google.common.base.Optional;
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     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());
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 updatedManagerRows updated {@link Manager} rows
79      * @param updatedOpenVSwitchRows updated {@link OpenVSwitch} rows
80      */
81     private void updateManagers(ReadWriteTransaction transaction,
82                                   Map<UUID, Manager> updatedManagerRows,
83                                   Map<UUID, OpenVSwitch> updatedOpenVSwitchRows) {
84
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),
92                         managerEntry);
93             }
94         }
95     }
96
97     /**
98      * Update the ManagerEntry values after finding the related {@OpenVSwitch} list.
99      *
100      * <p>
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.
106      * </p>
107      *
108      * @param transaction the {@link ReadWriteTransaction}
109      * @param updatedManagerRows updated {@link Manager} rows
110
111      */
112     private void updateManagers(ReadWriteTransaction transaction,
113                                   Map<Uri, Manager> updatedManagerRows) {
114
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);
120
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,
127                         iid, managerEntry);
128             }
129         }
130     }
131
132     /**
133      * Create the {@link InstanceIdentifier} for the {@link ManagerEntry}.
134      *
135      * @param managerEntry the {@link ManagerEntry}
136      * @return the {@link InstanceIdentifier}
137      */
138     private InstanceIdentifier<ManagerEntry> getManagerEntryIid(ManagerEntry managerEntry) {
139
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)
147                 .build();
148
149         return ovsdbNodeIid
150                 .augmentation(OvsdbNodeAugmentation.class)
151                 .child(ManagerEntry.class, managerEntry.getKey());
152     }
153
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()) {
157             uriManagerMap.put(
158                     new Uri(uuidManagerMapEntry.getValue().getTargetColumn().getData()),
159                     uuidManagerMapEntry.getValue());
160         }
161         return uriManagerMap;
162
163     }
164 }