2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.platform.mod.web.service.microserviceinstance;
23 import org.onap.dcaegen2.platform.mod.model.basemicroservice.BaseMicroservice;
24 import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifact;
25 import org.onap.dcaegen2.platform.mod.model.exceptions.msinstance.MsInstanceAlreadyExistsException;
26 import org.onap.dcaegen2.platform.mod.model.exceptions.msinstance.MsInstanceNotFoundException;
27 import org.onap.dcaegen2.platform.mod.model.microserviceinstance.MsInstance;
28 import org.onap.dcaegen2.platform.mod.model.microserviceinstance.MsInstanceStatus;
29 import org.onap.dcaegen2.platform.mod.model.restapi.MsInstanceRequest;
30 import org.onap.dcaegen2.platform.mod.model.restapi.MsInstanceUpdateRequest;
31 import org.onap.dcaegen2.platform.mod.web.service.basemicroservice.MsService;
32 import org.onap.dcaegen2.platform.mod.web.service.deploymentartifact.DeploymentArtifactService;
33 import org.onap.dcaegen2.platform.mod.web.service.specification.SpecificationService;
35 import lombok.extern.slf4j.Slf4j;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Service;
38 import org.springframework.transaction.annotation.Transactional;
40 import java.util.Date;
41 import java.util.HashMap;
42 import java.util.List;
46 * MsInstance Service implementation
51 public class MsInstanceServiceImpl implements MsInstanceService {
54 private MsInstanceGateway msInstanceRepository;
57 private MsService msService;
60 private MsInstanceStatusChangeHandler msInstanceStatusChangeHandler;
63 private SpecificationService specificationService;
66 private DeploymentArtifactService deploymentArtifactService;
69 public List<MsInstance> getAll() {
70 return msInstanceRepository.findAll();
75 public MsInstance createMicroserviceInstance(String msName, MsInstanceRequest request) {
76 BaseMicroservice microservice = msService.getMicroserviceByName(msName);
77 checkIftheCombinationOfNameAndReleaseIsUnique(request.getName(), request.getRelease());
78 MsInstance msInstance = new MsInstanceCreator(request, microservice).create();
79 MsInstance savedMsInstance = msInstanceRepository.save(msInstance);
80 msService.saveMsInstanceReferenceToMs(microservice, savedMsInstance);
81 return savedMsInstance;
84 private void checkIftheCombinationOfNameAndReleaseIsUnique(String name, String release) {
85 if (msInstanceRepository.findByNameAndRelease(name, release).isPresent())
86 throw new MsInstanceAlreadyExistsException();
90 public MsInstance getMsInstanceById(String id) {
91 return msInstanceRepository.findById(id).orElseThrow(() ->
92 new MsInstanceNotFoundException(String.format("Ms Instance with id %s not found", id)));
96 public void updateMsInstance(MsInstance msInstance) {
97 log.info("Saving the msInstance {} to database..", msInstance);
98 if(msInstance != null) msInstanceRepository.save(msInstance);
102 public void updateStatusBasedOnDeploymentArtifactsStatuses(String msInstanceId) {
103 MsInstance msInstance = getMsInstanceById(msInstanceId);
104 msInstanceStatusChangeHandler.updateStatusBasedOnDeploymentArtifactsStatuses(msInstance);
105 updateMsInstance(msInstance);
110 public void removeDeploymentArtifactFromMsInstance(DeploymentArtifact deploymentArtifact) {
111 MsInstance msInstance = getMsInstanceById(deploymentArtifact.getMsInstanceInfo().getId());
112 removeDeploymentArtifactReferenceFromMsInstance(msInstance, deploymentArtifact.getId());
113 msInstanceStatusChangeHandler.updateStatusBasedOnDeploymentArtifactsStatuses(msInstance);
114 updateMsInstance(msInstance);
118 //TODO: update msInstanceReference in specification and deployment artifact
119 public void updateMicroserviceReference(BaseMicroservice microservice) {
120 List<Map<String, String>> msInstanceRefs = microservice.getMsInstances();
121 for(Map<String, String> ref : msInstanceRefs){
122 MsInstance msInstance = getMsInstanceById(ref.get("id"));
123 msInstance.setName(microservice.getName());
124 msInstance.getMsInfo().put("name", microservice.getName());
125 cascadeUpdates(msInstance);
126 msInstanceRepository.save(msInstance);
132 public MsInstance updateMsInstance(MsInstanceUpdateRequest updateRequest, String msInstanceId) {
133 MsInstance msInstance = getMsInstanceById(msInstanceId);
134 updateRelease(updateRequest, msInstance);
135 updateVersion(updateRequest, msInstance);
136 updateMetadata(updateRequest, msInstance);
137 cascadeUpdates(msInstance);
138 return msInstanceRepository.save(msInstance);
141 private void cascadeUpdates(MsInstance msInstance) {
142 specificationService.updateMsInstanceRef(msInstance);
143 deploymentArtifactService.updateMsInstanceRef(msInstance);
144 msService.updateMsInstanceRef(msInstance);
147 private void updateMetadata(MsInstanceUpdateRequest updateRequest, MsInstance msInstance) {
148 if(updateRequest.getMetadata() != null){
149 msInstance.getMetadata().putAll(updateRequest.getMetadata());
152 msInstance.getMetadata().put("updatedOn", new Date());
153 msInstance.getMetadata().put("updatedBy", updateRequest.getUser());
156 private void updateVersion(MsInstanceUpdateRequest updateRequest, MsInstance msInstance) {
157 if(updateRequest.getVersion() != null){
158 msInstance.setVersion(updateRequest.getVersion());
162 private void updateRelease(MsInstanceUpdateRequest updateRequest, MsInstance msInstance) {
163 if(updateRequest.getRelease() != null) {
164 if(!updateRequest.getRelease().equals(msInstance.getRelease()))
165 checkIftheCombinationOfNameAndReleaseIsUnique(msInstance.getName(), updateRequest.getRelease());
166 msInstance.setRelease(updateRequest.getRelease());
170 private void removeDeploymentArtifactReferenceFromMsInstance(MsInstance msInstance, String deploymentArtifactId) {
171 if(msInstance.getDeploymentArtifactsInfo() != null){
172 List<String> refIds = msInstance.getDeploymentArtifactsInfo().getDeploymentArtifacts();
173 refIds.remove(deploymentArtifactId);
177 private class MsInstanceCreator {
178 private MsInstanceRequest request;
179 private BaseMicroservice microserviceDAO;
181 MsInstanceCreator(MsInstanceRequest request, BaseMicroservice microserviceDAO) {
182 this.request = request;
183 this.microserviceDAO = microserviceDAO;
186 MsInstance create() {
187 //prepare MsInstance from the request
188 return MsInstance.builder()
189 .name(request.getName())
190 .release(request.getRelease())
191 .status(MsInstanceStatus.NEW)
192 .version(request.getVersion())
193 .msInfo(getMsReference(microserviceDAO))
194 .metadata(getMetadata(request))
198 private Map<String, Object> getMsReference(BaseMicroservice microserviceDAO) {
199 Map<String,Object> msInfo = new HashMap<>();
200 msInfo.put("id", microserviceDAO.getId());
201 msInfo.put("name", microserviceDAO.getName());
202 msInfo.put("tag", microserviceDAO.getTag());
206 private Map<String, Object> getMetadata(MsInstanceRequest request) {
207 Map<String, Object> metadata = request.getMetadata();
208 metadata.put("createdBy", request.getUser());
209 metadata.put("createdOn", new Date());