2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.loop.template;
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;
39 public class PolicyModelsService {
40 private final PolicyModelsRepository policyModelsRepository;
41 private ToscaYamlToJsonConvertor toscaYamlToJsonConvertor;
44 public PolicyModelsService(PolicyModelsRepository policyModelrepo,
45 ToscaYamlToJsonConvertor convertor) {
46 policyModelsRepository = policyModelrepo;
47 toscaYamlToJsonConvertor = convertor;
50 public PolicyModel saveOrUpdatePolicyModel(PolicyModel policyModel) {
51 return policyModelsRepository.save(policyModel);
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.
58 * @param policyModelType The policyModeltype in Tosca yaml
59 * @param policyModelTosca The Policymodel object
60 * @return The Policy Model created
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);
79 * Update an existing Tosca Policy Model.
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
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);
96 public List<String> getAllPolicyModelTypes() {
97 return policyModelsRepository.getAllPolicyModelType();
100 public Iterable<PolicyModel> getAllPolicyModels() {
101 return policyModelsRepository.findAll();
104 public PolicyModel getPolicyModel(String type, String version) {
105 return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(null);
108 public Iterable<PolicyModel> getAllPolicyModelsByType(String type) {
109 return policyModelsRepository.findByPolicyModelType(type);
112 public PolicyModel getPolicyModelByType(String type, String version) {
113 return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(null);
117 * Retrieves the Tosca model Yaml string.
119 * @param type The Policy Model Type
120 * @param version The policy model version
121 * @return The Tosca model Yaml string
123 public String getPolicyModelTosca(String type, String version) {
124 return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(new PolicyModel())
125 .getPolicyModelTosca();
129 * This method creates an PolicyModel in Db if it does not exist.
131 * @param policyModel The policyModel to save
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);