You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ml...@apache.org on 2013/01/10 01:48:37 UTC
[8/52] [partial] Summary: Fixes for api_refactoring
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java
new file mode 100644
index 0000000..8a86f74
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java
@@ -0,0 +1,112 @@
+// 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 org.apache.cloudstack.api.command.user.loadbalancer;
+
+import org.apache.cloudstack.api.*;
+import org.apache.cloudstack.api.response.FirewallRuleResponse;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.response.LoadBalancerResponse;
+import com.cloud.event.EventTypes;
+import com.cloud.network.rules.LoadBalancer;
+import com.cloud.user.Account;
+import com.cloud.user.UserContext;
+
+@APICommand(name = "updateLoadBalancerRule", description="Updates load balancer", responseObject=LoadBalancerResponse.class)
+public class UpdateLoadBalancerRuleCmd extends BaseAsyncCmd {
+ public static final Logger s_logger = Logger.getLogger(UpdateLoadBalancerRuleCmd.class.getName());
+ private static final String s_name = "updateloadbalancerruleresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.ALGORITHM, type=CommandType.STRING, description="load balancer algorithm (source, roundrobin, leastconn)")
+ private String algorithm;
+
+ @Parameter(name=ApiConstants.DESCRIPTION, type=CommandType.STRING, description="the description of the load balancer rule", length=4096)
+ private String description;
+
+ @Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType = FirewallRuleResponse.class,
+ required=true, description="the id of the load balancer rule to update")
+ private Long id;
+
+ @Parameter(name=ApiConstants.NAME, type=CommandType.STRING, description="the name of the load balancer rule")
+ private String loadBalancerName;
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public String getAlgorithm() {
+ return algorithm;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public String getLoadBalancerName() {
+ return loadBalancerName;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ LoadBalancer lb = _entityMgr.findById(LoadBalancer.class, getId());
+ if (lb == null) {
+ return Account.ACCOUNT_ID_SYSTEM; // bad id given, parent this command to SYSTEM so ERROR events are tracked
+ }
+ return lb.getAccountId();
+ }
+
+ @Override
+ public String getEventType() {
+ return EventTypes.EVENT_LOAD_BALANCER_UPDATE;
+ }
+
+ @Override
+ public String getEventDescription() {
+ return "updating load balancer rule";
+ }
+
+ @Override
+ public void execute(){
+ UserContext.current().setEventDetails("Load balancer Id: "+getId());
+ LoadBalancer result = _lbService.updateLoadBalancerRule(this);
+ if (result != null){
+ LoadBalancerResponse response = _responseGenerator.createLoadBalancerResponse(result);
+ response.setResponseName(getCommandName());
+ this.setResponseObject(response);
+ } else {
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update load balancer rule");
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/nat/CreateIpForwardingRuleCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/nat/CreateIpForwardingRuleCmd.java b/api/src/org/apache/cloudstack/api/command/user/nat/CreateIpForwardingRuleCmd.java
new file mode 100644
index 0000000..e612b84
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/nat/CreateIpForwardingRuleCmd.java
@@ -0,0 +1,318 @@
+// 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 org.apache.cloudstack.api.command.user.nat;
+
+import java.util.List;
+
+import org.apache.cloudstack.api.response.IPAddressResponse;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseAsyncCmd;
+import org.apache.cloudstack.api.BaseAsyncCreateCmd;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.FirewallRuleResponse;
+import org.apache.cloudstack.api.response.IpForwardingRuleResponse;
+import com.cloud.async.AsyncJob;
+import com.cloud.event.EventTypes;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.exception.NetworkRuleConflictException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.network.IpAddress;
+import com.cloud.network.rules.FirewallRule;
+import com.cloud.network.rules.StaticNatRule;
+import com.cloud.user.Account;
+import com.cloud.user.UserContext;
+
+@APICommand(name = "createIpForwardingRule", description="Creates an ip forwarding rule", responseObject=FirewallRuleResponse.class)
+public class CreateIpForwardingRuleCmd extends BaseAsyncCreateCmd implements StaticNatRule {
+ public static final Logger s_logger = Logger.getLogger(CreateIpForwardingRuleCmd.class.getName());
+
+ private static final String s_name = "createipforwardingruleresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.IP_ADDRESS_ID, type=CommandType.UUID, entityType = IPAddressResponse.class,
+ required=true, description="the public IP address id of the forwarding rule, already associated via associateIp")
+ private Long ipAddressId;
+
+ @Parameter(name=ApiConstants.START_PORT, type=CommandType.INTEGER, required=true, description="the start port for the rule")
+ private Integer startPort;
+
+ @Parameter(name=ApiConstants.END_PORT, type=CommandType.INTEGER, description="the end port for the rule")
+ private Integer endPort;
+
+ @Parameter(name=ApiConstants.PROTOCOL, type=CommandType.STRING, required=true, description="the protocol for the rule. Valid values are TCP or UDP.")
+ private String protocol;
+
+ @Parameter(name = ApiConstants.OPEN_FIREWALL, type = CommandType.BOOLEAN, description = "if true, firewall rule for source/end pubic port is automatically created; if false - firewall rule has to be created explicitely. Has value true by default")
+ private Boolean openFirewall;
+
+ @Parameter(name = ApiConstants.CIDR_LIST, type = CommandType.LIST, collectionType = CommandType.STRING, description = "the cidr list to forward traffic from")
+ private List<String> cidrlist;
+
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public String getEntityTable() {
+ return "firewall_rules";
+ }
+
+ public Long getIpAddressId() {
+ return ipAddressId;
+ }
+
+ public int getStartPort() {
+ return startPort;
+ }
+
+ public int getEndPort() {
+ return endPort;
+ }
+
+ public Boolean getOpenFirewall() {
+ if (openFirewall != null) {
+ return openFirewall;
+ } else {
+ return true;
+ }
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public void execute() throws ResourceUnavailableException{
+
+ boolean result = true;
+ FirewallRule rule = null;
+ try {
+ UserContext.current().setEventDetails("Rule Id: "+ getEntityId());
+
+ if (getOpenFirewall()) {
+ result = result && _firewallService.applyFirewallRules(ipAddressId, UserContext.current().getCaller());
+ }
+
+ result = result && _rulesService.applyStaticNatRules(ipAddressId, UserContext.current().getCaller());
+ rule = _entityMgr.findById(FirewallRule.class, getEntityId());
+ StaticNatRule staticNatRule = _rulesService.buildStaticNatRule(rule, false);
+ IpForwardingRuleResponse fwResponse = _responseGenerator.createIpForwardingRuleResponse(staticNatRule);
+ fwResponse.setResponseName(getCommandName());
+ this.setResponseObject(fwResponse);
+ } finally {
+ if (!result || rule == null) {
+
+ if (getOpenFirewall()) {
+ _firewallService.revokeRelatedFirewallRule(getEntityId(), true);
+ }
+
+ _rulesService.revokeStaticNatRule(getEntityId(), true);
+
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Error in creating ip forwarding rule on the domr");
+ }
+ }
+ }
+
+ @Override
+ public void create() {
+
+ //cidr list parameter is deprecated
+ if (cidrlist != null) {
+ throw new InvalidParameterValueException("Parameter cidrList is deprecated; if you need to open firewall rule for the specific cidr, please refer to createFirewallRule command");
+ }
+
+ try {
+ StaticNatRule rule = _rulesService.createStaticNatRule(this, getOpenFirewall());
+ this.setEntityId(rule.getId());
+ } catch (NetworkRuleConflictException e) {
+ s_logger.info("Unable to create Static Nat Rule due to ", e);
+ throw new ServerApiException(BaseCmd.NETWORK_RULE_CONFLICT_ERROR, e.getMessage());
+ }
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ Account account = UserContext.current().getCaller();
+
+ if (account != null) {
+ return account.getId();
+ }
+
+ return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
+ }
+
+ @Override
+ public String getEventType() {
+ return EventTypes.EVENT_NET_RULE_ADD;
+ }
+
+ @Override
+ public String getEventDescription() {
+ IpAddress ip = _networkService.getIp(ipAddressId);
+ return ("Applying an ipforwarding 1:1 NAT rule for Ip: "+ip.getAddress()+" with virtual machine:"+ this.getVirtualMachineId());
+ }
+
+ private long getVirtualMachineId() {
+ Long vmId = _networkService.getIp(ipAddressId).getAssociatedWithVmId();
+
+ if (vmId == null) {
+ throw new InvalidParameterValueException("Ip address is not associated with any network, unable to create static nat rule");
+ }
+ return vmId;
+ }
+
+ @Override
+ public String getDestIpAddress(){
+ return null;
+ }
+
+ @Override
+ public long getId() {
+ throw new UnsupportedOperationException("Don't call me");
+ }
+
+ @Override
+ public Long getSourceIpAddressId() {
+ return ipAddressId;
+ }
+
+ @Override
+ public Integer getSourcePortStart() {
+ return startPort;
+ }
+
+ @Override
+ public Integer getSourcePortEnd() {
+ if (endPort == null) {
+ return startPort;
+ } else {
+ return endPort;
+ }
+ }
+
+ @Override
+ public String getProtocol() {
+ return protocol;
+ }
+
+ @Override
+ public FirewallRule.Purpose getPurpose() {
+ return FirewallRule.Purpose.StaticNat;
+ }
+
+ @Override
+ public FirewallRule.State getState() {
+ throw new UnsupportedOperationException("Don't call me");
+ }
+
+ @Override
+ public long getNetworkId() {
+ return -1;
+ }
+
+ @Override
+ public long getDomainId() {
+ IpAddress ip = _networkService.getIp(ipAddressId);
+ return ip.getDomainId();
+ }
+
+ @Override
+ public long getAccountId() {
+ IpAddress ip = _networkService.getIp(ipAddressId);
+ return ip.getAccountId();
+ }
+
+ @Override
+ public String getXid() {
+ // FIXME: We should allow for end user to specify Xid.
+ return null;
+ }
+
+
+ @Override
+ public String getUuid() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public String getSyncObjType() {
+ return BaseAsyncCmd.networkSyncObject;
+ }
+
+ @Override
+ public Long getSyncObjId() {
+ return getIp().getAssociatedWithNetworkId();
+ }
+
+ private IpAddress getIp() {
+ IpAddress ip = _networkService.getIp(ipAddressId);
+ if (ip == null) {
+ throw new InvalidParameterValueException("Unable to find ip address by id " + ipAddressId);
+ }
+ return ip;
+ }
+
+ @Override
+ public Integer getIcmpCode() {
+ return null;
+ }
+
+ @Override
+ public Integer getIcmpType() {
+ return null;
+ }
+
+ @Override
+ public List<String> getSourceCidrList() {
+ return null;
+ }
+
+ @Override
+ public Long getRelated() {
+ return null;
+ }
+
+ @Override
+ public FirewallRuleType getType() {
+ return FirewallRuleType.User;
+ }
+
+ @Override
+ public AsyncJob.Type getInstanceType() {
+ return AsyncJob.Type.FirewallRule;
+ }
+
+ @Override
+ public TrafficType getTrafficType() {
+ return null;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/nat/DeleteIpForwardingRuleCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/nat/DeleteIpForwardingRuleCmd.java b/api/src/org/apache/cloudstack/api/command/user/nat/DeleteIpForwardingRuleCmd.java
new file mode 100644
index 0000000..6300a41
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/nat/DeleteIpForwardingRuleCmd.java
@@ -0,0 +1,123 @@
+// 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 org.apache.cloudstack.api.command.user.nat;
+
+import org.apache.cloudstack.api.response.AccountResponse;
+import org.apache.cloudstack.api.response.FirewallRuleResponse;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseAsyncCmd;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import com.cloud.async.AsyncJob;
+import com.cloud.event.EventTypes;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.network.rules.FirewallRule;
+import com.cloud.user.UserContext;
+
+@APICommand(name = "deleteIpForwardingRule", description="Deletes an ip forwarding rule", responseObject=SuccessResponse.class)
+public class DeleteIpForwardingRuleCmd extends BaseAsyncCmd {
+ public static final Logger s_logger = Logger.getLogger(DeleteIpForwardingRuleCmd.class.getName());
+
+ private static final String s_name = "deleteipforwardingruleresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType = FirewallRuleResponse.class,
+ required=true, description="the id of the forwarding rule")
+ private Long id;
+
+ // unexposed parameter needed for events logging
+ @Parameter(name=ApiConstants.ACCOUNT_ID, type=CommandType.UUID, entityType = AccountResponse.class,
+ expose=false)
+ private Long ownerId;
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public Long getId() {
+ return id;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public void execute(){
+ UserContext.current().setEventDetails("Rule Id: "+id);
+ boolean result = _firewallService.revokeRelatedFirewallRule(id, true);
+ result = result && _rulesService.revokeStaticNatRule(id, true);
+
+ if (result) {
+ SuccessResponse response = new SuccessResponse(getCommandName());
+ this.setResponseObject(response);
+ } else {
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to delete ip forwarding rule");
+ }
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ if (ownerId == null) {
+ FirewallRule rule = _entityMgr.findById(FirewallRule.class, id);
+ if (rule == null) {
+ throw new InvalidParameterValueException("Unable to find static nat rule by id: " + id);
+ } else {
+ ownerId = rule.getAccountId();
+ }
+ }
+ return ownerId;
+ }
+
+ @Override
+ public String getEventType() {
+ return EventTypes.EVENT_NET_RULE_DELETE;
+ }
+
+ @Override
+ public String getEventDescription() {
+ return ("Deleting an ipforwarding 1:1 NAT rule id:"+id);
+ }
+
+ @Override
+ public String getSyncObjType() {
+ return BaseAsyncCmd.networkSyncObject;
+ }
+
+ @Override
+ public Long getSyncObjId() {
+ return _rulesService.getFirewallRule(id).getNetworkId();
+ }
+
+ @Override
+ public AsyncJob.Type getInstanceType() {
+ return AsyncJob.Type.FirewallRule;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/nat/DisableStaticNatCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/nat/DisableStaticNatCmd.java b/api/src/org/apache/cloudstack/api/command/user/nat/DisableStaticNatCmd.java
new file mode 100644
index 0000000..fbd0b5c
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/nat/DisableStaticNatCmd.java
@@ -0,0 +1,111 @@
+// 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 org.apache.cloudstack.api.command.user.nat;
+
+import org.apache.cloudstack.api.command.user.firewall.DeletePortForwardingRuleCmd;
+import org.apache.cloudstack.api.response.IPAddressResponse;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseAsyncCmd;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import com.cloud.event.EventTypes;
+import com.cloud.exception.InsufficientAddressCapacityException;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.exception.NetworkRuleConflictException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.network.IpAddress;
+
+@APICommand(name = "disableStaticNat", description="Disables static rule for given ip address", responseObject=SuccessResponse.class)
+public class DisableStaticNatCmd extends BaseAsyncCmd {
+ public static final Logger s_logger = Logger.getLogger(DeletePortForwardingRuleCmd.class.getName());
+ private static final String s_name = "disablestaticnatresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.IP_ADDRESS_ID, type=CommandType.UUID, entityType = IPAddressResponse.class,
+ required=true, description="the public IP address id for which static nat feature is being disableed")
+ private Long ipAddressId;
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public Long getIpAddress() {
+ return ipAddressId;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public String getEventType() {
+ return EventTypes.EVENT_DISABLE_STATIC_NAT;
+ }
+
+ @Override
+ public String getEventDescription() {
+ return ("Disabling static nat for ip id=" + ipAddressId);
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ return _entityMgr.findById(IpAddress.class, ipAddressId).getAccountId();
+ }
+
+ @Override
+ public void execute() throws ResourceUnavailableException, NetworkRuleConflictException, InsufficientAddressCapacityException {
+ boolean result = _rulesService.disableStaticNat(ipAddressId);
+
+ if (result) {
+ SuccessResponse response = new SuccessResponse(getCommandName());
+ this.setResponseObject(response);
+ } else {
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to disable static nat");
+ }
+ }
+
+
+ @Override
+ public String getSyncObjType() {
+ return BaseAsyncCmd.networkSyncObject;
+ }
+
+ @Override
+ public Long getSyncObjId() {
+ return getIp().getAssociatedWithNetworkId();
+ }
+
+ private IpAddress getIp() {
+ IpAddress ip = _networkService.getIp(ipAddressId);
+ if (ip == null) {
+ throw new InvalidParameterValueException("Unable to find ip address by id " + ipAddressId);
+ }
+ return ip;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/nat/EnableStaticNatCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/nat/EnableStaticNatCmd.java b/api/src/org/apache/cloudstack/api/command/user/nat/EnableStaticNatCmd.java
new file mode 100644
index 0000000..79a8e2b
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/nat/EnableStaticNatCmd.java
@@ -0,0 +1,126 @@
+// 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 org.apache.cloudstack.api.command.user.nat;
+
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import org.apache.cloudstack.api.response.IPAddressResponse;
+import org.apache.cloudstack.api.response.NetworkResponse;
+import org.apache.cloudstack.api.response.UserVmResponse;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.exception.NetworkRuleConflictException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.network.IpAddress;
+import com.cloud.user.Account;
+import com.cloud.uservm.UserVm;
+
+@APICommand(name = "enableStaticNat", description="Enables static nat for given ip address", responseObject=SuccessResponse.class)
+public class EnableStaticNatCmd extends BaseCmd{
+ public static final Logger s_logger = Logger.getLogger(CreateIpForwardingRuleCmd.class.getName());
+
+ private static final String s_name = "enablestaticnatresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.IP_ADDRESS_ID, type=CommandType.UUID, entityType = IPAddressResponse.class,
+ required=true, description="the public IP " +
+ "address id for which static nat feature is being enabled")
+ private Long ipAddressId;
+
+ @Parameter(name=ApiConstants.VIRTUAL_MACHINE_ID, type=CommandType.UUID, entityType = UserVmResponse.class,
+ required=true, description="the ID of " +
+ "the virtual machine for enabling static nat feature")
+ private Long virtualMachineId;
+
+ @Parameter(name=ApiConstants.NETWORK_ID, type=CommandType.UUID, entityType = NetworkResponse.class,
+ description="The network of the vm the static nat will be enabled for." +
+ " Required when public Ip address is not associated with any Guest network yet (VPC case)")
+ private Long networkId;
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public Long getIpAddressId() {
+ return ipAddressId;
+ }
+
+ public Long getVirtualMachineId() {
+ return virtualMachineId;
+ }
+
+ public long getNetworkId() {
+ IpAddress ip = _entityMgr.findById(IpAddress.class, getIpAddressId());
+ Long ntwkId = null;
+
+ if (ip.getAssociatedWithNetworkId() != null) {
+ ntwkId = ip.getAssociatedWithNetworkId();
+ } else {
+ ntwkId = networkId;
+ }
+ if (ntwkId == null) {
+ throw new InvalidParameterValueException("Unable to enable static nat for the ipAddress id=" + ipAddressId +
+ " as ip is not associated with any network and no networkId is passed in");
+ }
+ return ntwkId;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ UserVm userVm = _entityMgr.findById(UserVm.class, getVirtualMachineId());
+ if (userVm != null) {
+ return userVm.getAccountId();
+ }
+
+ return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
+ }
+
+ @Override
+ public void execute() throws ResourceUnavailableException{
+ try {
+ boolean result = _rulesService.enableStaticNat(ipAddressId, virtualMachineId, getNetworkId(), false);
+ if (result) {
+ SuccessResponse response = new SuccessResponse(getCommandName());
+ this.setResponseObject(response);
+ } else {
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to enable static nat");
+ }
+ } catch (NetworkRuleConflictException ex) {
+ s_logger.info("Network rule conflict: " + ex.getMessage());
+ s_logger.trace("Network Rule Conflict: ", ex);
+ throw new ServerApiException(BaseCmd.NETWORK_RULE_CONFLICT_ERROR, ex.getMessage());
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/nat/ListIpForwardingRulesCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/nat/ListIpForwardingRulesCmd.java b/api/src/org/apache/cloudstack/api/command/user/nat/ListIpForwardingRulesCmd.java
new file mode 100644
index 0000000..e4aaff3
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/nat/ListIpForwardingRulesCmd.java
@@ -0,0 +1,102 @@
+// 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 org.apache.cloudstack.api.command.user.nat;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.cloudstack.api.APICommand;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseListProjectAndAccountResourcesCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.FirewallRuleResponse;
+import org.apache.cloudstack.api.response.IPAddressResponse;
+import org.apache.cloudstack.api.response.IpForwardingRuleResponse;
+import org.apache.cloudstack.api.response.ListResponse;
+import org.apache.cloudstack.api.response.UserVmResponse;
+import com.cloud.network.rules.FirewallRule;
+import com.cloud.network.rules.StaticNatRule;
+import com.cloud.utils.Pair;
+
+@APICommand(name = "listIpForwardingRules", description="List the ip forwarding rules", responseObject=FirewallRuleResponse.class)
+public class ListIpForwardingRulesCmd extends BaseListProjectAndAccountResourcesCmd {
+ public static final Logger s_logger = Logger.getLogger(ListIpForwardingRulesCmd.class.getName());
+
+ private static final String s_name = "listipforwardingrulesresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.IP_ADDRESS_ID, type=CommandType.UUID, entityType = IPAddressResponse.class,
+ description="list the rule belonging to this public ip address")
+ private Long publicIpAddressId;
+
+ @Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType = FirewallRuleResponse.class,
+ description="Lists rule with the specified ID.")
+ private Long id;
+
+ @Parameter(name=ApiConstants.VIRTUAL_MACHINE_ID, type=CommandType.UUID, entityType = UserVmResponse.class,
+ description="Lists all rules applied to the specified Vm.")
+ private Long vmId;
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ public Long getPublicIpAddressId() {
+ return publicIpAddressId;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public Long getVmId() {
+ return vmId;
+ }
+
+ @Override
+ public void execute(){
+ Pair<List<? extends FirewallRule>, Integer> result = _rulesService.searchStaticNatRules(publicIpAddressId, id, vmId,
+ this.getStartIndex(), this.getPageSizeVal(), this.getAccountName(), this.getDomainId(), this.getProjectId(), this.isRecursive(), this.listAll());
+ ListResponse<IpForwardingRuleResponse> response = new ListResponse<IpForwardingRuleResponse>();
+ List<IpForwardingRuleResponse> ipForwardingResponses = new ArrayList<IpForwardingRuleResponse>();
+ for (FirewallRule rule : result.first()) {
+ StaticNatRule staticNatRule = _rulesService.buildStaticNatRule(rule, false);
+ IpForwardingRuleResponse resp = _responseGenerator.createIpForwardingRuleResponse(staticNatRule);
+ if (resp != null) {
+ ipForwardingResponses.add(resp);
+ }
+ }
+ response.setResponses(ipForwardingResponses, result.second());
+ response.setResponseName(getCommandName());
+ this.setResponseObject(response);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/network/CreateNetworkACLCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/network/CreateNetworkACLCmd.java b/api/src/org/apache/cloudstack/api/command/user/network/CreateNetworkACLCmd.java
new file mode 100644
index 0000000..e2aba5b
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/network/CreateNetworkACLCmd.java
@@ -0,0 +1,335 @@
+// 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 org.apache.cloudstack.api.command.user.network;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.cloudstack.api.response.NetworkResponse;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseAsyncCmd;
+import org.apache.cloudstack.api.BaseAsyncCreateCmd;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.NetworkACLResponse;
+import com.cloud.async.AsyncJob;
+import com.cloud.event.EventTypes;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.exception.NetworkRuleConflictException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.network.Network;
+import com.cloud.network.rules.FirewallRule;
+import com.cloud.network.vpc.Vpc;
+import com.cloud.user.Account;
+import com.cloud.user.UserContext;
+import com.cloud.utils.net.NetUtils;
+
+@APICommand(name = "createNetworkACL", description = "Creates a ACL rule the given network (the network has to belong to VPC)",
+responseObject = NetworkACLResponse.class)
+public class CreateNetworkACLCmd extends BaseAsyncCreateCmd implements FirewallRule {
+ public static final Logger s_logger = Logger.getLogger(CreateNetworkACLCmd.class.getName());
+
+ private static final String s_name = "createnetworkaclresponse";
+
+ // ///////////////////////////////////////////////////
+ // ////////////// API parameters /////////////////////
+ // ///////////////////////////////////////////////////
+
+ @Parameter(name = ApiConstants.PROTOCOL, type = CommandType.STRING, required = true, description =
+ "the protocol for the ACL rule. Valid values are TCP/UDP/ICMP.")
+ private String protocol;
+
+ @Parameter(name = ApiConstants.START_PORT, type = CommandType.INTEGER, description = "the starting port of ACL")
+ private Integer publicStartPort;
+
+ @Parameter(name = ApiConstants.END_PORT, type = CommandType.INTEGER, description = "the ending port of ACL")
+ private Integer publicEndPort;
+
+ @Parameter(name = ApiConstants.CIDR_LIST, type = CommandType.LIST, collectionType = CommandType.STRING,
+ description = "the cidr list to allow traffic from/to")
+ private List<String> cidrlist;
+
+ @Parameter(name = ApiConstants.ICMP_TYPE, type = CommandType.INTEGER, description = "type of the icmp message being sent")
+ private Integer icmpType;
+
+ @Parameter(name = ApiConstants.ICMP_CODE, type = CommandType.INTEGER, description = "error code for this icmp message")
+ private Integer icmpCode;
+
+ @Parameter(name=ApiConstants.NETWORK_ID, type=CommandType.UUID, entityType = NetworkResponse.class,
+ required=true,
+ description="The network of the vm the ACL will be created for")
+ private Long networkId;
+
+ @Parameter(name=ApiConstants.TRAFFIC_TYPE, type=CommandType.STRING, description="the traffic type for the ACL," +
+ "can be Ingress or Egress, defaulted to Ingress if not specified")
+ private String trafficType;
+
+ // ///////////////////////////////////////////////////
+ // ///////////////// Accessors ///////////////////////
+ // ///////////////////////////////////////////////////
+
+ public String getEntityTable() {
+ return "firewall_rules";
+ }
+
+ public Long getIpAddressId() {
+ return null;
+ }
+
+ @Override
+ public String getProtocol() {
+ return protocol.trim();
+ }
+
+ public List<String> getSourceCidrList() {
+ if (cidrlist != null) {
+ return cidrlist;
+ } else {
+ List<String> oneCidrList = new ArrayList<String>();
+ oneCidrList.add(NetUtils.ALL_CIDRS);
+ return oneCidrList;
+ }
+ }
+
+ public long getVpcId() {
+ Network network = _networkService.getNetwork(getNetworkId());
+ if (network == null) {
+ throw new InvalidParameterValueException("Invalid networkId is given");
+ }
+
+ Long vpcId = network.getVpcId();
+ if (vpcId == null) {
+ throw new InvalidParameterValueException("Can create network ACL only for the network belonging to the VPC");
+ }
+
+ return vpcId;
+ }
+
+ @Override
+ public FirewallRule.TrafficType getTrafficType() {
+ if (trafficType == null) {
+ return FirewallRule.TrafficType.Ingress;
+ }
+ for (FirewallRule.TrafficType type : FirewallRule.TrafficType.values()) {
+ if (type.toString().equalsIgnoreCase(trafficType)) {
+ return type;
+ }
+ }
+ throw new InvalidParameterValueException("Invalid traffic type " + trafficType);
+ }
+
+ // ///////////////////////////////////////////////////
+ // ///////////// API Implementation///////////////////
+ // ///////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ public void setSourceCidrList(List<String> cidrs){
+ cidrlist = cidrs;
+ }
+
+ @Override
+ public void execute() throws ResourceUnavailableException {
+ UserContext callerContext = UserContext.current();
+ boolean success = false;
+ FirewallRule rule = _networkACLService.getNetworkACL(getEntityId());
+ try {
+ UserContext.current().setEventDetails("Rule Id: " + getEntityId());
+ success = _networkACLService.applyNetworkACLs(rule.getNetworkId(), callerContext.getCaller());
+
+ // State is different after the rule is applied, so get new object here
+ NetworkACLResponse aclResponse = new NetworkACLResponse();
+ if (rule != null) {
+ aclResponse = _responseGenerator.createNetworkACLResponse(rule);
+ setResponseObject(aclResponse);
+ }
+ aclResponse.setResponseName(getCommandName());
+ } finally {
+ if (!success || rule == null) {
+ _networkACLService.revokeNetworkACL(getEntityId(), true);
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create network ACL");
+ }
+ }
+ }
+
+ @Override
+ public long getId() {
+ throw new UnsupportedOperationException("database id can only provided by VO objects");
+ }
+
+ @Override
+ public String getXid() {
+ // FIXME: We should allow for end user to specify Xid.
+ return null;
+ }
+
+
+ @Override
+ public String getUuid() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Long getSourceIpAddressId() {
+ return null;
+ }
+
+ @Override
+ public Integer getSourcePortStart() {
+ if (publicStartPort != null) {
+ return publicStartPort.intValue();
+ }
+ return null;
+ }
+
+ @Override
+ public Integer getSourcePortEnd() {
+ if (publicEndPort == null) {
+ if (publicStartPort != null) {
+ return publicStartPort.intValue();
+ }
+ } else {
+ return publicEndPort.intValue();
+ }
+
+ return null;
+ }
+
+ @Override
+ public Purpose getPurpose() {
+ return Purpose.Firewall;
+ }
+
+ @Override
+ public State getState() {
+ throw new UnsupportedOperationException("Should never call me to find the state");
+ }
+
+ @Override
+ public long getNetworkId() {
+ return networkId;
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ Vpc vpc = _vpcService.getVpc(getVpcId());
+ if (vpc == null) {
+ throw new InvalidParameterValueException("Invalid vpcId is given");
+ }
+
+ Account account = _accountService.getAccount(vpc.getAccountId());
+ return account.getId();
+ }
+
+ @Override
+ public long getDomainId() {
+ Vpc vpc = _vpcService.getVpc(getVpcId());
+ return vpc.getDomainId();
+ }
+
+ @Override
+ public void create() {
+ if (getSourceCidrList() != null) {
+ for (String cidr: getSourceCidrList()){
+ if (!NetUtils.isValidCIDR(cidr)){
+ throw new ServerApiException(BaseCmd.PARAM_ERROR, "Source cidrs formatting error " + cidr);
+ }
+ }
+ }
+
+ try {
+ FirewallRule result = _networkACLService.createNetworkACL(this);
+ setEntityId(result.getId());
+ } catch (NetworkRuleConflictException ex) {
+ s_logger.info("Network rule conflict: " + ex.getMessage());
+ s_logger.trace("Network Rule Conflict: ", ex);
+ throw new ServerApiException(BaseCmd.NETWORK_RULE_CONFLICT_ERROR, ex.getMessage());
+ }
+ }
+
+ @Override
+ public String getEventType() {
+ return EventTypes.EVENT_FIREWALL_OPEN;
+ }
+
+ @Override
+ public String getEventDescription() {
+ Network network = _networkService.getNetwork(networkId);
+ return ("Createing Network ACL for Netowrk: " + network + " for protocol:" + this.getProtocol());
+ }
+
+ @Override
+ public long getAccountId() {
+ Vpc vpc = _vpcService.getVpc(getVpcId());
+ return vpc.getAccountId();
+ }
+
+ @Override
+ public String getSyncObjType() {
+ return BaseAsyncCmd.networkSyncObject;
+ }
+
+ @Override
+ public Long getSyncObjId() {
+ return getNetworkId();
+ }
+
+ @Override
+ public Integer getIcmpCode() {
+ if (icmpCode != null) {
+ return icmpCode;
+ } else if (protocol.equalsIgnoreCase(NetUtils.ICMP_PROTO)) {
+ return -1;
+ }
+ return null;
+ }
+
+ @Override
+ public Integer getIcmpType() {
+ if (icmpType != null) {
+ return icmpType;
+ } else if (protocol.equalsIgnoreCase(NetUtils.ICMP_PROTO)) {
+ return -1;
+
+ }
+ return null;
+ }
+
+ @Override
+ public Long getRelated() {
+ return null;
+ }
+
+ @Override
+ public FirewallRuleType getType() {
+ return FirewallRuleType.User;
+ }
+
+ @Override
+ public AsyncJob.Type getInstanceType() {
+ return AsyncJob.Type.FirewallRule;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/network/CreateNetworkCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/network/CreateNetworkCmd.java b/api/src/org/apache/cloudstack/api/command/user/network/CreateNetworkCmd.java
new file mode 100644
index 0000000..3b26087
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/network/CreateNetworkCmd.java
@@ -0,0 +1,239 @@
+// 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 org.apache.cloudstack.api.command.user.network;
+
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.DomainResponse;
+import org.apache.cloudstack.api.response.NetworkResponse;
+import org.apache.cloudstack.api.response.NetworkOfferingResponse;
+import org.apache.cloudstack.api.response.PhysicalNetworkResponse;
+import org.apache.cloudstack.api.response.ProjectResponse;
+import org.apache.cloudstack.api.response.VpcResponse;
+import org.apache.cloudstack.api.response.ZoneResponse;
+import com.cloud.exception.ConcurrentOperationException;
+import com.cloud.exception.InsufficientCapacityException;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.exception.ResourceAllocationException;
+import com.cloud.network.Network;
+import com.cloud.network.Network.GuestType;
+import com.cloud.offering.NetworkOffering;
+import com.cloud.user.UserContext;
+
+@APICommand(name = "createNetwork", description="Creates a network", responseObject=NetworkResponse.class)
+public class CreateNetworkCmd extends BaseCmd {
+ public static final Logger s_logger = Logger.getLogger(CreateNetworkCmd.class.getName());
+
+ private static final String s_name = "createnetworkresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.NAME, type=CommandType.STRING, required=true, description="the name of the network")
+ private String name;
+
+ @Parameter(name=ApiConstants.DISPLAY_TEXT, type=CommandType.STRING, required=true, description="the display text of the network")
+ private String displayText;
+
+ @Parameter(name=ApiConstants.NETWORK_OFFERING_ID, type=CommandType.UUID, entityType = NetworkOfferingResponse.class,
+ required=true, description="the network offering id")
+ private Long networkOfferingId;
+
+ @Parameter(name=ApiConstants.ZONE_ID, type=CommandType.UUID, entityType = ZoneResponse.class,
+ required=true, description="the Zone ID for the network")
+ private Long zoneId;
+
+ @Parameter(name=ApiConstants.PHYSICAL_NETWORK_ID, type=CommandType.UUID, entityType = PhysicalNetworkResponse.class,
+ description="the Physical Network ID the network belongs to")
+ private Long physicalNetworkId;
+
+ @Parameter(name=ApiConstants.GATEWAY, type=CommandType.STRING, description="the gateway of the network. Required " +
+ "for Shared networks and Isolated networks when it belongs to VPC")
+ private String gateway;
+
+ @Parameter(name=ApiConstants.NETMASK, type=CommandType.STRING, description="the netmask of the network. Required " +
+ "for Shared networks and Isolated networks when it belongs to VPC")
+ private String netmask;
+
+ @Parameter(name=ApiConstants.START_IP, type=CommandType.STRING, description="the beginning IP address in the network IP range")
+ private String startIp;
+
+ @Parameter(name=ApiConstants.END_IP, type=CommandType.STRING, description="the ending IP address in the network IP" +
+ " range. If not specified, will be defaulted to startIP")
+ private String endIp;
+
+ @Parameter(name=ApiConstants.VLAN, type=CommandType.STRING, description="the ID or VID of the network")
+ private String vlan;
+
+ @Parameter(name=ApiConstants.NETWORK_DOMAIN, type=CommandType.STRING, description="network domain")
+ private String networkDomain;
+
+ @Parameter(name=ApiConstants.ACL_TYPE, type=CommandType.STRING, description="Access control type; supported values" +
+ " are account and domain. In 3.0 all shared networks should have aclType=Domain, and all Isolated networks" +
+ " - Account. Account means that only the account owner can use the network, domain - all accouns in the domain can use the network")
+ private String aclType;
+
+ @Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="account who will own the network")
+ private String accountName;
+
+ @Parameter(name=ApiConstants.PROJECT_ID, type=CommandType.UUID, entityType = ProjectResponse.class,
+ description="an optional project for the ssh key")
+ private Long projectId;
+
+ @Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.UUID, entityType = DomainResponse.class,
+ description="domain ID of the account owning a network")
+ private Long domainId;
+
+ @Parameter(name=ApiConstants.SUBDOMAIN_ACCESS, type=CommandType.BOOLEAN, description="Defines whether to allow" +
+ " subdomains to use networks dedicated to their parent domain(s). Should be used with aclType=Domain, defaulted to allow.subdomain.network.access global config if not specified")
+ private Boolean subdomainAccess;
+
+ @Parameter(name=ApiConstants.VPC_ID, type=CommandType.UUID, entityType = VpcResponse.class,
+ description="the VPC network belongs to")
+ private Long vpcId;
+
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+ public Long getNetworkOfferingId() {
+ return networkOfferingId;
+ }
+
+ public String getGateway() {
+ return gateway;
+ }
+
+ public String getVlan() {
+ return vlan;
+ }
+
+ public String getAccountName() {
+ return accountName;
+ }
+
+ public Long getDomainId() {
+ return domainId;
+ }
+
+ public String getNetmask() {
+ return netmask;
+ }
+
+ public String getStartIp() {
+ return startIp;
+ }
+
+ public String getEndIp() {
+ return endIp;
+ }
+
+ public String getNetworkName() {
+ return name;
+ }
+
+ public String getDisplayText() {
+ return displayText;
+ }
+
+ public String getNetworkDomain() {
+ return networkDomain;
+ }
+
+ public Long getProjectId() {
+ return projectId;
+ }
+
+ public String getAclType() {
+ return aclType;
+ }
+
+ public Boolean getSubdomainAccess() {
+ return subdomainAccess;
+ }
+
+ public Long getVpcId() {
+ return vpcId;
+ }
+
+ public Long getZoneId() {
+ Long physicalNetworkId = getPhysicalNetworkId();
+
+ if (physicalNetworkId == null && zoneId == null) {
+ throw new InvalidParameterValueException("Zone id is required");
+ }
+
+ return zoneId;
+ }
+
+ public Long getPhysicalNetworkId() {
+ NetworkOffering offering = _configService.getNetworkOffering(networkOfferingId);
+ if (offering == null) {
+ throw new InvalidParameterValueException("Unable to find network offering by id " + networkOfferingId);
+ }
+
+ if (physicalNetworkId != null) {
+ if (offering.getGuestType() == GuestType.Shared) {
+ return physicalNetworkId;
+ } else {
+ throw new InvalidParameterValueException("Physical network id can be specified for networks of guest ip type " + GuestType.Shared + " only.");
+ }
+ } else {
+ if (zoneId == null) {
+ throw new InvalidParameterValueException("ZoneId is required as physicalNetworkId is null");
+ }
+ return _networkService.findPhysicalNetworkId(zoneId, offering.getTags(), offering.getTrafficType());
+ }
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ Long accountId = finalyzeAccountId(accountName, domainId, projectId, true);
+ if (accountId == null) {
+ return UserContext.current().getCaller().getId();
+ }
+
+ return accountId;
+ }
+
+ @Override
+ // an exception thrown by createNetwork() will be caught by the dispatcher.
+ public void execute() throws InsufficientCapacityException, ConcurrentOperationException, ResourceAllocationException{
+ Network result = _networkService.createGuestNetwork(this);
+ if (result != null) {
+ NetworkResponse response = _responseGenerator.createNetworkResponse(result);
+ response.setResponseName(getCommandName());
+ this.setResponseObject(response);
+ }else {
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create network");
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/network/DeleteNetworkACLCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/network/DeleteNetworkACLCmd.java b/api/src/org/apache/cloudstack/api/command/user/network/DeleteNetworkACLCmd.java
new file mode 100644
index 0000000..4b078ed
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/network/DeleteNetworkACLCmd.java
@@ -0,0 +1,122 @@
+// 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 org.apache.cloudstack.api.command.user.network;
+
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseAsyncCmd;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import org.apache.cloudstack.api.response.AccountResponse;
+import org.apache.cloudstack.api.response.FirewallRuleResponse;
+import com.cloud.async.AsyncJob;
+import com.cloud.event.EventTypes;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.network.rules.FirewallRule;
+import com.cloud.user.UserContext;
+
+@APICommand(name = "deleteNetworkACL", description="Deletes a Network ACL", responseObject=SuccessResponse.class)
+public class DeleteNetworkACLCmd extends BaseAsyncCmd {
+ public static final Logger s_logger = Logger.getLogger(DeleteNetworkACLCmd.class.getName());
+ private static final String s_name = "deletenetworkaclresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType = FirewallRuleResponse.class,
+ required=true, description="the ID of the network ACL")
+ private Long id;
+
+ // unexposed parameter needed for events logging
+ @Parameter(name=ApiConstants.ACCOUNT_ID, type=CommandType.UUID, entityType = AccountResponse.class,
+ expose=false)
+ private Long ownerId;
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public Long getId() {
+ return id;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public String getEventType() {
+ return EventTypes.EVENT_FIREWALL_CLOSE;
+ }
+
+ @Override
+ public String getEventDescription() {
+ return ("Deleting Network ACL id=" + id);
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ if (ownerId == null) {
+ FirewallRule rule = _networkACLService.getNetworkACL(id);
+ if (rule == null) {
+ throw new InvalidParameterValueException("Unable to find network ACL by id=" + id);
+ } else {
+ ownerId = rule.getAccountId();
+ }
+ }
+ return ownerId;
+ }
+
+ @Override
+ public void execute() throws ResourceUnavailableException {
+ UserContext.current().setEventDetails("Network ACL Id: " + id);
+ boolean result = _networkACLService.revokeNetworkACL(id, true);
+
+ if (result) {
+ SuccessResponse response = new SuccessResponse(getCommandName());
+ this.setResponseObject(response);
+ } else {
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to delete network ACL");
+ }
+ }
+
+
+ @Override
+ public String getSyncObjType() {
+ return BaseAsyncCmd.networkSyncObject;
+ }
+
+ @Override
+ public Long getSyncObjId() {
+ return _firewallService.getFirewallRule(id).getNetworkId();
+ }
+
+ @Override
+ public AsyncJob.Type getInstanceType() {
+ return AsyncJob.Type.FirewallRule;
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/network/DeleteNetworkCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/network/DeleteNetworkCmd.java b/api/src/org/apache/cloudstack/api/command/user/network/DeleteNetworkCmd.java
new file mode 100644
index 0000000..df070ff
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/network/DeleteNetworkCmd.java
@@ -0,0 +1,105 @@
+// 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 org.apache.cloudstack.api.command.user.network;
+
+import org.apache.cloudstack.api.*;
+import org.apache.cloudstack.api.command.admin.network.DeleteNetworkOfferingCmd;
+import org.apache.cloudstack.api.response.NetworkResponse;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import com.cloud.event.EventTypes;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.network.Network;
+import com.cloud.user.UserContext;
+
+@APICommand(name = "deleteNetwork", description="Deletes a network", responseObject=SuccessResponse.class)
+public class DeleteNetworkCmd extends BaseAsyncCmd{
+ public static final Logger s_logger = Logger.getLogger(DeleteNetworkOfferingCmd.class.getName());
+ private static final String s_name = "deletenetworkresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+
+ @Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType = NetworkResponse.class,
+ required=true, description="the ID of the network")
+ private Long id;
+
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public Long getId() {
+ return id;
+ }
+
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public void execute(){
+ UserContext.current().setEventDetails("Network Id: " + id);
+ boolean result = _networkService.deleteNetwork(id);
+ if (result) {
+ SuccessResponse response = new SuccessResponse(getCommandName());
+ this.setResponseObject(response);
+ } else {
+ throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to delete network");
+ }
+ }
+
+
+ @Override
+ public String getSyncObjType() {
+ return BaseAsyncCmd.networkSyncObject;
+ }
+
+ @Override
+ public Long getSyncObjId() {
+ return id;
+ }
+
+ @Override
+ public String getEventType() {
+ return EventTypes.EVENT_NETWORK_DELETE;
+ }
+
+ @Override
+ public String getEventDescription() {
+ return "Deleting network: " + id;
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ Network network = _networkService.getNetwork(id);
+ if (network == null) {
+ throw new InvalidParameterValueException("Networkd id=" + id + " doesn't exist");
+ } else {
+ return _networkService.getNetwork(id).getAccountId();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/network/ListNetworkACLsCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/network/ListNetworkACLsCmd.java b/api/src/org/apache/cloudstack/api/command/user/network/ListNetworkACLsCmd.java
new file mode 100644
index 0000000..f556ce1
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/network/ListNetworkACLsCmd.java
@@ -0,0 +1,94 @@
+// 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 org.apache.cloudstack.api.command.user.network;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseListTaggedResourcesCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.ListResponse;
+import org.apache.cloudstack.api.response.NetworkACLResponse;
+import org.apache.cloudstack.api.response.FirewallRuleResponse;
+import org.apache.cloudstack.api.response.NetworkResponse;
+import com.cloud.network.rules.FirewallRule;
+import com.cloud.utils.Pair;
+
+@APICommand(name = "listNetworkACLs", description="Lists all network ACLs", responseObject=NetworkACLResponse.class)
+public class ListNetworkACLsCmd extends BaseListTaggedResourcesCmd {
+ public static final Logger s_logger = Logger.getLogger(ListNetworkACLsCmd.class.getName());
+
+ private static final String s_name = "listnetworkaclsresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+ @Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType = FirewallRuleResponse.class,
+ description="Lists network ACL with the specified ID.")
+ private Long id;
+
+ @Parameter(name=ApiConstants.NETWORK_ID, type=CommandType.UUID, entityType = NetworkResponse.class,
+ description="list network ACLs by network Id")
+ private Long networkId;
+
+ @Parameter(name=ApiConstants.TRAFFIC_TYPE, type=CommandType.STRING, description="list network ACLs by traffic type - Ingress or Egress")
+ private String trafficType;
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public Long getNetworkId() {
+ return networkId;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public String getTrafficType() {
+ return trafficType;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+
+ @Override
+ public String getCommandName() {
+ return s_name;
+ }
+
+ @Override
+ public void execute(){
+ Pair<List<? extends FirewallRule>,Integer> result = _networkACLService.listNetworkACLs(this);
+ ListResponse<NetworkACLResponse> response = new ListResponse<NetworkACLResponse>();
+ List<NetworkACLResponse> aclResponses = new ArrayList<NetworkACLResponse>();
+
+ for (FirewallRule acl : result.first()) {
+ NetworkACLResponse ruleData = _responseGenerator.createNetworkACLResponse(acl);
+ aclResponses.add(ruleData);
+ }
+ response.setResponses(aclResponses, result.second());
+ response.setResponseName(getCommandName());
+ this.setResponseObject(response);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/network/ListNetworkOfferingsCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/network/ListNetworkOfferingsCmd.java b/api/src/org/apache/cloudstack/api/command/user/network/ListNetworkOfferingsCmd.java
new file mode 100644
index 0000000..94bc71d
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/network/ListNetworkOfferingsCmd.java
@@ -0,0 +1,191 @@
+// 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 org.apache.cloudstack.api.command.user.network;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseListCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.ListResponse;
+import org.apache.cloudstack.api.response.NetworkOfferingResponse;
+import org.apache.cloudstack.api.response.NetworkResponse;
+import org.apache.cloudstack.api.response.ZoneResponse;
+import com.cloud.offering.NetworkOffering;
+
+@APICommand(name = "listNetworkOfferings", description="Lists all available network offerings.", responseObject=NetworkOfferingResponse.class)
+public class ListNetworkOfferingsCmd extends BaseListCmd {
+ public static final Logger s_logger = Logger.getLogger(ListNetworkOfferingsCmd.class.getName());
+ private static final String _name = "listnetworkofferingsresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+ @Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType = NetworkOfferingResponse.class,
+ description="list network offerings by id")
+ private Long id;
+
+ @Parameter(name=ApiConstants.NAME, type=CommandType.STRING, description="list network offerings by name")
+ private String networkOfferingName;
+
+ @Parameter(name=ApiConstants.DISPLAY_TEXT, type=CommandType.STRING, description="list network offerings by display text")
+ private String displayText;
+
+ @Parameter(name=ApiConstants.TRAFFIC_TYPE, type=CommandType.STRING, description="list by traffic type")
+ private String trafficType;
+
+ @Parameter(name=ApiConstants.IS_DEFAULT, type=CommandType.BOOLEAN, description="true if need to list only default network offerings. Default value is false")
+ private Boolean isDefault;
+
+ @Parameter(name=ApiConstants.SPECIFY_VLAN, type=CommandType.BOOLEAN, description="the tags for the network offering.")
+ private Boolean specifyVlan;
+
+ @Parameter(name=ApiConstants.AVAILABILITY, type=CommandType.STRING, description="the availability of network offering. Default value is Required")
+ private String availability;
+
+ @Parameter(name=ApiConstants.ZONE_ID, type=CommandType.UUID, entityType = ZoneResponse.class,
+ description="list netowrk offerings available for network creation in specific zone")
+ private Long zoneId;
+
+ @Parameter(name=ApiConstants.STATE, type=CommandType.STRING, description="list network offerings by state")
+ private String state;
+
+ @Parameter(name=ApiConstants.NETWORK_ID, type=CommandType.UUID, entityType = NetworkResponse.class,
+ description="the ID of the network. Pass this in if you want to see the available network offering that a network can be changed to.")
+ private Long networkId;
+
+ @Parameter(name=ApiConstants.GUEST_IP_TYPE, type=CommandType.STRING, description="list network offerings by guest type: Shared or Isolated")
+ private String guestIpType;
+
+ @Parameter(name=ApiConstants.SUPPORTED_SERVICES, type=CommandType.LIST, collectionType=CommandType.STRING, description="list network offerings supporting certain services")
+ private List<String> supportedServices;
+
+ @Parameter(name=ApiConstants.SOURCE_NAT_SUPPORTED, type=CommandType.BOOLEAN, description="true if need to list only netwok offerings where source nat is supported, false otherwise")
+ private Boolean sourceNatSupported;
+
+ @Parameter(name=ApiConstants.SPECIFY_IP_RANGES, type=CommandType.BOOLEAN, description="true if need to list only network offerings which support specifying ip ranges")
+ private Boolean specifyIpRanges;
+
+ @Parameter(name=ApiConstants.TAGS, type=CommandType.STRING, description="list network offerings by tags", length=4096)
+ private String tags;
+
+ @Parameter(name=ApiConstants.IS_TAGGED, type=CommandType.BOOLEAN, description="true if offering has tags specified")
+ private Boolean isTagged;
+
+ @Parameter(name=ApiConstants.FOR_VPC, type=CommandType.BOOLEAN, description="the network offering can be used" +
+ " only for network creation inside the VPC")
+ private Boolean forVpc;
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public String getNetworkOfferingName() {
+ return networkOfferingName;
+ }
+
+ public String getDisplayText() {
+ return displayText;
+ }
+
+ public String getTrafficType() {
+ return trafficType;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public Boolean getIsDefault() {
+ return isDefault;
+ }
+
+ public Boolean getSpecifyVlan() {
+ return specifyVlan;
+ }
+
+ public String getAvailability() {
+ return availability;
+ }
+
+ public Long getZoneId() {
+ return zoneId;
+ }
+
+ public String getState() {
+ return state;
+ }
+
+ public Long getNetworkId() {
+ return networkId;
+ }
+
+ public String getGuestIpType() {
+ return guestIpType;
+ }
+
+ public List<String> getSupportedServices() {
+ return supportedServices;
+ }
+
+ public Boolean getSourceNatSupported() {
+ return sourceNatSupported;
+ }
+
+ public Boolean getSpecifyIpRanges() {
+ return specifyIpRanges;
+ }
+
+ public String getTags() {
+ return tags;
+ }
+
+ public Boolean isTagged() {
+ return isTagged;
+ }
+
+ public Boolean getForVpc() {
+ return forVpc;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+ @Override
+ public String getCommandName() {
+ return _name;
+ }
+
+ @Override
+ public void execute(){
+ List<? extends NetworkOffering> offerings = _configService.searchForNetworkOfferings(this);
+ ListResponse<NetworkOfferingResponse> response = new ListResponse<NetworkOfferingResponse>();
+ List<NetworkOfferingResponse> offeringResponses = new ArrayList<NetworkOfferingResponse>();
+ for (NetworkOffering offering : offerings) {
+ NetworkOfferingResponse offeringResponse = _responseGenerator.createNetworkOfferingResponse(offering);
+ offeringResponses.add(offeringResponse);
+ }
+
+ response.setResponses(offeringResponses);
+ response.setResponseName(getCommandName());
+ this.setResponseObject(response);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/c4c9d2d8/api/src/org/apache/cloudstack/api/command/user/network/ListNetworksCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/user/network/ListNetworksCmd.java b/api/src/org/apache/cloudstack/api/command/user/network/ListNetworksCmd.java
new file mode 100644
index 0000000..1f366c2
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/network/ListNetworksCmd.java
@@ -0,0 +1,165 @@
+// 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 org.apache.cloudstack.api.command.user.network;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseListTaggedResourcesCmd;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.ListResponse;
+import org.apache.cloudstack.api.response.NetworkResponse;
+import org.apache.cloudstack.api.response.PhysicalNetworkResponse;
+import org.apache.cloudstack.api.response.VpcResponse;
+import org.apache.cloudstack.api.response.ZoneResponse;
+import com.cloud.network.Network;
+
+@APICommand(name = "listNetworks", description="Lists all available networks.", responseObject=NetworkResponse.class)
+public class ListNetworksCmd extends BaseListTaggedResourcesCmd {
+ public static final Logger s_logger = Logger.getLogger(ListNetworksCmd.class.getName());
+ private static final String _name = "listnetworksresponse";
+
+ /////////////////////////////////////////////////////
+ //////////////// API parameters /////////////////////
+ /////////////////////////////////////////////////////
+ @Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType = NetworkResponse.class,
+ description="list networks by id")
+ private Long id;
+
+ @Parameter(name=ApiConstants.ZONE_ID, type=CommandType.UUID, entityType = ZoneResponse.class,
+ description="the Zone ID of the network")
+ private Long zoneId;
+
+ @Parameter(name=ApiConstants.TYPE, type=CommandType.STRING, description="the type of the network. Supported values are: Isolated and Shared")
+ private String guestIpType;
+
+ @Parameter(name=ApiConstants.IS_SYSTEM, type=CommandType.BOOLEAN, description="true if network is system, false otherwise")
+ private Boolean isSystem;
+
+ @Parameter(name=ApiConstants.ACL_TYPE, type=CommandType.STRING, description="list networks by ACL (access control list) type. Supported values are Account and Domain")
+ private String aclType;
+
+ @Parameter(name=ApiConstants.TRAFFIC_TYPE, type=CommandType.STRING, description="type of the traffic")
+ private String trafficType;
+
+ @Parameter(name=ApiConstants.PHYSICAL_NETWORK_ID, type=CommandType.UUID, entityType = PhysicalNetworkResponse.class,
+ description="list networks by physical network id")
+ private Long physicalNetworkId;
+
+ @Parameter(name=ApiConstants.SUPPORTED_SERVICES, type=CommandType.LIST, collectionType=CommandType.STRING, description="list networks supporting certain services")
+ private List<String> supportedServices;
+
+ @Parameter(name=ApiConstants.RESTART_REQUIRED, type=CommandType.BOOLEAN, description="list networks by restartRequired")
+
+ private Boolean restartRequired;
+
+ @Parameter(name=ApiConstants.SPECIFY_IP_RANGES, type=CommandType.BOOLEAN, description="true if need to list only networks which support specifying ip ranges")
+ private Boolean specifyIpRanges;
+
+ @Parameter(name=ApiConstants.VPC_ID, type=CommandType.UUID, entityType = VpcResponse.class,
+ description="List networks by VPC")
+ private Long vpcId;
+
+ @Parameter(name=ApiConstants.CAN_USE_FOR_DEPLOY, type=CommandType.BOOLEAN, description="list networks available for vm deployment")
+ private Boolean canUseForDeploy;
+
+ @Parameter(name=ApiConstants.FOR_VPC, type=CommandType.BOOLEAN, description="the network belongs to vpc")
+ private Boolean forVpc;
+
+ /////////////////////////////////////////////////////
+ /////////////////// Accessors ///////////////////////
+ /////////////////////////////////////////////////////
+
+ public Long getId() {
+ return id;
+ }
+
+ public Long getZoneId() {
+ return zoneId;
+ }
+
+ public String getGuestIpType() {
+ return guestIpType;
+ }
+
+ public Boolean getIsSystem() {
+ return isSystem;
+ }
+
+ public String getAclType() {
+ return aclType;
+ }
+
+ public String getTrafficType() {
+ return trafficType;
+ }
+
+ public Long getPhysicalNetworkId() {
+ return physicalNetworkId;
+ }
+
+ public List<String> getSupportedServices() {
+ return supportedServices;
+ }
+
+ public Boolean getRestartRequired() {
+ return restartRequired;
+ }
+
+ public Boolean getSpecifyIpRanges() {
+ return specifyIpRanges;
+ }
+
+ public Long getVpcId() {
+ return vpcId;
+ }
+
+ public Boolean canUseForDeploy() {
+ return canUseForDeploy;
+ }
+
+ public Boolean getForVpc() {
+ return forVpc;
+ }
+
+ /////////////////////////////////////////////////////
+ /////////////// API Implementation///////////////////
+ /////////////////////////////////////////////////////
+ @Override
+ public String getCommandName() {
+ return _name;
+ }
+
+ @Override
+ public void execute(){
+ List<? extends Network> networks = _networkService.searchForNetworks(this);
+ ListResponse<NetworkResponse> response = new ListResponse<NetworkResponse>();
+ List<NetworkResponse> networkResponses = new ArrayList<NetworkResponse>();
+ for (Network network : networks) {
+ NetworkResponse networkResponse = _responseGenerator.createNetworkResponse(network);
+ networkResponses.add(networkResponse);
+ }
+
+ response.setResponses(networkResponses);
+ response.setResponseName(getCommandName());
+ this.setResponseObject(response);
+ }
+}