Fix blueprint installation
[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.JsonArray;
27 import com.google.gson.JsonObject;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import org.onap.clamp.clds.tosca.ToscaSchemaConstants;
32 import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
33 import org.onap.clamp.policy.pdpgroup.PdpGroup;
34 import org.onap.clamp.util.SemanticVersioning;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
37 import org.springframework.transaction.annotation.Propagation;
38 import org.springframework.transaction.annotation.Transactional;
39
40 @Service
41 public class PolicyModelsService {
42     private final PolicyModelsRepository policyModelsRepository;
43     private ToscaYamlToJsonConvertor toscaYamlToJsonConvertor;
44
45     @Autowired
46     public PolicyModelsService(PolicyModelsRepository policyModelrepo,
47         ToscaYamlToJsonConvertor convertor) {
48         policyModelsRepository = policyModelrepo;
49         toscaYamlToJsonConvertor = convertor;
50     }
51
52     /**
53      * Save or Update Policy Model.
54      *
55      * @param policyModel The policyModel
56      * @return The Policy Model
57      */
58     public PolicyModel saveOrUpdatePolicyModel(PolicyModel policyModel) {
59         return policyModelsRepository.saveAndFlush(policyModel);
60     }
61
62     /**
63      * Verify whether Policy Model exist by ID.
64      *
65      * @param policyModelId The policyModel Id
66      * @return The flag indicates whether Policy Model exist
67      */
68     public boolean existsById(PolicyModelId policyModelId) {
69         return policyModelsRepository.existsById(policyModelId);
70     }
71
72     /**
73      * Creates or updates the Tosca Policy Model.
74      *
75      * @param policyModelTosca The Policymodel object
76      * @return The Policy Model created
77      */
78     public PolicyModel createNewPolicyModelFromTosca(String policyModelTosca) {
79         JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(policyModelTosca);
80         String policyModelTypeFromTosca = toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
81             ToscaSchemaConstants.METADATA_POLICY_MODEL_TYPE);
82         Iterable<PolicyModel> models = getAllPolicyModelsByType(policyModelTypeFromTosca);
83         Collections.sort((List<PolicyModel>) models);
84         PolicyModel newPolicyModel = new PolicyModel(policyModelTypeFromTosca, policyModelTosca,
85             SemanticVersioning.incrementMajorVersion(((ArrayList) models).isEmpty() ? null
86                 : ((ArrayList<PolicyModel>) models).get(0).getVersion()),
87             toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
88                 ToscaSchemaConstants.METADATA_ACRONYM));
89         return saveOrUpdatePolicyModel(newPolicyModel);
90     }
91
92     /**
93      * Update an existing Tosca Policy Model.
94      *
95      * @param policyModelType The policy Model type in Tosca yaml
96      * @param policyModelVersion The policy Version to update
97      * @param policyModelTosca The Policy Model tosca
98      * @return The Policy Model updated
99      */
100     public PolicyModel updatePolicyModelTosca(String policyModelType, String policyModelVersion,
101         String policyModelTosca) {
102         JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(policyModelTosca);
103         PolicyModel thePolicyModel = getPolicyModel(policyModelType, policyModelVersion);
104         thePolicyModel.setPolicyAcronym(toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
105             ToscaSchemaConstants.METADATA_ACRONYM));
106         thePolicyModel.setPolicyModelTosca(policyModelTosca);
107         return saveOrUpdatePolicyModel(thePolicyModel);
108     }
109
110     public List<String> getAllPolicyModelTypes() {
111         return policyModelsRepository.getAllPolicyModelType();
112     }
113
114     public Iterable<PolicyModel> getAllPolicyModels() {
115         return policyModelsRepository.findAll();
116     }
117
118     public PolicyModel getPolicyModel(String type, String version) {
119         return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(null);
120     }
121
122     public Iterable<PolicyModel> getAllPolicyModelsByType(String type) {
123         return policyModelsRepository.findByPolicyModelType(type);
124     }
125
126     /**
127      * Retrieves the Tosca model Yaml string.
128      *
129      * @param type The Policy Model Type
130      * @param version The policy model version
131      * @return The Tosca model Yaml string
132      */
133     public String getPolicyModelTosca(String type, String version) {
134         return policyModelsRepository.findById(new PolicyModelId(type, version))
135             .orElse(new PolicyModel()).getPolicyModelTosca();
136     }
137
138     /**
139      * This method creates an PolicyModel in Db if it does not exist.
140      *
141      * @param policyModel The policyModel to save
142      */
143     @Transactional(propagation = Propagation.REQUIRES_NEW)
144     public PolicyModel savePolicyModelInNewTransaction(PolicyModel policyModel) {
145             return policyModelsRepository.saveAndFlush(policyModel);
146     }
147
148     /**
149      * Update the Pdp Group info in Policy Model DB.
150      *
151      * @param pdpGroupList The list of Pdp Group info received from Policy Engine
152      */
153     public void updatePdpGroupInfo(List<PdpGroup> pdpGroupList) {
154         List<PolicyModel> policyModelList = policyModelsRepository.findAll();
155         for (PolicyModel policyModel : policyModelList) {
156             JsonArray supportedPdpGroups = new JsonArray();
157             for (PdpGroup pdpGroup : pdpGroupList) {
158                 JsonObject supportedPdpGroup = pdpGroup.getSupportedSubgroups(
159                     policyModel.getPolicyModelType(), policyModel.getVersion());
160                 if (supportedPdpGroup != null) {
161                     supportedPdpGroups.add(supportedPdpGroup);
162                 }
163             }
164
165             if (supportedPdpGroups.size() > 0) {
166                 JsonObject supportedPdpJson = new JsonObject();
167                 supportedPdpJson.add("supportedPdpGroups", supportedPdpGroups);
168                 policyModel.setPolicyPdpGroup(supportedPdpJson);
169                 policyModelsRepository.saveAndFlush(policyModel);
170             }
171         }
172     }
173 }