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;
43 public CldsToscaModel() {
47 * Creates or updates Tosca Model to DB.
49 * @param cldsDao The cldsDao
50 * @param userId The user Id
52 public CldsToscaModel save(CldsDao cldsDao, ClampProperties refProp, PolicyClient policyClient, String userId) {
53 CldsToscaModel cldsToscaModel = null;
54 refProp.getStringList("tosca.policyTypes", ",").stream().forEach(policyType -> {
55 if (StringUtils.containsIgnoreCase(this.getToscaModelName(), policyType)) {
56 this.setPolicyType(policyType);
60 ToscaYamlToJsonConvertor convertor = new ToscaYamlToJsonConvertor(cldsDao);
61 this.setToscaModelJson(convertor.parseToscaYaml(this.getToscaModelYaml()));
62 List<CldsToscaModel> toscaModels = cldsDao.getToscaModelByName(this.getToscaModelName());
63 if (toscaModels != null && !toscaModels.isEmpty()) {
64 CldsToscaModel toscaModel = toscaModels.stream().findFirst().get();
65 this.setVersion(incrementVersion(toscaModel.getVersion()));
66 this.setId(toscaModel.getId());
67 this.setUserId(userId);
68 if (refProp.getStringValue("import.tosca.model").equalsIgnoreCase("true")) {
69 policyClient.importToscaModel(this);
71 cldsToscaModel = cldsDao.updateToscaModelWithNewVersion(this, userId);
74 if (refProp.getStringValue("import.tosca.model").equalsIgnoreCase("true")) {
75 policyClient.importToscaModel(this);
77 cldsToscaModel = cldsDao.insToscaModel(this, userId);
79 return cldsToscaModel;
82 private double incrementVersion(double curVersion) {
83 return curVersion + 1;
90 public String getId() {
99 public void setId(String id) {
104 * Get the policy type.
105 * @return the policyType
107 public String getPolicyType() {
112 * Set the policy type.
114 * the policyType to set
116 public void setPolicyType(String policyType) {
117 this.policyType = policyType;
121 * Get the tosca model name.
122 * @return the toscaModelName
124 public String getToscaModelName() {
125 return toscaModelName;
129 * Set the tosca model name.
130 * @param toscaModelName
131 * the toscaModelName to set
133 public void setToscaModelName(String toscaModelName) {
134 this.toscaModelName = toscaModelName;