5959import com .cloud .vm .VirtualMachineProfile ;
6060import com .cloud .vm .dao .NicDao ;
6161
62- @ Local (value = NetworkGuru .class )
62+ @ Local (value = NetworkGuru .class )
6363public class GuestNetworkGuru extends AdapterBase implements NetworkGuru {
6464 private static final Logger s_logger = Logger .getLogger (GuestNetworkGuru .class );
65- @ Inject protected NetworkManager _networkMgr ;
66- @ Inject protected DataCenterDao _dcDao ;
67- @ Inject protected VlanDao _vlanDao ;
68- @ Inject protected NicDao _nicDao ;
69- @ Inject protected NetworkDao _networkDao ;
70-
65+ @ Inject
66+ protected NetworkManager _networkMgr ;
67+ @ Inject
68+ protected DataCenterDao _dcDao ;
69+ @ Inject
70+ protected VlanDao _vlanDao ;
71+ @ Inject
72+ protected NicDao _nicDao ;
73+ @ Inject
74+ protected NetworkDao _networkDao ;
75+
7176 String _defaultGateway ;
7277 String _defaultCidr ;
7378 Random _rand = new Random (System .currentTimeMillis ());
74-
79+
7580 protected GuestNetworkGuru () {
7681 super ();
77- }
78-
82+ }
83+
7984 protected boolean canHandle (NetworkOffering offering , DataCenter dc ) {
80- //This guru handles only non-system Guest network
81- if (dc .getNetworkType () == NetworkType .Advanced && offering .getTrafficType () == TrafficType .Guest && offering .getGuestType () == GuestIpType .Virtual && !offering .isSystemOnly ()) {
85+ // This guru handles only non-system Guest network
86+ if (dc .getNetworkType () == NetworkType .Advanced && offering .getTrafficType () == TrafficType .Guest && offering .getGuestType () == GuestIpType .Virtual && !offering .isSystemOnly ()) {
8287 return true ;
8388 } else {
8489 s_logger .trace ("We only take care of Guest Virtual networks in zone of type " + NetworkType .Advanced );
8590 return false ;
8691 }
8792 }
88-
93+
8994 @ Override
9095 public Network design (NetworkOffering offering , DeploymentPlan plan , Network userSpecified , Account owner ) {
9196 DataCenter dc = _dcDao .findById (plan .getDataCenterId ());
@@ -95,124 +100,127 @@ public Network design(NetworkOffering offering, DeploymentPlan plan, Network use
95100
96101 NetworkVO network = new NetworkVO (offering .getTrafficType (), offering .getGuestType (), Mode .Dhcp , BroadcastDomainType .Vlan , offering .getId (), plan .getDataCenterId (), State .Allocated );
97102 if (userSpecified != null ) {
98- if ((userSpecified .getCidr () == null && userSpecified .getGateway () != null ) ||
99- (userSpecified .getCidr () != null && userSpecified .getGateway () == null )) {
103+ if ((userSpecified .getCidr () == null && userSpecified .getGateway () != null ) || (userSpecified .getCidr () != null && userSpecified .getGateway () == null )) {
100104 throw new InvalidParameterValueException ("cidr and gateway must be specified together." );
101105 }
102-
106+
103107 if (userSpecified .getCidr () != null ) {
104108 network .setCidr (userSpecified .getCidr ());
105109 network .setGateway (userSpecified .getGateway ());
106110 } else {
107111 String guestNetworkCidr = dc .getGuestNetworkCidr ();
108- //guest network cidr can be null for Basic zone
112+ // guest network cidr can be null for Basic zone
109113 if (guestNetworkCidr != null ) {
110114 String [] cidrTuple = guestNetworkCidr .split ("\\ /" );
111115 network .setGateway (NetUtils .getIpRangeStartIpFromCidr (cidrTuple [0 ], Long .parseLong (cidrTuple [1 ])));
112- network .setCidr (guestNetworkCidr );
116+ network .setCidr (guestNetworkCidr );
113117 }
114118 }
115-
119+
116120 if (userSpecified .getBroadcastUri () != null ) {
117121 network .setBroadcastUri (userSpecified .getBroadcastUri ());
118122 network .setState (State .Setup );
119123 }
120124 if (userSpecified .getNetworkDomain () != null ) {
121125 network .setNetworkDomain (userSpecified .getNetworkDomain ());
122126 }
123-
127+
124128 } else {
125129 String guestNetworkCidr = dc .getGuestNetworkCidr ();
126130 String [] cidrTuple = guestNetworkCidr .split ("\\ /" );
127131 network .setGateway (NetUtils .getIpRangeStartIpFromCidr (cidrTuple [0 ], Long .parseLong (cidrTuple [1 ])));
128- network .setCidr (guestNetworkCidr );;
132+ network .setCidr (guestNetworkCidr );
133+ ;
129134 }
130-
135+
131136 return network ;
132137 }
133138
134139 @ Override
135140 public void deallocate (Network network , NicProfile nic , VirtualMachineProfile <? extends VirtualMachine > vm ) {
136141 }
137-
142+
138143 @ Override
139- public Network implement (Network network , NetworkOffering offering , DeployDestination dest , ReservationContext context ) {
144+ public Network implement (Network network , NetworkOffering offering , DeployDestination dest , ReservationContext context ) throws InsufficientVirtualNetworkCapcityException {
140145 assert (network .getState () == State .Implementing ) : "Why are we implementing " + network ;
141-
146+
142147 long dcId = dest .getDataCenter ().getId ();
143-
144-
145- NetworkVO implemented = new NetworkVO ( network . getTrafficType (), network . getGuestType (), network . getMode (), network . getBroadcastDomainType (), network . getNetworkOfferingId (), network .getDataCenterId (), State .Allocated );
146-
148+
149+ NetworkVO implemented = new NetworkVO ( network . getTrafficType (), network . getGuestType (), network . getMode (), network . getBroadcastDomainType (), network . getNetworkOfferingId (),
150+ network .getDataCenterId (), State .Allocated );
151+
147152 if (network .getBroadcastUri () == null ) {
148153 String vnet = _dcDao .allocateVnet (dcId , network .getAccountId (), context .getReservationId ());
154+ if (vnet == null ) {
155+ throw new InsufficientVirtualNetworkCapcityException ("Unable to allocate vnet as a part of network " + network + " implement " , DataCenter .class , dcId );
156+ }
149157 implemented .setBroadcastUri (BroadcastDomainType .Vlan .toUri (vnet ));
150158 } else {
151159 implemented .setBroadcastUri (network .getBroadcastUri ());
152160 }
153-
161+
154162 if (network .getGateway () != null ) {
155163 implemented .setGateway (network .getGateway ());
156164 }
157-
165+
158166 if (network .getCidr () != null ) {
159167 implemented .setCidr (network .getCidr ());
160168 }
161-
169+
162170 return implemented ;
163171 }
164172
165173 @ Override
166- public NicProfile allocate (Network network , NicProfile nic , VirtualMachineProfile <? extends VirtualMachine > vm )
167- throws InsufficientVirtualNetworkCapcityException , InsufficientAddressCapacityException {
168-
174+ public NicProfile allocate (Network network , NicProfile nic , VirtualMachineProfile <? extends VirtualMachine > vm ) throws InsufficientVirtualNetworkCapcityException ,
175+ InsufficientAddressCapacityException {
176+
169177 assert (network .getTrafficType () == TrafficType .Guest ) : "Look at my name! Why are you calling me when the traffic type is : " + network .getTrafficType ();
170-
178+
171179 if (nic == null ) {
172180 nic = new NicProfile (ReservationStrategy .Start , null , null , null , null );
173- }
174-
181+ }
182+
175183 DataCenter dc = _dcDao .findById (network .getDataCenterId ());
176-
177- if (nic .getIp4Address () == null ){
184+
185+ if (nic .getIp4Address () == null ) {
178186 nic .setBroadcastUri (network .getBroadcastUri ());
179187 nic .setIsolationUri (network .getBroadcastUri ());
180188 nic .setGateway (network .getGateway ());
181189 nic .setIp4Address (acquireGuestIpAddress (network ));
182190 nic .setNetmask (NetUtils .cidr2Netmask (network .getCidr ()));
183191 nic .setFormat (AddressFormat .Ip4 );
184-
192+
185193 nic .setDns1 (dc .getDns1 ());
186194 nic .setDns2 (dc .getDns2 ());
187195 }
188-
196+
189197 nic .setStrategy (ReservationStrategy .Start );
190-
198+
191199 if (nic .getMacAddress () == null ) {
192200 nic .setMacAddress (_networkMgr .getNextAvailableMacAddressInNetwork (network .getId ()));
193201 if (nic .getMacAddress () == null ) {
194202 throw new InsufficientAddressCapacityException ("Unable to allocate more mac addresses" , Network .class , network .getId ());
195203 }
196204 }
197-
205+
198206 return nic ;
199207 }
200-
208+
201209 @ Override
202210 public void updateNicProfile (NicProfile profile , Network network ) {
203211 DataCenter dc = _dcDao .findById (network .getDataCenterId ());
204212 if (profile != null ) {
205213 profile .setDns1 (dc .getDns1 ());
206214 profile .setDns2 (dc .getDns2 ());
207- }
215+ }
208216 }
209-
217+
210218 @ DB
211219 protected String acquireGuestIpAddress (Network network ) {
212220 List <String > ips = _nicDao .listIpAddressInNetwork (network .getId ());
213221 String [] cidr = network .getCidr ().split ("/" );
214222 Set <Long > allPossibleIps = NetUtils .getAllIpsFromCidr (cidr [0 ], Integer .parseInt (cidr [1 ]));
215- Set <Long > usedIps = new TreeSet <Long > ();
223+ Set <Long > usedIps = new TreeSet <Long >();
216224 for (String ip : ips ) {
217225 usedIps .add (NetUtils .ip2Long (ip ));
218226 }
@@ -224,11 +232,11 @@ protected String acquireGuestIpAddress(Network network) {
224232 }
225233 Long [] array = allPossibleIps .toArray (new Long [allPossibleIps .size ()]);
226234 return NetUtils .long2Ip (array [_rand .nextInt (array .length )]);
227- }
235+ }
228236
229237 @ Override
230- public void reserve (NicProfile nic , Network network , VirtualMachineProfile <? extends VirtualMachine > vm , DeployDestination dest , ReservationContext context ) throws InsufficientVirtualNetworkCapcityException ,
231- InsufficientAddressCapacityException {
238+ public void reserve (NicProfile nic , Network network , VirtualMachineProfile <? extends VirtualMachine > vm , DeployDestination dest , ReservationContext context )
239+ throws InsufficientVirtualNetworkCapcityException , InsufficientAddressCapacityException {
232240 assert (nic .getReservationStrategy () == ReservationStrategy .Start ) : "What can I do for nics that are not allocated at start? " ;
233241
234242 nic .setBroadcastUri (network .getBroadcastUri ());
@@ -253,7 +261,7 @@ public void shutdown(NetworkProfile profile, NetworkOffering offering) {
253261 public boolean trash (Network network , NetworkOffering offering , Account owner ) {
254262 return true ;
255263 }
256-
264+
257265 @ Override
258266 public void updateNetworkProfile (NetworkProfile networkProfile ) {
259267 DataCenter dc = _dcDao .findById (networkProfile .getDataCenterId ());
0 commit comments