WIP rename device to resource to better reflect nature of VNMC

Signed-off-by: Chiradeep Vittal <chiradeep@apache.org>
This commit is contained in:
Chiradeep Vittal 2013-01-16 15:33:01 -08:00
parent 84d218f972
commit 1ae21ea49a
15 changed files with 988 additions and 842 deletions

View File

@ -1,4 +1,4 @@
for ((vlanid=2002; vlanid <=2030; vlanid++));
for ((vlanid=2000; vlanid <=2020; vlanid++));
do
#sed "s/vlanid/$vlanid/g" vsm_asa_inside_profiles;
sed "s/vlanid/$vlanid/g" vservice_node ;

View File

@ -26,25 +26,21 @@ import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.PlugService;
import com.cloud.api.ServerApiException;
import com.cloud.api.BaseCmd.CommandType;
import com.cloud.api.response.CiscoVnmcDeviceResponse;
import com.cloud.event.EventTypes;
import com.cloud.api.response.CiscoVnmcResourceResponse;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.NetworkRuleConflictException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.network.ExternalLoadBalancerDeviceVO;
import com.cloud.network.cisco.CiscoVnmcDeviceVO;
import com.cloud.network.cisco.CiscoVnmcResourceVO;
import com.cloud.network.element.CiscoVnmcElementService;
import com.cloud.user.UserContext;
import com.cloud.utils.exception.CloudRuntimeException;
@Implementation(responseObject=CiscoVnmcDeviceResponse.class, description="Adds a Cisco Vnmc Controller")
public class AddCiscoVnmcDeviceCmd extends BaseCmd {
private static final Logger s_logger = Logger.getLogger(AddCiscoVnmcDeviceCmd.class.getName());
private static final String s_name = "addCiscoVnmcdevice";
@Implementation(responseObject=CiscoVnmcResourceResponse.class, description="Adds a Cisco Vnmc Controller")
public class AddCiscoVnmcResourceCmd extends BaseCmd {
private static final Logger s_logger = Logger.getLogger(AddCiscoVnmcResourceCmd.class.getName());
private static final String s_name = "addCiscoVnmcResource";
@PlugService CiscoVnmcElementService _ciscoVnmcElementService;
/////////////////////////////////////////////////////
@ -55,13 +51,13 @@ public class AddCiscoVnmcDeviceCmd extends BaseCmd {
@Parameter(name=ApiConstants.PHYSICAL_NETWORK_ID, type=CommandType.LONG, required=true, description="the Physical Network ID")
private Long physicalNetworkId;
@Parameter(name=ApiConstants.HOST_NAME, type=CommandType.STRING, required = true, description="Hostname of ip address of the ciscoVnmc NVP Controller.")
@Parameter(name=ApiConstants.HOST_NAME, type=CommandType.STRING, required = true, description="Hostname of ip address of the Cisco VNMC Controller.")
private String host;
@Parameter(name=ApiConstants.USERNAME, type=CommandType.STRING, required = true, description="Credentials to access the ciscoVnmc Controller API")
@Parameter(name=ApiConstants.USERNAME, type=CommandType.STRING, required = true, description="Credentials to access the Cisco VNMC Controller API")
private String username;
@Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, required = true, description="Credentials to access the ciscoVnmc Controller API")
@Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, required = true, description="Credentials to access the Cisco VNMC Controller API")
private String password;
@ -93,14 +89,14 @@ public class AddCiscoVnmcDeviceCmd extends BaseCmd {
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException {
try {
CiscoVnmcDeviceVO CiscoVnmcDeviceVO = _ciscoVnmcElementService.addCiscoVnmcDevice(this);
if (CiscoVnmcDeviceVO != null) {
CiscoVnmcDeviceResponse response = _ciscoVnmcElementService.createCiscoVnmcDeviceResponse(CiscoVnmcDeviceVO);
response.setObjectName("CiscoVnmcdevice");
CiscoVnmcResourceVO CiscoVnmcResourceVO = _ciscoVnmcElementService.addCiscoVnmcResource(this);
if (CiscoVnmcResourceVO != null) {
CiscoVnmcResourceResponse response = _ciscoVnmcElementService.createCiscoVnmcResourceResponse(CiscoVnmcResourceVO);
response.setObjectName("CiscoVnmcResource");
response.setResponseName(getCommandName());
this.setResponseObject(response);
} else {
throw new ServerApiException(BaseAsyncCmd.INTERNAL_ERROR, "Failed to add ciscoVnmc NVP device due to internal error.");
throw new ServerApiException(BaseAsyncCmd.INTERNAL_ERROR, "Failed to add Cisco VNMC controller due to internal error.");
}
} catch (InvalidParameterValueException invalidParamExcp) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, invalidParamExcp.getMessage());

View File

@ -35,26 +35,26 @@ import com.cloud.network.element.CiscoVnmcElementService;
import com.cloud.user.UserContext;
import com.cloud.utils.exception.CloudRuntimeException;
@Implementation(responseObject=SuccessResponse.class, description=" delete a ciscoVnmc nvp device")
public class DeleteCiscoVnmcDeviceCmd extends BaseCmd {
private static final Logger s_logger = Logger.getLogger(DeleteCiscoVnmcDeviceCmd.class.getName());
private static final String s_name = "addCiscoVnmcdevice";
@Implementation(responseObject=SuccessResponse.class, description=" delete a Cisco Vnmc Resource")
public class DeleteCiscoVnmcResourceCmd extends BaseCmd {
private static final Logger s_logger = Logger.getLogger(DeleteCiscoVnmcResourceCmd.class.getName());
private static final String s_name = "addCiscoVnmcResource";
@PlugService CiscoVnmcElementService _ciscoVnmcElementService;
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@IdentityMapper(entityTableName="external_ciscoVnmc_nvp_devices")
@Parameter(name=ApiConstants.DEVICE_ID, type=CommandType.LONG, required=true, description="Cisco Vnmc device ID")
private Long CiscoVnmcDeviceId;
@IdentityMapper(entityTableName="external_cisco_vnmc_resources")
@Parameter(name=ApiConstants.RESOURCE_ID, type=CommandType.LONG, required=true, description="Cisco Vnmc resource ID")
private Long CiscoVnmcResourceId;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
public Long getCiscoVnmcDeviceId() {
return CiscoVnmcDeviceId;
public Long getCiscoVnmcResourceId() {
return CiscoVnmcResourceId;
}
/////////////////////////////////////////////////////
@ -64,13 +64,13 @@ public class DeleteCiscoVnmcDeviceCmd extends BaseCmd {
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException {
try {
boolean result = _ciscoVnmcElementService.deleteCiscoVnmcDevice(this);
boolean result = _ciscoVnmcElementService.deleteCiscoVnmcResource(this);
if (result) {
SuccessResponse response = new SuccessResponse(getCommandName());
response.setResponseName(getCommandName());
this.setResponseObject(response);
} else {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to delete ciscoVnmc device.");
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to delete Cisco Vnmc resource.");
}
} catch (InvalidParameterValueException invalidParamExcp) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, invalidParamExcp.getMessage());

View File

@ -30,20 +30,20 @@ import com.cloud.api.Parameter;
import com.cloud.api.PlugService;
import com.cloud.api.ServerApiException;
import com.cloud.api.response.ListResponse;
import com.cloud.api.response.CiscoVnmcDeviceResponse;
import com.cloud.api.response.CiscoVnmcResourceResponse;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.network.cisco.CiscoVnmcDeviceVO;
import com.cloud.network.cisco.CiscoVnmcResourceVO;
import com.cloud.network.element.CiscoVnmcElementService;
import com.cloud.utils.exception.CloudRuntimeException;
@Implementation(responseObject=CiscoVnmcDeviceResponse.class, description="Lists ciscoVnmc NVP devices")
public class ListCiscoVnmcDevicesCmd extends BaseListCmd {
private static final Logger s_logger = Logger.getLogger(ListCiscoVnmcDevicesCmd.class.getName());
private static final String s_name = "listCiscoVnmcDevices";
@Implementation(responseObject=CiscoVnmcResourceResponse.class, description="Lists Cisco VNMC controllers")
public class ListCiscoVnmcResourcesCmd extends BaseListCmd {
private static final Logger s_logger = Logger.getLogger(ListCiscoVnmcResourcesCmd.class.getName());
private static final String s_name = "listCiscoVnmcResources";
@PlugService CiscoVnmcElementService _ciscoVnmcElementService;
/////////////////////////////////////////////////////
@ -54,16 +54,16 @@ public class ListCiscoVnmcDevicesCmd extends BaseListCmd {
@Parameter(name=ApiConstants.PHYSICAL_NETWORK_ID, type=CommandType.LONG, description="the Physical Network ID")
private Long physicalNetworkId;
@IdentityMapper(entityTableName="external_cisco_vnmc_devices")
@Parameter(name=ApiConstants.DEVICE_ID, type=CommandType.LONG, description="Cisco Vnmc device ID")
private Long CiscoVnmcDeviceId;
@IdentityMapper(entityTableName="external_cisco_vnmc_resources")
@Parameter(name=ApiConstants.RESOURCE_ID, type=CommandType.LONG, description="Cisco VNMC resource ID")
private Long ciscoVnmcResourceId;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
public Long getCiscoVnmcDeviceId() {
return CiscoVnmcDeviceId;
public Long getCiscoVnmcResourceId() {
return ciscoVnmcResourceId;
}
public Long getPhysicalNetworkId() {
@ -77,18 +77,18 @@ public class ListCiscoVnmcDevicesCmd extends BaseListCmd {
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException {
try {
List<CiscoVnmcDeviceVO> ciscoVnmcDevices = _ciscoVnmcElementService.listCiscoVnmcDevices(this);
ListResponse<CiscoVnmcDeviceResponse> response = new ListResponse<CiscoVnmcDeviceResponse>();
List<CiscoVnmcDeviceResponse> ciscoVnmcDevicesResponse = new ArrayList<CiscoVnmcDeviceResponse>();
List<CiscoVnmcResourceVO> CiscoVnmcResources = _ciscoVnmcElementService.listCiscoVnmcResources(this);
ListResponse<CiscoVnmcResourceResponse> response = new ListResponse<CiscoVnmcResourceResponse>();
List<CiscoVnmcResourceResponse> CiscoVnmcResourcesResponse = new ArrayList<CiscoVnmcResourceResponse>();
if (ciscoVnmcDevices != null && !ciscoVnmcDevices.isEmpty()) {
for (CiscoVnmcDeviceVO ciscoVnmcDeviceVO : ciscoVnmcDevices) {
CiscoVnmcDeviceResponse ciscoVnmcDeviceResponse = _ciscoVnmcElementService.createCiscoVnmcDeviceResponse(ciscoVnmcDeviceVO);
ciscoVnmcDevicesResponse.add(ciscoVnmcDeviceResponse);
if (CiscoVnmcResources != null && !CiscoVnmcResources.isEmpty()) {
for (CiscoVnmcResourceVO CiscoVnmcResourceVO : CiscoVnmcResources) {
CiscoVnmcResourceResponse CiscoVnmcResourceResponse = _ciscoVnmcElementService.createCiscoVnmcResourceResponse(CiscoVnmcResourceVO);
CiscoVnmcResourcesResponse.add(CiscoVnmcResourceResponse);
}
}
response.setResponses(ciscoVnmcDevicesResponse);
response.setResponses(CiscoVnmcResourcesResponse);
response.setResponseName(getCommandName());
this.setResponseObject(response);
} catch (InvalidParameterValueException invalidParamExcp) {

View File

@ -21,21 +21,36 @@ import com.cloud.serializer.Param;
import com.cloud.utils.IdentityProxy;
import com.google.gson.annotations.SerializedName;
public class CiscoVnmcDeviceResponse extends BaseResponse {
@SerializedName(ApiConstants.DEVICE_ID) @Param(description="device id of the Cisco VNMC controller")
private IdentityProxy id = new IdentityProxy("external_devices");
public class CiscoVnmcResourceResponse extends BaseResponse {
public static final String RESOURCE_NAME = "resourcename";
@SerializedName(ApiConstants.RESOURCE_ID) @Param(description="resource id of the Cisco VNMC controller")
private IdentityProxy id = new IdentityProxy("external_cisco_vnmc_resources");
@SerializedName(ApiConstants.PHYSICAL_NETWORK_ID) @Param(description="the physical network to which this Nirica Nvp belongs to")
@SerializedName(ApiConstants.PHYSICAL_NETWORK_ID) @Param(description="the physical network to which this VNMC belongs to")
private IdentityProxy physicalNetworkId = new IdentityProxy("physical_network");
@SerializedName(ApiConstants.PROVIDER) @Param(description="name of the provider")
public IdentityProxy getPhysicalNetworkId() {
return physicalNetworkId;
}
public String getProviderName() {
return providerName;
}
public String getResourceName() {
return resourceName;
}
@SerializedName(ApiConstants.PROVIDER) @Param(description="name of the provider")
private String providerName;
@SerializedName(ApiConstants.DEVICE_NAME) @Param(description="device name")
private String deviceName;
@SerializedName(RESOURCE_NAME)
@Param(description="Cisco VNMC resource name")
private String resourceName;
public void setId(long nvpDeviceId) {
this.id.setValue(nvpDeviceId);
public void setId(long ciscoVnmcResourceId) {
this.id.setValue(ciscoVnmcResourceId);
}
public void setPhysicalNetworkId(long physicalNetworkId) {
@ -46,8 +61,8 @@ public class CiscoVnmcDeviceResponse extends BaseResponse {
this.providerName = providerName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
public void setResourceName(String resourceName) {
this.resourceName = resourceName;
}
}

View File

@ -0,0 +1,60 @@
package com.cloud.network.cisco;
import java.util.List;
import com.cloud.utils.exception.ExecutionException;
public interface CiscoVnmcConnection {
public boolean createTenant(String tenantName) throws ExecutionException;
public boolean createTenantVDC(String tenantName) throws ExecutionException;
public boolean createTenantVDCEdgeDeviceProfile(String tenantName)
throws ExecutionException;
public boolean createTenantVDCEdgeStaticRoutePolicy(String tenantName)
throws ExecutionException;
public boolean createTenantVDCEdgeStaticRoute(String tenantName,
String nextHopIp, String outsideIntf, String destination,
String netmask) throws ExecutionException;
public boolean associateTenantVDCEdgeStaticRoutePolicy(String tenantName)
throws ExecutionException;
public boolean associateTenantVDCEdgeDhcpPolicy(String tenantName,
String intfName) throws ExecutionException;
public boolean createTenantVDCEdgeDhcpPolicy(String tenantName,
String startIp, String endIp, String subnet, String nameServerIp,
String domain) throws ExecutionException;
public boolean associateTenantVDCEdgeDhcpServerPolicy(String tenantName,
String intfName) throws ExecutionException;
public boolean createTenantVDCEdgeSecurityProfile(String tenantName)
throws ExecutionException;
public boolean createTenantVDCSourceNATPool(String tenantName,
String publicIp) throws ExecutionException;
public boolean createTenantVDCSourceNATPolicy(String tenantName,
String startSourceIp, String endSourceIp) throws ExecutionException;
public boolean createTenantVDCNatPolicySet(String tenantName)
throws ExecutionException;
public boolean associateNatPolicySet(String tenantName)
throws ExecutionException;
public boolean createEdgeFirewall(String tenantName, String publicIp,
String insideIp, String insideSubnet, String outsideSubnet)
throws ExecutionException;
public List<String> listUnAssocAsa1000v() throws ExecutionException;
public boolean assocAsa1000v(String tenantName, String firewallDn)
throws ExecutionException;
}

View File

@ -0,0 +1,757 @@
package com.cloud.network.cisco;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import com.cloud.utils.exception.ExecutionException;
import com.cloud.utils.script.Script;
public class CiscoVnmcConnectionImpl implements CiscoVnmcConnection {
private String _ip;
private String _username;
private String _password;
private String _cookie;
private final Logger s_logger = Logger.getLogger(CiscoVnmcConnectionImpl.class);
private enum VnmcXml {
LOGIN("login.xml", "mgmt-controller"),
CREATE_TENANT("create-tenant.xml", "service-reg"),
CREATE_VDC("create-vdc.xml", "service-reg"),
CREATE_EDGE_DEVICE_PROFILE("create-edge-device-profile.xml", "policy-mgr"),
CREATE_EDGE_ROUTE_POLICY("create-edge-device-route-policy.xml", "policy-mgr"),
CREATE_EDGE_ROUTE("create-edge-device-route.xml", "policy-mgr"),
RESOLVE_EDGE_ROUTE_POLICY("associate-route-policy.xml", "policy-mgr"),
RESOLVE_EDGE_DHCP_POLICY("associate-dhcp-policy.xml", "policy-mgr"),
CREATE_DHCP_POLICY("create-dhcp-policy.xml", "policy-mgr"),
RESOLVE_EDGE_DHCP_SERVER_POLICY("associate-dhcp-server.xml", "policy-mgr"),
CREATE_EDGE_SECURITY_PROFILE("create-edge-security-profile.xml", "policy-mgr"),
CREATE_SOURCE_NAT_POOL("create-source-nat-pool.xml", "policy-mgr"),
CREATE_SOURCE_NAT_POLICY("create-source-nat-policy.xml", "policy-mgr"),
CREATE_NAT_POLICY_SET("create-nat-policy-set.xml", "policy-mgr"),
RESOLVE_NAT_POLICY_SET("associate-nat-policy-set.xml", "policy-mgr"),
CREATE_EDGE_FIREWALL("create-edge-firewall.xml", "resource-mgr"),
LIST_UNASSOC_ASA1000V("list-unassigned-asa1000v.xml", "resource-mgr"),
ASSIGN_ASA1000V("assoc-asa1000v.xml", "resource-mgr");
private String scriptsDir = "scripts/network/cisco";
private String xml;
private String service;
private final Logger s_logger = Logger.getLogger(CiscoVnmcConnectionImpl.class);
private VnmcXml(String filename, String service) {
this.xml = getXml(filename);
this.service = service;
}
public String getXml() {
return xml;
}
private String getXml(String filename) {
try {
String xmlFilePath = Script.findScript(scriptsDir, filename);
if (xmlFilePath == null) {
throw new Exception("Failed to find Cisco VNMC XML file: " + filename);
}
FileReader fr = new FileReader(xmlFilePath);
BufferedReader br = new BufferedReader(fr);
String xml = "";
String line;
while ((line = br.readLine()) != null) {
//xml += line.replaceAll("\n"," ");
xml += line;
}
return xml;
} catch (Exception e) {
s_logger.debug(e);
return null;
}
}
public String getService() {
return service;
}
}
public CiscoVnmcConnectionImpl(String hostIp, String userName, String password) {
this._ip = hostIp;
this._username = userName;
this._password = password;
}
public boolean login() throws ExecutionException {
String xml = VnmcXml.LOGIN.getXml();
String service = VnmcXml.LOGIN.getService();
xml = replaceXmlValue(xml, "username", _username);
xml = replaceXmlValue(xml, "password", _password);
String response = sendRequest(service, xml);
Map<String, String> checked = checkResponse(response, "outCookie", "errorCode", "response");
if (checked.get("errorCode") != null)
return false;
_cookie = checked.get("outCookie");
if (_cookie == null) {
return false;
}
return true;
}
private String getDnForTenant(String tenantName) {
return "org-root/org-" + tenantName;
}
private String getDnForTenantVDC(String tenantName) {
return getDnForTenant(tenantName) + "/org-VDC-" + tenantName;
}
private String getDnForTenantVDCEdgeDeviceProfile(String tenantName) {
return getDnForTenantVDC(tenantName) + "/edsp-" + getNameForEdgeDeviceServiceProfile(tenantName);
}
private String getDnForTenantVDCEdgeSecurityProfile(String tenantName) {
return getDnForTenantVDC(tenantName) + "/vnep-" + getNameForEdgeDeviceSecurityProfile(tenantName);
}
private String getDnForEdgeDeviceRoutingPolicy(String tenantName) {
return getDnForTenantVDC(tenantName) + "/routing-policy-" + getNameForEdgeDeviceRoutePolicy(tenantName);
//FIXME: any other construct is unreliable. why?
}
private String getDnForEdgeDeviceRoute(String tenantName, int id) {
return getDnForEdgeDeviceRoutingPolicy(tenantName) + "/sroute-" + id ;
}
private String getDnForDhcpPolicy(String tenantName, String intfName) {
return getDnForTenantVDCEdgeDeviceProfile(tenantName) + "/dhcp-" + intfName;
}
private String getNameForDhcpPolicy(String tenantName) {
return tenantName + "-Dhcp-Policy";
}
private String getNameForDhcpServer(String tenantName) {
return tenantName + "-Dhcp-Server";
}
private String getDnForDhcpServerPolicy(String tenantName) {
return getDnForTenantVDC(tenantName) + "/dhcp-server-" + getNameForDhcpPolicy(tenantName);
}
private String getNameForIpRange() {
return "iprange";
}
private String getDnForDhcpIpRange(String tenantName) {
return getDnForDhcpServerPolicy(tenantName) + "/ip-range-" + getNameForIpRange();
}
private String getNameForDNSService(String tenantName) {
return tenantName + "-DNS";
}
private String getDnForDnsService(String tenantName) {
return getDnForDhcpServerPolicy(tenantName) + "/dns-svc-" + getNameForDNSService(tenantName);
}
private String getDnForDnsServer(String tenantName, String dnsip) {
return getDnForDnsService(tenantName) + "/dns-" + dnsip;
}
private String getNameForTenantVDC(String tenantName) {
return "VDC-" + tenantName;
}
private String getNameForEdgeDeviceServiceProfile(String tenantName) {
return "EDSP-" + tenantName;
}
private String getNameForEdgeDeviceSecurityProfile(String tenantName) {
return "ESP-" + tenantName;
}
private String getNameForEdgeDeviceRoutePolicy(String tenantName) {
return "EDSP-" + tenantName + "-Routes";//FIXME: this has to match DN somehow?
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#createTenant(java.lang.String)
*/
@Override
public boolean createTenant(String tenantName) throws ExecutionException {
String xml = VnmcXml.CREATE_TENANT.getXml();
String service = VnmcXml.CREATE_TENANT.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "descr", "Tenant for account " + tenantName);
xml = replaceXmlValue(xml, "name", tenantName);
xml = replaceXmlValue(xml, "dn", getDnForTenant(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#createTenantVDC(java.lang.String)
*/
@Override
public boolean createTenantVDC(String tenantName) throws ExecutionException {
String xml = VnmcXml.CREATE_VDC.getXml();
String service = VnmcXml.CREATE_VDC.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "descr", "VDC for Tenant" + tenantName);
xml = replaceXmlValue(xml, "name", getNameForTenantVDC(tenantName));
xml = replaceXmlValue(xml, "dn", getDnForTenantVDC(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#createTenantVDCEdgeDeviceProfile(java.lang.String)
*/
@Override
public boolean createTenantVDCEdgeDeviceProfile(String tenantName) throws ExecutionException {
String xml = VnmcXml.CREATE_EDGE_DEVICE_PROFILE.getXml();
String service = VnmcXml.CREATE_EDGE_DEVICE_PROFILE.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "descr", "Edge Device Profile for Tenant VDC" + tenantName);
xml = replaceXmlValue(xml, "name", getNameForEdgeDeviceServiceProfile(tenantName));
xml = replaceXmlValue(xml, "dn", getDnForTenantVDCEdgeDeviceProfile(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#createTenantVDCEdgeStaticRoutePolicy(java.lang.String)
*/
@Override
public boolean createTenantVDCEdgeStaticRoutePolicy(String tenantName) throws ExecutionException {
String xml = VnmcXml.CREATE_EDGE_ROUTE_POLICY.getXml();
String service = VnmcXml.CREATE_EDGE_ROUTE_POLICY.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "name", getNameForEdgeDeviceRoutePolicy(tenantName));//FIXME: this has to match DN somehow?
xml = replaceXmlValue(xml, "routepolicydn", getDnForEdgeDeviceRoutingPolicy(tenantName));
xml = replaceXmlValue(xml, "descr", "Routing Policy for Edge Device for Tenant " + tenantName);
String response = sendRequest(service, xml);
return verifySuccess(response);
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#createTenantVDCEdgeStaticRoute(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public boolean createTenantVDCEdgeStaticRoute(String tenantName,
String nextHopIp, String outsideIntf,
String destination, String netmask) throws ExecutionException {
String xml = VnmcXml.CREATE_EDGE_ROUTE.getXml();
String service = VnmcXml.CREATE_EDGE_ROUTE.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "routedn", getDnForEdgeDeviceRoute(tenantName, 2));//TODO: why 2?
xml = replaceXmlValue(xml, "id", "2"); // TODO:2?
xml = replaceXmlValue(xml, "nexthop", nextHopIp);
xml = replaceXmlValue(xml, "nexthopintf", outsideIntf);
xml = replaceXmlValue(xml, "destination", destination);
xml = replaceXmlValue(xml, "netmask", netmask);
//TODO: this adds default route, make it more generic
String response = sendRequest(service, xml);
return verifySuccess(response);
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#associateTenantVDCEdgeStaticRoutePolicy(java.lang.String)
*/
@Override
public boolean associateTenantVDCEdgeStaticRoutePolicy(String tenantName) throws ExecutionException {
String xml = VnmcXml.RESOLVE_EDGE_ROUTE_POLICY.getXml();
String service = VnmcXml.RESOLVE_EDGE_ROUTE_POLICY.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "profilename", getNameForEdgeDeviceServiceProfile(tenantName));
xml = replaceXmlValue(xml, "profiledn", getDnForTenantVDC(tenantName) + "/edsp-" + getNameForEdgeDeviceServiceProfile(tenantName));
xml = replaceXmlValue(xml, "routepolicyname", getNameForEdgeDeviceRoutePolicy(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#associateTenantVDCEdgeDhcpPolicy(java.lang.String, java.lang.String)
*/
@Override
public boolean associateTenantVDCEdgeDhcpPolicy(String tenantName, String intfName) throws ExecutionException {
String xml = VnmcXml.RESOLVE_EDGE_DHCP_POLICY.getXml();
String service = VnmcXml.RESOLVE_EDGE_DHCP_POLICY.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "dhcpdn", getDnForDhcpPolicy(tenantName, intfName));
xml = replaceXmlValue(xml, "insideintf", intfName);
String response = sendRequest(service, xml);
return verifySuccess(response);
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#createTenantVDCEdgeDhcpPolicy(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public boolean createTenantVDCEdgeDhcpPolicy(String tenantName,
String startIp, String endIp, String subnet, String nameServerIp, String domain) throws ExecutionException {
String xml = VnmcXml.CREATE_DHCP_POLICY.getXml();
String service = VnmcXml.CREATE_DHCP_POLICY.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "dhcpserverdn", getDnForDhcpServerPolicy(tenantName));
xml = replaceXmlValue(xml, "dhcpserverdescr", "DHCP server for " + tenantName);
xml = replaceXmlValue(xml, "dhcpservername", getNameForDhcpPolicy(tenantName));
xml = replaceXmlValue(xml, "iprangedn", getDnForDhcpIpRange(tenantName));
xml = replaceXmlValue(xml, "startip", startIp);
xml = replaceXmlValue(xml, "endip", endIp);
xml = replaceXmlValue(xml, "subnet", subnet);
xml = replaceXmlValue(xml, "domain", domain);
xml = replaceXmlValue(xml, "dnsservicedn", getDnForDnsService(tenantName));
xml = replaceXmlValue(xml, "dnsservicename", getNameForDNSService(tenantName));
xml = replaceXmlValue(xml, "nameserverip", nameServerIp);
xml = replaceXmlValue(xml, "nameserverdn", getDnForDnsServer(tenantName, nameServerIp));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#associateTenantVDCEdgeDhcpServerPolicy(java.lang.String, java.lang.String)
*/
@Override
public boolean associateTenantVDCEdgeDhcpServerPolicy(String tenantName, String intfName) throws ExecutionException {
String xml = VnmcXml.RESOLVE_EDGE_DHCP_SERVER_POLICY.getXml();
String service = VnmcXml.RESOLVE_EDGE_DHCP_SERVER_POLICY.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "dhcpdn", getDnForDhcpPolicy(tenantName, intfName));
xml = replaceXmlValue(xml, "insideintf", intfName);
xml = replaceXmlValue(xml, "dhcpserverpolicyname", getNameForDhcpServer(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#createTenantVDCEdgeSecurityProfile(java.lang.String)
*/
@Override
public boolean createTenantVDCEdgeSecurityProfile(String tenantName) throws ExecutionException {
String xml = VnmcXml.CREATE_EDGE_SECURITY_PROFILE.getXml();
String service = VnmcXml.CREATE_EDGE_SECURITY_PROFILE.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "descr", "Edge Security Profile for Tenant VDC" + tenantName);
xml = replaceXmlValue(xml, "name", getNameForEdgeDeviceSecurityProfile(tenantName));
xml = replaceXmlValue(xml, "espdn", getDnForTenantVDCEdgeSecurityProfile(tenantName));
xml = replaceXmlValue(xml, "egressref", "default-egress");
xml = replaceXmlValue(xml, "ingressref", "default-ingress"); //FIXME: allows everything
String response = sendRequest(service, xml);
return verifySuccess(response);
}
private String getNameForSourceNatPool(String tenantName) {
return "Source-NAT-Pool-For-" + tenantName;
}
private String getDnForSourceNatPool(String tenantName) {
return getDnForTenantVDC(tenantName) + "/objgrp-" + getNameForSourceNatPool(tenantName);
}
private String getDnForSourceNatPoolExpr(String tenantName) {
return getDnForSourceNatPool(tenantName) + "/objgrp-expr-2";
}
private String getDnForSourceNatPublicIp(String tenantName) {
return getDnForSourceNatPoolExpr(tenantName) + "/nw-ip-2";
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#createTenantVDCSourceNATPool(java.lang.String, java.lang.String)
*/
@Override
public boolean createTenantVDCSourceNATPool(String tenantName, String publicIp) throws ExecutionException {
String xml = VnmcXml.CREATE_SOURCE_NAT_POOL.getXml();
String service = VnmcXml.CREATE_SOURCE_NAT_POOL.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "descr", "Source NAT pool for Tenant VDC " + tenantName);
xml = replaceXmlValue(xml, "name", getNameForSourceNatPool(tenantName));
xml = replaceXmlValue(xml, "snatpooldn", getDnForSourceNatPool(tenantName));
xml = replaceXmlValue(xml, "snatpoolexprdn", getDnForSourceNatPoolExpr(tenantName));
xml = replaceXmlValue(xml, "publicipdn", getDnForSourceNatPublicIp(tenantName));
xml = replaceXmlValue(xml, "publicip", publicIp);
String response = sendRequest(service, xml);
return verifySuccess(response);
}
private String getNameForSourceNatPolicy(String tenantName) {
return "Source-NAT-For-" + tenantName;
}
private String getDnForSourceNatPolicy(String tenantName) {
return getDnForTenantVDC(tenantName) + "/natpol-" + getNameForSourceNatPolicy(tenantName);
}
private String getNameForSourceNatRule(String tenantName) {
return "Source-NAT-Policy-Rule-" + tenantName;
}
private String getDnForSourceNatRule(String tenantName) {
return getDnForSourceNatPolicy(tenantName) + "/rule-" + getNameForSourceNatRule(tenantName);
}
private String getDnForSourceNatRuleAction(String tenantName) {
return getDnForSourceNatRule(tenantName) + "/nat-action";
}
private String getDnForSourceNatRuleRule(String tenantName) {
return getDnForSourceNatRule(tenantName) + "/rule-cond-2";
}
private String getDnForSourceNatRuleRange(String tenantName) {
return getDnForSourceNatRuleRule(tenantName) + "/nw-expr2";
}
private String getDnForSourceNatRuleRangeIp(String tenantName, int id) {
return getDnForSourceNatRuleRange(tenantName) + "/nw-ip-" + id;
}
private String getDnForSourceNatRuleRangeAttr(String tenantName) {
return getDnForSourceNatRuleRange(tenantName) + "/nw-attr-qual";
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#createTenantVDCSourceNATPolicy(java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public boolean createTenantVDCSourceNATPolicy(String tenantName,
String startSourceIp, String endSourceIp) throws ExecutionException {
String xml = VnmcXml.CREATE_SOURCE_NAT_POLICY.getXml();
String service = VnmcXml.CREATE_SOURCE_NAT_POLICY.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "descr", "Source NAT Policy for Tenant VDC " + tenantName);
xml = replaceXmlValue(xml, "srcTranslatedIpPool", getNameForSourceNatPool(tenantName));
xml = replaceXmlValue(xml, "natrulename", getNameForSourceNatRule(tenantName));
xml = replaceXmlValue(xml, "natpolname", getNameForSourceNatPolicy(tenantName));
xml = replaceXmlValue(xml, "natruleruledescr", "Source NAT Policy for Tenant " + tenantName);
xml = replaceXmlValue(xml, "natpoldescr", "Source NAT Rule for Tenant " + tenantName);
xml = replaceXmlValue(xml, "natpoldn", getDnForSourceNatPolicy(tenantName));
xml = replaceXmlValue(xml, "natruledn", getDnForSourceNatRule(tenantName));
xml = replaceXmlValue(xml, "sourcestartip", startSourceIp);
xml = replaceXmlValue(xml, "sourceendip", endSourceIp);
xml = replaceXmlValue(xml, "sourcenatpoolname", getNameForSourceNatPool(tenantName));
xml = replaceXmlValue(xml, "natactiondn", getDnForSourceNatRuleAction(tenantName));
xml = replaceXmlValue(xml, "natruleruledn", getDnForSourceNatRuleRule(tenantName));
xml = replaceXmlValue(xml, "natrangedn", getDnForSourceNatRuleRange(tenantName));
xml = replaceXmlValue(xml, "natipdn2", getDnForSourceNatRuleRangeIp(tenantName, 2));
xml = replaceXmlValue(xml, "natipdn3", getDnForSourceNatRuleRangeIp(tenantName, 3));
xml = replaceXmlValue(xml, "natsnatruleconddn", getDnForSourceNatRuleRangeAttr(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
private String getNameForNatPolicySet(String tenantName) {
return "NAT-Policy-Set-" + tenantName;
}
private String getDnForNatPolicySet(String tenantName) {
return getDnForTenantVDC(tenantName) + "/natpset-" + getNameForNatPolicySet(tenantName) ;
}
private String getDnForNatPolicySetRef(String tenantName) {
return getDnForNatPolicySet(tenantName) + "/polref-" + getNameForSourceNatPolicy(tenantName) ;
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#createTenantVDCNatPolicySet(java.lang.String)
*/
@Override
public boolean createTenantVDCNatPolicySet(String tenantName) throws ExecutionException {
String xml = VnmcXml.CREATE_NAT_POLICY_SET.getXml();
String service = VnmcXml.CREATE_NAT_POLICY_SET.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
//xml = replaceXmlValue(xml, "descr", "Nat Policy Set for Tenant VDC " + tenantName);
xml = replaceXmlValue(xml, "natpolicyname", getNameForSourceNatPolicy(tenantName));
xml = replaceXmlValue(xml, "natpolicysetname", getNameForNatPolicySet(tenantName));
xml = replaceXmlValue(xml, "natpolicysetdn", getDnForNatPolicySet(tenantName));
xml = replaceXmlValue(xml, "natpolicyrefdn", getDnForNatPolicySetRef(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#associateNatPolicySet(java.lang.String)
*/
@Override
public boolean associateNatPolicySet(String tenantName) throws ExecutionException {
String xml = VnmcXml.RESOLVE_NAT_POLICY_SET.getXml();
String service = VnmcXml.RESOLVE_NAT_POLICY_SET.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "descr", "Edge Security Profile for Tenant VDC" + tenantName);
xml = replaceXmlValue(xml, "name", getNameForEdgeDeviceSecurityProfile(tenantName));
xml = replaceXmlValue(xml, "espdn", getDnForTenantVDCEdgeSecurityProfile(tenantName));
xml = replaceXmlValue(xml, "egressref", "default-egress");
xml = replaceXmlValue(xml, "ingressref", "default-ingress");
xml = replaceXmlValue(xml, "natpolicysetname", getNameForNatPolicySet(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
private String getNameForEdgeFirewall(String tenantName) {
return "ASA-1000v-" + tenantName;
}
private String getDnForEdgeFirewall(String tenantName) {
return getDnForTenantVDC(tenantName) + "/efw-" + getNameForEdgeFirewall(tenantName);
}
private String getNameForEdgeInsideIntf(String tenantName) {
return "Edge_Inside"; //TODO: make this configurable
}
private String getNameForEdgeOutsideIntf(String tenantName) {
return "Edge_Outside"; //TODO: make this configurable
}
private String getDnForOutsideIntf(String tenantName) {
return getDnForEdgeFirewall(tenantName) + "/interface-" + getNameForEdgeOutsideIntf(tenantName);
}
private String getDnForInsideIntf(String tenantName) {
return getDnForEdgeFirewall(tenantName) + "/interface-" + getNameForEdgeInsideIntf(tenantName);
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#createEdgeFirewall(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public boolean createEdgeFirewall(String tenantName, String publicIp, String insideIp,
String insideSubnet, String outsideSubnet) throws ExecutionException {
String xml = VnmcXml.CREATE_EDGE_FIREWALL.getXml();
String service = VnmcXml.CREATE_EDGE_FIREWALL.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "edgefwdescr", "Edge Firewall for Tenant VDC " + tenantName);
xml = replaceXmlValue(xml, "edgefwname", getNameForEdgeFirewall(tenantName));
xml = replaceXmlValue(xml, "edgefwdn", getDnForEdgeFirewall(tenantName));
xml = replaceXmlValue(xml, "insideintfname", getNameForEdgeInsideIntf(tenantName));
xml = replaceXmlValue(xml, "outsideintfname", getNameForEdgeOutsideIntf(tenantName));
xml = replaceXmlValue(xml, "insideintfdn", getDnForInsideIntf(tenantName));
xml = replaceXmlValue(xml, "outsideintfdn", getDnForOutsideIntf(tenantName));
xml = replaceXmlValue(xml, "deviceserviceprofiledn", getDnForEdgeFirewall(tenantName) + "/device-service-profile");
xml = replaceXmlValue(xml, "outsideintfsp", getDnForOutsideIntf(tenantName) + "/interface-service-profile");
xml = replaceXmlValue(xml, "secprofileref", getNameForEdgeDeviceSecurityProfile(tenantName));
xml = replaceXmlValue(xml, "deviceserviceprofile", getNameForEdgeDeviceServiceProfile(tenantName));
xml = replaceXmlValue(xml, "insideip", insideIp);
xml = replaceXmlValue(xml, "publicip", publicIp);
xml = replaceXmlValue(xml, "insidesubnet", insideSubnet);
xml = replaceXmlValue(xml, "outsidesubnet", outsideSubnet);
String response = sendRequest(service, xml);
return verifySuccess(response);
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#listUnAssocAsa1000v()
*/
@Override
public List<String> listUnAssocAsa1000v() throws ExecutionException {
String xml = VnmcXml.LIST_UNASSOC_ASA1000V.getXml();
String service = VnmcXml.LIST_UNASSOC_ASA1000V.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
String response = sendRequest(service, xml);
List<String> result = new ArrayList<String>();
Document xmlDoc = getDocument(response);
xmlDoc.normalize();
NodeList fwList = xmlDoc.getElementsByTagName("fwInstance");
for (int j=0; j < fwList.getLength(); j++) {
Node fwNode = fwList.item(j);
result.add (fwNode.getAttributes().getNamedItem("dn").getNodeValue());
}
return result;
}
/* (non-Javadoc)
* @see com.cloud.network.resource.CiscoVnmcConnection#assocAsa1000v(java.lang.String, java.lang.String)
*/
@Override
public boolean assocAsa1000v(String tenantName, String firewallDn) throws ExecutionException {
String xml = VnmcXml.ASSIGN_ASA1000V.getXml();
String service = VnmcXml.ASSIGN_ASA1000V.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "binddn", getDnForEdgeFirewall(tenantName) + "/binding");
xml = replaceXmlValue(xml, "fwdn", firewallDn);
String response = sendRequest(service, xml);
return verifySuccess(response);
}
private String sendRequest(String service, String xmlRequest) throws ExecutionException {
org.apache.commons.httpclient.protocol.Protocol myhttps =
new org.apache.commons.httpclient.protocol.Protocol("https", new EasySSLProtocolSocketFactory(), 443);
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(_ip, 443, myhttps);
byte[] response = null;
PostMethod method = new PostMethod("/xmlIM/" + service);
method.setRequestBody(xmlRequest);
try{
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
throw new Exception("Error code : " + statusCode);
}
response = method.getResponseBody();
}catch(Exception e){
System.out.println(e.getMessage());
throw new ExecutionException(e.getMessage());
}
System.out.println(new String(response));
return new String(response);
}
private Map<String, String> checkResponse(String xmlResponse, String... keys) throws ExecutionException {
Document xmlDoc = getDocument(xmlResponse);
Map<String, String> result = new HashMap<String, String>();
Node topElement = xmlDoc.getChildNodes().item(0);
if (topElement != null) {
for (String key: keys){
Node valueNode = topElement.getAttributes().getNamedItem(key);
result.put(key, valueNode==null?null:valueNode.getNodeValue());
}
}
return result;
}
private boolean verifySuccess(String xmlResponse) throws ExecutionException {
Map<String, String> checked = checkResponse(xmlResponse, "errorCode", "errorDescr");
if (checked.get("errorCode") != null) {
String errorCode = checked.get("errorCode");
if (errorCode.equals("103")) {
//tenant already exists
return true;
}
String errorDescr = checked.get("errorDescr");
throw new ExecutionException(errorDescr);
}
return true;
}
/*
* XML utils
*/
private Document getDocument(String xml) throws ExecutionException {
StringReader xmlReader = new StringReader("<?xml version=\"1.0\"?> \n" + xml.trim());
InputSource xmlSource = new InputSource(xmlReader);
Document doc = null;
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlSource);
} catch (Exception e) {
s_logger.error(e);
throw new ExecutionException(e.getMessage());
}
if (doc == null) {
throw new ExecutionException("Failed to parse xml " + xml);
} else {
return doc;
}
}
private String replaceXmlTag(String xml, String oldTag, String newTag) {
return xml.replaceAll(oldTag, newTag);
}
private String replaceXmlValue(String xml, String marker, String value) {
marker = "\\s*%" + marker + "%\\s*";
if (value == null) {
value = "";
}
return xml.replaceAll(marker, value);
}
private String extractXml(String xml, String marker) {
String startMarker = "<" + marker + ">";
String endMarker = "</" + marker + ">";
if (xml.contains(startMarker) && xml.contains(endMarker)) {
return xml.substring(xml.indexOf(startMarker) + startMarker.length(), xml.indexOf(endMarker));
} else {
return null;
}
}
}

View File

@ -1,5 +1,6 @@
package com.cloud.network.cisco;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -8,10 +9,10 @@ import java.util.UUID;
import javax.naming.ConfigurationException;
import com.cloud.agent.AgentManager;
import com.cloud.api.commands.AddCiscoVnmcDeviceCmd;
import com.cloud.api.commands.DeleteCiscoVnmcDeviceCmd;
import com.cloud.api.commands.ListCiscoVnmcDevicesCmd;
import com.cloud.api.response.CiscoVnmcDeviceResponse;
import com.cloud.api.commands.AddCiscoVnmcResourceCmd;
import com.cloud.api.commands.DeleteCiscoVnmcResourceCmd;
import com.cloud.api.commands.ListCiscoVnmcResourcesCmd;
import com.cloud.api.response.CiscoVnmcResourceResponse;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.host.DetailVO;
import com.cloud.host.Host;
@ -53,17 +54,19 @@ public class CiscoVnmcManager implements Manager, CiscoVnmcElementService {
@Inject
NetworkDao _networkDao;
CiscoVnmcConnection _vnmcConnection;
@Override
public String getPropertiesFile() {
return null;
}
@Override
public CiscoVnmcDeviceVO addCiscoVnmcDevice(AddCiscoVnmcDeviceCmd cmd) {
public CiscoVnmcResourceVO addCiscoVnmcResource(AddCiscoVnmcResourceCmd cmd) {
String deviceName = CiscoVnmc.getName();
NetworkDevice networkDevice = NetworkDevice.getNetworkDevice(deviceName);
Long physicalNetworkId = cmd.getPhysicalNetworkId();
CiscoVnmcDeviceVO ciscoVnmcDevice = null;
CiscoVnmcResourceVO CiscoVnmcResource = null;
PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
if (physicalNetwork == null) {
@ -107,14 +110,14 @@ public class CiscoVnmcManager implements Manager, CiscoVnmcElementService {
if (host != null) {
txn.start();
ciscoVnmcDevice = new CiscoVnmcDeviceVO(host.getId(), physicalNetworkId, ntwkSvcProvider.getProviderName(), deviceName);
_ciscoVnmcDao.persist(ciscoVnmcDevice);
CiscoVnmcResource = new CiscoVnmcResourceVO(host.getId(), physicalNetworkId, ntwkSvcProvider.getProviderName(), deviceName);
_ciscoVnmcDao.persist(CiscoVnmcResource);
DetailVO detail = new DetailVO(host.getId(), "deviceid", String.valueOf(ciscoVnmcDevice.getId()));
DetailVO detail = new DetailVO(host.getId(), "deviceid", String.valueOf(CiscoVnmcResource.getId()));
_hostDetailsDao.persist(detail);
txn.commit();
return ciscoVnmcDevice;
return CiscoVnmcResource;
} else {
throw new CloudRuntimeException("Failed to add Cisco Vnmc Device due to internal error.");
}
@ -125,14 +128,14 @@ public class CiscoVnmcManager implements Manager, CiscoVnmcElementService {
}
@Override
public CiscoVnmcDeviceResponse createCiscoVnmcDeviceResponse(
CiscoVnmcDeviceVO ciscoVnmcDeviceVO) {
public CiscoVnmcResourceResponse createCiscoVnmcResourceResponse(
CiscoVnmcResourceVO CiscoVnmcResourceVO) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean deleteCiscoVnmcDevice(DeleteCiscoVnmcDeviceCmd cmd) {
public boolean deleteCiscoVnmcResource(DeleteCiscoVnmcResourceCmd cmd) {
// TODO Auto-generated method stub
return false;
}
@ -140,10 +143,32 @@ public class CiscoVnmcManager implements Manager, CiscoVnmcElementService {
@Override
public List<CiscoVnmcDeviceVO> listCiscoVnmcDevices(
ListCiscoVnmcDevicesCmd cmd) {
// TODO Auto-generated method stub
return null;
public List<CiscoVnmcResourceVO> listCiscoVnmcResources(
ListCiscoVnmcResourcesCmd cmd) {
Long physicalNetworkId = cmd.getPhysicalNetworkId();
Long CiscoVnmcResourceId = cmd.getCiscoVnmcResourceId();
List<CiscoVnmcResourceVO> responseList = new ArrayList<CiscoVnmcResourceVO>();
if (physicalNetworkId == null && CiscoVnmcResourceId == null) {
throw new InvalidParameterValueException("Either physical network Id or vnmc device Id must be specified");
}
if (CiscoVnmcResourceId != null) {
CiscoVnmcResourceVO CiscoVnmcResource = _ciscoVnmcDao.findById(CiscoVnmcResourceId);
if (CiscoVnmcResource == null) {
throw new InvalidParameterValueException("Could not find Cisco Vnmc device with id: " + CiscoVnmcResource);
}
responseList.add(CiscoVnmcResource);
}
else {
PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
if (physicalNetwork == null) {
throw new InvalidParameterValueException("Could not find a physical network with id: " + physicalNetworkId);
}
responseList = _ciscoVnmcDao.listByPhysicalNetwork(physicalNetworkId);
}
return responseList;
}
@Override

View File

@ -27,7 +27,7 @@ import javax.persistence.Table;
@Entity
@Table(name="external_cisco_vnmc_devices")
public class CiscoVnmcDeviceVO {
public class CiscoVnmcResourceVO {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ -50,11 +50,11 @@ public class CiscoVnmcDeviceVO {
private String deviceName;
public CiscoVnmcDeviceVO() {
public CiscoVnmcResourceVO() {
this.uuid = UUID.randomUUID().toString();
}
public CiscoVnmcDeviceVO(long hostId, long physicalNetworkId,
public CiscoVnmcResourceVO(long hostId, long physicalNetworkId,
String providerName, String deviceName) {
super();
this.hostId = hostId;

View File

@ -14,7 +14,7 @@
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.network.resource.test;
package com.cloud.network.cisco.test;
import static org.junit.Assert.assertTrue;
@ -24,21 +24,21 @@ import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import com.cloud.network.resource.CiscoVnmcResource;
import com.cloud.network.cisco.CiscoVnmcConnectionImpl;
import com.cloud.utils.exception.ExecutionException;
public class CiscoVnmcResourceTest {
static CiscoVnmcResource resource;
public class CiscoVnmcConnectionTest {
static CiscoVnmcConnectionImpl connection;
static String tenantName = "TenantE";
static List<String> fwDns = null;
@BeforeClass
public static void setUpClass() throws Exception {
resource = new CiscoVnmcResource("10.223.56.5", "admin", "C1sco123");
connection = new CiscoVnmcConnectionImpl("10.223.56.5", "admin", "C1sco123");
try {
boolean response = resource.login();
boolean response = connection.login();
assertTrue(response);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
@ -51,7 +51,7 @@ public class CiscoVnmcResourceTest {
public void testLogin() {
//fail("Not yet implemented");
try {
boolean response = resource.login();
boolean response = connection.login();
assertTrue(response);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
@ -64,7 +64,7 @@ public class CiscoVnmcResourceTest {
public void testCreateTenant() {
//fail("Not yet implemented");
try {
boolean response = resource.createTenant(tenantName);
boolean response = connection.createTenant(tenantName);
assertTrue(response);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
@ -77,7 +77,7 @@ public class CiscoVnmcResourceTest {
public void testCreateTenantVDC() {
//fail("Not yet implemented");
try {
boolean response = resource.createTenantVDC(tenantName);
boolean response = connection.createTenantVDC(tenantName);
assertTrue(response);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
@ -90,7 +90,7 @@ public class CiscoVnmcResourceTest {
public void testCreateTenantVDCEdgeDeviceProfile() {
//fail("Not yet implemented");
try {
boolean response = resource.createTenantVDCEdgeDeviceProfile(tenantName);
boolean response = connection.createTenantVDCEdgeDeviceProfile(tenantName);
assertTrue(response);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
@ -102,7 +102,7 @@ public class CiscoVnmcResourceTest {
@Test
public void testCreateTenantVDCEdgeDeviceRoutePolicy() {
try {
boolean response = resource.createTenantVDCEdgeStaticRoutePolicy(tenantName);
boolean response = connection.createTenantVDCEdgeStaticRoutePolicy(tenantName);
assertTrue(response);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
@ -114,7 +114,7 @@ public class CiscoVnmcResourceTest {
@Test
public void testCreateTenantVDCEdgeDeviceRoute() {
try {
boolean response = resource.createTenantVDCEdgeStaticRoute(tenantName,
boolean response = connection.createTenantVDCEdgeStaticRoute(tenantName,
"10.223.136.1", "Edge_Outside", "0.0.0.0", "0.0.0.0");
assertTrue(response);
} catch (ExecutionException e) {
@ -127,7 +127,7 @@ public class CiscoVnmcResourceTest {
@Test
public void testAssociateRoutePolicyWithEdgeProfile() {
try {
boolean response = resource.associateTenantVDCEdgeStaticRoutePolicy(tenantName);
boolean response = connection.associateTenantVDCEdgeStaticRoutePolicy(tenantName);
assertTrue(response);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
@ -139,7 +139,7 @@ public class CiscoVnmcResourceTest {
@Test
public void testAssociateTenantVDCEdgeDhcpPolicy() {
try {
boolean response = resource.associateTenantVDCEdgeDhcpPolicy(tenantName, "Edge_Inside");
boolean response = connection.associateTenantVDCEdgeDhcpPolicy(tenantName, "Edge_Inside");
assertTrue(response);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
@ -151,7 +151,7 @@ public class CiscoVnmcResourceTest {
@Test
public void testCreateTenantVDCEdgeDhcpPolicy() {
try {
boolean response = resource.createTenantVDCEdgeDhcpPolicy(tenantName,
boolean response = connection.createTenantVDCEdgeDhcpPolicy(tenantName,
"10.1.1.2", "10.1.1.254", "255.255.255.0","4.4.4.4", tenantName+ ".net");
assertTrue(response);
} catch (ExecutionException e) {
@ -164,7 +164,7 @@ public class CiscoVnmcResourceTest {
@Test
public void testCreateTenantVDCEdgeSecurityProfile() {
try {
boolean response = resource.createTenantVDCEdgeSecurityProfile(tenantName);
boolean response = connection.createTenantVDCEdgeSecurityProfile(tenantName);
assertTrue(response);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
@ -176,7 +176,7 @@ public class CiscoVnmcResourceTest {
@Test
public void testCreateTenantVDCSourceNATPool() {
try {
boolean response = resource.createTenantVDCSourceNATPool(tenantName, "10.223.136.10");
boolean response = connection.createTenantVDCSourceNATPool(tenantName, "10.223.136.10");
assertTrue(response);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
@ -188,7 +188,7 @@ public class CiscoVnmcResourceTest {
@Test
public void testCreateTenantVDCSourceNATPolicy() {
try {
boolean response = resource.createTenantVDCSourceNATPolicy(tenantName, "10.1.1.2", "10.1.1.254");
boolean response = connection.createTenantVDCSourceNATPolicy(tenantName, "10.1.1.2", "10.1.1.254");
assertTrue(response);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
@ -200,7 +200,7 @@ public class CiscoVnmcResourceTest {
@Test
public void testCreateTenantVDCNatPolicySet() {
try {
boolean response = resource.createTenantVDCNatPolicySet(tenantName);
boolean response = connection.createTenantVDCNatPolicySet(tenantName);
assertTrue(response);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
@ -212,7 +212,7 @@ public class CiscoVnmcResourceTest {
@Test
public void testAssociateNatPolicySet() {
try {
boolean response = resource.associateNatPolicySet(tenantName);
boolean response = connection.associateNatPolicySet(tenantName);
assertTrue(response);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
@ -224,7 +224,7 @@ public class CiscoVnmcResourceTest {
@Test
public void testCreateEdgeFirewall() {
try {
boolean response = resource.createEdgeFirewall(tenantName,
boolean response = connection.createEdgeFirewall(tenantName,
"44.44.44.44", "192.168.1.1", "255.255.255.0", "255.255.255.192");
assertTrue(response);
} catch (ExecutionException e) {
@ -235,7 +235,7 @@ public class CiscoVnmcResourceTest {
@Test
public void testListUnassocAsa1000v() {
try {
List<String> response = resource.listUnAssocAsa1000v();
List<String> response = connection.listUnAssocAsa1000v();
assertTrue(response.size() >=0);
fwDns = response;
} catch (ExecutionException e) {
@ -244,10 +244,11 @@ public class CiscoVnmcResourceTest {
}
}
@Ignore
@Test
public void assocAsa1000v() {
try {
boolean result = resource.assocAsa1000v(tenantName, fwDns.get(0));
boolean result = connection.assocAsa1000v(tenantName, fwDns.get(0));
assertTrue(result);
} catch (ExecutionException e) {
// TODO Auto-generated catch block

View File

@ -18,15 +18,15 @@ package com.cloud.network.dao;
import java.util.List;
import com.cloud.network.cisco.CiscoVnmcDeviceVO;
import com.cloud.network.cisco.CiscoVnmcResourceVO;
import com.cloud.utils.db.GenericDao;
public interface CiscoVnmcDao extends GenericDao<CiscoVnmcDeviceVO, Long>{
public interface CiscoVnmcDao extends GenericDao<CiscoVnmcResourceVO, Long>{
/**
* list all the Cisco VNMC devices added in to this physical network
* @param physicalNetworkId physical Network Id
* @return list of CiscoVnmcDeviceVO for this physical network.
*/
List<CiscoVnmcDeviceVO> listByPhysicalNetwork(long physicalNetworkId);
List<CiscoVnmcResourceVO> listByPhysicalNetwork(long physicalNetworkId);
}

View File

@ -20,17 +20,17 @@ import java.util.List;
import javax.ejb.Local;
import com.cloud.network.cisco.CiscoVnmcDeviceVO;
import com.cloud.network.cisco.CiscoVnmcResourceVO;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.SearchCriteria.Op;
@Local(value=CiscoVnmcDao.class)
public class CiscoVnmcDaoImpl extends GenericDaoBase<CiscoVnmcDeviceVO, Long>
public class CiscoVnmcDaoImpl extends GenericDaoBase<CiscoVnmcResourceVO, Long>
implements CiscoVnmcDao {
protected final SearchBuilder<CiscoVnmcDeviceVO> physicalNetworkIdSearch;
protected final SearchBuilder<CiscoVnmcResourceVO> physicalNetworkIdSearch;
public CiscoVnmcDaoImpl() {
physicalNetworkIdSearch = createSearchBuilder();
@ -39,8 +39,8 @@ public class CiscoVnmcDaoImpl extends GenericDaoBase<CiscoVnmcDeviceVO, Long>
}
@Override
public List<CiscoVnmcDeviceVO> listByPhysicalNetwork(long physicalNetworkId) {
SearchCriteria<CiscoVnmcDeviceVO> sc = physicalNetworkIdSearch.create();
public List<CiscoVnmcResourceVO> listByPhysicalNetwork(long physicalNetworkId) {
SearchCriteria<CiscoVnmcResourceVO> sc = physicalNetworkIdSearch.create();
sc.setParameters("physicalNetworkId", physicalNetworkId);
return search(sc, null);
}

View File

@ -22,6 +22,7 @@ import com.cloud.network.Network.Service;
import com.cloud.network.Networks.BroadcastDomainType;
import com.cloud.network.rules.FirewallRule;
import com.cloud.offering.NetworkOffering;
import com.cloud.utils.component.Inject;
import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.VirtualMachine;
@ -33,6 +34,9 @@ public class CiscoVnmcElement implements DhcpServiceProvider,
private static final Logger s_logger = Logger.getLogger(CiscoVnmcElement.class);
private static final Map<Service, Map<Capability, String>> capabilities = setCapabilities();
@Inject
CiscoVnmcElementService _vnmcService;
private boolean canHandle(Network network) {
if (network.getBroadcastDomainType() != BroadcastDomainType.Vlan) {
@ -102,8 +106,10 @@ public class CiscoVnmcElement implements DhcpServiceProvider,
DeployDestination dest, ReservationContext context)
throws ConcurrentOperationException, ResourceUnavailableException,
InsufficientCapacityException {
// TODO Auto-generated method stub
return false;
//Ensure that there is an ASA 1000v assigned to this network
_vnmcService.assignAsa1000vToNetwork(network);
return true;
}
@Override
@ -112,8 +118,8 @@ public class CiscoVnmcElement implements DhcpServiceProvider,
DeployDestination dest, ReservationContext context)
throws ConcurrentOperationException, ResourceUnavailableException,
InsufficientCapacityException {
// TODO Auto-generated method stub
return false;
//Ensure that there is an ASA 1000v assigned to this network
return true;
}
@Override

View File

@ -18,27 +18,27 @@ package com.cloud.network.element;
import java.util.List;
import com.cloud.api.commands.AddCiscoVnmcDeviceCmd;
import com.cloud.api.commands.DeleteCiscoVnmcDeviceCmd;
import com.cloud.api.commands.ListCiscoVnmcDevicesCmd;
import com.cloud.api.response.CiscoVnmcDeviceResponse;
import com.cloud.api.commands.AddCiscoVnmcResourceCmd;
import com.cloud.api.commands.DeleteCiscoVnmcResourceCmd;
import com.cloud.api.commands.ListCiscoVnmcResourcesCmd;
import com.cloud.api.response.CiscoVnmcResourceResponse;
import com.cloud.network.Network;
import com.cloud.network.Network.Provider;
import com.cloud.network.cisco.CiscoVnmcDeviceVO;
import com.cloud.network.cisco.CiscoVnmcResourceVO;
import com.cloud.utils.component.PluggableService;
public interface CiscoVnmcElementService extends PluggableService {
public static final Provider CiscoVnmc = new Provider("CiscoVnmc", true);
public CiscoVnmcDeviceVO addCiscoVnmcDevice(AddCiscoVnmcDeviceCmd cmd);
public CiscoVnmcResourceVO addCiscoVnmcResource(AddCiscoVnmcResourceCmd cmd);
public CiscoVnmcDeviceResponse createCiscoVnmcDeviceResponse(
CiscoVnmcDeviceVO ciscoVnmcDeviceVO);
public CiscoVnmcResourceResponse createCiscoVnmcResourceResponse(
CiscoVnmcResourceVO CiscoVnmcResourceVO);
boolean deleteCiscoVnmcDevice(DeleteCiscoVnmcDeviceCmd cmd);
boolean deleteCiscoVnmcResource(DeleteCiscoVnmcResourceCmd cmd);
List<CiscoVnmcDeviceVO> listCiscoVnmcDevices(ListCiscoVnmcDevicesCmd cmd);
List<CiscoVnmcResourceVO> listCiscoVnmcResources(ListCiscoVnmcResourcesCmd cmd);
void assignAsa1000vToNetwork(Network network);

View File

@ -16,27 +16,11 @@
// under the License.
package com.cloud.network.resource;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.lang.model.element.Element;
import javax.naming.ConfigurationException;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import com.cloud.agent.IAgentControl;
import com.cloud.agent.api.Answer;
@ -57,10 +41,8 @@ import com.cloud.agent.api.routing.SetStaticNatRulesCommand;
import com.cloud.host.Host;
import com.cloud.resource.ServerResource;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.exception.ExecutionException;
import com.cloud.utils.script.Script;
public class CiscoVnmcResource implements ServerResource {
public class CiscoVnmcResource implements ServerResource{
private String _name;
private String _zoneId;
@ -69,86 +51,16 @@ public class CiscoVnmcResource implements ServerResource {
private String _username;
private String _password;
private String _guid;
private String _objectNameWordSep;
private Integer _numRetries;
private String _publicZone;
private String _privateZone;
private String _publicInterface;
private String _privateInterface;
private String _cookie;
private String _primaryDnsAddress;
private String _primaryDnsAddress;
private final Logger s_logger = Logger.getLogger(CiscoVnmcResource.class);
private enum VnmcXml {
LOGIN("login.xml", "mgmt-controller"),
CREATE_TENANT("create-tenant.xml", "service-reg"),
CREATE_VDC("create-vdc.xml", "service-reg"),
CREATE_EDGE_DEVICE_PROFILE("create-edge-device-profile.xml", "policy-mgr"),
CREATE_EDGE_ROUTE_POLICY("create-edge-device-route-policy.xml", "policy-mgr"),
CREATE_EDGE_ROUTE("create-edge-device-route.xml", "policy-mgr"),
RESOLVE_EDGE_ROUTE_POLICY("associate-route-policy.xml", "policy-mgr"),
RESOLVE_EDGE_DHCP_POLICY("associate-dhcp-policy.xml", "policy-mgr"),
CREATE_DHCP_POLICY("create-dhcp-policy.xml", "policy-mgr"),
RESOLVE_EDGE_DHCP_SERVER_POLICY("associate-dhcp-server.xml", "policy-mgr"),
CREATE_EDGE_SECURITY_PROFILE("create-edge-security-profile.xml", "policy-mgr"),
CREATE_SOURCE_NAT_POOL("create-source-nat-pool.xml", "policy-mgr"),
CREATE_SOURCE_NAT_POLICY("create-source-nat-policy.xml", "policy-mgr"),
CREATE_NAT_POLICY_SET("create-nat-policy-set.xml", "policy-mgr"),
RESOLVE_NAT_POLICY_SET("associate-nat-policy-set.xml", "policy-mgr"),
CREATE_EDGE_FIREWALL("create-edge-firewall.xml", "resource-mgr"),
LIST_UNASSOC_ASA1000V("list-unassigned-asa1000v.xml", "resource-mgr"),
ASSIGN_ASA1000V("assoc-asa1000v.xml", "resource-mgr");
private String scriptsDir = "scripts/network/cisco";
private String xml;
private String service;
private final Logger s_logger = Logger.getLogger(CiscoVnmcResource.class);
private VnmcXml(String filename, String service) {
this.xml = getXml(filename);
this.service = service;
}
public String getXml() {
return xml;
}
private String getXml(String filename) {
try {
String xmlFilePath = Script.findScript(scriptsDir, filename);
if (xmlFilePath == null) {
throw new Exception("Failed to find Cisco VNMC XML file: " + filename);
}
FileReader fr = new FileReader(xmlFilePath);
BufferedReader br = new BufferedReader(fr);
String xml = "";
String line;
while ((line = br.readLine()) != null) {
//xml += line.replaceAll("\n"," ");
xml += line;
}
return xml;
} catch (Exception e) {
s_logger.debug(e);
return null;
}
}
public String getService() {
return service;
}
}
public CiscoVnmcResource(String ip, String username, String password) {
_ip = ip;
_username = username;
@ -235,8 +147,6 @@ public class CiscoVnmcResource implements ServerResource {
NumbersUtil.parseInt((String) params.get("timeout"), 300);
_objectNameWordSep = "-";
_primaryDnsAddress = "4.2.2.2";
// Open a socket and login
@ -321,512 +231,7 @@ public class CiscoVnmcResource implements ServerResource {
return true;
}
public boolean login() throws ExecutionException {
String xml = VnmcXml.LOGIN.getXml();
String service = VnmcXml.LOGIN.getService();
xml = replaceXmlValue(xml, "username", _username);
xml = replaceXmlValue(xml, "password", _password);
String response = sendRequest(service, xml);
Map<String, String> checked = checkResponse(response, "outCookie", "errorCode", "response");
if (checked.get("errorCode") != null)
return false;
_cookie = checked.get("outCookie");
if (_cookie == null) {
return false;
}
return true;
}
private String getDnForTenant(String tenantName) {
return "org-root/org-" + tenantName;
}
private String getDnForTenantVDC(String tenantName) {
return getDnForTenant(tenantName) + "/org-VDC-" + tenantName;
}
private String getDnForTenantVDCEdgeDeviceProfile(String tenantName) {
return getDnForTenantVDC(tenantName) + "/edsp-" + getNameForEdgeDeviceServiceProfile(tenantName);
}
private String getDnForTenantVDCEdgeSecurityProfile(String tenantName) {
return getDnForTenantVDC(tenantName) + "/vnep-" + getNameForEdgeDeviceSecurityProfile(tenantName);
}
private String getDnForEdgeDeviceRoutingPolicy(String tenantName) {
return getDnForTenantVDC(tenantName) + "/routing-policy-" + getNameForEdgeDeviceRoutePolicy(tenantName);
//FIXME: any other construct is unreliable. why?
}
private String getDnForEdgeDeviceRoute(String tenantName, int id) {
return getDnForEdgeDeviceRoutingPolicy(tenantName) + "/sroute-" + id ;
}
private String getDnForDhcpPolicy(String tenantName, String intfName) {
return getDnForTenantVDCEdgeDeviceProfile(tenantName) + "/dhcp-" + intfName;
}
private String getNameForDhcpPolicy(String tenantName) {
return tenantName + "-Dhcp-Policy";
}
private String getNameForDhcpServer(String tenantName) {
return tenantName + "-Dhcp-Server";
}
private String getDnForDhcpServerPolicy(String tenantName) {
return getDnForTenantVDC(tenantName) + "/dhcp-server-" + getNameForDhcpPolicy(tenantName);
}
private String getNameForIpRange() {
return "iprange";
}
private String getDnForDhcpIpRange(String tenantName) {
return getDnForDhcpServerPolicy(tenantName) + "/ip-range-" + getNameForIpRange();
}
private String getNameForDNSService(String tenantName) {
return tenantName + "-DNS";
}
private String getDnForDnsService(String tenantName) {
return getDnForDhcpServerPolicy(tenantName) + "/dns-svc-" + getNameForDNSService(tenantName);
}
private String getDnForDnsServer(String tenantName, String dnsip) {
return getDnForDnsService(tenantName) + "/dns-" + dnsip;
}
private String getNameForTenantVDC(String tenantName) {
return "VDC-" + tenantName;
}
private String getNameForEdgeDeviceServiceProfile(String tenantName) {
return "EDSP-" + tenantName;
}
private String getNameForEdgeDeviceSecurityProfile(String tenantName) {
return "ESP-" + tenantName;
}
private String getNameForEdgeDeviceRoutePolicy(String tenantName) {
return "EDSP-" + tenantName + "-Routes";//FIXME: this has to match DN somehow?
}
public boolean createTenant(String tenantName) throws ExecutionException {
String xml = VnmcXml.CREATE_TENANT.getXml();
String service = VnmcXml.CREATE_TENANT.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "descr", "Tenant for account " + tenantName);
xml = replaceXmlValue(xml, "name", tenantName);
xml = replaceXmlValue(xml, "dn", getDnForTenant(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
public boolean createTenantVDC(String tenantName) throws ExecutionException {
String xml = VnmcXml.CREATE_VDC.getXml();
String service = VnmcXml.CREATE_VDC.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "descr", "VDC for Tenant" + tenantName);
xml = replaceXmlValue(xml, "name", getNameForTenantVDC(tenantName));
xml = replaceXmlValue(xml, "dn", getDnForTenantVDC(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
public boolean createTenantVDCEdgeDeviceProfile(String tenantName) throws ExecutionException {
String xml = VnmcXml.CREATE_EDGE_DEVICE_PROFILE.getXml();
String service = VnmcXml.CREATE_EDGE_DEVICE_PROFILE.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "descr", "Edge Device Profile for Tenant VDC" + tenantName);
xml = replaceXmlValue(xml, "name", getNameForEdgeDeviceServiceProfile(tenantName));
xml = replaceXmlValue(xml, "dn", getDnForTenantVDCEdgeDeviceProfile(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
public boolean createTenantVDCEdgeStaticRoutePolicy(String tenantName) throws ExecutionException {
String xml = VnmcXml.CREATE_EDGE_ROUTE_POLICY.getXml();
String service = VnmcXml.CREATE_EDGE_ROUTE_POLICY.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "name", getNameForEdgeDeviceRoutePolicy(tenantName));//FIXME: this has to match DN somehow?
xml = replaceXmlValue(xml, "routepolicydn", getDnForEdgeDeviceRoutingPolicy(tenantName));
xml = replaceXmlValue(xml, "descr", "Routing Policy for Edge Device for Tenant " + tenantName);
String response = sendRequest(service, xml);
return verifySuccess(response);
}
public boolean createTenantVDCEdgeStaticRoute(String tenantName,
String nextHopIp, String outsideIntf,
String destination, String netmask) throws ExecutionException {
String xml = VnmcXml.CREATE_EDGE_ROUTE.getXml();
String service = VnmcXml.CREATE_EDGE_ROUTE.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "routedn", getDnForEdgeDeviceRoute(tenantName, 2));//TODO: why 2?
xml = replaceXmlValue(xml, "id", "2"); // TODO:2?
xml = replaceXmlValue(xml, "nexthop", nextHopIp);
xml = replaceXmlValue(xml, "nexthopintf", outsideIntf);
xml = replaceXmlValue(xml, "destination", destination);
xml = replaceXmlValue(xml, "netmask", netmask);
//TODO: this adds default route, make it more generic
String response = sendRequest(service, xml);
return verifySuccess(response);
}
public boolean associateTenantVDCEdgeStaticRoutePolicy(String tenantName) throws ExecutionException {
String xml = VnmcXml.RESOLVE_EDGE_ROUTE_POLICY.getXml();
String service = VnmcXml.RESOLVE_EDGE_ROUTE_POLICY.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "profilename", getNameForEdgeDeviceServiceProfile(tenantName));
xml = replaceXmlValue(xml, "profiledn", getDnForTenantVDC(tenantName) + "/edsp-" + getNameForEdgeDeviceServiceProfile(tenantName));
xml = replaceXmlValue(xml, "routepolicyname", getNameForEdgeDeviceRoutePolicy(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
public boolean associateTenantVDCEdgeDhcpPolicy(String tenantName, String intfName) throws ExecutionException {
String xml = VnmcXml.RESOLVE_EDGE_DHCP_POLICY.getXml();
String service = VnmcXml.RESOLVE_EDGE_DHCP_POLICY.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "dhcpdn", getDnForDhcpPolicy(tenantName, intfName));
xml = replaceXmlValue(xml, "insideintf", intfName);
String response = sendRequest(service, xml);
return verifySuccess(response);
}
public boolean createTenantVDCEdgeDhcpPolicy(String tenantName,
String startIp, String endIp, String subnet, String nameServerIp, String domain) throws ExecutionException {
String xml = VnmcXml.CREATE_DHCP_POLICY.getXml();
String service = VnmcXml.CREATE_DHCP_POLICY.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "dhcpserverdn", getDnForDhcpServerPolicy(tenantName));
xml = replaceXmlValue(xml, "dhcpserverdescr", "DHCP server for " + tenantName);
xml = replaceXmlValue(xml, "dhcpservername", getNameForDhcpPolicy(tenantName));
xml = replaceXmlValue(xml, "iprangedn", getDnForDhcpIpRange(tenantName));
xml = replaceXmlValue(xml, "startip", startIp);
xml = replaceXmlValue(xml, "endip", endIp);
xml = replaceXmlValue(xml, "subnet", subnet);
xml = replaceXmlValue(xml, "domain", domain);
xml = replaceXmlValue(xml, "dnsservicedn", getDnForDnsService(tenantName));
xml = replaceXmlValue(xml, "dnsservicename", getNameForDNSService(tenantName));
xml = replaceXmlValue(xml, "nameserverip", nameServerIp);
xml = replaceXmlValue(xml, "nameserverdn", getDnForDnsServer(tenantName, nameServerIp));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
public boolean associateTenantVDCEdgeDhcpServerPolicy(String tenantName, String intfName) throws ExecutionException {
String xml = VnmcXml.RESOLVE_EDGE_DHCP_SERVER_POLICY.getXml();
String service = VnmcXml.RESOLVE_EDGE_DHCP_SERVER_POLICY.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "dhcpdn", getDnForDhcpPolicy(tenantName, intfName));
xml = replaceXmlValue(xml, "insideintf", intfName);
xml = replaceXmlValue(xml, "dhcpserverpolicyname", getNameForDhcpServer(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
public boolean createTenantVDCEdgeSecurityProfile(String tenantName) throws ExecutionException {
String xml = VnmcXml.CREATE_EDGE_SECURITY_PROFILE.getXml();
String service = VnmcXml.CREATE_EDGE_SECURITY_PROFILE.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "descr", "Edge Security Profile for Tenant VDC" + tenantName);
xml = replaceXmlValue(xml, "name", getNameForEdgeDeviceSecurityProfile(tenantName));
xml = replaceXmlValue(xml, "espdn", getDnForTenantVDCEdgeSecurityProfile(tenantName));
xml = replaceXmlValue(xml, "egressref", "default-egress");
xml = replaceXmlValue(xml, "ingressref", "default-ingress"); //FIXME: allows everything
String response = sendRequest(service, xml);
return verifySuccess(response);
}
private String getNameForSourceNatPool(String tenantName) {
return "Source-NAT-Pool-For-" + tenantName;
}
private String getDnForSourceNatPool(String tenantName) {
return getDnForTenantVDC(tenantName) + "/objgrp-" + getNameForSourceNatPool(tenantName);
}
private String getDnForSourceNatPoolExpr(String tenantName) {
return getDnForSourceNatPool(tenantName) + "/objgrp-expr-2";
}
private String getDnForSourceNatPublicIp(String tenantName) {
return getDnForSourceNatPoolExpr(tenantName) + "/nw-ip-2";
}
public boolean createTenantVDCSourceNATPool(String tenantName, String publicIp) throws ExecutionException {
String xml = VnmcXml.CREATE_SOURCE_NAT_POOL.getXml();
String service = VnmcXml.CREATE_SOURCE_NAT_POOL.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "descr", "Source NAT pool for Tenant VDC " + tenantName);
xml = replaceXmlValue(xml, "name", getNameForSourceNatPool(tenantName));
xml = replaceXmlValue(xml, "snatpooldn", getDnForSourceNatPool(tenantName));
xml = replaceXmlValue(xml, "snatpoolexprdn", getDnForSourceNatPoolExpr(tenantName));
xml = replaceXmlValue(xml, "publicipdn", getDnForSourceNatPublicIp(tenantName));
xml = replaceXmlValue(xml, "publicip", publicIp);
String response = sendRequest(service, xml);
return verifySuccess(response);
}
private String getNameForSourceNatPolicy(String tenantName) {
return "Source-NAT-For-" + tenantName;
}
private String getDnForSourceNatPolicy(String tenantName) {
return getDnForTenantVDC(tenantName) + "/natpol-" + getNameForSourceNatPolicy(tenantName);
}
private String getNameForSourceNatRule(String tenantName) {
return "Source-NAT-Policy-Rule-" + tenantName;
}
private String getDnForSourceNatRule(String tenantName) {
return getDnForSourceNatPolicy(tenantName) + "/rule-" + getNameForSourceNatRule(tenantName);
}
private String getDnForSourceNatRuleAction(String tenantName) {
return getDnForSourceNatRule(tenantName) + "/nat-action";
}
private String getDnForSourceNatRuleRule(String tenantName) {
return getDnForSourceNatRule(tenantName) + "/rule-cond-2";
}
private String getDnForSourceNatRuleRange(String tenantName) {
return getDnForSourceNatRuleRule(tenantName) + "/nw-expr2";
}
private String getDnForSourceNatRuleRangeIp(String tenantName, int id) {
return getDnForSourceNatRuleRange(tenantName) + "/nw-ip-" + id;
}
private String getDnForSourceNatRuleRangeAttr(String tenantName) {
return getDnForSourceNatRuleRange(tenantName) + "/nw-attr-qual";
}
public boolean createTenantVDCSourceNATPolicy(String tenantName,
String startSourceIp, String endSourceIp) throws ExecutionException {
String xml = VnmcXml.CREATE_SOURCE_NAT_POLICY.getXml();
String service = VnmcXml.CREATE_SOURCE_NAT_POLICY.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "descr", "Source NAT Policy for Tenant VDC " + tenantName);
xml = replaceXmlValue(xml, "srcTranslatedIpPool", getNameForSourceNatPool(tenantName));
xml = replaceXmlValue(xml, "natrulename", getNameForSourceNatRule(tenantName));
xml = replaceXmlValue(xml, "natpolname", getNameForSourceNatPolicy(tenantName));
xml = replaceXmlValue(xml, "natruleruledescr", "Source NAT Policy for Tenant " + tenantName);
xml = replaceXmlValue(xml, "natpoldescr", "Source NAT Rule for Tenant " + tenantName);
xml = replaceXmlValue(xml, "natpoldn", getDnForSourceNatPolicy(tenantName));
xml = replaceXmlValue(xml, "natruledn", getDnForSourceNatRule(tenantName));
xml = replaceXmlValue(xml, "sourcestartip", startSourceIp);
xml = replaceXmlValue(xml, "sourceendip", endSourceIp);
xml = replaceXmlValue(xml, "sourcenatpoolname", getNameForSourceNatPool(tenantName));
xml = replaceXmlValue(xml, "natactiondn", getDnForSourceNatRuleAction(tenantName));
xml = replaceXmlValue(xml, "natruleruledn", getDnForSourceNatRuleRule(tenantName));
xml = replaceXmlValue(xml, "natrangedn", getDnForSourceNatRuleRange(tenantName));
xml = replaceXmlValue(xml, "natipdn2", getDnForSourceNatRuleRangeIp(tenantName, 2));
xml = replaceXmlValue(xml, "natipdn3", getDnForSourceNatRuleRangeIp(tenantName, 3));
xml = replaceXmlValue(xml, "natsnatruleconddn", getDnForSourceNatRuleRangeAttr(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
private String getNameForNatPolicySet(String tenantName) {
return "NAT-Policy-Set-" + tenantName;
}
private String getDnForNatPolicySet(String tenantName) {
return getDnForTenantVDC(tenantName) + "/natpset-" + getNameForNatPolicySet(tenantName) ;
}
private String getDnForNatPolicySetRef(String tenantName) {
return getDnForNatPolicySet(tenantName) + "/polref-" + getNameForSourceNatPolicy(tenantName) ;
}
public boolean createTenantVDCNatPolicySet(String tenantName) throws ExecutionException {
String xml = VnmcXml.CREATE_NAT_POLICY_SET.getXml();
String service = VnmcXml.CREATE_NAT_POLICY_SET.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
//xml = replaceXmlValue(xml, "descr", "Nat Policy Set for Tenant VDC " + tenantName);
xml = replaceXmlValue(xml, "natpolicyname", getNameForSourceNatPolicy(tenantName));
xml = replaceXmlValue(xml, "natpolicysetname", getNameForNatPolicySet(tenantName));
xml = replaceXmlValue(xml, "natpolicysetdn", getDnForNatPolicySet(tenantName));
xml = replaceXmlValue(xml, "natpolicyrefdn", getDnForNatPolicySetRef(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
public boolean associateNatPolicySet(String tenantName) throws ExecutionException {
String xml = VnmcXml.RESOLVE_NAT_POLICY_SET.getXml();
String service = VnmcXml.RESOLVE_NAT_POLICY_SET.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "descr", "Edge Security Profile for Tenant VDC" + tenantName);
xml = replaceXmlValue(xml, "name", getNameForEdgeDeviceSecurityProfile(tenantName));
xml = replaceXmlValue(xml, "espdn", getDnForTenantVDCEdgeSecurityProfile(tenantName));
xml = replaceXmlValue(xml, "egressref", "default-egress");
xml = replaceXmlValue(xml, "ingressref", "default-ingress");
xml = replaceXmlValue(xml, "natpolicysetname", getNameForNatPolicySet(tenantName));
String response = sendRequest(service, xml);
return verifySuccess(response);
}
private String getNameForEdgeFirewall(String tenantName) {
return "ASA-1000v-" + tenantName;
}
private String getDnForEdgeFirewall(String tenantName) {
return getDnForTenantVDC(tenantName) + "/efw-" + getNameForEdgeFirewall(tenantName);
}
private String getNameForEdgeInsideIntf(String tenantName) {
return "Edge_Inside";
}
private String getNameForEdgeOutsideIntf(String tenantName) {
return "Edge_Outside";
}
private String getDnForOutsideIntf(String tenantName) {
return getDnForEdgeFirewall(tenantName) + "/interface-" + getNameForEdgeOutsideIntf(tenantName);
}
private String getDnForInsideIntf(String tenantName) {
return getDnForEdgeFirewall(tenantName) + "/interface-" + getNameForEdgeInsideIntf(tenantName);
}
public boolean createEdgeFirewall(String tenantName, String publicIp, String insideIp,
String insideSubnet, String outsideSubnet) throws ExecutionException {
String xml = VnmcXml.CREATE_EDGE_FIREWALL.getXml();
String service = VnmcXml.CREATE_EDGE_FIREWALL.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "edgefwdescr", "Edge Firewall for Tenant VDC " + tenantName);
xml = replaceXmlValue(xml, "edgefwname", getNameForEdgeFirewall(tenantName));
xml = replaceXmlValue(xml, "edgefwdn", getDnForEdgeFirewall(tenantName));
xml = replaceXmlValue(xml, "insideintfname", getNameForEdgeInsideIntf(tenantName));
xml = replaceXmlValue(xml, "outsideintfname", getNameForEdgeOutsideIntf(tenantName));
xml = replaceXmlValue(xml, "insideintfdn", getDnForInsideIntf(tenantName));
xml = replaceXmlValue(xml, "outsideintfdn", getDnForOutsideIntf(tenantName));
xml = replaceXmlValue(xml, "deviceserviceprofiledn", getDnForEdgeFirewall(tenantName) + "/device-service-profile");
xml = replaceXmlValue(xml, "outsideintfsp", getDnForOutsideIntf(tenantName) + "/interface-service-profile");
xml = replaceXmlValue(xml, "secprofileref", getNameForEdgeDeviceSecurityProfile(tenantName));
xml = replaceXmlValue(xml, "deviceserviceprofile", getNameForEdgeDeviceServiceProfile(tenantName));
xml = replaceXmlValue(xml, "insideip", insideIp);
xml = replaceXmlValue(xml, "publicip", publicIp);
xml = replaceXmlValue(xml, "insidesubnet", insideSubnet);
xml = replaceXmlValue(xml, "outsidesubnet", outsideSubnet);
String response = sendRequest(service, xml);
return verifySuccess(response);
}
public List<String> listUnAssocAsa1000v() throws ExecutionException {
String xml = VnmcXml.LIST_UNASSOC_ASA1000V.getXml();
String service = VnmcXml.LIST_UNASSOC_ASA1000V.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
String response = sendRequest(service, xml);
List<String> result = new ArrayList<String>();
Document xmlDoc = getDocument(response);
xmlDoc.normalize();
NodeList fwList = xmlDoc.getElementsByTagName("fwInstance");
for (int j=0; j < fwList.getLength(); j++) {
Node fwNode = fwList.item(j);
result.add (fwNode.getAttributes().getNamedItem("dn").getNodeValue());
}
return result;
}
public boolean assocAsa1000v(String tenantName, String firewallDn) throws ExecutionException {
String xml = VnmcXml.ASSIGN_ASA1000V.getXml();
String service = VnmcXml.ASSIGN_ASA1000V.getService();
xml = replaceXmlValue(xml, "cookie", _cookie);
xml = replaceXmlValue(xml, "binddn", getDnForEdgeFirewall(tenantName) + "/binding");
xml = replaceXmlValue(xml, "fwdn", firewallDn);
String response = sendRequest(service, xml);
return verifySuccess(response);
}
private String sendRequest(String service, String xmlRequest) throws ExecutionException {
org.apache.commons.httpclient.protocol.Protocol myhttps =
new org.apache.commons.httpclient.protocol.Protocol("https", new EasySSLProtocolSocketFactory(), 443);
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(_ip, 443, myhttps);
byte[] response = null;
PostMethod method = new PostMethod("/xmlIM/" + service);
method.setRequestBody(xmlRequest);
try{
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
throw new Exception("Error code : " + statusCode);
}
response = method.getResponseBody();
}catch(Exception e){
System.out.println(e.getMessage());
throw new ExecutionException(e.getMessage());
}
System.out.println(new String(response));
return new String(response);
}
private synchronized Answer execute(IpAssocCommand cmd) {
refreshVnmcConnection();
@ -856,10 +261,6 @@ public class CiscoVnmcResource implements ServerResource {
}
/*
* Destination NAT
@ -876,120 +277,5 @@ public class CiscoVnmcResource implements ServerResource {
}
/*
* XML API commands
*/
private Map<String, String> checkResponse(String xmlResponse, String... keys) throws ExecutionException {
Document xmlDoc = getDocument(xmlResponse);
Map<String, String> result = new HashMap<String, String>();
Node topElement = xmlDoc.getChildNodes().item(0);
if (topElement != null) {
for (String key: keys){
Node valueNode = topElement.getAttributes().getNamedItem(key);
result.put(key, valueNode==null?null:valueNode.getNodeValue());
}
}
return result;
}
private boolean verifySuccess(String xmlResponse) throws ExecutionException {
Map<String, String> checked = checkResponse(xmlResponse, "errorCode", "errorDescr");
if (checked.get("errorCode") != null) {
String errorCode = checked.get("errorCode");
if (errorCode.equals("103")) {
//tenant already exists
return true;
}
String errorDescr = checked.get("errorDescr");
throw new ExecutionException(errorDescr);
}
return true;
}
/*
* XML utils
*/
private String replaceXmlTag(String xml, String oldTag, String newTag) {
return xml.replaceAll(oldTag, newTag);
}
private String replaceXmlValue(String xml, String marker, String value) {
marker = "\\s*%" + marker + "%\\s*";
if (value == null) {
value = "";
}
return xml.replaceAll(marker, value);
}
private String extractXml(String xml, String marker) {
String startMarker = "<" + marker + ">";
String endMarker = "</" + marker + ">";
if (xml.contains(startMarker) && xml.contains(endMarker)) {
return xml.substring(xml.indexOf(startMarker) + startMarker.length(), xml.indexOf(endMarker));
} else {
return null;
}
}
/*
* Misc
*/
private Long getVlanTag(String vlan) throws ExecutionException {
Long publicVlanTag = null;
if (!vlan.equals("untagged")) {
try {
publicVlanTag = Long.parseLong(vlan);
} catch (Exception e) {
throw new ExecutionException("Unable to parse VLAN tag: " + vlan);
}
}
return publicVlanTag;
}
private String genObjectName(String... args) {
String objectName = "";
for (int i = 0; i < args.length; i++) {
objectName += args[i];
if (i != args.length -1) {
objectName += _objectNameWordSep;
}
}
return objectName;
}
private Document getDocument(String xml) throws ExecutionException {
StringReader xmlReader = new StringReader("<?xml version=\"1.0\"?> \n" + xml.trim());
InputSource xmlSource = new InputSource(xmlReader);
Document doc = null;
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlSource);
} catch (Exception e) {
s_logger.error(e);
throw new ExecutionException(e.getMessage());
}
if (doc == null) {
throw new ExecutionException("Failed to parse xml " + xml);
} else {
return doc;
}
}
}