diff options
author | Seshu Kumar M <seshu.kumar.m@huawei.com> | 2020-09-02 04:13:29 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-09-02 04:13:29 +0000 |
commit | bca09ad035b7f21385dc70dba5baff587399d9df (patch) | |
tree | f54285b0d908c8aa92525e07fd4c7fb292b1906b /bpmn/so-bpmn-tasks/src/main | |
parent | 8130b6d10da62329f3f43b7819f40ecf02282ffa (diff) | |
parent | 3f4981493008ccf8eeb23f2aac5c2fcf40a0be49 (diff) |
Merge "Bpmn to cnf adapter implementation"
Diffstat (limited to 'bpmn/so-bpmn-tasks/src/main')
10 files changed, 544 insertions, 1 deletions
diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/cnf/tasks/CnfAdapterCreateTasks.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/cnf/tasks/CnfAdapterCreateTasks.java new file mode 100644 index 0000000000..93d30512b6 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/cnf/tasks/CnfAdapterCreateTasks.java @@ -0,0 +1,121 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright (c) 2019 Samsung + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.bpmn.infrastructure.adapter.cnf.tasks; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import org.onap.so.bpmn.common.BuildingBlockExecution; +import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion; +import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf; +import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance; +import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule; +import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock; +import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey; +import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext; +import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB; +import org.onap.so.client.adapter.cnf.CnfAdapterClient; +import org.onap.so.client.adapter.cnf.entities.InstanceRequest; +import org.onap.so.client.adapter.cnf.entities.InstanceResponse; +import org.onap.so.client.adapter.vnf.mapper.AttributeNameValue; +import org.onap.so.client.adapter.vnf.mapper.Attributes; +import org.onap.so.client.adapter.vnf.mapper.VnfAdapterVfModuleObjectMapper; +import org.onap.so.client.exception.ExceptionBuilder; +import org.onap.so.openstack.utils.MsoMulticloudUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +@Component +public class CnfAdapterCreateTasks { + private static final Logger logger = LoggerFactory.getLogger(CnfAdapterCreateTasks.class); + public static final String SDNCQUERY_RESPONSE = "SDNCQueryResponse_"; + + @Autowired + private ExtractPojosForBB extractPojosForBB; + @Autowired + private ExceptionBuilder exceptionUtil; + @Autowired + private CnfAdapterClient cnfAdapterClient; + @Autowired + private VnfAdapterVfModuleObjectMapper vfModuleMapper; + + private ObjectMapper mapper = new ObjectMapper(); + + /** + * This method is used for creating the request for an Instance in Multicloud K8s Plugin. + * + * @param execution + * @return + */ + public void createInstance(BuildingBlockExecution execution) { + try { + GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock(); + ServiceInstance serviceInstance = + gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0); + GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID); + VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID); + RequestContext requestContext = gBBInput.getRequestContext(); + CloudRegion cloudRegion = gBBInput.getCloudRegion(); + String sdncVfModuleQueryResponse = execution.getVariable(SDNCQUERY_RESPONSE + vfModule.getVfModuleId()); + String sdncVnfQueryResponse = execution.getVariable(SDNCQUERY_RESPONSE + genericVnf.getVnfId()); + Map<String, Object> paramsMap = vfModuleMapper.buildVfModuleParamsMap(requestContext, serviceInstance, + genericVnf, vfModule, sdncVnfQueryResponse, sdncVfModuleQueryResponse); + Map<String, String> sdncDirectives = getSdncDirectives(paramsMap); + InstanceRequest createInstanceRequest = createInstanceRequest(vfModule, cloudRegion, sdncDirectives); + InstanceResponse response = cnfAdapterClient.createVfModule(createInstanceRequest); + execution.setVariable("heatStackId", response.getId()); + } catch (Exception ex) { + logger.error("Exception occurred", ex); + exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex); + } + } + + protected Map<String, String> getSdncDirectives(Map<String, Object> paramsMap) + throws JsonParseException, JsonMappingException, IOException { + Map<String, String> sdncDirectivesMap = new HashMap<>(); + String sdncDirectivesString = (String) paramsMap.get(MsoMulticloudUtils.SDNC_DIRECTIVES); + Attributes sdncDirectives = mapper.readValue(sdncDirectivesString, Attributes.class); + for (AttributeNameValue nameVal : sdncDirectives.getAttributes()) { + sdncDirectivesMap.put(nameVal.getAttributeName(), (String) nameVal.getAttributeValue()); + } + return sdncDirectivesMap; + } + + protected InstanceRequest createInstanceRequest(VfModule vfModule, CloudRegion cloudRegion, + Map<String, String> sdncDirectives) { + InstanceRequest request = new InstanceRequest(); + request.setRbName(vfModule.getModelInfoVfModule().getModelInvariantUUID()); + request.setRbVersion(vfModule.getModelInfoVfModule().getModelUUID()); + request.setCloudRegion(cloudRegion.getLcpCloudRegionId()); + request.setReleaseName(vfModule.getVfModuleId()); + request.setProfileName(sdncDirectives.get("k8s-rb-profile-name")); + request.setOverrideValues(sdncDirectives); + return request; + } + +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/CnfAdapterClient.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/CnfAdapterClient.java new file mode 100644 index 0000000000..f44c15ac54 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/CnfAdapterClient.java @@ -0,0 +1,97 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.adapter.cnf; + +import java.util.ArrayList; +import java.util.List; +import javax.persistence.EntityNotFoundException; +import javax.ws.rs.core.UriBuilder; +import org.apache.http.HttpStatus; +import org.onap.so.client.adapter.cnf.entities.InstanceRequest; +import org.onap.so.client.adapter.cnf.entities.InstanceResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.retry.annotation.Backoff; +import org.springframework.retry.annotation.Retryable; +import org.springframework.stereotype.Component; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.HttpServerErrorException; +import org.springframework.web.client.RestTemplate; + +@Component +public class CnfAdapterClient { + + private static final Logger logger = LoggerFactory.getLogger(CnfAdapterClient.class); + + @Autowired + private RestTemplate restTemplate; + + @Autowired + private Environment env; + + private static final String INSTANCE_CREATE_PATH = "/api/multicloud-k8s/v1/v1/instance"; + + @Retryable(value = {HttpServerErrorException.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000)) + public InstanceResponse createVfModule(InstanceRequest request) throws CnfAdapterClientException { + try { + // String uri = env.getRequiredProperty("mso.cnf.adapter.endpoint"); //TODO: This needs to be added as well + // for configuration + String uri = "https://localhost:32780"; // TODO: What is the correct uri? + String endpoint = UriBuilder.fromUri(uri).path(INSTANCE_CREATE_PATH).build().toString(); + HttpEntity<?> entity = getHttpEntity(request); + ResponseEntity<InstanceResponse> result = + restTemplate.exchange(endpoint, HttpMethod.POST, entity, InstanceResponse.class); + return result.getBody(); + } catch (HttpClientErrorException e) { + logger.error("Error Calling CNF Adapter, e"); + if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) { + throw new EntityNotFoundException(e.getResponseBodyAsString()); + } + throw e; + } + } + + protected HttpHeaders getHttpHeaders() { + HttpHeaders headers = new HttpHeaders(); + List<org.springframework.http.MediaType> acceptableMediaTypes = new ArrayList<>(); + acceptableMediaTypes.add(org.springframework.http.MediaType.APPLICATION_JSON); + headers.setAccept(acceptableMediaTypes); + /* + * try { String userCredentials = CryptoUtils.decrypt(env.getRequiredProperty("mso.cnf.adapter.auth"), + * env.getRequiredProperty("mso.msoKey")); if (userCredentials != null) { headers.add(HttpHeaders.AUTHORIZATION, + * "Basic " + DatatypeConverter.printBase64Binary(userCredentials.getBytes())); } } catch + * (GeneralSecurityException e) { logger.error("Security exception", e); } + */ + return headers; + } + + protected HttpEntity<?> getHttpEntity(InstanceRequest request) { + HttpHeaders headers = getHttpHeaders(); + return new HttpEntity<>(request, headers); + } + +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/CnfAdapterClientException.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/CnfAdapterClientException.java new file mode 100644 index 0000000000..d716070ffa --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/CnfAdapterClientException.java @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.adapter.cnf; + +public class CnfAdapterClientException extends Exception { + + /** + * + */ + private static final long serialVersionUID = -7154784472485852602L; + + public CnfAdapterClientException(String message) { + super(message); + } + +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/entities/GVK.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/entities/GVK.java new file mode 100644 index 0000000000..3ed6894fcf --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/entities/GVK.java @@ -0,0 +1,49 @@ + +package org.onap.so.client.adapter.cnf.entities; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({"Group", "Version", "Kind"}) +public class GVK { + + @JsonProperty("Group") + private String group; + @JsonProperty("Version") + private String version; + @JsonProperty("Kind") + private String kind; + + @JsonProperty("Group") + public String getGroup() { + return group; + } + + @JsonProperty("Group") + public void setGroup(String group) { + this.group = group; + } + + @JsonProperty("Version") + public String getVersion() { + return version; + } + + @JsonProperty("Version") + public void setVersion(String version) { + this.version = version; + } + + @JsonProperty("Kind") + public String getKind() { + return kind; + } + + @JsonProperty("Kind") + public void setKind(String kind) { + this.kind = kind; + } + +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/entities/InstanceRequest.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/entities/InstanceRequest.java new file mode 100644 index 0000000000..e4058097ab --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/entities/InstanceRequest.java @@ -0,0 +1,95 @@ + +package org.onap.so.client.adapter.cnf.entities; + +import java.util.Map; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({"rb-name", "rb-version", "profile-name", "release-name", "cloud-region", "labels", + "override-values"}) +public class InstanceRequest { + + @JsonProperty("rb-name") + private String rbName; + @JsonProperty("rb-version") + private String rbVersion; + @JsonProperty("profile-name") + private String profileName; + @JsonProperty("release-name") + private String releaseName; + @JsonProperty("cloud-region") + private String cloudRegion; + @JsonProperty("labels") + private Labels labels; + @JsonProperty(value = "override-values") + private Map<String, String> overrideValues; + + @JsonProperty("rb-name") + public String getRbName() { + return rbName; + } + + @JsonProperty("rb-name") + public void setRbName(String rbName) { + this.rbName = rbName; + } + + @JsonProperty("rb-version") + public String getRbVersion() { + return rbVersion; + } + + @JsonProperty("rb-version") + public void setRbVersion(String rbVersion) { + this.rbVersion = rbVersion; + } + + @JsonProperty("profile-name") + public String getProfileName() { + return profileName; + } + + @JsonProperty("profile-name") + public void setProfileName(String profileName) { + this.profileName = profileName; + } + + @JsonProperty("cloud-region") + public String getCloudRegion() { + return cloudRegion; + } + + @JsonProperty("cloud-region") + public void setCloudRegion(String cloudRegion) { + this.cloudRegion = cloudRegion; + } + + @JsonProperty("labels") + public Labels getLabels() { + return labels; + } + + @JsonProperty("labels") + public void setLabels(Labels labels) { + this.labels = labels; + } + + public String getReleaseName() { + return releaseName; + } + + public void setReleaseName(String releaseName) { + this.releaseName = releaseName; + } + + public Map<String, String> getOverrideValues() { + return overrideValues; + } + + public void setOverrideValues(Map<String, String> overrideValues) { + this.overrideValues = overrideValues; + } + +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/entities/InstanceResponse.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/entities/InstanceResponse.java new file mode 100644 index 0000000000..e38bcc2664 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/entities/InstanceResponse.java @@ -0,0 +1,62 @@ + +package org.onap.so.client.adapter.cnf.entities; + +import java.util.List; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({"id", "request", "namespace", "resources"}) +public class InstanceResponse { + + @JsonProperty("id") + private String id; + @JsonProperty("request") + private InstanceRequest request; + @JsonProperty("namespace") + private String namespace; + @JsonProperty("resources") + private List<Resource> resources = null; + + @JsonProperty("id") + public String getId() { + return id; + } + + @JsonProperty("id") + public void setId(String id) { + this.id = id; + } + + @JsonProperty("request") + public InstanceRequest getRequest() { + return request; + } + + @JsonProperty("request") + public void setRequest(InstanceRequest request) { + this.request = request; + } + + @JsonProperty("namespace") + public String getNamespace() { + return namespace; + } + + @JsonProperty("namespace") + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + @JsonProperty("resources") + public List<Resource> getResources() { + return resources; + } + + @JsonProperty("resources") + public void setResources(List<Resource> resources) { + this.resources = resources; + } + +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/entities/Labels.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/entities/Labels.java new file mode 100644 index 0000000000..1df2e59459 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/entities/Labels.java @@ -0,0 +1,25 @@ + +package org.onap.so.client.adapter.cnf.entities; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({"testCaseName"}) +public class Labels { + + @JsonProperty("testCaseName") + private String testCaseName; + + @JsonProperty("testCaseName") + public String getTestCaseName() { + return testCaseName; + } + + @JsonProperty("testCaseName") + public void setTestCaseName(String testCaseName) { + this.testCaseName = testCaseName; + } + +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/entities/Resource.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/entities/Resource.java new file mode 100644 index 0000000000..bde495aaa2 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/cnf/entities/Resource.java @@ -0,0 +1,37 @@ + +package org.onap.so.client.adapter.cnf.entities; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({"GVK", "Name"}) +public class Resource { + + @JsonProperty("GVK") + private GVK gVK; + @JsonProperty("Name") + private String name; + + @JsonProperty("GVK") + public GVK getGVK() { + return gVK; + } + + @JsonProperty("GVK") + public void setGVK(GVK gVK) { + this.gVK = gVK; + } + + @JsonProperty("Name") + public String getName() { + return name; + } + + @JsonProperty("Name") + public void setName(String name) { + this.name = name; + } + +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/vnf/mapper/Attributes.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/vnf/mapper/Attributes.java new file mode 100644 index 0000000000..6d79c20616 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/vnf/mapper/Attributes.java @@ -0,0 +1,23 @@ +package org.onap.so.client.adapter.vnf.mapper; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Attributes implements Serializable { + + private static final long serialVersionUID = -5782985934617532582L; + + @JsonProperty("attributes") + private List<AttributeNameValue> attributes = new ArrayList<>(); + + public List<AttributeNameValue> getAttributes() { + return attributes; + } + + public void setAttributes(List<AttributeNameValue> attributes) { + this.attributes = attributes; + } + +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/vnf/mapper/VnfAdapterVfModuleObjectMapper.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/vnf/mapper/VnfAdapterVfModuleObjectMapper.java index bc618e17de..59da22f8e1 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/vnf/mapper/VnfAdapterVfModuleObjectMapper.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/adapter/vnf/mapper/VnfAdapterVfModuleObjectMapper.java @@ -178,7 +178,7 @@ public class VnfAdapterVfModuleObjectMapper { return msoRequest; } - private Map<String, Object> buildVfModuleParamsMap(RequestContext requestContext, ServiceInstance serviceInstance, + public Map<String, Object> buildVfModuleParamsMap(RequestContext requestContext, ServiceInstance serviceInstance, GenericVnf genericVnf, VfModule vfModule, String sdncVnfQueryResponse, String sdncVfModuleQueryResponse) throws IOException, MissingValueTagException { |