diff options
Diffstat (limited to 'mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact')
6 files changed, 469 insertions, 0 deletions
diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/ArtifactFileNameCreator.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/ArtifactFileNameCreator.java new file mode 100644 index 0000000..7713020 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/ArtifactFileNameCreator.java @@ -0,0 +1,59 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 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.dcaegen2.platform.mod.web.service.deploymentartifact; + +import org.onap.dcaegen2.platform.mod.model.exceptions.deploymentartifact.BlueprintFileNameCreateException; +import org.onap.dcaegen2.platform.mod.model.microserviceinstance.MsInstance; +import org.springframework.stereotype.Component; + +/** + * A name creator for Deployment Artifact files. + */ +@Component +public class ArtifactFileNameCreator { + + private static final String FILE_FORMAT = ".yaml"; + + /** + * creates a file name + * @param msInstance + * @param version + * @return + */ + public String createFileName(MsInstance msInstance, int version) { + if(msInstance.getMsInfo() == null || !msInstance.getMsInfo().containsKey("tag")){ + throwException("MS-tag"); + } + if(msInstance.getActiveSpec() == null){ + throwException("active-spec"); + } + return msInstance.getMsInfo().get("tag") + "_" + + msInstance.getActiveSpec().getType().toString().toLowerCase() + "_" + + msInstance.getRelease() + "_" + + version + + FILE_FORMAT; + } + + private void throwException(String missingProperty) { + throw new BlueprintFileNameCreateException("Can not create bluerprint file name: " + + missingProperty + " is missing"); + } +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactGateway.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactGateway.java new file mode 100644 index 0000000..6bf2c2a --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactGateway.java @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 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.dcaegen2.platform.mod.web.service.deploymentartifact; + +import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifact; +import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifactSearch; + +import java.util.List; +import java.util.Optional; + + /** + * An interface to interact with DeploymentArtifact persistence + */ +public interface DeploymentArtifactGateway { + + List<DeploymentArtifact> findAll(); + + List<DeploymentArtifact> findByMsInstanceId(String id); + + Optional<DeploymentArtifact> findById(String id); + + void deleteById(String deploymentArtifactId); + + DeploymentArtifact save(DeploymentArtifact deploymentArtifact); + + List<DeploymentArtifact> findAll(DeploymentArtifactSearch search); +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactGeneratorStrategy.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactGeneratorStrategy.java new file mode 100644 index 0000000..9bb0870 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactGeneratorStrategy.java @@ -0,0 +1,32 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 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.dcaegen2.platform.mod.web.service.deploymentartifact; + +import org.onap.dcaegen2.platform.mod.model.specification.Specification; + +import java.util.Map; + +/** + * provides abstraction to generate Deployment Artifacts + */ +public interface DeploymentArtifactGeneratorStrategy { + Map<String, Object> generateForRelease(Specification activeSpec, String release); +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactService.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactService.java new file mode 100644 index 0000000..3e7f899 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactService.java @@ -0,0 +1,51 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 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.dcaegen2.platform.mod.web.service.deploymentartifact; + +import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifact; +import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifactSearch; +import org.onap.dcaegen2.platform.mod.model.microserviceinstance.MsInstance; +import org.onap.dcaegen2.platform.mod.model.restapi.DeploymentArtifactPatchRequest; + +import java.util.List; + +/** + * An interface to access DeploymentArtifact Services + */ +public interface DeploymentArtifactService { + + DeploymentArtifact generateDeploymentArtifact(String msInstanceId, String user); + + List<DeploymentArtifact> getAllDeploymentArtifacts(); + + DeploymentArtifact findDeploymentArtifactById(String id); + + void updateDeploymentArtifact(String deploymentArtifactId, DeploymentArtifactPatchRequest deploymentArtifactPatchRequest, String user); + + List<DeploymentArtifact> findByMsInstanceId(String msInstanceId); + + void deleteDeploymentArtifact(String deploymentArtifactId); + + void updateMsInstanceRef(MsInstance msInstance); + + List<DeploymentArtifact> searchDeploymentArtifacts(DeploymentArtifactSearch search); +} + diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactServiceImpl.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactServiceImpl.java new file mode 100644 index 0000000..8b97bba --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactServiceImpl.java @@ -0,0 +1,205 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 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.dcaegen2.platform.mod.web.service.deploymentartifact; + +import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifact; +import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifactSearch; +import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifactStatus; +import org.onap.dcaegen2.platform.mod.model.deploymentartifact.MsInstanceInfo; +import org.onap.dcaegen2.platform.mod.model.exceptions.deploymentartifact.DeploymentArtifactNotFound; +import org.onap.dcaegen2.platform.mod.model.microserviceinstance.DeploymentArtifactsRef; +import org.onap.dcaegen2.platform.mod.model.microserviceinstance.MsInstance; +import org.onap.dcaegen2.platform.mod.model.restapi.DeploymentArtifactPatchRequest; +import org.onap.dcaegen2.platform.mod.model.specification.Specification; +import org.onap.dcaegen2.platform.mod.web.service.microserviceinstance.MsInstanceService; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.*; + +/** + * DeploymentArtifact Service implementation + */ +@Service +@Slf4j +@Setter +public class DeploymentArtifactServiceImpl implements DeploymentArtifactService{ + + private static final String VERSION_KEY = "mostRecentVersion"; + + @Autowired + private MsInstanceService msInstanceService; + + @Autowired + private DeploymentArtifactGeneratorStrategy deploymentArtifactGeneratorStrategy; + + @Autowired + private DeploymentArtifactGateway deploymentArtifactGateway; + + @Autowired + private ArtifactFileNameCreator fileNameCreator; + + @Autowired + private DeploymentArtifactStatusChangeHandler statusChangeHandler; + + ///////////////FIND METHODS////////////////////////// + @Override + public List<DeploymentArtifact> getAllDeploymentArtifacts() { + return deploymentArtifactGateway.findAll(); + } + + @Override + public List<DeploymentArtifact> searchDeploymentArtifacts(DeploymentArtifactSearch search) { + return deploymentArtifactGateway.findAll(search); + } + + @Override + public DeploymentArtifact findDeploymentArtifactById(String id){ + return deploymentArtifactGateway.findById(id).orElseThrow( + () -> new DeploymentArtifactNotFound("Deployment Artifact with id " + id + " not found") + ); + } + + @Override + public List<DeploymentArtifact> findByMsInstanceId(String msInstanceId) { + return deploymentArtifactGateway.findByMsInstanceId(msInstanceId); + } + + @Override + @Transactional + public void deleteDeploymentArtifact(String deploymentArtifactId) { + DeploymentArtifact deploymentArtifact = findDeploymentArtifactById(deploymentArtifactId); + log.info("deleting {}", deploymentArtifact.getFileName()); + deploymentArtifactGateway.deleteById(deploymentArtifactId); + msInstanceService.removeDeploymentArtifactFromMsInstance(deploymentArtifact); + } + + @Override + @Transactional + public void updateMsInstanceRef(MsInstance msInstance) { + List<DeploymentArtifact> deploymentArtifacts = findByMsInstanceId(msInstance.getId()); + deploymentArtifacts.forEach((deploymentArtifact) -> { + deploymentArtifact.getMsInstanceInfo().setName(msInstance.getName()); + deploymentArtifact.getMsInstanceInfo().setRelease(msInstance.getRelease()); + deploymentArtifactGateway.save(deploymentArtifact); + }); + } + + ////////////////////////////////////////////////////// + + @Override + @Transactional + //only status update was implemented + public void updateDeploymentArtifact(String deploymentArtifactId, DeploymentArtifactPatchRequest deploymentArtifactPatchRequest, + String user) { + DeploymentArtifact deploymentArtifact = findDeploymentArtifactById(deploymentArtifactId); + updateStatus(deploymentArtifactPatchRequest, deploymentArtifact); + updateMetadata(user, deploymentArtifact); + log.info("Updating the artifact in database.."); + deploymentArtifactGateway.save(deploymentArtifact); + msInstanceService.updateStatusBasedOnDeploymentArtifactsStatuses(deploymentArtifact.getMsInstanceInfo().getId()); + } + + private void updateMetadata(String user, DeploymentArtifact deploymentArtifact) { + deploymentArtifact.getMetadata().put("updatedBy", user); + deploymentArtifact.getMetadata().put("updatedOn", new Date()); + } + + private void updateStatus(DeploymentArtifactPatchRequest deploymentArtifactPatchRequest, DeploymentArtifact deploymentArtifact) { + DeploymentArtifactStatus changeToStatus = deploymentArtifactPatchRequest.getStatus(); + if(changeToStatus != null){ + log.info("Sent request to deployment artifact status change handler: {}", changeToStatus); + statusChangeHandler.handleStatusChange(changeToStatus, deploymentArtifact); + } + } + + @Override + @Transactional + public DeploymentArtifact generateDeploymentArtifact(String msInstanceId, String user) { + MsInstance msInstance = msInstanceService.getMsInstanceById(msInstanceId); + + //Generate the Blueprint for the active specification for the instance + Map<String, Object> deploymentArtifact = deploymentArtifactGeneratorStrategy.generateForRelease(msInstance.getActiveSpec(), msInstance.getRelease()); + + DeploymentArtifact artifact = new DeploymentArtifact(); + artifact.setContent(String.valueOf(deploymentArtifact.get("content"))); + artifact.setVersion(updateLatestVersion(msInstance.getDeploymentArtifactsInfo())); + artifact.setStatus(DeploymentArtifactStatus.IN_DEV); + artifact.setMsInstanceInfo(createMsInstanceReferenceInfo(msInstance)); + artifact.setSpecificationInfo(createSpecificationReferenceInfo(msInstance.getActiveSpec())); + artifact.setMetadata(createMetadata(user)); + + artifact.setFileName(fileNameCreator.createFileName(msInstance, artifact.getVersion())); + + DeploymentArtifact savedDao = deploymentArtifactGateway.save(artifact); + artifact.setId(savedDao.getId()); + + msInstance.setDeploymentArtifactsInfo(updateMsDeploymentArtifactRef(msInstance.getDeploymentArtifactsInfo(), savedDao.getId())); + msInstanceService.updateMsInstance(msInstance); + + return artifact; + } + + private int updateLatestVersion(DeploymentArtifactsRef ref) { + if(ref == null) return 1; + else return ref.getMostRecentVersion() + 1; + } + + private DeploymentArtifactsRef updateMsDeploymentArtifactRef(DeploymentArtifactsRef ref, String deploymentArtifactId) { + if(ref == null){ + ref = new DeploymentArtifactsRef(); + ref.setMostRecentVersion(1); + List<String> deploymentArtifacts = new ArrayList<>(); + deploymentArtifacts.add(deploymentArtifactId); + ref.setDeploymentArtifacts(deploymentArtifacts); + } + else{ + ref.setMostRecentVersion(ref.getMostRecentVersion() + 1); + List<String> deploymentArtifactList = ref.getDeploymentArtifacts(); + deploymentArtifactList.add(deploymentArtifactId); + } + return ref; + } + + private Map<String, Object> createMetadata(String user) { + Map<String, Object> metadata = new HashMap<>(); + metadata.put("createdOn", new Date()); + metadata.put("createdBy", user); + return metadata; + } + + private Map<String, Object> createSpecificationReferenceInfo(Specification activeSpec) { + Map<String, Object> specInfo = new HashMap<>(); + specInfo.put("id", activeSpec.getId()); + return specInfo; + } + + private MsInstanceInfo createMsInstanceReferenceInfo(MsInstance msInstance) { + MsInstanceInfo msInstanceInfo = new MsInstanceInfo(); + msInstanceInfo.setId(msInstance.getId()); + msInstanceInfo.setName(msInstance.getName()); + msInstanceInfo.setRelease(msInstance.getRelease()); + return msInstanceInfo; + } +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactStatusChangeHandler.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactStatusChangeHandler.java new file mode 100644 index 0000000..48b18bf --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactStatusChangeHandler.java @@ -0,0 +1,77 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 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.dcaegen2.platform.mod.web.service.deploymentartifact; + +import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifact; +import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifactStatus; +import org.onap.dcaegen2.platform.mod.model.exceptions.deploymentartifact.StatusChangeNotValidException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * A class responsible for handling status changes of Deployment Artifacts + */ +@Component +@Slf4j +public class DeploymentArtifactStatusChangeHandler { + + @Autowired + DeploymentArtifactService deploymentArtifactService; + + /** + * setter + * @param deploymentArtifactService + */ + public void setDeploymentArtifactService(DeploymentArtifactService deploymentArtifactService) { + this.deploymentArtifactService = deploymentArtifactService; + } + + /** + * handles status changes + * @param status + * @param deploymentArtifact + */ + public void handleStatusChange(DeploymentArtifactStatus status, DeploymentArtifact deploymentArtifact) { + String msInstanceId = deploymentArtifact.getMsInstanceInfo().getId(); + List<DeploymentArtifact> artifacts = deploymentArtifactService.findByMsInstanceId(msInstanceId); + if( status == DeploymentArtifactStatus.DEV_COMPLETE){ + for(DeploymentArtifact artifact : artifacts){ + if(artifact.getStatus() == DeploymentArtifactStatus.DEV_COMPLETE){ + log.error("Status change is not allowed."); + throw new StatusChangeNotValidException(createValidationErrorMessage(deploymentArtifact)); + } + } + } + deploymentArtifact.setStatus(status); + log.info("Deployment Artifact's status changed successfully."); + } + + private String createValidationErrorMessage(DeploymentArtifact artifact) { + return String.format( "%s (v%d) for %s - Status change not allowed." + + " Only 1 blueprint can be in the DEV_COMPLETE state. " + + "Change the current DEV_COMPLETE blueprint to NOT_NEEDED or IN_DEV before changing another" + + " to DEV_COMPLETE.", artifact.getMsInstanceInfo().getName(), + artifact.getVersion(), artifact.getMsInstanceInfo().getRelease()); + } +} |