@@ -173,92 +173,113 @@ public PortForwardingRule createPortForwardingRule(PortForwardingRule rule, Long
173173
174174 Long networkId = rule .getNetworkId ();
175175 //associate ip address to network (if needed)
176+ boolean performedIpAssoc = false ;
176177 if (ipAddress .getAssociatedWithNetworkId () == null ) {
178+ //set networkId just for verification purposes
179+ ipAddress .setAssociatedWithNetworkId (networkId );
180+ _networkMgr .checkIpForService (ipAddress , Service .PortForwarding );
181+
177182 s_logger .debug ("The ip is not associated with the network id=" + networkId + " so assigning" );
178183 try {
179184 ipAddress = _networkMgr .associateIPToGuestNetwork (ipAddrId , networkId );
185+ performedIpAssoc = true ;
180186 } catch (Exception ex ) {
181187 throw new CloudRuntimeException ("Failed to associate ip to network as " +
182188 "a part of port forwarding rule creation" );
183189 }
190+ } else {
191+ _networkMgr .checkIpForService (ipAddress , Service .PortForwarding );
184192 }
185193
186- _networkMgr .checkIpForService (ipAddress , Service .PortForwarding );
187-
188- _firewallMgr .validateFirewallRule (caller , ipAddress , rule .getSourcePortStart (), rule .getSourcePortEnd (),
189- rule .getProtocol (), Purpose .PortForwarding , FirewallRuleType .User );
190-
191- Long accountId = ipAddress .getAllocatedToAccountId ();
192- Long domainId = ipAddress .getAllocatedInDomainId ();
194+ try {
195+ _firewallMgr .validateFirewallRule (caller , ipAddress , rule .getSourcePortStart (), rule .getSourcePortEnd (),
196+ rule .getProtocol (), Purpose .PortForwarding , FirewallRuleType .User );
193197
194- // start port can't be bigger than end port
195- if (rule .getDestinationPortStart () > rule .getDestinationPortEnd ()) {
196- throw new InvalidParameterValueException ("Start port can't be bigger than end port" );
197- }
198+ Long accountId = ipAddress .getAllocatedToAccountId ();
199+ Long domainId = ipAddress .getAllocatedInDomainId ();
198200
199- // check that the port ranges are of equal size
200- if (( rule . getDestinationPortEnd () - rule . getDestinationPortStart ()) != (rule .getSourcePortEnd () - rule .getSourcePortStart () )) {
201- throw new InvalidParameterValueException ("Source port and destination port ranges should be of equal sizes. " );
202- }
201+ // start port can't be bigger than end port
202+ if (rule .getDestinationPortStart () > rule .getDestinationPortEnd ( )) {
203+ throw new InvalidParameterValueException ("Start port can't be bigger than end port " );
204+ }
203205
204- // validate user VM exists
205- UserVm vm = _vmDao .findById (vmId );
206- if (vm == null ) {
207- throw new InvalidParameterValueException ("Unable to create port forwarding rule on address " + ipAddress +
208- ", invalid virtual machine id specified (" + vmId + ")." );
209- } else {
210- checkRuleAndUserVm (rule , vm , caller );
211- }
206+ // check that the port ranges are of equal size
207+ if ((rule .getDestinationPortEnd () - rule .getDestinationPortStart ()) != (rule .getSourcePortEnd () - rule .getSourcePortStart ())) {
208+ throw new InvalidParameterValueException ("Source port and destination port ranges should be of equal sizes." );
209+ }
212210
211+ // validate user VM exists
212+ UserVm vm = _vmDao .findById (vmId );
213+ if (vm == null ) {
214+ throw new InvalidParameterValueException ("Unable to create port forwarding rule on address " + ipAddress +
215+ ", invalid virtual machine id specified (" + vmId + ")." );
216+ } else {
217+ checkRuleAndUserVm (rule , vm , caller );
218+ }
213219
214- // Verify that vm has nic in the network
215- Ip dstIp = rule .getDestinationIpAddress ();
216- Nic guestNic = _networkMgr .getNicInNetwork (vmId , networkId );
217- if (guestNic == null || guestNic .getIp4Address () == null ) {
218- throw new InvalidParameterValueException ("Vm doesn't belong to network associated with ipAddress" );
219- } else {
220- dstIp = new Ip (guestNic .getIp4Address ());
221- }
222220
223- Transaction txn = Transaction .currentTxn ();
224- txn .start ();
221+ // Verify that vm has nic in the network
222+ Ip dstIp = rule .getDestinationIpAddress ();
223+ Nic guestNic = _networkMgr .getNicInNetwork (vmId , networkId );
224+ if (guestNic == null || guestNic .getIp4Address () == null ) {
225+ throw new InvalidParameterValueException ("Vm doesn't belong to network associated with ipAddress" );
226+ } else {
227+ dstIp = new Ip (guestNic .getIp4Address ());
228+ }
225229
226- PortForwardingRuleVO newRule = new PortForwardingRuleVO (rule .getXid (), rule .getSourceIpAddressId (),
227- rule .getSourcePortStart (), rule .getSourcePortEnd (), dstIp , rule .getDestinationPortStart (),
228- rule .getDestinationPortEnd (), rule .getProtocol ().toLowerCase (), networkId , accountId , domainId , vmId );
229- newRule = _portForwardingDao .persist (newRule );
230+ Transaction txn = Transaction .currentTxn ();
231+ txn .start ();
230232
231- // create firewallRule for 0.0.0.0/0 cidr
232- if (openFirewall ) {
233- _firewallMgr .createRuleForAllCidrs (ipAddrId , caller , rule .getSourcePortStart (), rule .getSourcePortEnd (),
234- rule .getProtocol (), null , null , newRule .getId (), networkId );
235- }
233+ PortForwardingRuleVO newRule = new PortForwardingRuleVO (rule .getXid (), rule .getSourceIpAddressId (),
234+ rule .getSourcePortStart (), rule .getSourcePortEnd (), dstIp , rule .getDestinationPortStart (),
235+ rule .getDestinationPortEnd (), rule .getProtocol ().toLowerCase (), networkId , accountId , domainId , vmId );
236+ newRule = _portForwardingDao .persist (newRule );
236237
237- try {
238- _firewallMgr . detectRulesConflict ( newRule );
239- if (! _firewallDao . setStateToAdd ( newRule )) {
240- throw new CloudRuntimeException ( "Unable to update the state to add for " + newRule );
238+ // create firewallRule for 0.0.0.0/0 cidr
239+ if ( openFirewall ) {
240+ _firewallMgr . createRuleForAllCidrs ( ipAddrId , caller , rule . getSourcePortStart (), rule . getSourcePortEnd (),
241+ rule . getProtocol (), null , null , newRule . getId (), networkId );
241242 }
242- UserContext .current ().setEventDetails ("Rule Id: " + newRule .getId ());
243- UsageEventVO usageEvent = new UsageEventVO (EventTypes .EVENT_NET_RULE_ADD , newRule .getAccountId (),
244- ipAddress .getDataCenterId (), newRule .getId (), null );
245- _usageEventDao .persist (usageEvent );
246- txn .commit ();
247- return newRule ;
248- } catch (Exception e ) {
249- if (newRule != null ) {
250- txn .start ();
251- // no need to apply the rule as it wasn't programmed on the backend yet
252- _firewallMgr .revokeRelatedFirewallRule (newRule .getId (), false );
253- _portForwardingDao .remove (newRule .getId ());
243+
244+ try {
245+ _firewallMgr .detectRulesConflict (newRule );
246+ if (!_firewallDao .setStateToAdd (newRule )) {
247+ throw new CloudRuntimeException ("Unable to update the state to add for " + newRule );
248+ }
249+ UserContext .current ().setEventDetails ("Rule Id: " + newRule .getId ());
250+ UsageEventVO usageEvent = new UsageEventVO (EventTypes .EVENT_NET_RULE_ADD , newRule .getAccountId (),
251+ ipAddress .getDataCenterId (), newRule .getId (), null );
252+ _usageEventDao .persist (usageEvent );
254253 txn .commit ();
255- }
254+ return newRule ;
255+ } catch (Exception e ) {
256+ if (newRule != null ) {
257+ txn .start ();
258+ // no need to apply the rule as it wasn't programmed on the backend yet
259+ _firewallMgr .revokeRelatedFirewallRule (newRule .getId (), false );
260+ removePFRule (newRule );
261+ txn .commit ();
262+ }
256263
257- if (e instanceof NetworkRuleConflictException ) {
258- throw (NetworkRuleConflictException ) e ;
264+ if (e instanceof NetworkRuleConflictException ) {
265+ throw (NetworkRuleConflictException ) e ;
266+ }
267+
268+ throw new CloudRuntimeException ("Unable to add rule for the ip id=" + ipAddrId , e );
269+ }
270+ } finally {
271+ // release ip address if ipassoc was perfored
272+ if (performedIpAssoc ) {
273+ //if the rule is the last one for the ip address assigned to VPC, unassign it from the network
274+ IpAddress ip = _ipAddressDao .findById (ipAddress .getId ());
275+ if (ip != null && ip .getVpcId () != null && _firewallDao .listByIp (ip .getId ()).isEmpty ()) {
276+ s_logger .debug ("Releasing VPC ip address " + ip + " as PF rule failed to create" );
277+ _networkMgr .unassignIPFromVpcNetwork (ip .getId ());
278+ }
259279 }
260- throw new CloudRuntimeException ("Unable to add rule for the ip id=" + ipAddrId , e );
261280 }
281+
282+
262283 }
263284
264285 @ Override
@@ -326,7 +347,7 @@ public StaticNatRule createStaticNatRule(StaticNatRule rule, boolean openFirewal
326347 txn .start ();
327348 // no need to apply the rule as it wasn't programmed on the backend yet
328349 _firewallMgr .revokeRelatedFirewallRule (newRule .getId (), false );
329- _portForwardingDao . remove (newRule . getId () );
350+ _firewallMgr . removeRule (newRule );
330351 txn .commit ();
331352 }
332353
@@ -1017,7 +1038,7 @@ public FirewallRuleVO[] reservePorts(IpAddress ip, String protocol, FirewallRule
10171038 txn .start ();
10181039
10191040 for (FirewallRuleVO newRule : rules ) {
1020- _portForwardingDao . remove (newRule . getId () );
1041+ _firewallMgr . removeRule (newRule );
10211042 }
10221043 txn .commit ();
10231044 }
@@ -1271,4 +1292,18 @@ public void getSystemIpAndEnableStaticNatForVm(VirtualMachine vm, boolean getNew
12711292 }
12721293 }
12731294 }
1295+
1296+ @ DB
1297+ protected void removePFRule (PortForwardingRuleVO rule ) {
1298+ Transaction txn = Transaction .currentTxn ();
1299+ txn .start ();
1300+ _portForwardingDao .remove (rule .getId ());
1301+ //if the rule is the last one for the ip address assigned to VPC, unassign it from the network
1302+ IpAddress ip = _ipAddressDao .findById (rule .getSourceIpAddressId ());
1303+ if (ip != null && ip .getVpcId () != null && _firewallDao .listByIp (ip .getId ()).isEmpty ()) {
1304+ _networkMgr .unassignIPFromVpcNetwork (ip .getId ());
1305+ }
1306+
1307+ txn .commit ();
1308+ }
12741309}
0 commit comments