2 * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.nemo.renderer.openflow.phtysicalnetwork;
\r
10 import junit.framework.TestCase;
\r
11 import org.junit.Assert;
\r
12 import org.junit.Before;
\r
13 import org.junit.Test;
\r
15 import static org.junit.Assert.*;
\r
16 import com.fasterxml.jackson.databind.JsonNode;
\r
17 import com.fasterxml.jackson.databind.ObjectMapper;
\r
18 import com.google.common.collect.HashBasedTable;
\r
19 import com.google.common.collect.Table;
\r
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
\r
21 import org.opendaylight.nemo.renderer.openflow.physicalnetwork.PhyConfigLoader;
\r
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
\r
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
\r
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalNodeInstance;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalPortInstance;
\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.attribute.instance.AttributeValueBuilder;
\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.host.instance.IpAddressesBuilder;
\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHost;
\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostBuilder;
\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostKey;
\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLink;
\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLinkBuilder;
\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNode;
\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeBuilder;
\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeKey;
\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPort;
\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPortBuilder;
\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.Attribute;
\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeBuilder;
\r
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeKey;
\r
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.*;
\r
43 import org.slf4j.Logger;
\r
44 import org.slf4j.LoggerFactory;
\r
46 import javax.xml.crypto.Data;
\r
47 import java.io.IOException;
\r
48 import java.lang.reflect.Member;
\r
49 import java.lang.reflect.Method;
\r
50 import java.util.ArrayList;
\r
51 import java.util.HashMap;
\r
52 import java.util.List;
\r
53 import static org.mockito.Mockito.*;
\r
55 * Created by zhangmeng on 2015/11/30.
\r
57 public class PhyConfigLoaderTest extends TestCase {
\r
58 private PhyConfigLoader phyConfigLoader;
\r
59 private DataBroker dataBroker;
\r
61 public void setUp() throws Exception {
\r
62 dataBroker = mock(DataBroker.class);
\r
63 phyConfigLoader = new PhyConfigLoader(dataBroker);
\r
67 public void testGetExternalNetworkMac() throws Exception {
\r
68 Table<PhysicalNodeId, PhysicalPortId, MacAddress> table;
\r
69 table = phyConfigLoader.getExternalNetworkMac();
\r
70 Assert.assertTrue(table != null);
\r
74 public void testGetPhysicalNode() throws Exception {
\r
75 PhysicalNodeId physicalNodeId = mock(PhysicalNodeId.class);
\r
76 PhysicalNode physicalNode ;
\r
78 Class<PhyConfigLoader> class1 = PhyConfigLoader.class;
\r
79 Method method = class1.getDeclaredMethod("getPhysicalNode",new Class[]{PhysicalNodeId.class});
\r
80 method.setAccessible(true);
\r
82 physicalNode = (PhysicalNode) method.invoke(phyConfigLoader,physicalNodeId);
\r
84 Assert.assertTrue( physicalNode == null);
\r
88 public void testGetPhysicalPort() throws Exception {
\r
89 PhysicalPortId physicalPortId = mock(PhysicalPortId.class);
\r
90 PhysicalPort physicalPort;
\r
92 Class<PhyConfigLoader> class1 = PhyConfigLoader.class;
\r
93 Method method = class1.getDeclaredMethod("getPhysicalPort",new Class[]{PhysicalPortId.class});
\r
94 method.setAccessible(true);
\r
96 physicalPort = (PhysicalPort) method.invoke(phyConfigLoader,physicalPortId);
\r
98 Assert.assertTrue(physicalPort == null);
\r
102 public void testGetPhysicalLink() throws Exception {
\r
103 PhysicalLinkId physicalLinkId = mock(PhysicalLinkId.class);
\r
104 PhysicalLink physicalLink;
\r
106 Class<PhyConfigLoader> class1 = PhyConfigLoader.class;
\r
107 Method method = class1.getDeclaredMethod("getPhysicalLink",new Class[]{PhysicalLinkId.class});
\r
108 method.setAccessible(true);
\r
110 physicalLink = (PhysicalLink) method.invoke(phyConfigLoader,physicalLinkId);
\r
112 Assert.assertTrue(physicalLink == null);
\r
116 public void testClose() throws Exception {
\r
117 phyConfigLoader.close();
\r
121 public void testBuildExternals() throws Exception{
\r
122 JsonNode externalRoot = mock(JsonNode.class);
\r
123 JsonNode exNetworkNodes = mock(JsonNode.class);
\r
124 JsonNode exNetworkNode = mock(JsonNode.class);
\r
125 JsonNode jsonNode = mock(JsonNode.class);
\r
126 String nodeId = new String("1");
\r
127 String portId = new String("2");
\r
128 String peerMac = new String("00:11:22:33:44:55");
\r
130 Class<PhyConfigLoader> class1 = PhyConfigLoader.class;
\r
131 Method method = class1.getDeclaredMethod("buildExternals",new Class[]{JsonNode.class});
\r
132 method.setAccessible(true);
\r
134 when(externalRoot.path(any(String.class))).thenReturn(exNetworkNodes);
\r
135 when(exNetworkNodes.size()).thenReturn(1);
\r
136 when(exNetworkNodes.get(any(Integer.class))).thenReturn(exNetworkNode);
\r
138 //get into method "buildExNetwork"
\r
139 when(exNetworkNode.get(any(String.class))).thenReturn(jsonNode);
\r
140 when(jsonNode.asText())
\r
141 .thenReturn(nodeId)
\r
142 .thenReturn(portId)
\r
143 .thenReturn(peerMac);
\r
145 method.invoke(phyConfigLoader, externalRoot);
\r
147 verify(externalRoot).path(any(String.class));
\r
148 verify(exNetworkNodes,times(2)).size();
\r
149 verify(exNetworkNodes).get(any(Integer.class));
\r
150 verify(exNetworkNode,times(3)).get(any(String.class));
\r
151 verify(jsonNode,times(3)).asText();
\r
156 public PhysicalLink testBuildLink(JsonNode arg)throws Exception{
\r
157 if(arg == null)return null;
\r
159 PhysicalLink physicalLink = mock(PhysicalLink.class);
\r
160 JsonNode jsonNode = mock(JsonNode.class);
\r
161 JsonNode jsonNode1 = mock(JsonNode.class);
\r
164 Class<PhyConfigLoader> class1 = PhyConfigLoader.class;
\r
165 Method method = class1.getDeclaredMethod("buildLink",new Class[]{JsonNode.class});
\r
166 method.setAccessible(true);
\r
168 when(jsonNode.get(any(String.class)))
\r
169 .thenReturn(jsonNode1)
\r
170 .thenReturn(jsonNode1);
\r
171 when(jsonNode1.asText()).thenReturn(new String("test"));
\r
172 when(jsonNode1.asLong()).thenReturn((long) 1);
\r
174 physicalLink = (PhysicalLink)method.invoke(phyConfigLoader,jsonNode);
\r
175 Assert.assertTrue(physicalLink != null);
\r
176 Assert.assertTrue(physicalLink != mock(PhysicalLink.class));
\r
178 return physicalLink;
\r
182 public void testBuildLinks()throws Exception{
\r
183 List<PhysicalLink> list;
\r
184 JsonNode hostsNode = mock(JsonNode.class);
\r
185 JsonNode hosts = mock(JsonNode.class);
\r
186 JsonNode jsonNode = mock(JsonNode.class);
\r
187 JsonNode jsonNode1 = mock(JsonNode.class);
\r
188 PhysicalHost physicalHost;
\r
190 Class<PhyConfigLoader> class1 = PhyConfigLoader.class;
\r
191 Method method = class1.getDeclaredMethod("buildLinks",new Class[]{JsonNode.class});
\r
192 method.setAccessible(true);
\r
194 when(hostsNode.path(any(String.class))).thenReturn(hosts);
\r
195 when(hosts.size()).thenReturn(1);
\r
196 when(hosts.get(any(Integer.class))).thenReturn(jsonNode);
\r
197 when(jsonNode.get(any(String.class)))
\r
198 .thenReturn(jsonNode1)
\r
199 .thenReturn(jsonNode1);
\r
200 when(jsonNode1.asText()).thenReturn(new String("test"));
\r
201 when(jsonNode1.asLong()).thenReturn((long) 1);
\r
203 list = (List<PhysicalLink>)method.invoke(phyConfigLoader,hostsNode);
\r
204 Assert.assertTrue(list.size() == 1);
\r
208 public void testBuildHosts()throws Exception{
\r
209 Class<PhyConfigLoader> class1 = PhyConfigLoader.class;
\r
210 Method method = class1.getDeclaredMethod("buildHosts", new Class[]{JsonNode.class});
\r
211 method.setAccessible(true);
\r
213 JsonNode hostsNode = mock(JsonNode.class);
\r
214 JsonNode hosts = mock(JsonNode.class);
\r
215 JsonNode host = mock(JsonNode.class);
\r
216 JsonNode host_temp = mock(JsonNode.class);
\r
217 JsonNode host_temp1 = mock(JsonNode.class);
\r
218 JsonNode ipaddrs = mock(JsonNode.class);
\r
219 JsonNode ipaddr = mock(JsonNode.class);
\r
220 List<PhysicalHost> list;
\r
222 when(hostsNode.path(any(String.class))).thenReturn(hosts);
\r
223 when(hosts.size()).thenReturn(1);
\r
224 when(hosts.get(any(Integer.class))).thenReturn(host);
\r
225 //get into method "buildhost"
\r
226 when(host.get(any(String.class))).thenReturn(host_temp);
\r
227 when(host_temp.asText())
\r
228 .thenReturn(new String("00001111-0000-0000-0000-000011112222")) //HOST_ID
\r
229 .thenReturn(new String("hostName")) //HOST_NAME
\r
230 .thenReturn(new String("00:11:22:33:44:55")) //MAC_ADDRESS
\r
231 .thenReturn(new String("nodeId")) //NODE_ID
\r
232 .thenReturn(new String("connetionId"));//PhysicalPortId
\r
233 when(host.path(any(String.class))).thenReturn(ipaddrs);
\r
234 when(ipaddrs.size()).thenReturn(1);
\r
235 when(ipaddrs.get(any(Integer.class))).thenReturn(ipaddr);
\r
236 when(ipaddr.get(any(String.class))).thenReturn(host_temp1);
\r
237 when(host_temp1.asText()).thenReturn(new String("192.168.1.1"));//ipv4_address
\r
240 list = (List<PhysicalHost>) method.invoke(phyConfigLoader,hostsNode);
\r
241 Assert.assertTrue(list.size() == 1);
\r
244 public void testBuildPortAttributes()throws Exception{
\r
245 Class<PhyConfigLoader>class1 = PhyConfigLoader.class;
\r
246 Method method = class1.getDeclaredMethod("buildPortAttributes",new Class[]{JsonNode.class});
\r
247 method.setAccessible(true);
\r
249 List<Attribute> attributeList;
\r
250 JsonNode attributes = mock(JsonNode.class);
\r
251 JsonNode portAttribute = mock(JsonNode.class);
\r
252 JsonNode jsonNode_temp = mock(JsonNode.class);
\r
254 when(attributes.size()).thenReturn(1);
\r
255 when(attributes.get(any(Integer.class))).thenReturn(portAttribute);
\r
256 //get into method "buildPortAttribute"
\r
257 when(portAttribute.path(any(String.class))).thenReturn(jsonNode_temp);
\r
258 when(jsonNode_temp.asText())
\r
259 .thenReturn(new String(""))//branch null
\r
260 .thenReturn(new String("zm"));
\r
261 attributeList = (List<Attribute>)method.invoke(phyConfigLoader,attributes);
\r
262 Assert.assertTrue(attributeList.size() == 0);
\r
263 //new AttributeName("zm");
\r
264 attributeList = (List<Attribute>)method.invoke(phyConfigLoader,attributes);
\r
265 Assert.assertTrue(attributeList.size() == 1);
\r
266 verify(portAttribute,times(3)).path(any(String.class));
\r
267 verify(jsonNode_temp,times(3)).asText();
\r
271 public void testBuildNodes() throws Exception{
\r
272 Class<PhyConfigLoader>class1 = PhyConfigLoader.class;
\r
273 Method method = class1.getDeclaredMethod("buildNodes", new Class[]{JsonNode.class});
\r
274 method.setAccessible(true);
\r
276 JsonNode nodesRoot = mock(JsonNode.class);
\r
277 JsonNode nodes = mock(JsonNode.class);
\r
278 JsonNode node = mock(JsonNode.class);
\r
279 JsonNode node_temp = mock(JsonNode.class);
\r
280 JsonNode port_temp = mock(JsonNode.class);
\r
281 JsonNode attribute_temp = mock(JsonNode.class);
\r
282 JsonNode attribute_temp_son = mock(JsonNode.class);
\r
283 JsonNode ports = mock(JsonNode.class);
\r
284 JsonNode attributes = mock(JsonNode.class);
\r
285 JsonNode portAttributes = mock(JsonNode.class);
\r
286 JsonNode portAttribute = mock(JsonNode.class);
\r
287 JsonNode attributes_son = mock(JsonNode.class);
\r
288 JsonNode port = mock(JsonNode.class);
\r
289 List<PhysicalNode> result;
\r
291 when(nodesRoot.path(any(String.class))).thenReturn(nodes);
\r
292 when(nodes.size()).thenReturn(1);
\r
293 when(nodes.get(any(Integer.class))).thenReturn(node);
\r
294 //get into method "buildnode"
\r
295 when(node.get(any(String.class))).thenReturn(node_temp);
\r
296 when(node_temp.asText())
\r
297 .thenReturn(new String(""))//branch null
\r
298 .thenReturn(new String("test")) //node_id
\r
299 .thenReturn(new String("switch")); //NODE_TYPE
\r
302 result = (List<PhysicalNode>) method.invoke(phyConfigLoader,nodesRoot); //return empty list
\r
303 Assert.assertTrue(result.size() == 0);
\r
304 verify(node_temp).asText();
\r
306 when(node.path(any(String.class)))
\r
307 .thenReturn(ports)//PORTS
\r
308 .thenReturn(attributes);//ATTRIBUTES
\r
309 ////get into method "buildports"
\r
310 when(ports.size()).thenReturn(1);
\r
311 when(ports.get(any(Integer.class))).thenReturn(port);
\r
312 ///////get into method "buildport"
\r
313 when(port.get(any(String.class))).thenReturn(port_temp);
\r
314 when(port_temp.asText())
\r
315 .thenReturn(new String("test"))//PORT_ID
\r
316 .thenReturn(new String("switch"));//PORT_TYPE
\r
317 when(port.path(any(String.class))).thenReturn(portAttributes);
\r
318 ////////get into method "buildportattributes"
\r
319 when(portAttributes.size()).thenReturn(1);
\r
320 when(portAttributes.get(any(Integer.class))).thenReturn(attributes_son);
\r
321 //////////get into method "buildPortAttribute"
\r
322 when(attributes_son.path(any(String.class))).thenReturn(attribute_temp_son);
\r
323 when(attribute_temp_son.asText())
\r
324 .thenReturn(new String("zm"))
\r
325 .thenReturn(new String("zm"));
\r
326 ////return to method "buildnode" and get into method "..............buildNodeAttributes"
\r
327 when(attributes.size()).thenReturn(1);
\r
328 when(attributes.get(any(Integer.class))).thenReturn(portAttribute);
\r
329 //////get into method "..............buildNodeAttribute"
\r
330 when(portAttribute.path(any(String.class))).thenReturn(attribute_temp);
\r
331 when(attribute_temp.asText())
\r
332 .thenReturn(new String("test"))//ATTRIBUTE_NAME
\r
333 .thenReturn(new String("test"));//ATTRIBUTE_VALUE
\r
335 result = (List<PhysicalNode>) method.invoke(phyConfigLoader,nodesRoot); //return empty list
\r
336 Assert.assertTrue(result.size() == 1);
\r