[DCAE] INFO.yaml update
[dcaegen2/platform.git] / mod2 / catalog-service / src / main / java / org / onap / dcaegen2 / platform / mod / web / service / microserviceinstance / MsInstanceStatusChangeHandler.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  org.onap.dcae
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
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.platform.mod.web.service.microserviceinstance;
22
23 import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifact;
24 import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifactStatus;
25 import org.onap.dcaegen2.platform.mod.model.microserviceinstance.MsInstance;
26 import org.onap.dcaegen2.platform.mod.model.microserviceinstance.MsInstanceStatus;
27 import org.onap.dcaegen2.platform.mod.web.service.deploymentartifact.DeploymentArtifactService;
28 import lombok.extern.slf4j.Slf4j;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Component;
31
32 import java.util.List;
33
34 /**
35  * A class responsible for handling status changes of Ms Instances
36  */
37 @Component
38 @Slf4j
39 public class MsInstanceStatusChangeHandler {
40
41     @Autowired
42     private MsInstanceService msInstanceService;
43
44     @Autowired
45     private DeploymentArtifactService deploymentArtifactService;
46
47     public void setMsInstanceService(MsInstanceService msInstanceService) {
48         this.msInstanceService = msInstanceService;
49     }
50
51     public void setDeploymentArtifactService(DeploymentArtifactService deploymentArtifactService) {
52         this.deploymentArtifactService = deploymentArtifactService;
53     }
54
55     public void updateStatusBasedOnDeploymentArtifactsStatuses(MsInstance msInstance) {
56         log.info("Checking if any Status change required for msInstance {}...", msInstance);
57         List<DeploymentArtifact> artifacts = deploymentArtifactService.findByMsInstanceId(msInstance.getId());
58         MsInstanceStatus newStatus = getValidStatusBasedOnArtifacts(artifacts);
59         msInstance.setStatus(newStatus);
60         log.info("Changed Status to {}", newStatus);
61     }
62
63     private MsInstanceStatus getValidStatusBasedOnArtifacts(List<DeploymentArtifact> artifacts) {
64         if(atLeastOneArtifactHasDevCompleteStatus(artifacts)){
65             return MsInstanceStatus.DEV_COMPLETE;
66         }
67         return MsInstanceStatus.IN_DEV;
68     }
69
70     private boolean atLeastOneArtifactHasDevCompleteStatus(List<DeploymentArtifact> artifacts) {
71          return artifacts
72                  .stream()
73                  .anyMatch(artifact -> artifact.getStatus() == DeploymentArtifactStatus.DEV_COMPLETE);
74     }
75 }