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.List;
28 import org.onap.clamp.clds.tosca.ToscaSchemaConstants;
29 import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
30 import org.onap.clamp.util.SemanticVersioning;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Service;
35 public class PolicyModelsService {
36 private final PolicyModelsRepository policyModelsRepository;
37 private ToscaYamlToJsonConvertor toscaYamlToJsonConvertor;
40 public PolicyModelsService(PolicyModelsRepository policyModelrepo,
41 ToscaYamlToJsonConvertor convertor) {
42 policyModelsRepository = policyModelrepo;
43 toscaYamlToJsonConvertor = convertor;
46 public PolicyModel saveOrUpdatePolicyModel(PolicyModel policyModel) {
47 return policyModelsRepository.save(policyModel);
51 * Creates or updates the Tosca Policy Model.
53 * @param policyModelType
54 * The policyModeltype in Tosca yaml
56 * The Policymodel object
57 * @return The Policy Model
59 public PolicyModel saveOrUpdateByPolicyModelType(String policyModelType,
60 String policyModelTosca) {
61 JsonObject jsonObject = toscaYamlToJsonConvertor.validateAndConvertToJson(policyModelTosca);
63 String policyModelTypeName = toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
64 ToscaSchemaConstants.METADATA_POLICY_MODEL_TYPE);
65 String acronym = toscaYamlToJsonConvertor.getValueFromMetadata(jsonObject,
66 ToscaSchemaConstants.METADATA_ACRONYM);
67 PolicyModel model = getPolicyModelByType(
68 policyModelTypeName != null ? policyModelTypeName : policyModelType);
71 model = new PolicyModel(policyModelTypeName, policyModelTosca,
72 SemanticVersioning.incrementMajorVersion(null), acronym);
74 model.setVersion(SemanticVersioning
75 .incrementMajorVersion(model.getVersion() != null ? model.getVersion() : null));
76 model.setPolicyModelType(policyModelTypeName);
77 model.setPolicyAcronym(acronym);
79 return saveOrUpdatePolicyModel(model);
82 public List<String> getAllPolicyModelTypes() {
83 return policyModelsRepository.getAllPolicyModelType();
86 public Iterable<PolicyModel> getAllPolicyModels() {
87 return policyModelsRepository.findAll();
90 public PolicyModel getPolicyModel(String type, String version) {
91 return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(null);
94 public Iterable<PolicyModel> getAllPolicyModelsByType(String type) {
95 return policyModelsRepository.findByPolicyModelType(type);
98 public PolicyModel getPolicyModelByType(String type) {
99 List<PolicyModel> list = policyModelsRepository.findByPolicyModelType(type);
100 return list.stream().sorted().findFirst().orElse(null);
104 * Retrieves the Tosca model Yaml string.
106 * @param type The PolicyModelType
107 * @return The Tosca model Yaml string
109 public String getPolicyModelTosca(String type) {
110 PolicyModel policyModel = getPolicyModelByType(type);
111 return policyModel != null ? policyModel.getPolicyModelTosca() : null;