2 * Copyright (c) 2016 Red Hat, Inc. 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.netvirt.natservice.internal.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.when;
14 import com.google.common.util.concurrent.Futures;
15 import org.junit.Before;
16 import org.junit.Ignore;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.genius.mdsalutil.MDSALUtil;
24 import org.opendaylight.netvirt.natservice.internal.IPAddress;
25 import org.opendaylight.netvirt.natservice.internal.NaptManager;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpMap;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMappingKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMap;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMapBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMapKey;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
41 @RunWith(PowerMockRunner.class)
42 @PrepareForTest(MDSALUtil.class)
43 public class NaptManagerTest {
46 IdManagerService idMgr;
48 DataBroker dataBroker;
49 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111
50 .intext.ip.map.ip.mapping.IpMap> ipmapId = null;
51 org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext
52 .ip.map.ip.mapping.IpMap ipmap = null;
54 private NaptManager naptManager;
58 MockitoAnnotations.initMocks(this);
59 naptManager = new NaptManager(dataBroker, idMgr);
60 when(idMgr.createIdPool(any(CreateIdPoolInput.class))).thenReturn(
61 Futures.immediateFuture(RpcResultBuilder.success(new CreateIdPoolOutputBuilder().build()).build()));
63 PowerMockito.mockStatic(MDSALUtil.class);
68 // TODO Clean up the exception handling
69 @SuppressWarnings("checkstyle:IllegalCatch")
70 public void testRegisterMappingIpIP() {
71 // TODO : This needs to be modified to make it work
72 // TODO : Issue with Mockito.any() usage, so for now run registerMapping testcases as seperate Tests.
73 // This needs to be fixed properly.
74 ipmapId = InstanceIdentifier.builder(
75 IntextIpMap.class).child(IpMapping.class, new IpMappingKey(5L))
76 .child(IpMap.class, new IpMapKey("10.0.0.1")).build();
77 ipmap = new IpMapBuilder().withKey(new IpMapKey("10.0.0.1")).setInternalIp("10.0.0.1")
78 .setExternalIp("192.17.13.1").build();
80 PowerMockito.doNothing()
81 .when(MDSALUtil.class, "syncWrite", dataBroker, LogicalDatastoreType.OPERATIONAL, ipmapId, ipmap);
82 } catch (Exception e) {
83 // Test failed anyways
84 assertEquals("true", "false");
86 IPAddress internal = new IPAddress("10.0.0.1", 0);
87 IPAddress external = new IPAddress("192.17.13.1", 0);
88 naptManager.registerMapping(5, internal, external);
89 PowerMockito.verifyStatic();
95 // TODO Clean up the exception handling
96 @SuppressWarnings("checkstyle:IllegalCatch")
97 public void testRegisterMappingIpSubnet() {
98 // TODO : This needs to be modified to make it work
99 ipmapId = InstanceIdentifier.builder(IntextIpMap.class)
100 .child(IpMapping.class, new IpMappingKey(5L)).child(IpMap.class, new IpMapKey("10.0.0.1")).build();
101 ipmap = new IpMapBuilder().withKey(new IpMapKey("10.0.0.1")).setInternalIp("10.0.0.1")
102 .setExternalIp("192.17.13.1/24").build();
104 PowerMockito.doNothing()
105 .when(MDSALUtil.class, "syncWrite", dataBroker, LogicalDatastoreType.OPERATIONAL, ipmapId, ipmap);
106 } catch (Exception e) {
107 // Test failed anyways
108 assertEquals("true", "false");
110 IPAddress internal = new IPAddress("10.0.0.1", 0);
111 IPAddress external = new IPAddress("192.17.13.1", 24);
112 naptManager.registerMapping(5, internal, external);
113 PowerMockito.verifyStatic();
118 // TODO Clean up the exception handling
119 @SuppressWarnings("checkstyle:IllegalCatch")
120 public void testRegisterMappingSubnetIp() {
121 // TODO : This needs to be modified to make it work
122 ipmapId = InstanceIdentifier.builder(IntextIpMap.class)
123 .child(IpMapping.class, new IpMappingKey(6L)).child(IpMap.class, new IpMapKey("10.0.2.1/16")).build();
124 ipmap = new IpMapBuilder().withKey(new IpMapKey("10.0.0.1")).setInternalIp("10.0.0.1")
125 .setExternalIp("192.19.15.3").build();
127 PowerMockito.doNothing()
128 .when(MDSALUtil.class, "syncWrite", dataBroker, LogicalDatastoreType.OPERATIONAL, ipmapId, ipmap);
129 } catch (Exception e) {
130 // Test failed anyways
131 assertEquals("true", "false");
133 IPAddress internal = new IPAddress("10.0.2.1", 16);
134 IPAddress external = new IPAddress("192.19.15.3", 0);
135 naptManager.registerMapping(6, internal, external);
136 PowerMockito.verifyStatic();
141 // TODO Clean up the exception handling
142 @SuppressWarnings("checkstyle:IllegalCatch")
143 public void testRegisterMappingSubnetSubnet() {
144 // TODO : This needs to be modified to make it work
145 ipmapId = InstanceIdentifier.builder(IntextIpMap.class)
146 .child(IpMapping.class, new IpMappingKey(6L)).child(IpMap.class, new IpMapKey("10.2.0.2/24")).build();
147 ipmap = new IpMapBuilder().withKey(new IpMapKey("10.2.0.2/24")).setInternalIp("10.2.0.2/24")
148 .setExternalIp("192.21.16.1/16").build();
150 PowerMockito.doNothing()
151 .when(MDSALUtil.class, "syncWrite", dataBroker, LogicalDatastoreType.OPERATIONAL, ipmapId, ipmap);
152 } catch (Exception e) {
153 // Test failed anyways
154 assertEquals("true", "false");
156 IPAddress internal = new IPAddress("10.2.0.2", 24);
157 IPAddress external = new IPAddress("192.21.16.1", 16);
158 naptManager.registerMapping(6, internal, external);
159 PowerMockito.verifyStatic();
164 public void testgetExternalAddressMapping() {
165 // TODO : This needs to be modified to make it work
166 // Testcase to test when no entry exists in ip-pot-map
167 /*SessionAddress internalIpPort = new SessionAddress("10.0.0.1", 2);
168 InstanceIdentifierBuilder<IpPortMapping> idBuilder =
169 InstanceIdentifier.builder(IntextIpPortMap.class).child(IpPortMapping.class, new IpPortMappingKey(5L));
170 InstanceIdentifier<IpPortMapping> id = idBuilder.build();
172 PowerMockito.when(MDSALUtil.class, "read", dataBroker, LogicalDatastoreType.CONFIGURATION, id)
174 } catch (Exception e) {
175 // Test failed anyways
176 assertEquals("true", "false");
178 naptManager.getExternalAddressMapping(5, internalIpPort);
179 PowerMockito.verifyStatic(); */
183 public void testReleaseAddressMapping() {
184 // TODO : Below needs to be modified to make it work
185 /* InstanceIdentifierBuilder<IpMapping> idBuilder =
186 InstanceIdentifier.builder(IntextIpMap.class).child(IpMapping.class, new IpMappingKey(5L));
187 InstanceIdentifier<IpMapping> id = idBuilder.build();
189 PowerMockito.doNothing().when(MDSALUtil.class, "read", dataBroker, LogicalDatastoreType.OPERATIONAL, id);
190 } catch (Exception e) {
191 // Test failed anyways
192 assertEquals("true", "false");
194 IPAddress internal = new IPAddress("10.0.0.1",0);
195 IPAddress external = new IPAddress("192.17.13.1", 0);
196 naptManager.registerMapping(5, internal, external);
197 SessionAddress internalSession = new SessionAddress("10.0.0.1", 0);
198 naptManager.releaseAddressMapping(5L, internalSession);*/