use of org.onap.so.openstack.exceptions.MsoAdapterException in project so by onap.
the class MsoCommonUtilsTest method testRuntimeExceptionToMsoException.
@Test
public final void testRuntimeExceptionToMsoException() {
RuntimeException re = new RuntimeException("runtime");
MsoException me = commonUtils.runtimeExceptionToMsoException(re, "ContextError");
assertTrue(me instanceof MsoAdapterException);
assertTrue("ContextError".equals(me.getContext()));
assertTrue(MsoExceptionCategory.INTERNAL.equals(me.getCategory()));
}
use of org.onap.so.openstack.exceptions.MsoAdapterException in project so by onap.
the class MsoNetworkAdapterImpl method mergeSubnets.
private String mergeSubnets(String heatTemplate, List<Subnet> subnets) throws MsoException {
String resourceTempl = " subnet_%subnetId%:\n" + " type: OS::Neutron::Subnet\n" + " properties:\n" + " name: %name%\n" + " network_id: { get_resource: network }\n" + " cidr: %cidr%\n";
/*
* make these optional + " ip_version: %ipversion%\n" + " enable_dhcp: %enabledhcp%\n" +
* " gateway_ip: %gatewayip%\n" + " allocation_pools:\n" + " - start: %poolstart%\n" +
* " end: %poolend%\n";
*
*/
String outputTempl = " subnet_id_%subnetId%:\n" + " description: Openstack subnet identifier\n" + " value: {get_resource: subnet_%subnetId%}\n";
String curR;
String curO;
StringBuilder resourcesBuf = new StringBuilder();
StringBuilder outputsBuf = new StringBuilder();
for (Subnet subnet : subnets) {
// build template for each subnet
curR = resourceTempl;
if (subnet.getSubnetId() != null) {
curR = curR.replace("%subnetId%", subnet.getSubnetId());
} else {
String error = "Missing Required AAI SubnetId for subnet in HEAT Template";
logger.error(LoggingAnchor.THREE, MessageEnum.RA_MISSING_PARAM, ErrorCode.DataError.getValue(), error);
throw new MsoAdapterException(error);
}
if (subnet.getSubnetName() != null) {
curR = curR.replace("%name%", subnet.getSubnetName());
} else {
curR = curR.replace("%name%", subnet.getSubnetId());
}
if (subnet.getCidr() != null) {
curR = curR.replace("%cidr%", subnet.getCidr());
} else {
String error = "Missing Required cidr for subnet in HEAT Template";
logger.error(LoggingAnchor.THREE, MessageEnum.RA_MISSING_PARAM, ErrorCode.DataError.getValue(), error);
throw new MsoAdapterException(error);
}
if (subnet.getIpVersion() != null) {
curR = curR + " ip_version: " + subnet.getIpVersion() + "\n";
}
if (subnet.getEnableDHCP() != null) {
curR = curR + " enable_dhcp: " + Boolean.toString(subnet.getEnableDHCP()) + "\n";
}
if (subnet.getGatewayIp() != null && !subnet.getGatewayIp().isEmpty()) {
curR = curR + " gateway_ip: " + subnet.getGatewayIp() + "\n";
}
if (subnet.getAllocationPools() != null) {
StringBuilder tempBuf = new StringBuilder();
tempBuf.append(curR);
tempBuf.append(" allocation_pools:\n");
for (Pool pool : subnet.getAllocationPools()) {
if (!commonUtils.isNullOrEmpty(pool.getStart()) && !commonUtils.isNullOrEmpty(pool.getEnd())) {
tempBuf.append(" - start: ");
tempBuf.append(pool.getStart());
tempBuf.append("\n end: ");
tempBuf.append(pool.getEnd());
tempBuf.append("\n");
}
}
curR = tempBuf.toString();
}
resourcesBuf.append(curR);
curO = outputTempl;
curO = curO.replace("%subnetId%", subnet.getSubnetId());
outputsBuf.append(curO);
}
// append resources and outputs in heatTemplate
logger.debug("Tempate initial:{}", heatTemplate);
int outputsIdx = heatTemplate.indexOf("outputs:");
heatTemplate = insertStr(heatTemplate, outputsBuf.toString(), outputsIdx + 8);
int resourcesIdx = heatTemplate.indexOf("resources:");
heatTemplate = insertStr(heatTemplate, resourcesBuf.toString(), resourcesIdx + 10);
logger.debug("Template updated with all subnets:{}", heatTemplate);
return heatTemplate;
}
use of org.onap.so.openstack.exceptions.MsoAdapterException in project so by onap.
the class MsoCommonUtils method getKeystoneAuthHolder.
/**
* Gets the Keystone Authorization
*
* @param cloudSite the cloud site
* @param tenantId the tenant id
* @return the Neutron client
* @throws MsoException the mso exception
*/
protected KeystoneAuthHolder getKeystoneAuthHolder(String cloudSiteId, String tenantId, String serviceName) throws MsoException {
CloudIdentity cloudIdentity = null;
try {
CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
String cloudId = cloudSite.getId();
String region = cloudSite.getRegionId();
cloudIdentity = cloudSite.getIdentityService();
MsoTenantUtils tenantUtils = tenantUtilsFactory.getTenantUtilsByServerType(cloudIdentity.getIdentityServerType());
String keystoneUrl = tenantUtils.getKeystoneUrl(cloudId, cloudIdentity);
if (ServerType.KEYSTONE.equals(cloudIdentity.getIdentityServerType())) {
Access access = getKeystone(tenantId, cloudIdentity, keystoneUrl);
try {
KeystoneAuthHolder keystoneAuthV2 = new KeystoneAuthHolder();
keystoneAuthV2.setServiceUrl(KeystoneUtils.findEndpointURL(access.getServiceCatalog(), serviceName, region, "public"));
keystoneAuthV2.setId(access.getToken().getId());
return keystoneAuthV2;
} catch (RuntimeException e) {
String error = "Openstack did not match an orchestration service for: region=" + region + ",cloud=" + cloudIdentity.getIdentityUrl();
throw new MsoAdapterException(error, e);
}
} else if (ServerType.KEYSTONE_V3.equals(cloudIdentity.getIdentityServerType())) {
try {
return keystoneV3Authentication.getToken(cloudSite, tenantId, serviceName);
} catch (ServiceEndpointNotFoundException e) {
String error = "cloud did not match an orchestration service for: region=" + region + ",cloud=" + cloudIdentity.getIdentityUrl();
throw new MsoAdapterException(error, e);
}
} else {
throw new MsoAdapterException("Unknown Keystone Server Type");
}
} catch (OpenStackResponseException e) {
if (e.getStatus() == 401) {
String error = "Authentication Failure: tenant=" + tenantId + ",cloud=" + cloudIdentity.getId();
throw new MsoAdapterException(error);
} else {
throw keystoneErrorToMsoException(e, TOKEN_AUTH);
}
} catch (OpenStackConnectException e) {
MsoIOException me = new MsoIOException(e.getMessage(), e);
me.addContext(TOKEN_AUTH);
throw me;
} catch (RuntimeException e) {
throw runtimeExceptionToMsoException(e, TOKEN_AUTH);
}
}
use of org.onap.so.openstack.exceptions.MsoAdapterException in project so by onap.
the class MsoNetworkAdapterImpl method mergeSubnetsAIC3.
/**
* Subnet Output structure from Juniper { "ipam_subnets": [ { "subnet": { "ip_prefix": "10.100.1.0",
* "ip_prefix_len": 28 }, "addr_from_start": null, "enable_dhcp": false, "default_gateway": "10.100.1.1",
* "dns_nameservers": [], "dhcp_option_list": null, "subnet_uuid": "10391fbf-6b9c-4160-825d-2d018b7649cf",
* "allocation_pools": [ { "start": "10.100.1.3", "end": "10.100.1.5" }, { "start": "10.100.1.6", "end":
* "10.100.1.9" } ], "host_routes": null, "dns_server_address": "10.100.1.13", "subnet_name":
* "subnet_MsoNW1_692c9032-e1a2-4d64-828c-7b9a4fcc05b0" }, { "subnet": { "ip_prefix": "10.100.2.16",
* "ip_prefix_len": 28 }, "addr_from_start": null, "enable_dhcp": true, "default_gateway": "10.100.2.17",
* "dns_nameservers": [], "dhcp_option_list": null, "subnet_uuid": "c7aac5ea-66fe-443a-85f9-9c38a608c0f6",
* "allocation_pools": [ { "start": "10.100.2.18", "end": "10.100.2.20" } ], "host_routes": null,
* "dns_server_address": "10.100.2.29", "subnet_name": "subnet_MsoNW1_692c9032-e1a2-4d64-828c-7b9a4fcc05b1" } ],
* "host_routes": null }
**
*/
private String mergeSubnetsAIC3(String heatTemplate, List<Subnet> subnets, Map<String, Object> stackParams) throws MsoException {
// Resource Property
List<ContrailSubnet> cslist = new ArrayList<>();
for (Subnet subnet : subnets) {
logger.debug("Input Subnet:{}", subnet);
ContrailSubnet cs = new ContrailSubnetMapper(subnet).map();
logger.debug("Contrail Subnet:{}", cs);
cslist.add(cs);
}
JsonNode node = null;
try {
ObjectMapper mapper = new ObjectMapper();
node = mapper.convertValue(cslist, JsonNode.class);
String jsonString = mapper.writeValueAsString(cslist);
logger.debug("Json Subnet List:{}", jsonString);
} catch (Exception e) {
String error = "Error creating JsonNode from input subnets";
logger.error(LoggingAnchor.THREE, MessageEnum.RA_MARSHING_ERROR, ErrorCode.DataError.getValue(), error, e);
throw new MsoAdapterException(error);
}
// update parameters
if (node != null) {
stackParams.put("subnet_list", node);
}
// Outputs - All subnets are in one ipam_subnets structure
String outputTempl = " subnet:\n" + " description: Openstack subnet identifier\n" + " value: { get_attr: [network, network_ipam_refs, 0, attr]}\n";
// append outputs in heatTemplate
int outputsIdx = heatTemplate.indexOf("outputs:");
heatTemplate = insertStr(heatTemplate, outputTempl, outputsIdx + 8);
logger.debug("Template updated with all AIC3.0 subnets:{}", heatTemplate);
return heatTemplate;
}
use of org.onap.so.openstack.exceptions.MsoAdapterException in project so by onap.
the class MsoNetworkAdapterImpl method mergePolicyRefs.
/**
* policyRef_list structure in stackParams [ { "network_policy_refs_data_sequence": {
* "network_policy_refs_data_sequence_major": "1", "network_policy_refs_data_sequence_minor": "0" } }, {
* "network_policy_refs_data_sequence": { "network_policy_refs_data_sequence_major": "2",
* "network_policy_refs_data_sequence_minor": "0" } } ]
*/
private void mergePolicyRefs(List<String> pFqdns, Map<String, Object> stackParams) throws MsoException {
// Resource Property
List<ContrailPolicyRef> prlist = new ArrayList<>();
int index = 1;
if (pFqdns != null) {
for (String pf : pFqdns) {
if (!commonUtils.isNullOrEmpty(pf)) {
ContrailPolicyRef pr = new ContrailPolicyRef();
ContrailPolicyRefSeq refSeq = new ContrailPolicyRefSeq(String.valueOf(index), "0");
pr.setSeq(refSeq);
index++;
logger.debug("Contrail PolicyRefs Data:{}", pr);
prlist.add(pr);
}
}
} else {
String error = "Null pFqdns at start of mergePolicyRefs";
logger.error(LoggingAnchor.THREE, MessageEnum.RA_MARSHING_ERROR, ErrorCode.BusinessProcessError.getValue(), error);
throw new MsoAdapterException(error);
}
JsonNode node = null;
try {
ObjectMapper mapper = new ObjectMapper();
node = mapper.convertValue(prlist, JsonNode.class);
String jsonString = mapper.writeValueAsString(prlist);
logger.debug("Json PolicyRefs Data:{}", jsonString);
} catch (Exception e) {
String error = "Error creating JsonNode for policyRefs Data";
logger.error(LoggingAnchor.THREE, MessageEnum.RA_MARSHING_ERROR, ErrorCode.BusinessProcessError.getValue(), error, e);
throw new MsoAdapterException(error);
}
// update parameters
if (pFqdns != null && node != null) {
StringBuilder buf = new StringBuilder();
String sep = "";
for (String pf : pFqdns) {
if (!commonUtils.isNullOrEmpty(pf)) {
buf.append(sep).append(pf);
sep = ",";
}
}
String csl = buf.toString();
stackParams.put("policy_refs", csl);
stackParams.put("policy_refsdata", node);
}
logger.debug("StackParams updated with policy refs");
return;
}
Aggregations