incubator-cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chirad...@apache.org
Subject [22/39] git commit: Network-refactor: add a method for plugins to get the source nat ip
Date Fri, 18 Jan 2013 01:01:22 GMT
Network-refactor: add a method for plugins to get the source nat ip

Signed-off-by: Chiradeep Vittal <chiradeep@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/88df984f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/88df984f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/88df984f

Branch: refs/heads/master
Commit: 88df984ff3edddaf502692b4e37bba2cc45365fa
Parents: a34ce77
Author: Chiradeep Vittal <chiradeep@apache.org>
Authored: Tue Jan 8 18:39:25 2013 -0800
Committer: Chiradeep Vittal <chiradeep@apache.org>
Committed: Wed Jan 9 15:49:02 2013 -0800

----------------------------------------------------------------------
 api/src/com/cloud/network/NetworkModel.java        |    2 +
 server/src/com/cloud/network/NetworkModelImpl.java |   21 ++++
 .../com/cloud/network/MockNetworkModelImpl.java    |    9 ++
 .../test/com/cloud/network/NetworkModelTest.java   |   88 +++++++++++++++
 4 files changed, 120 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/88df984f/api/src/com/cloud/network/NetworkModel.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/network/NetworkModel.java b/api/src/com/cloud/network/NetworkModel.java
index f53cfbd..bd3acbe 100644
--- a/api/src/com/cloud/network/NetworkModel.java
+++ b/api/src/com/cloud/network/NetworkModel.java
@@ -241,5 +241,7 @@ public interface NetworkModel {
     Set<Long> getAvailableIps(Network network, String requestedIp);
 
     String getDomainNetworkDomain(long domainId, long zoneId);
+    
+    PublicIpAddress getSourceNatIpAddressForGuestNetwork(Account owner, Network guestNetwork);
 
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/88df984f/server/src/com/cloud/network/NetworkModelImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/NetworkModelImpl.java b/server/src/com/cloud/network/NetworkModelImpl.java
index 8dee11e..dcd3c25 100644
--- a/server/src/com/cloud/network/NetworkModelImpl.java
+++ b/server/src/com/cloud/network/NetworkModelImpl.java
@@ -1833,4 +1833,25 @@ public class NetworkModelImpl  implements NetworkModel, Manager{
     public String getName() {
         return _name;
     }
+
+    @Override
+    public PublicIpAddress getSourceNatIpAddressForGuestNetwork(Account owner, Network guestNetwork)
{
+        List<? extends IpAddress> addrs = listPublicIpsAssignedToGuestNtwk(owner.getId(),
guestNetwork.getId(), true);
+        
+        IPAddressVO sourceNatIp = null;
+        if (addrs.isEmpty()) {
+            return null;
+        } else {
+            for (IpAddress addr : addrs) {
+                if (addr.isSourceNat()) {
+                    sourceNatIp = _ipAddressDao.findById(addr.getId());
+                    return new PublicIp(sourceNatIp, _vlanDao.findById(sourceNatIp.getVlanId()),

+                            NetUtils.createSequenceBasedMacAddress(sourceNatIp.getMacAddress()));
+                }
+            }
+    
+        } 
+        
+        return null;
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/88df984f/server/test/com/cloud/network/MockNetworkModelImpl.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/network/MockNetworkModelImpl.java b/server/test/com/cloud/network/MockNetworkModelImpl.java
index 76e7171..e1113a4 100644
--- a/server/test/com/cloud/network/MockNetworkModelImpl.java
+++ b/server/test/com/cloud/network/MockNetworkModelImpl.java
@@ -795,4 +795,13 @@ public class MockNetworkModelImpl implements NetworkModel, Manager {
         return null;
     }
 
+    /* (non-Javadoc)
+     * @see com.cloud.network.NetworkModel#getSourceNatIpAddressForGuestNetwork(com.cloud.user.Account,
com.cloud.network.Network)
+     */
+    @Override
+    public PublicIpAddress getSourceNatIpAddressForGuestNetwork(Account owner, Network guestNetwork)
{
+        // TODO Auto-generated method stub
+        return null;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/88df984f/server/test/com/cloud/network/NetworkModelTest.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/network/NetworkModelTest.java b/server/test/com/cloud/network/NetworkModelTest.java
new file mode 100644
index 0000000..52b3187
--- /dev/null
+++ b/server/test/com/cloud/network/NetworkModelTest.java
@@ -0,0 +1,88 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package com.cloud.network;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.mockito.Matchers.*;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.cloud.dc.VlanVO;
+import com.cloud.dc.dao.VlanDao;
+import com.cloud.network.dao.IPAddressDao;
+import com.cloud.user.Account;
+import com.cloud.utils.db.Filter;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.net.Ip;
+
+public class NetworkModelTest {
+    @Before
+    public void setUp() {
+        
+    }
+    
+    @Test
+    public void testGetSourceNatIpAddressForGuestNetwork() {
+        NetworkModelImpl modelImpl = new NetworkModelImpl();
+        IPAddressDao ipAddressDao = mock(IPAddressDao.class);
+        modelImpl._ipAddressDao = ipAddressDao;
+        List<IPAddressVO> fakeList = new ArrayList<IPAddressVO>();
+        IPAddressVO fakeIp = new IPAddressVO(new Ip("75.75.75.75"), 1, 0xaabbccddeeffL, 10,
false);
+        fakeList.add(fakeIp);
+        SearchBuilder<IPAddressVO> fakeSearch = mock(SearchBuilder.class);
+        modelImpl.IpAddressSearch = fakeSearch;
+        VlanDao fakeVlanDao = mock(VlanDao.class);
+        when (fakeVlanDao.findById(anyLong())).thenReturn(mock(VlanVO.class));
+        modelImpl._vlanDao = fakeVlanDao;
+        when(fakeSearch.create()).thenReturn(mock(SearchCriteria.class));
+        when(
+            ipAddressDao.search(
+                    any(SearchCriteria.class), 
+                    (Filter)org.mockito.Matchers.isNull()
+                    )
+            ).thenReturn(fakeList);
+        when (
+                ipAddressDao.findById(anyLong())
+             ).thenReturn(fakeIp);
+        Account fakeAccount = mock(Account.class);
+        when(fakeAccount.getId()).thenReturn(1L);
+        Network fakeNetwork = mock(Network.class);
+        when(fakeNetwork.getId()).thenReturn(1L);
+        PublicIpAddress answer = modelImpl.getSourceNatIpAddressForGuestNetwork(fakeAccount,
fakeNetwork);
+        Assert.assertNull(answer);
+        IPAddressVO fakeIp2 = new IPAddressVO(new Ip("76.75.75.75"), 1, 0xaabb10ddeeffL,
10, true);
+        fakeList.add(fakeIp2);
+        when (
+                ipAddressDao.findById(anyLong())
+             ).thenReturn(fakeIp2);
+        answer = modelImpl.getSourceNatIpAddressForGuestNetwork(fakeAccount, fakeNetwork);
+        Assert.assertNotNull(answer);
+        Assert.assertEquals(answer.getAddress().addr(), "76.75.75.75");
+
+    }
+
+}


Mime
View raw message