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