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