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.basemicroservice;
23 import org.onap.dcaegen2.platform.mod.model.basemicroservice.BaseMicroservice;
24 import org.onap.dcaegen2.platform.mod.model.basemicroservice.BaseMsStatus;
25 import org.onap.dcaegen2.platform.mod.model.common.AuditFields;
26 import org.onap.dcaegen2.platform.mod.model.exceptions.ErrorMessages;
27 import org.onap.dcaegen2.platform.mod.model.exceptions.ResourceConflictException;
28 import org.onap.dcaegen2.platform.mod.model.exceptions.basemicroservice.BaseMicroserviceNotFoundException;
29 import org.onap.dcaegen2.platform.mod.model.microserviceinstance.MsInstance;
30 import org.onap.dcaegen2.platform.mod.model.restapi.MicroserviceCreateRequest;
31 import org.onap.dcaegen2.platform.mod.model.restapi.MicroserviceUpdateRequest;
32 import org.onap.dcaegen2.platform.mod.web.service.microserviceinstance.MsInstanceService;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Service;
37 import java.util.Date;
38 import java.util.HashMap;
39 import java.util.List;
43 * MsService implementation
46 public class MsServiceImpl implements MsService {
50 private BaseMicroserviceGateway repository;
54 private MsInstanceService msInstanceService;
57 * creates Microservice record
58 * @param microserviceRequest
62 public BaseMicroservice createMicroservice(MicroserviceCreateRequest microserviceRequest) {
63 checkIfThereAreAnyConflicts(microserviceRequest); //TODO: Make fields unique in entity itself
64 BaseMicroservice microservice = new BaseMsCreator().create(microserviceRequest);
65 return repository.save(microservice);
69 * name, tag and serviceName are unique for the given ms. This method make sure that.
71 private void checkIfThereAreAnyConflicts(MicroserviceCreateRequest microserviceRequest) {
72 checkIfMsNameAlreadyExists(microserviceRequest.getName());
73 checkIfMsTagAlreadyExists(microserviceRequest.getTag());
74 checkiIfServiceNameAlreadyExists(microserviceRequest.getServiceName());
77 private void checkIfMsNameAlreadyExists(String msName) {
78 if (repository.findByName(msName).isPresent())
79 throw new ResourceConflictException(ErrorMessages.MICROSERVICE_NAME_CONFLICT_MESSAGE);
82 private void checkIfMsTagAlreadyExists(String msTag) {
83 if (repository.findByTag(msTag).isPresent())
84 throw new ResourceConflictException(ErrorMessages.MICROSERVICE_TAG_CONFLICT_MESSAGE);
87 private void checkiIfServiceNameAlreadyExists(String serviceName) {
88 boolean serviceNameIsEmpty = serviceName == null || serviceName.isEmpty();
89 if(serviceNameIsEmpty)
91 if (repository.findByServiceName(serviceName).isPresent())
92 throw new ResourceConflictException(ErrorMessages.MS_SERVICE_NAME_CONFLICT_MESSAGE);
96 * lists all microservice records
100 public List<BaseMicroservice> getAllMicroservices() {
101 return repository.findAll();
105 * gets a Mioroservice by id
110 public BaseMicroservice getMicroserviceById(String baseMsId) {
111 return repository.findById(baseMsId).orElseThrow(() ->
112 new BaseMicroserviceNotFoundException(String.format("Microservice with id %s not found", baseMsId)));
116 * gets a Microservice by name
121 public BaseMicroservice getMicroserviceByName(String msName) {
122 return repository.findByName(msName).orElseThrow(() ->
123 new BaseMicroserviceNotFoundException(String.format("Microservice with name %s not found", msName)));
127 * updates a Microservice
128 * @param requestedMsId
129 * @param updateRequest
132 public void updateMicroservice(String requestedMsId, MicroserviceUpdateRequest updateRequest) {
133 BaseMicroservice microservice = getMicroserviceById(requestedMsId);
134 updateMetadata(updateRequest, microservice);
135 updateOtherFields(updateRequest, microservice);
136 repository.save(microservice);
137 msInstanceService.updateMicroserviceReference(microservice);
140 //TODO: Get rid of nulls!
141 private void updateMetadata(MicroserviceUpdateRequest updateRequest, BaseMicroservice microservice) {
142 if(updateRequest.getUser() != null){
143 microservice.getMetadata().setUpdatedBy(updateRequest.getUser());
145 if(updateRequest.getMetadata() != null && updateRequest.getMetadata().containsKey("notes")){
146 microservice.getMetadata().setNotes((String) updateRequest.getMetadata().get("notes"));
148 if(updateRequest.getMetadata() != null && updateRequest.getMetadata().containsKey("labels")){
149 microservice.getMetadata().setLabels((List<String>) updateRequest.getMetadata().get("labels"));
151 microservice.getMetadata().setUpdatedOn(new Date());
154 private void updateOtherFields(MicroserviceUpdateRequest updateRequest, BaseMicroservice microservice) {
155 if(updateRequest.getName() != null){
156 updateName(updateRequest, microservice);
158 if(updateRequest.getType() != null){
159 microservice.setType(updateRequest.getType());
161 if(updateRequest.getLocation() != null){
162 microservice.setLocation(updateRequest.getLocation());
164 if(updateRequest.getServiceName() != null){
165 updateServiceName(updateRequest, microservice);
167 if(updateRequest.getNamespace() != null){
168 microservice.setNamespace(updateRequest.getNamespace());
173 * If name requested in the updateRequest doesn't match the name of the ms record which is being worked on,
174 * then only check for the uniqueness.
176 private void updateName(MicroserviceUpdateRequest updateRequest, BaseMicroservice microservice) {
177 boolean notMatchesWithCurrentName = !updateRequest.getName().equals(microservice.getName());
178 if(notMatchesWithCurrentName)
179 checkIfMsNameAlreadyExists(updateRequest.getName());
180 microservice.setName(updateRequest.getName());
184 * If serviceName requested in the updateRequest doesn't match the serviceName of the ms record which is
185 * being worked on, then only check for the uniqueness.
187 private void updateServiceName(MicroserviceUpdateRequest updateRequest, BaseMicroservice microservice) {
188 boolean notMatchesWithCurrentServiceName = !updateRequest.getServiceName().equals(microservice.getServiceName());
189 if(notMatchesWithCurrentServiceName)
190 checkiIfServiceNameAlreadyExists(updateRequest.getServiceName());
191 microservice.setServiceName(updateRequest.getServiceName());
195 * saves msInstance reference in a given Microservice
196 * @param microservice
200 public void saveMsInstanceReferenceToMs(BaseMicroservice microservice, MsInstance msInstance) {
201 microservice.getMsInstances().add(getMsInstanceReference(msInstance));
202 repository.save(microservice);
206 * updates MsIntstance ref in Microservice record
210 public void updateMsInstanceRef(MsInstance msInstance) {
211 BaseMicroservice microservice = getMicroserviceById((String) msInstance.getMsInfo().get("id"));
212 List<Map<String, String>> msInstancesRef = microservice.getMsInstances();
213 msInstancesRef.forEach((ref) -> {
214 if(ref.get("id").equals(msInstance.getId()))
215 ref.put("name", msInstance.getName());
217 repository.save(microservice);
220 private Map<String, String> getMsInstanceReference(MsInstance msInstance) {
221 Map<String,String> msInstanceInfo = new HashMap<>();
222 msInstanceInfo.put("id", msInstance.getId());
223 msInstanceInfo.put("name", msInstance.getName());
224 return msInstanceInfo;
227 private class BaseMsCreator {
229 BaseMicroservice create(MicroserviceCreateRequest createRequest) {
230 BaseMicroservice microservice = new BaseMicroservice();
231 microservice.setLocation(createRequest.getLocation());
232 microservice.setName(createRequest.getName());
233 microservice.setTag(createRequest.getTag());
234 microservice.setServiceName(createRequest.getServiceName());
235 microservice.setNamespace(createRequest.getNamespace());
236 microservice.setStatus(BaseMsStatus.ACTIVE);
237 microservice.setType(createRequest.getType());
238 microservice.setMetadata(getMetadataFields(createRequest));
242 private AuditFields getMetadataFields(MicroserviceCreateRequest request) {
243 AuditFields auditFields = AuditFields.builder().build();
244 auditFields.setCreatedBy(request.getUser());
245 auditFields.setCreatedOn(new Date());
247 if (request.getMetadata().containsKey("notes"))
248 auditFields.setNotes((String) request.getMetadata().get("notes"));
249 if (request.getMetadata().containsKey("labels"))
250 // auditFields.setLabels((List<String>) request.getMetadata().get("labels"));
251 auditFields.setLabels(request.getMetadata().get("labels"));