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;
37 public class PolicyModelsService {
38 private final PolicyModelsRepository policyModelsRepository;
39 private ToscaYamlToJsonConvertor toscaYamlToJsonConvertor;
42 public PolicyModelsService(PolicyModelsRepository policyModelrepo,
43 ToscaYamlToJsonConvertor convertor) {
44 policyModelsRepository = policyModelrepo;
45 toscaYamlToJsonConvertor = convertor;
48 public PolicyModel saveOrUpdatePolicyModel(PolicyModel policyModel) {
49 return policyModelsRepository.save(policyModel);
53 * Creates the Tosca Policy Model from a policy tosca file,
54 * if the same type already exists in database, it increases the version automatically.
56 * @param policyModelType The policyModeltype in Tosca yaml
57 * @param policyModelTosca The Policymodel object
58 * @return The Policy Model created
60 public PolicyModel createNewPolicyModelFromTosca(String policyModelType,
61 String policyModelTosca) {
62 JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(policyModelTosca);
63 String policyModelTypeFromTosca = toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
64 ToscaSchemaConstants.METADATA_POLICY_MODEL_TYPE);
65 String policyModelTypeToUse = policyModelTypeFromTosca != null ? policyModelTypeFromTosca : policyModelType;
66 Iterable<PolicyModel> models = getAllPolicyModelsByType(policyModelTypeToUse);
67 Collections.sort((List<PolicyModel>) models);
68 PolicyModel newPolicyModel = new PolicyModel(policyModelTypeToUse, policyModelTosca,
69 SemanticVersioning.incrementMajorVersion(
70 ((ArrayList) models).isEmpty() ? null : ((ArrayList<PolicyModel>) models).get(0).getVersion()),
71 toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
72 ToscaSchemaConstants.METADATA_ACRONYM));
73 return saveOrUpdatePolicyModel(newPolicyModel);
77 * Update an existing Tosca Policy Model.
79 * @param policyModelType The policy Model type in Tosca yaml
80 * @param policyModelVersion The policy Version to update
81 * @param policyModelTosca The Policy Model tosca
82 * @return The Policy Model updated
84 public PolicyModel updatePolicyModelTosca(String policyModelType, String policyModelVersion,
85 String policyModelTosca) {
86 JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(policyModelTosca);
87 PolicyModel thePolicyModel = getPolicyModelByType(policyModelType, policyModelVersion);
88 thePolicyModel.setPolicyAcronym(toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
89 ToscaSchemaConstants.METADATA_ACRONYM));
90 thePolicyModel.setPolicyModelTosca(policyModelTosca);
91 return saveOrUpdatePolicyModel(thePolicyModel);
94 public List<String> getAllPolicyModelTypes() {
95 return policyModelsRepository.getAllPolicyModelType();
98 public Iterable<PolicyModel> getAllPolicyModels() {
99 return policyModelsRepository.findAll();
102 public PolicyModel getPolicyModel(String type, String version) {
103 return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(null);
106 public Iterable<PolicyModel> getAllPolicyModelsByType(String type) {
107 return policyModelsRepository.findByPolicyModelType(type);
110 public PolicyModel getPolicyModelByType(String type, String version) {
111 return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(null);
115 * Retrieves the Tosca model Yaml string.
117 * @param type The Policy Model Type
118 * @param version The policy model version
119 * @return The Tosca model Yaml string
121 public String getPolicyModelTosca(String type, String version) {
122 return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(new PolicyModel())
123 .getPolicyModelTosca();