Changes include Metadata support, Upload tosca policy model and Loop Template
[clamp.git] / src / main / java / org / onap / clamp / loop / template / PolicyModelsService.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 com.google.gson.JsonObject;
27 import java.util.List;
28 import org.onap.clamp.clds.tosca.ToscaSchemaConstants;
29 import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
30 import org.onap.clamp.util.SemanticVersioning;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Service;
33
34 @Service
35 public class PolicyModelsService {
36     private final PolicyModelsRepository policyModelsRepository;
37     private ToscaYamlToJsonConvertor toscaYamlToJsonConvertor;
38
39     @Autowired
40     public PolicyModelsService(PolicyModelsRepository policyModelrepo,
41         ToscaYamlToJsonConvertor convertor) {
42         policyModelsRepository = policyModelrepo;
43         toscaYamlToJsonConvertor = convertor;
44     }
45
46     public PolicyModel saveOrUpdatePolicyModel(PolicyModel policyModel) {
47         return policyModelsRepository.save(policyModel);
48     }
49
50     /**
51      * Creates or updates the Tosca Policy Model.
52      *
53      * @param policyModelType
54      *        The policyModeltype in Tosca yaml
55      * @param policyModel
56      *        The Policymodel object
57      * @return The Policy Model
58      */
59     public PolicyModel saveOrUpdateByPolicyModelType(String policyModelType,
60         String policyModelTosca) {
61         JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(policyModelTosca);
62
63         String policyModelTypeName = toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
64             ToscaSchemaConstants.METADATA_POLICY_MODEL_TYPE);
65         String acronym = toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
66             ToscaSchemaConstants.METADATA_ACRONYM);
67         PolicyModel model = getPolicyModelByType(
68             policyModelTypeName != null ? policyModelTypeName : policyModelType);
69
70         if (model == null) {
71             model = new PolicyModel(policyModelTypeName, policyModelTosca,
72                 SemanticVersioning.incrementMajorVersion(null), acronym);
73         } else {
74             model.setVersion(SemanticVersioning
75                 .incrementMajorVersion(model.getVersion() != null ? model.getVersion() : null));
76             model.setPolicyModelType(policyModelTypeName);
77             model.setPolicyAcronym(acronym);
78         }
79         return saveOrUpdatePolicyModel(model);
80     }
81
82     public List<String> getAllPolicyModelTypes() {
83         return policyModelsRepository.getAllPolicyModelType();
84     }
85
86     public Iterable<PolicyModel> getAllPolicyModels() {
87         return policyModelsRepository.findAll();
88     }
89
90     public PolicyModel getPolicyModel(String type, String version) {
91         return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(null);
92     }
93
94     public Iterable<PolicyModel> getAllPolicyModelsByType(String type) {
95         return policyModelsRepository.findByPolicyModelType(type);
96     }
97
98     public PolicyModel getPolicyModelByType(String type) {
99         List<PolicyModel> list = policyModelsRepository.findByPolicyModelType(type);
100         return list.stream().sorted().findFirst().orElse(null);
101     }
102
103     /**
104      * Retrieves the Tosca model Yaml string.
105      *
106      * @param type The PolicyModelType
107      * @return The Tosca model Yaml string
108      */
109     public String getPolicyModelTosca(String type) {
110         PolicyModel policyModel = getPolicyModelByType(type);
111         return policyModel != null ? policyModel.getPolicyModelTosca() : null;
112     }
113 }