2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.loop.template;
26 import java.util.List;
27 import org.onap.clamp.clds.exception.sdc.controller.BlueprintParserException;
28 import org.onap.clamp.clds.sdc.controller.installer.BlueprintMicroService;
29 import org.onap.clamp.clds.sdc.controller.installer.BlueprintParser;
30 import org.onap.clamp.clds.sdc.controller.installer.ChainGenerator;
31 import org.onap.clamp.clds.util.drawing.SvgFacade;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Service;
36 public class LoopTemplatesService {
38 private final LoopTemplatesRepository loopTemplatesRepository;
41 ChainGenerator chainGenerator;
44 private SvgFacade svgFacade;
50 public LoopTemplatesService(LoopTemplatesRepository loopTemplatesRepository) {
51 this.loopTemplatesRepository = loopTemplatesRepository;
55 public LoopTemplate saveOrUpdateLoopTemplate(LoopTemplate loopTemplate) {
56 return loopTemplatesRepository.save(loopTemplate);
60 * Saves or updates loop template Object.
62 * @param templateName the loop template name
63 * @param loopTemplate the loop template object
64 * @return the loop template
65 * @throws BlueprintParserException In case of issues with the blueprint
68 public LoopTemplate saveOrUpdateLoopTemplateByName(String templateName,
69 LoopTemplate loopTemplate) throws BlueprintParserException {
71 if (getLoopTemplate(templateName) != null) {
72 loopTemplate.setName(getLoopTemplate(templateName).getName());
74 return saveOrUpdateLoopTemplate(createTemplateFromBlueprint(templateName, loopTemplate));
77 public List<String> getLoopTemplateNames() {
78 return loopTemplatesRepository.getAllLoopTemplateNames();
81 public List<LoopTemplate> getAllLoopTemplates() {
82 return loopTemplatesRepository.findAll();
85 public LoopTemplate getLoopTemplate(String name) {
86 return loopTemplatesRepository.findById(name).orElse(null);
89 public void deleteLoopTemplate(String name) {
90 loopTemplatesRepository.deleteById(name);
93 private LoopTemplate createTemplateFromBlueprint(String templateName, LoopTemplate loopTemplate)
94 throws BlueprintParserException {
96 String blueprintYaml = loopTemplate.getBlueprint();
97 List<BlueprintMicroService> microServicesChain =
98 chainGenerator.getChainOfMicroServices(BlueprintParser.getMicroServices(blueprintYaml));
99 if (microServicesChain.isEmpty()) {
100 microServicesChain = BlueprintParser.fallbackToOneMicroService();
102 loopTemplate.setSvgRepresentation(svgFacade.getSvgImage(microServicesChain));
103 loopTemplate.setName(templateName);
105 LoopTemplate existingTemplate = getLoopTemplate(templateName);
106 if (existingTemplate != null) {
107 loopTemplate.setName(existingTemplate.getName());