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.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;
44 public final class OvsdbTestUtil {
45 private OvsdbTestUtil() {
50 public static ConnectionInfo getConnectionInfo(int port, String strIpAddress) {
51 IpAddress ipAddress = IpAddressBuilder.getDefaultInstance(strIpAddress);
52 PortNumber portNumber = new PortNumber(port);
54 ConnectionInfo connectionInfo =
55 new ConnectionInfoBuilder().setRemoteIp(ipAddress).setRemotePort(portNumber).build();
57 return connectionInfo;
60 public static FluentFuture<? extends @NonNull CommitInfo> createNode(
61 ConnectionInfo connectionInfo, String tepIp, String tzName, DataBroker dataBroker)
63 final InstanceIdentifier<Node> iid = SouthboundUtils.createInstanceIdentifier(connectionInfo);
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);
71 // build OvsdbNodeAugmentation for Node
72 OvsdbNodeAugmentationBuilder ovsdbNodeAugBuilder = new OvsdbNodeAugmentationBuilder();
73 ovsdbNodeAugBuilder.setConnectionInfo(connectionInfo);
75 // create map of key-val pairs
76 Map<String, String> externalIds = new HashMap<>();
77 Map<String, String> otherConfigs = new HashMap<>();
79 if (tepIp != null && !tepIp.isEmpty()) {
80 otherConfigs.put(ItmTestConstants.OTHER_CFG_TEP_IP_KEY, tepIp);
84 externalIds.put(ItmTestConstants.EXTERNAL_ID_TZNAME_KEY, tzName);
87 // get map-keys into set.
88 Set<String> externalIdKeys = externalIds.keySet();
89 Set<String> otherConfigKeys = otherConfigs.keySet();
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());
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());
111 // set ExternalIds list into Node
112 ovsdbNodeAugBuilder.setOpenvswitchExternalIds(externalIdsList);
114 // set OtherConfig list into Node
115 ovsdbNodeAugBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
117 // add OvsdbNodeAugmentation into Node
118 nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, ovsdbNodeAugBuilder.build());
119 Node ovsdbNode = nodeBuilder.build();
121 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
122 transaction.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, iid, ovsdbNode);
123 return transaction.commit();
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);
131 Node oldOvsdbNode = dataBroker.newReadOnlyTransaction()
132 .read(LogicalDatastoreType.OPERATIONAL, iid).get().get();
134 // build Node using its builder class
135 NodeBuilder nodeBuilder = new NodeBuilder();
136 nodeBuilder.setNodeId(oldOvsdbNode.getNodeId());
138 // build OvsdbNodeAugmentation for Node
139 OvsdbNodeAugmentationBuilder ovsdbNodeAugBuilder = new OvsdbNodeAugmentationBuilder();
140 ovsdbNodeAugBuilder.setConnectionInfo(connectionInfo);
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);
149 if (tzName != null) {
150 externalIds.put(ItmTestConstants.EXTERNAL_ID_TZNAME_KEY, tzName);
153 if (brName != null && !brName.isEmpty()) {
154 externalIds.put(ItmTestConstants.EXTERNAL_ID_BR_NAME_KEY, brName);
157 // get map-keys into set.
158 Set<String> externalIdKeys = externalIds.keySet();
159 Set<String> otherConfigKeys = otherConfigs.keySet();
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());
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());
181 // set ExternalIds list into Node
182 ovsdbNodeAugBuilder.setOpenvswitchExternalIds(externalIdsList);
184 // set OtherConfigs list into Node
185 ovsdbNodeAugBuilder.setOpenvswitchOtherConfigs(otherConfigsList);
187 // add OvsdbNodeAugmentation into Node
188 nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, ovsdbNodeAugBuilder.build());
189 Node ovsdbNode = nodeBuilder.build();
191 //ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
192 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
193 transaction.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, iid, ovsdbNode);
194 return transaction.commit();
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);
203 NodeBuilder bridgeNodeBuilder = new NodeBuilder();
205 InstanceIdentifier<Node> bridgeIid = SouthboundUtils.createInstanceIdentifier(nodeKey, bridgeName);
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())));
215 bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
217 Node bridgeNode = bridgeNodeBuilder.build();
219 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
220 tx.mergeParentStructureMerge(LogicalDatastoreType.OPERATIONAL, bridgeIid,