summaryrefslogtreecommitdiffstats
path: root/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/microserviceinstance/MsInstanceServiceImpl.java
blob: e4d5694e19f56c03529735e9775dece846cadc1a (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
206
207
208
209
210
211
212
213
/*
 * ============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.microserviceinstance;

import org.onap.dcaegen2.platform.mod.model.basemicroservice.BaseMicroservice;
import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifact;
import org.onap.dcaegen2.platform.mod.model.exceptions.msinstance.MsInstanceAlreadyExistsException;
import org.onap.dcaegen2.platform.mod.model.exceptions.msinstance.MsInstanceNotFoundException;
import org.onap.dcaegen2.platform.mod.model.microserviceinstance.MsInstance;
import org.onap.dcaegen2.platform.mod.model.microserviceinstance.MsInstanceStatus;
import org.onap.dcaegen2.platform.mod.model.restapi.MsInstanceRequest;
import org.onap.dcaegen2.platform.mod.model.restapi.MsInstanceUpdateRequest;
import org.onap.dcaegen2.platform.mod.web.service.basemicroservice.MsService;
import org.onap.dcaegen2.platform.mod.web.service.deploymentartifact.DeploymentArtifactService;
import org.onap.dcaegen2.platform.mod.web.service.specification.SpecificationService;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * MsInstance Service implementation
 */
@Service
@Setter
@Slf4j
public class MsInstanceServiceImpl implements MsInstanceService {

    @Autowired
    private MsInstanceGateway msInstanceRepository;

    @Autowired
    private MsService msService;

    @Autowired
    private MsInstanceStatusChangeHandler msInstanceStatusChangeHandler;

    @Autowired
    private SpecificationService specificationService;

    @Autowired
    private DeploymentArtifactService deploymentArtifactService;

    @Override
    public List<MsInstance> getAll() {
        return msInstanceRepository.findAll();
    }

    @Override
    @Transactional
    public MsInstance createMicroserviceInstance(String msName, MsInstanceRequest request) {
        BaseMicroservice microservice = msService.getMicroserviceByName(msName);
        checkIftheCombinationOfNameAndReleaseIsUnique(request.getName(), request.getRelease());
        MsInstance msInstance = new MsInstanceCreator(request, microservice).create();
        MsInstance savedMsInstance = msInstanceRepository.save(msInstance);
        msService.saveMsInstanceReferenceToMs(microservice, savedMsInstance);
        return savedMsInstance;
    }

    private void checkIftheCombinationOfNameAndReleaseIsUnique(String name, String release) {
        if (msInstanceRepository.findByNameAndRelease(name, release).isPresent())
            throw new MsInstanceAlreadyExistsException();
    }

    @Override
    public MsInstance getMsInstanceById(String id) {
        return msInstanceRepository.findById(id).orElseThrow(() ->
                new MsInstanceNotFoundException(String.format("Ms Instance with id %s not found", id)));
    }

    @Override
    public void updateMsInstance(MsInstance msInstance) {
        log.info("Saving the msInstance {} to database..", msInstance);
        if(msInstance != null) msInstanceRepository.save(msInstance);
    }

    @Override
    public void updateStatusBasedOnDeploymentArtifactsStatuses(String msInstanceId) {
        MsInstance msInstance = getMsInstanceById(msInstanceId);
        msInstanceStatusChangeHandler.updateStatusBasedOnDeploymentArtifactsStatuses(msInstance);
        updateMsInstance(msInstance);
    }

    @Override
    @Transactional
    public void removeDeploymentArtifactFromMsInstance(DeploymentArtifact deploymentArtifact) {
        MsInstance msInstance = getMsInstanceById(deploymentArtifact.getMsInstanceInfo().getId());
        removeDeploymentArtifactReferenceFromMsInstance(msInstance, deploymentArtifact.getId());
        msInstanceStatusChangeHandler.updateStatusBasedOnDeploymentArtifactsStatuses(msInstance);
        updateMsInstance(msInstance);
    }

    @Override
    //TODO: update msInstanceReference in specification and deployment artifact
    public void updateMicroserviceReference(BaseMicroservice microservice) {
        List<Map<String, String>> msInstanceRefs = microservice.getMsInstances();
        for(Map<String, String> ref : msInstanceRefs){
            MsInstance msInstance = getMsInstanceById(ref.get("id"));
            msInstance.setName(microservice.getName());
            msInstance.getMsInfo().put("name", microservice.getName());
            cascadeUpdates(msInstance);
            msInstanceRepository.save(msInstance);
        }
    }

    @Override
    @Transactional
    public MsInstance updateMsInstance(MsInstanceUpdateRequest updateRequest, String msInstanceId) {
        MsInstance msInstance = getMsInstanceById(msInstanceId);
        updateRelease(updateRequest, msInstance);
        updateVersion(updateRequest, msInstance);
        updateMetadata(updateRequest, msInstance);
        cascadeUpdates(msInstance);
        return msInstanceRepository.save(msInstance);
    }

    private void cascadeUpdates(MsInstance msInstance) {
        specificationService.updateMsInstanceRef(msInstance);
        deploymentArtifactService.updateMsInstanceRef(msInstance);
        msService.updateMsInstanceRef(msInstance);
    }

    private void updateMetadata(MsInstanceUpdateRequest updateRequest, MsInstance msInstance) {
        if(updateRequest.getMetadata() != null){
            msInstance.getMetadata().putAll(updateRequest.getMetadata());
        }

        msInstance.getMetadata().put("updatedOn", new Date());
        msInstance.getMetadata().put("updatedBy", updateRequest.getUser());
    }

    private void updateVersion(MsInstanceUpdateRequest updateRequest, MsInstance msInstance) {
        if(updateRequest.getVersion() != null){
            msInstance.setVersion(updateRequest.getVersion());
        }
    }

    private void updateRelease(MsInstanceUpdateRequest updateRequest, MsInstance msInstance) {
        if(updateRequest.getRelease() != null) {
            if(!updateRequest.getRelease().equals(msInstance.getRelease()))
                checkIftheCombinationOfNameAndReleaseIsUnique(msInstance.getName(), updateRequest.getRelease());
            msInstance.setRelease(updateRequest.getRelease());
        }
    }

    private void removeDeploymentArtifactReferenceFromMsInstance(MsInstance msInstance, String deploymentArtifactId) {
        if(msInstance.getDeploymentArtifactsInfo() != null){
            List<String> refIds = msInstance.getDeploymentArtifactsInfo().getDeploymentArtifacts();
            refIds.remove(deploymentArtifactId);
        }
    }

    private class MsInstanceCreator {
        private MsInstanceRequest request;
        private BaseMicroservice microserviceDAO;

        MsInstanceCreator(MsInstanceRequest request, BaseMicroservice microserviceDAO) {
            this.request = request;
            this.microserviceDAO = microserviceDAO;
        }

        MsInstance create() {
            //prepare MsInstance from the request
            return MsInstance.builder()
                    .name(request.getName())
                    .release(request.getRelease())
                    .status(MsInstanceStatus.NEW)
                    .version(request.getVersion())
                    .msInfo(getMsReference(microserviceDAO))
                    .metadata(getMetadata(request))
                    .build();
        }

        private Map<String, Object> getMsReference(BaseMicroservice microserviceDAO) {
            Map<String,Object> msInfo = new HashMap<>();
            msInfo.put("id", microserviceDAO.getId());
            msInfo.put("name", microserviceDAO.getName());
            msInfo.put("tag", microserviceDAO.getTag());
            return msInfo;
        }

        private Map<String, Object> getMetadata(MsInstanceRequest request) {
            Map<String, Object> metadata = request.getMetadata();
            metadata.put("createdBy", request.getUser());
            metadata.put("createdOn", new Date());
            return metadata;
        }
    }
}