1616// under the License.
1717package com .cloud .network .element ;
1818
19+ import java .util .ArrayList ;
1920import java .util .Collections ;
21+ import java .util .HashSet ;
22+ import java .util .List ;
23+ import java .util .Set ;
2024
2125import javax .naming .ConfigurationException ;
2226
2327import org .junit .Before ;
2428import org .junit .Test ;
29+ import org .mockito .ArgumentMatcher ;
2530
31+ import com .cloud .agent .AgentManager ;
32+ import com .cloud .agent .api .Command ;
33+ import com .cloud .agent .api .ConfigurePublicIpsOnLogicalRouterAnswer ;
34+ import com .cloud .agent .api .ConfigurePublicIpsOnLogicalRouterCommand ;
2635import com .cloud .deploy .DeployDestination ;
2736import com .cloud .domain .Domain ;
2837import com .cloud .exception .ConcurrentOperationException ;
2938import com .cloud .exception .InsufficientCapacityException ;
3039import com .cloud .exception .ResourceUnavailableException ;
40+ import com .cloud .host .HostVO ;
41+ import com .cloud .host .dao .HostDao ;
3142import com .cloud .network .Network ;
3243import com .cloud .network .Network .GuestType ;
3344import com .cloud .network .Network .Provider ;
3445import com .cloud .network .Network .Service ;
46+ import com .cloud .network .IpAddress ;
3547import com .cloud .network .NetworkManager ;
3648import com .cloud .network .NetworkModel ;
3749import com .cloud .network .Networks .BroadcastDomainType ;
3850import com .cloud .network .Networks .TrafficType ;
51+ import com .cloud .network .NiciraNvpDeviceVO ;
52+ import com .cloud .network .NiciraNvpRouterMappingVO ;
53+ import com .cloud .network .PublicIpAddress ;
3954import com .cloud .network .dao .NetworkServiceMapDao ;
55+ import com .cloud .network .dao .NiciraNvpDao ;
56+ import com .cloud .network .dao .NiciraNvpRouterMappingDao ;
57+ import com .cloud .network .nicira .NatRule ;
4058import com .cloud .offering .NetworkOffering ;
4159import com .cloud .resource .ResourceManager ;
4260import com .cloud .user .Account ;
61+ import com .cloud .utils .net .Ip ;
4362import com .cloud .vm .ReservationContext ;
4463
4564import static org .junit .Assert .*;
65+ import static org .mockito .Matchers .argThat ;
66+ import static org .mockito .Matchers .eq ;
4667import static org .mockito .Mockito .*;
4768
4869public class NiciraNvpElementTest {
49-
50- NiciraNvpElement _element = new NiciraNvpElement ();
51- NetworkManager _networkManager = mock (NetworkManager .class );
52- NetworkModel _networkModel = mock (NetworkModel .class );
53- NetworkServiceMapDao _ntwkSrvcDao = mock (NetworkServiceMapDao .class );
54-
55- @ Before
56- public void setUp () throws ConfigurationException {
57- _element ._resourceMgr = mock (ResourceManager .class );
58- _element ._networkManager = _networkManager ;
59- _element ._ntwkSrvcDao = _ntwkSrvcDao ;
60- _element ._networkModel = _networkModel ;
61-
62- // Standard responses
63- when (_networkModel .isProviderForNetwork (Provider .NiciraNvp , 42L )).thenReturn (true );
64-
65- _element .configure ("NiciraNvpTestElement" , Collections .<String , Object > emptyMap ());
66- }
67-
68- @ Test
69- public void canHandleTest () {
70- Network net = mock (Network .class );
71- when (net .getBroadcastDomainType ()).thenReturn (BroadcastDomainType .Lswitch );
72- when (net .getId ()).thenReturn (42L );
73-
74- when (_ntwkSrvcDao .canProviderSupportServiceInNetwork (42L , Service .Connectivity , Provider .NiciraNvp )).thenReturn (true );
75- // Golden path
76- assertTrue (_element .canHandle (net , Service .Connectivity ));
77-
78- when (net .getBroadcastDomainType ()).thenReturn (BroadcastDomainType .Vlan );
79- // Only broadcastdomaintype lswitch is supported
80- assertFalse (_element .canHandle (net , Service .Connectivity ));
81-
82- when (net .getBroadcastDomainType ()).thenReturn (BroadcastDomainType .Lswitch );
83- when (_ntwkSrvcDao .canProviderSupportServiceInNetwork (42L , Service .Connectivity , Provider .NiciraNvp )).thenReturn (false );
84- // No nvp provider in the network
85- assertFalse (_element .canHandle (net , Service .Connectivity ));
86-
87- when (_networkModel .isProviderForNetwork (Provider .NiciraNvp , 42L )).thenReturn (false );
88- when (_ntwkSrvcDao .canProviderSupportServiceInNetwork (42L , Service .Connectivity , Provider .NiciraNvp )).thenReturn (true );
89- // NVP provider does not provide Connectivity for this network
90- assertFalse (_element .canHandle (net , Service .Connectivity ));
91-
92- when (_networkModel .isProviderForNetwork (Provider .NiciraNvp , 42L )).thenReturn (true );
93- // Only service Connectivity is supported
94- assertFalse (_element .canHandle (net , Service .Dhcp ));
95-
96- }
97-
98- @ Test
99- public void implementTest () throws ConcurrentOperationException , ResourceUnavailableException , InsufficientCapacityException {
100- Network network = mock (Network .class );
101- when (network .getBroadcastDomainType ()).thenReturn (BroadcastDomainType .Lswitch );
102- when (network .getId ()).thenReturn (42L );
103-
104- NetworkOffering offering = mock (NetworkOffering .class );
105- when (offering .getId ()).thenReturn (42L );
106- when (offering .getTrafficType ()).thenReturn (TrafficType .Guest );
107- when (offering .getGuestType ()).thenReturn (GuestType .Isolated );
108-
109- DeployDestination dest = mock (DeployDestination .class );
110-
111- Domain dom = mock (Domain .class );
112- when (dom .getName ()).thenReturn ("domain" );
113- Account acc = mock (Account .class );
114- when (acc .getAccountName ()).thenReturn ("accountname" );
115- ReservationContext context = mock (ReservationContext .class );
116- when (context .getDomain ()).thenReturn (dom );
117- when (context .getAccount ()).thenReturn (acc );
118-
119- //assertTrue(_element.implement(network, offering, dest, context));
120- }
121-
122- }
70+
71+ NiciraNvpElement _element = new NiciraNvpElement ();
72+ NetworkManager _networkManager = mock (NetworkManager .class );
73+ NetworkModel _networkModel = mock (NetworkModel .class );
74+ NetworkServiceMapDao _ntwkSrvcDao = mock (NetworkServiceMapDao .class );
75+ AgentManager _agentManager = mock (AgentManager .class );
76+ HostDao _hostDao = mock (HostDao .class );
77+ NiciraNvpDao _niciraNvpDao = mock (NiciraNvpDao .class );
78+ NiciraNvpRouterMappingDao _niciraNvpRouterMappingDao = mock (NiciraNvpRouterMappingDao .class );
79+
80+ @ Before
81+ public void setUp () throws ConfigurationException {
82+ _element ._resourceMgr = mock (ResourceManager .class );
83+ _element ._networkManager = _networkManager ;
84+ _element ._ntwkSrvcDao = _ntwkSrvcDao ;
85+ _element ._networkModel = _networkModel ;
86+ _element ._agentMgr = _agentManager ;
87+ _element ._hostDao = _hostDao ;
88+ _element ._niciraNvpDao = _niciraNvpDao ;
89+ _element ._niciraNvpRouterMappingDao = _niciraNvpRouterMappingDao ;
90+
91+ // Standard responses
92+ when (_networkModel .isProviderForNetwork (Provider .NiciraNvp , 42L )).thenReturn (true );
93+
94+ _element .configure ("NiciraNvpTestElement" , Collections .<String , Object > emptyMap ());
95+ }
96+
97+ @ Test
98+ public void canHandleTest () {
99+ Network net = mock (Network .class );
100+ when (net .getBroadcastDomainType ()).thenReturn (BroadcastDomainType .Lswitch );
101+ when (net .getId ()).thenReturn (42L );
102+
103+ when (_ntwkSrvcDao .canProviderSupportServiceInNetwork (42L , Service .Connectivity , Provider .NiciraNvp )).thenReturn (true );
104+ // Golden path
105+ assertTrue (_element .canHandle (net , Service .Connectivity ));
106+
107+ when (net .getBroadcastDomainType ()).thenReturn (BroadcastDomainType .Vlan );
108+ // Only broadcastdomaintype lswitch is supported
109+ assertFalse (_element .canHandle (net , Service .Connectivity ));
110+
111+ when (net .getBroadcastDomainType ()).thenReturn (BroadcastDomainType .Lswitch );
112+ when (_ntwkSrvcDao .canProviderSupportServiceInNetwork (42L , Service .Connectivity , Provider .NiciraNvp )).thenReturn (false );
113+ // No nvp provider in the network
114+ assertFalse (_element .canHandle (net , Service .Connectivity ));
115+
116+ when (_networkModel .isProviderForNetwork (Provider .NiciraNvp , 42L )).thenReturn (false );
117+ when (_ntwkSrvcDao .canProviderSupportServiceInNetwork (42L , Service .Connectivity , Provider .NiciraNvp )).thenReturn (true );
118+ // NVP provider does not provide Connectivity for this network
119+ assertFalse (_element .canHandle (net , Service .Connectivity ));
120+
121+ when (_networkModel .isProviderForNetwork (Provider .NiciraNvp , 42L )).thenReturn (true );
122+ // Only service Connectivity is supported
123+ assertFalse (_element .canHandle (net , Service .Dhcp ));
124+
125+ }
126+
127+ @ Test
128+ public void implementTest () throws ConcurrentOperationException , ResourceUnavailableException , InsufficientCapacityException {
129+ Network network = mock (Network .class );
130+ when (network .getBroadcastDomainType ()).thenReturn (BroadcastDomainType .Lswitch );
131+ when (network .getId ()).thenReturn (42L );
132+
133+ NetworkOffering offering = mock (NetworkOffering .class );
134+ when (offering .getId ()).thenReturn (42L );
135+ when (offering .getTrafficType ()).thenReturn (TrafficType .Guest );
136+ when (offering .getGuestType ()).thenReturn (GuestType .Isolated );
137+
138+ DeployDestination dest = mock (DeployDestination .class );
139+
140+ Domain dom = mock (Domain .class );
141+ when (dom .getName ()).thenReturn ("domain" );
142+ Account acc = mock (Account .class );
143+ when (acc .getAccountName ()).thenReturn ("accountname" );
144+ ReservationContext context = mock (ReservationContext .class );
145+ when (context .getDomain ()).thenReturn (dom );
146+ when (context .getAccount ()).thenReturn (acc );
147+
148+ // assertTrue(_element.implement(network, offering, dest, context));
149+ }
150+
151+ @ Test
152+ public void applyIpTest () throws ResourceUnavailableException {
153+ Network network = mock (Network .class );
154+ when (network .getBroadcastDomainType ()).thenReturn (BroadcastDomainType .Lswitch );
155+ when (network .getId ()).thenReturn (42L );
156+ when (network .getPhysicalNetworkId ()).thenReturn (42L );
157+
158+ NetworkOffering offering = mock (NetworkOffering .class );
159+ when (offering .getId ()).thenReturn (42L );
160+ when (offering .getTrafficType ()).thenReturn (TrafficType .Guest );
161+ when (offering .getGuestType ()).thenReturn (GuestType .Isolated );
162+
163+ List <PublicIpAddress > ipAddresses = new ArrayList <PublicIpAddress >();
164+ PublicIpAddress pipReleased = mock (PublicIpAddress .class );
165+ PublicIpAddress pipAllocated = mock (PublicIpAddress .class );
166+ Ip ipReleased = new Ip ("42.10.10.10" );
167+ Ip ipAllocated = new Ip ("10.10.10.10" );
168+ when (pipAllocated .getState ()).thenReturn (IpAddress .State .Allocated );
169+ when (pipAllocated .getAddress ()).thenReturn (ipAllocated );
170+ when (pipAllocated .getNetmask ()).thenReturn ("255.255.255.0" );
171+ when (pipReleased .getState ()).thenReturn (IpAddress .State .Releasing );
172+ when (pipReleased .getAddress ()).thenReturn (ipReleased );
173+ when (pipReleased .getNetmask ()).thenReturn ("255.255.255.0" );
174+ ipAddresses .add (pipAllocated );
175+ ipAddresses .add (pipReleased );
176+
177+ Set <Service > services = new HashSet <Service >();
178+ services .add (Service .SourceNat );
179+ services .add (Service .StaticNat );
180+ services .add (Service .PortForwarding );
181+
182+ List <NiciraNvpDeviceVO > deviceList = new ArrayList <NiciraNvpDeviceVO >();
183+ NiciraNvpDeviceVO nndVO = mock (NiciraNvpDeviceVO .class );
184+ NiciraNvpRouterMappingVO nnrmVO = mock (NiciraNvpRouterMappingVO .class );
185+ when (_niciraNvpRouterMappingDao .findByNetworkId (42L )).thenReturn (nnrmVO );
186+ when (nnrmVO .getLogicalRouterUuid ()).thenReturn ("abcde" );
187+ when (nndVO .getHostId ()).thenReturn (42L );
188+ HostVO hvo = mock (HostVO .class );
189+ when (hvo .getId ()).thenReturn (42L );
190+ when (hvo .getDetail ("l3gatewayserviceuuid" )).thenReturn ("abcde" );
191+ when (_hostDao .findById (42L )).thenReturn (hvo );
192+ deviceList .add (nndVO );
193+ when (_niciraNvpDao .listByPhysicalNetwork (42L )).thenReturn (deviceList );
194+
195+ ConfigurePublicIpsOnLogicalRouterAnswer answer = mock (ConfigurePublicIpsOnLogicalRouterAnswer .class );
196+ when (answer .getResult ()).thenReturn (true );
197+ when (_agentManager .easySend (eq (42L ), any (ConfigurePublicIpsOnLogicalRouterCommand .class ))).thenReturn (answer );
198+
199+ assertTrue (_element .applyIps (network , ipAddresses , services ));
200+
201+ verify (_agentManager , atLeast (1 )).easySend (eq (42L ),
202+ argThat (new ArgumentMatcher <ConfigurePublicIpsOnLogicalRouterCommand >() {
203+ @ Override
204+ public boolean matches (Object argument ) {
205+ ConfigurePublicIpsOnLogicalRouterCommand command = (ConfigurePublicIpsOnLogicalRouterCommand ) argument ;
206+ if (command .getPublicCidrs ().size () == 1 )
207+ return true ;
208+ return false ;
209+ }
210+ }));
211+ }
212+ }
0 commit comments