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;
41 import org.opendaylight.yangtools.yang.common.Uint16;
43 public final class OvsdbTestUtil {
44 private OvsdbTestUtil() {
49 public static ConnectionInfo getConnectionInfo(int port, String strIpAddress) {
50 IpAddress ipAddress = IpAddressBuilder.getDefaultInstance(strIpAddress);
51 PortNumber portNumber = new PortNumber(Uint16.valueOf(port));
53 ConnectionInfo connectionInfo =
54 new ConnectionInfoBuilder().setRemoteIp(ipAddress).setRemotePort(portNumber).build();
56 return connectionInfo;
59 public static FluentFuture<? extends @NonNull CommitInfo> createNode(
60 ConnectionInfo connectionInfo, String tepIp, String tzName, DataBroker dataBroker)
62 final InstanceIdentifier<Node> iid = SouthboundUtils.createInstanceIdentifier(connectionInfo);
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);
70 // build OvsdbNodeAugmentation for Node
71 OvsdbNodeAugmentationBuilder ovsdbNodeAugBuilder = new OvsdbNodeAugmentationBuilder();
72 ovsdbNodeAugBuilder.setConnectionInfo(connectionInfo);
74 // create map of key-val pairs
75 Map<String, String> externalIds = new HashMap<>();
76 Map<String, String> otherConfigs = new HashMap<>();
78 if (tepIp != null && !tepIp.isEmpty()) {
79 otherConfigs.put(ItmTestConstants.OTHER_CFG_TEP_IP_KEY, tepIp);
83 externalIds.put(ItmTestConstants.EXTERNAL_ID_TZNAME_KEY, tzName);
86 // get map-keys into set.
87 Set<String> externalIdKeys = externalIds.keySet();
88 Set<String> otherConfigKeys = otherConfigs.keySet();
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());
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());
110 // set ExternalIds list into Node
111 ovsdbNodeAugBuilder.setOpenvswitchExternalIds(externalIdsList);
113 // set OtherConfig list into Node
114 ovsdbNodeAugBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
116 // add OvsdbNodeAugmentation into Node
117 nodeBuilder.addAugmentation(ovsdbNodeAugBuilder.build());
118 Node ovsdbNode = nodeBuilder.build();
120 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
121 transaction.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, iid, ovsdbNode);
122 return transaction.commit();
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);
130 Node oldOvsdbNode = dataBroker.newReadOnlyTransaction()
131 .read(LogicalDatastoreType.OPERATIONAL, iid).get().get();
133 // build Node using its builder class
134 NodeBuilder nodeBuilder = new NodeBuilder();
135 nodeBuilder.setNodeId(oldOvsdbNode.getNodeId());
137 // build OvsdbNodeAugmentation for Node
138 OvsdbNodeAugmentationBuilder ovsdbNodeAugBuilder = new OvsdbNodeAugmentationBuilder();
139 ovsdbNodeAugBuilder.setConnectionInfo(connectionInfo);
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);
148 if (tzName != null) {
149 externalIds.put(ItmTestConstants.EXTERNAL_ID_TZNAME_KEY, tzName);
152 if (brName != null && !brName.isEmpty()) {
153 externalIds.put(ItmTestConstants.EXTERNAL_ID_BR_NAME_KEY, brName);
156 // get map-keys into set.
157 Set<String> externalIdKeys = externalIds.keySet();
158 Set<String> otherConfigKeys = otherConfigs.keySet();
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());
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());
180 // set ExternalIds list into Node
181 ovsdbNodeAugBuilder.setOpenvswitchExternalIds(externalIdsList);
183 // set OtherConfigs list into Node
184 ovsdbNodeAugBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
186 // add OvsdbNodeAugmentation into Node
187 nodeBuilder.addAugmentation(ovsdbNodeAugBuilder.build());
188 Node ovsdbNode = nodeBuilder.build();
190 //ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
191 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
192 transaction.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, iid, ovsdbNode);
193 return transaction.commit();
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);
202 NodeBuilder bridgeNodeBuilder = new NodeBuilder();
204 InstanceIdentifier<Node> bridgeIid = SouthboundUtils.createInstanceIdentifier(nodeKey, bridgeName);
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())));
214 bridgeNodeBuilder.addAugmentation(ovsdbBridgeAugmentationBuilder.build());
216 Node bridgeNode = bridgeNodeBuilder.build();
218 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
219 tx.mergeParentStructureMerge(LogicalDatastoreType.OPERATIONAL, bridgeIid,