Add Expose annotation
[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 import com.google.gson.annotations.Expose;
35
36 public class CldsToscaModel extends CldsToscaModelRevision {
37
38         @Expose
39     private String id;
40         @Expose
41         private String policyType;
42         @Expose
43         private String toscaModelName;
44
45     /**
46      * Construct.
47      */
48     public CldsToscaModel() {
49     }
50
51     /**
52      * Creates or updates Tosca Model to DB.
53      *
54      * @param cldsDao The cldsDao
55      * @param userId The user Id
56      */
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);
62             }
63         });
64
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);
75             }
76             cldsToscaModel = cldsDao.updateToscaModelWithNewVersion(this, userId);
77         } else {
78             this.setVersion(1);
79             if (refProp.getStringValue("import.tosca.model").equalsIgnoreCase("true")) {
80                 policyClient.importToscaModel(this);
81             }
82             cldsToscaModel = cldsDao.insToscaModel(this, userId);
83         }
84         return cldsToscaModel;
85     }
86
87     private double incrementVersion(double curVersion) {
88         return curVersion + 1;
89     }
90
91     /**
92      * Get the Id.
93      * @return the id
94      */
95     public String getId() {
96         return id;
97     }
98
99     /**
100      * Set the id.
101      * @param id
102      *        the id to set
103      */
104     public void setId(String id) {
105         this.id = id;
106     }
107
108     /**
109      * Get the policy type.
110      * @return the policyType
111      */
112     public String getPolicyType() {
113         return policyType;
114     }
115
116     /**
117      * Set the policy type.
118      * @param policyType
119      *        the policyType to set
120      */
121     public void setPolicyType(String policyType) {
122         this.policyType = policyType;
123     }
124
125     /**
126      * Get the tosca model name.
127      * @return the toscaModelName
128      */
129     public String getToscaModelName() {
130         return toscaModelName;
131     }
132
133     /**
134      * Set the tosca model name.
135      * @param toscaModelName
136      *        the toscaModelName to set
137      */
138     public void setToscaModelName(String toscaModelName) {
139         this.toscaModelName = toscaModelName;
140     }
141
142 }