2 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.genius.itm.tests;
10 import com.google.common.util.concurrent.FluentFuture;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
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;
42 public final class OvsdbTestUtil {
43 private OvsdbTestUtil() {
48 public static ConnectionInfo getConnectionInfo(int port, String strIpAddress) {
49 IpAddress ipAddress = IpAddressBuilder.getDefaultInstance(strIpAddress);
50 PortNumber portNumber = new PortNumber(port);
52 ConnectionInfo connectionInfo =
53 new ConnectionInfoBuilder().setRemoteIp(ipAddress).setRemotePort(portNumber).build();
55 return connectionInfo;
58 public static FluentFuture<? extends @NonNull CommitInfo> createNode(
59 ConnectionInfo connectionInfo, String tepIp, String tzName, DataBroker dataBroker)
61 final InstanceIdentifier<Node> iid = SouthboundUtils.createInstanceIdentifier(connectionInfo);
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);
69 // build OvsdbNodeAugmentation for Node
70 OvsdbNodeAugmentationBuilder ovsdbNodeAugBuilder = new OvsdbNodeAugmentationBuilder();
71 ovsdbNodeAugBuilder.setConnectionInfo(connectionInfo);
73 // create map of key-val pairs
74 Map<String, String> externalIds = new HashMap<>();
75 Map<String, String> otherConfigs = new HashMap<>();
77 if (tepIp != null && !tepIp.isEmpty()) {
78 otherConfigs.put(ItmTestConstants.OTHER_CFG_TEP_IP_KEY, tepIp);
82 externalIds.put(ItmTestConstants.EXTERNAL_ID_TZNAME_KEY, tzName);
85 // get map-keys into set.
86 Set<String> externalIdKeys = externalIds.keySet();
87 Set<String> otherConfigKeys = otherConfigs.keySet();
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());
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());
109 // set ExternalIds list into Node
110 ovsdbNodeAugBuilder.setOpenvswitchExternalIds(externalIdsList);
112 // set OtherConfig list into Node
113 ovsdbNodeAugBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
115 // add OvsdbNodeAugmentation into Node
116 nodeBuilder.addAugmentation(ovsdbNodeAugBuilder.build());
117 Node ovsdbNode = nodeBuilder.build();
119 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
120 transaction.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, iid, ovsdbNode);
121 return transaction.commit();
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);
129 Node oldOvsdbNode = dataBroker.newReadOnlyTransaction()
130 .read(LogicalDatastoreType.OPERATIONAL, iid).get().get();
132 // build Node using its builder class
133 NodeBuilder nodeBuilder = new NodeBuilder();
134 nodeBuilder.setNodeId(oldOvsdbNode.getNodeId());
136 // build OvsdbNodeAugmentation for Node
137 OvsdbNodeAugmentationBuilder ovsdbNodeAugBuilder = new OvsdbNodeAugmentationBuilder();
138 ovsdbNodeAugBuilder.setConnectionInfo(connectionInfo);
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);
147 if (tzName != null) {
148 externalIds.put(ItmTestConstants.EXTERNAL_ID_TZNAME_KEY, tzName);
151 if (brName != null && !brName.isEmpty()) {
152 externalIds.put(ItmTestConstants.EXTERNAL_ID_BR_NAME_KEY, brName);
155 // get map-keys into set.
156 Set<String> externalIdKeys = externalIds.keySet();
157 Set<String> otherConfigKeys = otherConfigs.keySet();
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());
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());
179 // set ExternalIds list into Node
180 ovsdbNodeAugBuilder.setOpenvswitchExternalIds(externalIdsList);
182 // set OtherConfigs list into Node
183 ovsdbNodeAugBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
185 // add OvsdbNodeAugmentation into Node
186 nodeBuilder.addAugmentation(ovsdbNodeAugBuilder.build());
187 Node ovsdbNode = nodeBuilder.build();
189 //ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
190 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
191 transaction.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, iid, ovsdbNode);
192 return transaction.commit();
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);
201 NodeBuilder bridgeNodeBuilder = new NodeBuilder();
203 InstanceIdentifier<Node> bridgeIid = SouthboundUtils.createInstanceIdentifier(nodeKey, bridgeName);
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())));
213 bridgeNodeBuilder.addAugmentation(ovsdbBridgeAugmentationBuilder.build());
215 Node bridgeNode = bridgeNodeBuilder.build();
217 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
218 tx.mergeParentStructureMerge(LogicalDatastoreType.OPERATIONAL, bridgeIid,