Additional code for Tosca
[clamp.git] / src / main / java / org / onap / clamp / clds / model / CldsToscaModel.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.model;
25
26 import java.util.List;
27
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;
33
34 public class CldsToscaModel extends CldsToscaModelRevision {
35
36     private String id;
37     private String policyType;
38     private String toscaModelName;
39     private String toscaModelYaml;
40
41     /**
42      * Creates or updates Tosca Model to DB
43      *
44      * @param cldsDao
45      * @param userId
46      */
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);
52             }
53         });
54
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);
65             }
66             cldsToscaModel = cldsDao.updateToscaModelWithNewVersion(this, userId);
67         } else {
68             this.setVersion(1);
69             if (refProp.getStringValue("import.tosca.model").equalsIgnoreCase("true")) {
70                 policyClient.importToscaModel(this);
71             }
72             cldsToscaModel = cldsDao.insToscaModel(this, userId);
73         }
74         return cldsToscaModel;
75     }
76
77     private double incrementVersion(double curVersion) {
78         return curVersion + 1;
79     }
80
81     /**
82      * @return the id
83      */
84     public String getId() {
85         return id;
86     }
87
88     /**
89      * @param id
90      *        the id to set
91      */
92     public void setId(String id) {
93         this.id = id;
94     }
95
96     /**
97      * @return the policyType
98      */
99     public String getPolicyType() {
100         return policyType;
101     }
102
103     /**
104      * @param policyType
105      *        the policyType to set
106      */
107     public void setPolicyType(String policyType) {
108         this.policyType = policyType;
109     }
110
111     /**
112      * @return the toscaModelName
113      */
114     public String getToscaModelName() {
115         return toscaModelName;
116     }
117
118     /**
119      * @param toscaModelName
120      *        the toscaModelName to set
121      */
122     public void setToscaModelName(String toscaModelName) {
123         this.toscaModelName = toscaModelName;
124     }
125
126     /**
127      * @return the toscaModelYaml
128      */
129     @Override
130     public String getToscaModelYaml() {
131         return toscaModelYaml;
132     }
133
134     /**
135      * @param toscaModelYaml
136      *        the toscaModelYaml to set
137      */
138     @Override
139     public void setToscaModelYaml(String toscaModelYaml) {
140         this.toscaModelYaml = toscaModelYaml;
141     }
142
143 }