Changes include Metadata support, Upload tosca policy model and Loop Template
[clamp.git] / src / main / java / org / onap / clamp / loop / template / LoopTemplatesService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop.template;
25
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;
34
35 @Service
36 public class LoopTemplatesService {
37
38     private final LoopTemplatesRepository loopTemplatesRepository;
39
40     @Autowired
41     ChainGenerator chainGenerator;
42
43     @Autowired
44     private SvgFacade svgFacade;
45
46     /**
47      * Constructor.
48      */
49     @Autowired
50     public LoopTemplatesService(LoopTemplatesRepository loopTemplatesRepository) {
51         this.loopTemplatesRepository = loopTemplatesRepository;
52
53     }
54
55     public LoopTemplate saveOrUpdateLoopTemplate(LoopTemplate loopTemplate) {
56         return loopTemplatesRepository.save(loopTemplate);
57     }
58
59     /**
60      * Saves or updates loop template Object.
61      *
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
66      *         parsing
67      */
68     public LoopTemplate saveOrUpdateLoopTemplateByName(String templateName,
69         LoopTemplate loopTemplate) throws BlueprintParserException {
70
71         if (getLoopTemplate(templateName) != null) {
72             loopTemplate.setName(getLoopTemplate(templateName).getName());
73         }
74         return saveOrUpdateLoopTemplate(createTemplateFromBlueprint(templateName, loopTemplate));
75     }
76
77     public List<String> getLoopTemplateNames() {
78         return loopTemplatesRepository.getAllLoopTemplateNames();
79     }
80
81     public List<LoopTemplate> getAllLoopTemplates() {
82         return loopTemplatesRepository.findAll();
83     }
84
85     public LoopTemplate getLoopTemplate(String name) {
86         return loopTemplatesRepository.findById(name).orElse(null);
87     }
88
89     public void deleteLoopTemplate(String name) {
90         loopTemplatesRepository.deleteById(name);
91     }
92
93     private LoopTemplate createTemplateFromBlueprint(String templateName, LoopTemplate loopTemplate)
94         throws BlueprintParserException {
95
96         String blueprintYaml = loopTemplate.getBlueprint();
97         List<BlueprintMicroService> microServicesChain =
98             chainGenerator.getChainOfMicroServices(BlueprintParser.getMicroServices(blueprintYaml));
99         if (microServicesChain.isEmpty()) {
100             microServicesChain = BlueprintParser.fallbackToOneMicroService();
101         }
102         loopTemplate.setSvgRepresentation(svgFacade.getSvgImage(microServicesChain));
103         loopTemplate.setName(templateName);
104
105         LoopTemplate existingTemplate = getLoopTemplate(templateName);
106         if (existingTemplate != null) {
107             loopTemplate.setName(existingTemplate.getName());
108         }
109         return loopTemplate;
110     }
111 }