Fix Bug due to user management changes
[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.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30 import org.onap.clamp.clds.tosca.ToscaSchemaConstants;
31 import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
32 import org.onap.clamp.util.SemanticVersioning;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Service;
35 import org.springframework.transaction.annotation.Propagation;
36 import org.springframework.transaction.annotation.Transactional;
37
38 @Service
39 public class PolicyModelsService {
40     private final PolicyModelsRepository policyModelsRepository;
41     private ToscaYamlToJsonConvertor toscaYamlToJsonConvertor;
42
43     @Autowired
44     public PolicyModelsService(PolicyModelsRepository policyModelrepo,
45                                ToscaYamlToJsonConvertor convertor) {
46         policyModelsRepository = policyModelrepo;
47         toscaYamlToJsonConvertor = convertor;
48     }
49
50     public PolicyModel saveOrUpdatePolicyModel(PolicyModel policyModel) {
51         return policyModelsRepository.save(policyModel);
52     }
53
54     /**
55      * Creates the Tosca Policy Model from a policy tosca file,
56      * if the same type already exists in database, it increases the version automatically.
57      *
58      * @param policyModelType  The policyModeltype in Tosca yaml
59      * @param policyModelTosca The Policymodel object
60      * @return The Policy Model created
61      */
62     public PolicyModel createNewPolicyModelFromTosca(String policyModelType,
63                                                      String policyModelTosca) {
64         JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(policyModelTosca);
65         String policyModelTypeFromTosca = toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
66                 ToscaSchemaConstants.METADATA_POLICY_MODEL_TYPE);
67         String policyModelTypeToUse = policyModelTypeFromTosca != null ? policyModelTypeFromTosca : policyModelType;
68         Iterable<PolicyModel> models = getAllPolicyModelsByType(policyModelTypeToUse);
69         Collections.sort((List<PolicyModel>) models);
70         PolicyModel newPolicyModel = new PolicyModel(policyModelTypeToUse, policyModelTosca,
71                 SemanticVersioning.incrementMajorVersion(
72                         ((ArrayList) models).isEmpty() ? null : ((ArrayList<PolicyModel>) models).get(0).getVersion()),
73                 toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
74                         ToscaSchemaConstants.METADATA_ACRONYM));
75         return saveOrUpdatePolicyModel(newPolicyModel);
76     }
77
78     /**
79      * Update an existing Tosca Policy Model.
80      *
81      * @param policyModelType    The policy Model type in Tosca yaml
82      * @param policyModelVersion The policy Version to update
83      * @param policyModelTosca   The Policy Model tosca
84      * @return The Policy Model updated
85      */
86     public PolicyModel updatePolicyModelTosca(String policyModelType, String policyModelVersion,
87                                               String policyModelTosca) {
88         JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(policyModelTosca);
89         PolicyModel thePolicyModel = getPolicyModelByType(policyModelType, policyModelVersion);
90         thePolicyModel.setPolicyAcronym(toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
91                 ToscaSchemaConstants.METADATA_ACRONYM));
92         thePolicyModel.setPolicyModelTosca(policyModelTosca);
93         return saveOrUpdatePolicyModel(thePolicyModel);
94     }
95
96     public List<String> getAllPolicyModelTypes() {
97         return policyModelsRepository.getAllPolicyModelType();
98     }
99
100     public Iterable<PolicyModel> getAllPolicyModels() {
101         return policyModelsRepository.findAll();
102     }
103
104     public PolicyModel getPolicyModel(String type, String version) {
105         return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(null);
106     }
107
108     public Iterable<PolicyModel> getAllPolicyModelsByType(String type) {
109         return policyModelsRepository.findByPolicyModelType(type);
110     }
111
112     public PolicyModel getPolicyModelByType(String type, String version) {
113         return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(null);
114     }
115
116     /**
117      * Retrieves the Tosca model Yaml string.
118      *
119      * @param type    The Policy Model Type
120      * @param version The policy model version
121      * @return The Tosca model Yaml string
122      */
123     public String getPolicyModelTosca(String type, String version) {
124         return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(new PolicyModel())
125                 .getPolicyModelTosca();
126     }
127
128     /**
129      * This method creates an PolicyModel in Db if it does not exist.
130      *
131      * @param policyModel The policyModel to save
132      */
133     @Transactional(propagation = Propagation.REQUIRES_NEW)
134     public void createPolicyInDbIfNeeded(PolicyModel policyModel) {
135         if (!policyModelsRepository
136                 .existsById(new PolicyModelId(policyModel.getPolicyModelType(), policyModel.getVersion()))) {
137             policyModelsRepository.save(policyModel);
138         }
139     }
140 }