Merge "Add JUnit testing for NodeConfiguration class."
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbBridgeUpdateCommand.java
1 package org.opendaylight.ovsdb.southbound.transactions.md;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.Map.Entry;
7 import java.util.Set;
8
9 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
10 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
11 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
12 import org.opendaylight.ovsdb.lib.message.TableUpdates;
13 import org.opendaylight.ovsdb.lib.notation.UUID;
14 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
15 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
16 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
17 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
18 import org.opendaylight.ovsdb.southbound.OvsdbClientKey;
19 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
20 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
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.OvsdbNodeAugmentationBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeRef;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIds;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeExternalIdsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntryBuilder;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.google.common.base.Optional;
44
45 public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
46     private static final Logger LOG = LoggerFactory.getLogger(OvsdbBridgeUpdateCommand.class);
47
48     public OvsdbBridgeUpdateCommand(OvsdbClientKey key, TableUpdates updates,
49             DatabaseSchema dbSchema) {
50         super(key,updates,dbSchema);
51     }
52
53     @Override
54     public void execute(ReadWriteTransaction transaction) {
55         Map<UUID,Bridge> updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
56         Map<UUID,Controller> updatedControllerRows = TyperUtils.extractRowsUpdated(Controller.class,
57                 getUpdates(), getDbSchema());
58         for (Entry<UUID, Bridge> entry : updatedBridgeRows.entrySet()) {
59             Bridge bridge = entry.getValue();
60             final InstanceIdentifier<Node> nodePath = getKey().toInstanceIndentifier();
61             Optional<Node> node = Optional.absent();
62             try {
63                 node = transaction.read(LogicalDatastoreType.OPERATIONAL, nodePath).checkedGet();
64             } catch (final ReadFailedException e) {
65                 LOG.debug("Read Operational/DS for Node fail! {}", nodePath, e);
66             }
67             if (node.isPresent()) {
68                 LOG.debug("Node {} is present",node);
69                 NodeBuilder managedNodeBuilder = new NodeBuilder();
70                 InstanceIdentifier<Node> managedNodePath = SouthboundMapper.createInstanceIdentifier(getKey(),bridge);
71                 NodeId manageNodeId = SouthboundMapper.createManagedNodeId(managedNodePath);
72                 managedNodeBuilder.setNodeId(manageNodeId);
73                 OvsdbBridgeAugmentationBuilder ovsdbManagedNodeBuilder = new OvsdbBridgeAugmentationBuilder();
74                 ovsdbManagedNodeBuilder.setBridgeName(new OvsdbBridgeName(bridge.getName()));
75                 ovsdbManagedNodeBuilder.setBridgeUuid(new Uuid(bridge.getUuid().toString()));
76                 DatapathId dpid = SouthboundMapper.createDatapathId(bridge);
77                 if (dpid != null) {
78                     ovsdbManagedNodeBuilder.setDatapathId(dpid);
79                 }
80                 ovsdbManagedNodeBuilder.setDatapathType(
81                         SouthboundMapper.createDatapathType(bridge.getDatapathTypeColumn().getData()));
82                 if (SouthboundMapper.createMdsalProtocols(bridge) != null
83                         && SouthboundMapper.createMdsalProtocols(bridge).size() > 0) {
84                     ovsdbManagedNodeBuilder.setProtocolEntry(SouthboundMapper.createMdsalProtocols(bridge));
85                 }
86
87                 Map<String, String> externalIds = bridge.getExternalIdsColumn()
88                         .getData();
89                 if (externalIds != null && !externalIds.isEmpty()) {
90                     Set<String> externalIdKeys = externalIds.keySet();
91                     List<BridgeExternalIds> externalIdsList = new ArrayList<BridgeExternalIds>();
92                     String externalIdValue;
93                     for (String externalIdKey : externalIdKeys) {
94                         externalIdValue = externalIds.get(externalIdKey);
95                         if (externalIdKey != null && externalIdValue != null) {
96                             externalIdsList.add(new BridgeExternalIdsBuilder()
97                                     .setBridgeExternalIdKey(externalIdKey)
98                                     .setBridgeExternalIdValue(externalIdValue)
99                                     .build());
100                         }
101                     }
102                     ovsdbManagedNodeBuilder.setBridgeExternalIds(externalIdsList);
103                 }
104
105                 Map<String, String> otherConfigs = bridge
106                         .getOtherConfigColumn().getData();
107                 if (otherConfigs != null && !otherConfigs.isEmpty()) {
108                     Set<String> otherConfigKeys = otherConfigs.keySet();
109                     List<BridgeOtherConfigs> otherConfigList = new ArrayList<BridgeOtherConfigs>();
110                     String otherConfigValue;
111                     for (String otherConfigKey : otherConfigKeys) {
112                         otherConfigValue = otherConfigs.get(otherConfigKey);
113                         if (otherConfigKey != null && otherConfigValue != null) {
114                             otherConfigList.add(new BridgeOtherConfigsBuilder()
115                                     .setBridgeOtherConfigKey(otherConfigKey)
116                                     .setBridgeOtherConfigValue(otherConfigValue)
117                                     .build());
118                         }
119                     }
120                     ovsdbManagedNodeBuilder.setBridgeOtherConfigs(otherConfigList);
121                 }
122
123                 if (!SouthboundMapper.createControllerEntries(bridge, updatedControllerRows).isEmpty()) {
124                     ovsdbManagedNodeBuilder.setControllerEntry(
125                             SouthboundMapper.createControllerEntries(bridge, updatedControllerRows));
126                 }
127
128                 if (bridge.getFailModeColumn() != null
129                         && bridge.getFailModeColumn().getData() != null
130                         && !bridge.getFailModeColumn().getData().isEmpty()) {
131                     String[] failmodeArray = new String[bridge.getFailModeColumn().getData().size()];
132                     bridge.getFailModeColumn().getData().toArray(failmodeArray);
133                     ovsdbManagedNodeBuilder.setFailMode(
134                             SouthboundConstants.OVSDB_FAIL_MODE_MAP.inverse().get(failmodeArray[0]));
135                 }
136                 ovsdbManagedNodeBuilder.setManagedBy(new OvsdbNodeRef(nodePath));
137                 managedNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbManagedNodeBuilder.build());
138
139                 LOG.debug("Store managed node augmentation data {}",ovsdbManagedNodeBuilder.toString());
140                 transaction.merge(LogicalDatastoreType.OPERATIONAL, managedNodePath, managedNodeBuilder.build());
141
142                 //Update node with managed node reference
143                 NodeBuilder nodeBuilder = new NodeBuilder();
144                 nodeBuilder.setNodeId(SouthboundMapper.createNodeId(getKey().getIp(),getKey().getPort()));
145
146                 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
147                 List<ManagedNodeEntry> managedNodes = new ArrayList<ManagedNodeEntry>();
148                 ManagedNodeEntry managedNodeEntry = new ManagedNodeEntryBuilder().setBridgeRef(
149                         new OvsdbBridgeRef(managedNodePath)).build();
150                 managedNodes.add(managedNodeEntry);
151                 ovsdbNodeBuilder.setManagedNodeEntry(managedNodes);
152
153                 nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, ovsdbNodeBuilder.build());
154
155                 LOG.debug("Update node with managed node ref {}",ovsdbNodeBuilder.toString());
156                 transaction.merge(LogicalDatastoreType.OPERATIONAL, nodePath, nodeBuilder.build());
157
158             }
159         }
160     }
161 }