2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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.clds.model;
26 import com.google.gson.annotations.Expose;
28 import java.util.List;
30 import org.apache.commons.lang3.StringUtils;
31 import org.onap.clamp.clds.client.req.policy.PolicyClient;
32 import org.onap.clamp.clds.config.ClampProperties;
33 import org.onap.clamp.clds.dao.CldsDao;
34 import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
36 public class CldsToscaModel extends CldsToscaModelRevision {
41 private String policyType;
43 private String toscaModelName;
48 public CldsToscaModel() {
52 * Creates or updates Tosca Model to DB.
54 * @param cldsDao The cldsDao
55 * @param userId The user Id
57 public CldsToscaModel save(CldsDao cldsDao, ClampProperties refProp, PolicyClient policyClient, String userId) {
58 CldsToscaModel cldsToscaModel = null;
59 refProp.getStringList("tosca.policyTypes", ",").stream().forEach(policyType -> {
60 if (StringUtils.containsIgnoreCase(this.getToscaModelName(), policyType)) {
61 this.setPolicyType(policyType);
65 ToscaYamlToJsonConvertor convertor = new ToscaYamlToJsonConvertor(cldsDao);
66 this.setToscaModelJson(convertor.parseToscaYaml(this.getToscaModelYaml()));
67 List<CldsToscaModel> toscaModels = cldsDao.getToscaModelByName(this.getToscaModelName());
68 if (toscaModels != null && !toscaModels.isEmpty()) {
69 CldsToscaModel toscaModel = toscaModels.stream().findFirst().get();
70 this.setVersion(incrementVersion(toscaModel.getVersion()));
71 this.setId(toscaModel.getId());
72 this.setUserId(userId);
73 if (refProp.getStringValue("import.tosca.model").equalsIgnoreCase("true")) {
74 policyClient.importToscaModel(this);
76 cldsToscaModel = cldsDao.updateToscaModelWithNewVersion(this, userId);
79 if (refProp.getStringValue("import.tosca.model").equalsIgnoreCase("true")) {
80 policyClient.importToscaModel(this);
82 cldsToscaModel = cldsDao.insToscaModel(this, userId);
84 return cldsToscaModel;
87 private double incrementVersion(double curVersion) {
88 return curVersion + 1;
96 public String getId() {
103 * @param id the id to set
105 public void setId(String id) {
110 * Get the policy type.
112 * @return the policyType
114 public String getPolicyType() {
119 * Set the policy type.
121 * @param policyType the policyType to set
123 public void setPolicyType(String policyType) {
124 this.policyType = policyType;
128 * Get the tosca model name.
130 * @return the toscaModelName
132 public String getToscaModelName() {
133 return toscaModelName;
137 * Set the tosca model name.
139 * @param toscaModelName the toscaModelName to set
141 public void setToscaModelName(String toscaModelName) {
142 this.toscaModelName = toscaModelName;