summaryrefslogtreecommitdiffstats
path: root/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/deploymentartifact/DeploymentArtifactServiceImpl.java
blob: 8b97bbaa5e4374764091207c2ce6f5b0328e98a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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;
    }
}