Create get Pdp Groups flow
[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
56      *        The policyModel
57      * @return The Policy Model
58      */
59     public PolicyModel saveOrUpdatePolicyModel(PolicyModel policyModel) {
60         return policyModelsRepository.save(policyModel);
61     }
62
63     /**
64      * Verify whether Policy Model exist by ID.
65      *
66      * @param policyModelId
67      *        The policyModel Id
68      * @return The flag indicates whether Policy Model exist
69      */
70     public boolean existsById(PolicyModelId policyModelId) {
71         return policyModelsRepository.existsById(policyModelId);
72     }
73
74     /**
75      * Creates or updates the Tosca Policy Model.
76      *
77      * @param policyModelType  The policyModeltype in Tosca yaml
78      * @param policyModelTosca The Policymodel object
79      * @return The Policy Model created
80      */
81     public PolicyModel createNewPolicyModelFromTosca(String policyModelType,
82                                                      String policyModelTosca) {
83         JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(policyModelTosca);
84         String policyModelTypeFromTosca = toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
85                 ToscaSchemaConstants.METADATA_POLICY_MODEL_TYPE);
86         String policyModelTypeToUse = policyModelTypeFromTosca != null ? policyModelTypeFromTosca : policyModelType;
87         Iterable<PolicyModel> models = getAllPolicyModelsByType(policyModelTypeToUse);
88         Collections.sort((List<PolicyModel>) models);
89         PolicyModel newPolicyModel = new PolicyModel(policyModelTypeToUse, policyModelTosca,
90                 SemanticVersioning.incrementMajorVersion(
91                         ((ArrayList) models).isEmpty() ? null : ((ArrayList<PolicyModel>) models).get(0).getVersion()),
92                 toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
93                         ToscaSchemaConstants.METADATA_ACRONYM));
94         return saveOrUpdatePolicyModel(newPolicyModel);
95     }
96
97     /**
98      * Update an existing Tosca Policy Model.
99      *
100      * @param policyModelType    The policy Model type in Tosca yaml
101      * @param policyModelVersion The policy Version to update
102      * @param policyModelTosca   The Policy Model tosca
103      * @return The Policy Model updated
104      */
105     public PolicyModel updatePolicyModelTosca(String policyModelType, String policyModelVersion,
106                                               String policyModelTosca) {
107         JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(policyModelTosca);
108         PolicyModel thePolicyModel = getPolicyModelByType(policyModelType, policyModelVersion);
109         thePolicyModel.setPolicyAcronym(toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
110                 ToscaSchemaConstants.METADATA_ACRONYM));
111         thePolicyModel.setPolicyModelTosca(policyModelTosca);
112         return saveOrUpdatePolicyModel(thePolicyModel);
113     }
114
115     public List<String> getAllPolicyModelTypes() {
116         return policyModelsRepository.getAllPolicyModelType();
117     }
118
119     public Iterable<PolicyModel> getAllPolicyModels() {
120         return policyModelsRepository.findAll();
121     }
122
123     public PolicyModel getPolicyModel(String type, String version) {
124         return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(null);
125     }
126
127     public Iterable<PolicyModel> getAllPolicyModelsByType(String type) {
128         return policyModelsRepository.findByPolicyModelType(type);
129     }
130
131     public PolicyModel getPolicyModelByType(String type, String version) {
132         return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(null);
133     }
134
135     /**
136      * Retrieves the Tosca model Yaml string.
137      *
138      * @param type    The Policy Model Type
139      * @param version The policy model version
140      * @return The Tosca model Yaml string
141      */
142     public String getPolicyModelTosca(String type, String version) {
143         return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(new PolicyModel())
144                 .getPolicyModelTosca();
145     }
146
147     /**
148      * This method creates an PolicyModel in Db if it does not exist.
149      *
150      * @param policyModel The policyModel to save
151      */
152     @Transactional(propagation = Propagation.REQUIRES_NEW)
153     public void createPolicyInDbIfNeeded(PolicyModel policyModel) {
154         if (!policyModelsRepository
155                 .existsById(new PolicyModelId(policyModel.getPolicyModelType(), policyModel.getVersion()))) {
156             policyModelsRepository.save(policyModel);
157         }
158     }
159
160      /**
161      * Update the Pdp Group info in Policy Model DB.
162      *
163      * @param pdpGroupList The list of Pdp Group info received from Policy Engine
164      */
165     public void updatePdpGroupInfo(List<PdpGroup> pdpGroupList) {
166         List<PolicyModel> policyModelList = policyModelsRepository.findAll();
167         for (PolicyModel policyModel :  policyModelList) {
168             JsonArray supportedPdpGroups = new JsonArray();
169             for (PdpGroup pdpGroup : pdpGroupList) {
170                 JsonObject supportedPdpGroup = pdpGroup.getSupportedSubgroups(policyModel.getPolicyModelType(),
171                         policyModel.getVersion());
172                 if (supportedPdpGroup != null) {
173                     supportedPdpGroups.add(supportedPdpGroup);
174                 }
175             }
176
177             if (supportedPdpGroups.size() > 0) {
178                 JsonObject supportedPdpJson = new JsonObject ();
179                 supportedPdpJson.add("supportedPdpGroups", supportedPdpGroups);
180                 policyModel.setPolicyPdpGroup(supportedPdpJson);
181                 policyModelsRepository.save(policyModel);
182             }
183         }
184     }
185 }