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 java.util.List;
28 import org.apache.commons.lang3.StringUtils;
29 import org.onap.clamp.clds.client.req.policy.PolicyClient;
30 import org.onap.clamp.clds.config.ClampProperties;
31 import org.onap.clamp.clds.dao.CldsDao;
32 import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
34 public class CldsToscaModel extends CldsToscaModelRevision {
37 private String policyType;
38 private String toscaModelName;
39 private String toscaModelYaml;
42 * Creates or updates Tosca Model to DB
47 public CldsToscaModel save(CldsDao cldsDao, ClampProperties refProp, PolicyClient policyClient, String userId) {
48 CldsToscaModel cldsToscaModel = null;
49 refProp.getStringList("tosca.policyTypes", ",").stream().forEach(policyType -> {
50 if (StringUtils.containsIgnoreCase(this.getToscaModelName(), policyType)) {
51 this.setPolicyType(policyType);
55 ToscaYamlToJsonConvertor convertor = new ToscaYamlToJsonConvertor(cldsDao);
56 this.setToscaModelJson(convertor.parseToscaYaml(this.getToscaModelYaml()));
57 List<CldsToscaModel> toscaModels = cldsDao.getToscaModelByName(this.getToscaModelName());
58 if (toscaModels != null && !toscaModels.isEmpty()) {
59 CldsToscaModel toscaModel = toscaModels.stream().findFirst().get();
60 this.setVersion(incrementVersion(toscaModel.getVersion()));
61 this.setId(toscaModel.getId());
62 this.setUserId(userId);
63 if (refProp.getStringValue("import.tosca.model").equalsIgnoreCase("true")) {
64 policyClient.importToscaModel(this);
66 cldsToscaModel = cldsDao.updateToscaModelWithNewVersion(this, userId);
69 if (refProp.getStringValue("import.tosca.model").equalsIgnoreCase("true")) {
70 policyClient.importToscaModel(this);
72 cldsToscaModel = cldsDao.insToscaModel(this, userId);
74 return cldsToscaModel;
77 private double incrementVersion(double curVersion) {
78 return curVersion + 1;
84 public String getId() {
92 public void setId(String id) {
97 * @return the policyType
99 public String getPolicyType() {
105 * the policyType to set
107 public void setPolicyType(String policyType) {
108 this.policyType = policyType;
112 * @return the toscaModelName
114 public String getToscaModelName() {
115 return toscaModelName;
119 * @param toscaModelName
120 * the toscaModelName to set
122 public void setToscaModelName(String toscaModelName) {
123 this.toscaModelName = toscaModelName;
127 * @return the toscaModelYaml
130 public String getToscaModelYaml() {
131 return toscaModelYaml;
135 * @param toscaModelYaml
136 * the toscaModelYaml to set
139 public void setToscaModelYaml(String toscaModelYaml) {
140 this.toscaModelYaml = toscaModelYaml;