Upgrade ietf-{inet,yang}-types to 2013-07-15
[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.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.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;
38
39 public class OvsdbManagersUpdateCommand extends AbstractTransactionCommand {
40     private static final Logger LOG = LoggerFactory.getLogger(OvsdbManagersUpdateCommand.class);
41
42     private Map<UUID, Manager> updatedManagerRows;
43     private Map<UUID, OpenVSwitch> updatedOpenVSwitchRows;
44
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());
50     }
51
52     @Override
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);
58             } else {
59                 updateManagers(transaction, updatedManagerRowsWithUri);
60             }
61         }
62     }
63
64     /**
65      * Update the Manager values for the given {@link OpenVSwitch} list.
66      *
67      * <p>
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
72      * assumes the former.
73      * </p>
74      *
75      * @param transaction the {@link ReadWriteTransaction}
76      * @param newUpdatedManagerRows updated {@link Manager} rows
77      * @param newUpdatedOpenVSwitchRows updated {@link OpenVSwitch} rows
78      */
79     private void updateManagers(ReadWriteTransaction transaction,
80                                   Map<UUID, Manager> newUpdatedManagerRows,
81                                   Map<UUID, OpenVSwitch> newUpdatedOpenVSwitchRows) {
82
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),
91                         managerEntry);
92             }
93         }
94     }
95
96     /**
97      * Update the ManagerEntry values after finding the related {@OpenVSwitch} list.
98      *
99      * <p>
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.
105      * </p>
106      *
107      * @param transaction the {@link ReadWriteTransaction}
108      * @param newUpdatedManagerRows updated {@link Manager} rows
109
110      */
111     private void updateManagers(ReadWriteTransaction transaction,
112                                   Map<Uri, Manager> newUpdatedManagerRows) {
113
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);
119
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,
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 }