Skip to content

Commit 88df984

Browse files
committed
Network-refactor: add a method for plugins to get the source nat ip
Signed-off-by: Chiradeep Vittal <chiradeep@apache.org>
1 parent a34ce77 commit 88df984

4 files changed

Lines changed: 120 additions & 0 deletions

File tree

api/src/com/cloud/network/NetworkModel.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,5 +241,7 @@ Map<PublicIpAddress, Set<Service>> getIpToServices(List<? extends PublicIpAddres
241241
Set<Long> getAvailableIps(Network network, String requestedIp);
242242

243243
String getDomainNetworkDomain(long domainId, long zoneId);
244+
245+
PublicIpAddress getSourceNatIpAddressForGuestNetwork(Account owner, Network guestNetwork);
244246

245247
}

server/src/com/cloud/network/NetworkModelImpl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,4 +1833,25 @@ public boolean stop() {
18331833
public String getName() {
18341834
return _name;
18351835
}
1836+
1837+
@Override
1838+
public PublicIpAddress getSourceNatIpAddressForGuestNetwork(Account owner, Network guestNetwork) {
1839+
List<? extends IpAddress> addrs = listPublicIpsAssignedToGuestNtwk(owner.getId(), guestNetwork.getId(), true);
1840+
1841+
IPAddressVO sourceNatIp = null;
1842+
if (addrs.isEmpty()) {
1843+
return null;
1844+
} else {
1845+
for (IpAddress addr : addrs) {
1846+
if (addr.isSourceNat()) {
1847+
sourceNatIp = _ipAddressDao.findById(addr.getId());
1848+
return new PublicIp(sourceNatIp, _vlanDao.findById(sourceNatIp.getVlanId()),
1849+
NetUtils.createSequenceBasedMacAddress(sourceNatIp.getMacAddress()));
1850+
}
1851+
}
1852+
1853+
}
1854+
1855+
return null;
1856+
}
18361857
}

server/test/com/cloud/network/MockNetworkModelImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,4 +795,13 @@ public Map<Provider, ArrayList<PublicIpAddress>> getProviderToIpList(Network net
795795
return null;
796796
}
797797

798+
/* (non-Javadoc)
799+
* @see com.cloud.network.NetworkModel#getSourceNatIpAddressForGuestNetwork(com.cloud.user.Account, com.cloud.network.Network)
800+
*/
801+
@Override
802+
public PublicIpAddress getSourceNatIpAddressForGuestNetwork(Account owner, Network guestNetwork) {
803+
// TODO Auto-generated method stub
804+
return null;
805+
}
806+
798807
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package com.cloud.network;
19+
20+
import static org.mockito.Matchers.any;
21+
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.when;
23+
import static org.mockito.Matchers.*;
24+
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
28+
import junit.framework.Assert;
29+
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
33+
import com.cloud.dc.VlanVO;
34+
import com.cloud.dc.dao.VlanDao;
35+
import com.cloud.network.dao.IPAddressDao;
36+
import com.cloud.user.Account;
37+
import com.cloud.utils.db.Filter;
38+
import com.cloud.utils.db.SearchBuilder;
39+
import com.cloud.utils.db.SearchCriteria;
40+
import com.cloud.utils.net.Ip;
41+
42+
public class NetworkModelTest {
43+
@Before
44+
public void setUp() {
45+
46+
}
47+
48+
@Test
49+
public void testGetSourceNatIpAddressForGuestNetwork() {
50+
NetworkModelImpl modelImpl = new NetworkModelImpl();
51+
IPAddressDao ipAddressDao = mock(IPAddressDao.class);
52+
modelImpl._ipAddressDao = ipAddressDao;
53+
List<IPAddressVO> fakeList = new ArrayList<IPAddressVO>();
54+
IPAddressVO fakeIp = new IPAddressVO(new Ip("75.75.75.75"), 1, 0xaabbccddeeffL, 10, false);
55+
fakeList.add(fakeIp);
56+
SearchBuilder<IPAddressVO> fakeSearch = mock(SearchBuilder.class);
57+
modelImpl.IpAddressSearch = fakeSearch;
58+
VlanDao fakeVlanDao = mock(VlanDao.class);
59+
when (fakeVlanDao.findById(anyLong())).thenReturn(mock(VlanVO.class));
60+
modelImpl._vlanDao = fakeVlanDao;
61+
when(fakeSearch.create()).thenReturn(mock(SearchCriteria.class));
62+
when(
63+
ipAddressDao.search(
64+
any(SearchCriteria.class),
65+
(Filter)org.mockito.Matchers.isNull()
66+
)
67+
).thenReturn(fakeList);
68+
when (
69+
ipAddressDao.findById(anyLong())
70+
).thenReturn(fakeIp);
71+
Account fakeAccount = mock(Account.class);
72+
when(fakeAccount.getId()).thenReturn(1L);
73+
Network fakeNetwork = mock(Network.class);
74+
when(fakeNetwork.getId()).thenReturn(1L);
75+
PublicIpAddress answer = modelImpl.getSourceNatIpAddressForGuestNetwork(fakeAccount, fakeNetwork);
76+
Assert.assertNull(answer);
77+
IPAddressVO fakeIp2 = new IPAddressVO(new Ip("76.75.75.75"), 1, 0xaabb10ddeeffL, 10, true);
78+
fakeList.add(fakeIp2);
79+
when (
80+
ipAddressDao.findById(anyLong())
81+
).thenReturn(fakeIp2);
82+
answer = modelImpl.getSourceNatIpAddressForGuestNetwork(fakeAccount, fakeNetwork);
83+
Assert.assertNotNull(answer);
84+
Assert.assertEquals(answer.getAddress().addr(), "76.75.75.75");
85+
86+
}
87+
88+
}

0 commit comments

Comments
 (0)