Migrate to simplified addAugmentation() method
[genius.git] / itm / itm-impl / src / test / java / org / opendaylight / genius / itm / tests / OvsdbTestUtil.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. 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.genius.itm.tests;
9
10 import com.google.common.util.concurrent.FluentFuture;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.WriteTransaction;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentationBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
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.node.attributes.ConnectionInfo;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfoBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIdsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigsBuilder;
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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41
42 public final class OvsdbTestUtil {
43     private OvsdbTestUtil() {
44
45     }
46
47     /* methods */
48     public static ConnectionInfo getConnectionInfo(int port, String strIpAddress) {
49         IpAddress ipAddress = IpAddressBuilder.getDefaultInstance(strIpAddress);
50         PortNumber portNumber = new PortNumber(port);
51
52         ConnectionInfo connectionInfo =
53             new ConnectionInfoBuilder().setRemoteIp(ipAddress).setRemotePort(portNumber).build();
54
55         return connectionInfo;
56     }
57
58     public static FluentFuture<? extends @NonNull CommitInfo> createNode(
59         ConnectionInfo connectionInfo, String tepIp, String tzName, DataBroker dataBroker)
60         throws Exception {
61         final InstanceIdentifier<Node> iid = SouthboundUtils.createInstanceIdentifier(connectionInfo);
62
63         // build Node using its builder class
64         NodeBuilder nodeBuilder = new NodeBuilder();
65         NodeId ovsdbNodeId = SouthboundUtils.createNodeId(connectionInfo.getRemoteIp(),
66             connectionInfo.getRemotePort());
67         nodeBuilder.setNodeId(ovsdbNodeId);
68
69         // build OvsdbNodeAugmentation for Node
70         OvsdbNodeAugmentationBuilder ovsdbNodeAugBuilder = new OvsdbNodeAugmentationBuilder();
71         ovsdbNodeAugBuilder.setConnectionInfo(connectionInfo);
72
73         // create map of key-val pairs
74         Map<String, String> externalIds = new HashMap<>();
75         Map<String, String> otherConfigs = new HashMap<>();
76
77         if (tepIp != null && !tepIp.isEmpty()) {
78             otherConfigs.put(ItmTestConstants.OTHER_CFG_TEP_IP_KEY, tepIp);
79         }
80
81         if (tzName != null) {
82             externalIds.put(ItmTestConstants.EXTERNAL_ID_TZNAME_KEY, tzName);
83         }
84
85         // get map-keys into set.
86         Set<String> externalIdKeys = externalIds.keySet();
87         Set<String> otherConfigKeys = otherConfigs.keySet();
88
89         List<OpenvswitchExternalIds> externalIdsList = new ArrayList<>();
90         String externalIdValue = null;
91         for (String externalIdKey : externalIdKeys) {
92             externalIdValue = externalIds.get(externalIdKey);
93             if (externalIdKey != null && externalIdValue != null) {
94                 externalIdsList.add(new OpenvswitchExternalIdsBuilder().setExternalIdKey(externalIdKey)
95                     .setExternalIdValue(externalIdValue).build());
96             }
97         }
98
99         List<OpenvswitchOtherConfigs> otherConfigsList = new ArrayList<>();
100         String otherConfigValue = null;
101         for (String otherConfigKey : otherConfigKeys) {
102             otherConfigValue = otherConfigs.get(otherConfigKey);
103             if (otherConfigKey != null && otherConfigValue != null) {
104                 otherConfigsList.add(new OpenvswitchOtherConfigsBuilder().setOtherConfigKey(otherConfigKey)
105                     .setOtherConfigValue(otherConfigValue).build());
106             }
107         }
108
109         // set ExternalIds list into Node
110         ovsdbNodeAugBuilder.setOpenvswitchExternalIds(externalIdsList);
111
112         // set OtherConfig list into Node
113         ovsdbNodeAugBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
114
115         // add OvsdbNodeAugmentation into Node
116         nodeBuilder.addAugmentation(ovsdbNodeAugBuilder.build());
117         Node ovsdbNode = nodeBuilder.build();
118
119         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
120         transaction.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, iid, ovsdbNode);
121         return transaction.commit();
122     }
123
124     public static FluentFuture<? extends @NonNull CommitInfo> updateNode(
125         ConnectionInfo connectionInfo, String tepIp, String tzName, String brName,
126         DataBroker dataBroker) throws Exception {
127         final InstanceIdentifier<Node> iid = SouthboundUtils.createInstanceIdentifier(connectionInfo);
128
129         Node oldOvsdbNode = dataBroker.newReadOnlyTransaction()
130             .read(LogicalDatastoreType.OPERATIONAL, iid).get().get();
131
132         // build Node using its builder class
133         NodeBuilder nodeBuilder = new NodeBuilder();
134         nodeBuilder.setNodeId(oldOvsdbNode.getNodeId());
135
136         // build OvsdbNodeAugmentation for Node
137         OvsdbNodeAugmentationBuilder ovsdbNodeAugBuilder = new OvsdbNodeAugmentationBuilder();
138         ovsdbNodeAugBuilder.setConnectionInfo(connectionInfo);
139
140         // create map of key-val pairs
141         Map<String, String> externalIds = new HashMap<>();
142         Map<String, String> otherConfigs = new HashMap<>();
143         if (tepIp != null && !tepIp.isEmpty()) {
144             otherConfigs.put(ItmTestConstants.OTHER_CFG_TEP_IP_KEY, tepIp);
145         }
146
147         if (tzName != null) {
148             externalIds.put(ItmTestConstants.EXTERNAL_ID_TZNAME_KEY, tzName);
149         }
150
151         if (brName != null && !brName.isEmpty()) {
152             externalIds.put(ItmTestConstants.EXTERNAL_ID_BR_NAME_KEY, brName);
153         }
154
155         // get map-keys into set.
156         Set<String> externalIdKeys = externalIds.keySet();
157         Set<String> otherConfigKeys = otherConfigs.keySet();
158
159         List<OpenvswitchExternalIds> externalIdsList = new ArrayList<>();
160         String externalIdValue = null;
161         for (String externalIdKey : externalIdKeys) {
162             externalIdValue = externalIds.get(externalIdKey);
163             if (externalIdKey != null && externalIdValue != null) {
164                 externalIdsList.add(new OpenvswitchExternalIdsBuilder().setExternalIdKey(externalIdKey)
165                     .setExternalIdValue(externalIdValue).build());
166             }
167         }
168
169         List<OpenvswitchOtherConfigs> otherConfigsList = new ArrayList<>();
170         String otherConfigsValue = null;
171         for (String otherConfigKey : otherConfigKeys) {
172             otherConfigsValue = otherConfigs.get(otherConfigKey);
173             if (otherConfigKey != null && otherConfigsValue != null) {
174                 otherConfigsList.add(new OpenvswitchOtherConfigsBuilder().setOtherConfigKey(otherConfigKey)
175                         .setOtherConfigValue(otherConfigsValue).build());
176             }
177         }
178
179         // set ExternalIds list into Node
180         ovsdbNodeAugBuilder.setOpenvswitchExternalIds(externalIdsList);
181
182         // set OtherConfigs list into Node
183         ovsdbNodeAugBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
184
185         // add OvsdbNodeAugmentation into Node
186         nodeBuilder.addAugmentation(ovsdbNodeAugBuilder.build());
187         Node ovsdbNode = nodeBuilder.build();
188
189         //ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
190         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
191         transaction.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, iid, ovsdbNode);
192         return transaction.commit();
193     }
194
195     public static FluentFuture<? extends @NonNull CommitInfo> addBridgeIntoNode(
196         ConnectionInfo connectionInfo, String bridgeName, String dpid, DataBroker dataBroker) throws Exception {
197         NodeId ovsdbNodeId = SouthboundUtils.createNodeId(connectionInfo.getRemoteIp(),
198             connectionInfo.getRemotePort());
199         NodeKey nodeKey = new NodeKey(ovsdbNodeId);
200
201         NodeBuilder bridgeNodeBuilder = new NodeBuilder();
202
203         InstanceIdentifier<Node> bridgeIid = SouthboundUtils.createInstanceIdentifier(nodeKey, bridgeName);
204
205         NodeId bridgeNodeId = SouthboundUtils.createManagedNodeId(bridgeIid);
206         bridgeNodeBuilder.setNodeId(bridgeNodeId);
207         OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
208         ovsdbBridgeAugmentationBuilder.setBridgeName(new OvsdbBridgeName(bridgeName))
209             .setDatapathId(new DatapathId(dpid));
210         ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(
211             SouthboundUtils.createInstanceIdentifier(nodeKey.getNodeId())));
212
213         bridgeNodeBuilder.addAugmentation(ovsdbBridgeAugmentationBuilder.build());
214
215         Node bridgeNode = bridgeNodeBuilder.build();
216
217         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
218         tx.mergeParentStructureMerge(LogicalDatastoreType.OPERATIONAL, bridgeIid,
219             bridgeNode);
220         return tx.commit();
221     }
222 }