Remove checkstyle issues
[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 com.google.gson.annotations.Expose;
27
28 import java.util.List;
29
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;
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      *
94      * @return the id
95      */
96     public String getId() {
97         return id;
98     }
99
100     /**
101      * Set the id.
102      *
103      * @param id the id to set
104      */
105     public void setId(String id) {
106         this.id = id;
107     }
108
109     /**
110      * Get the policy type.
111      *
112      * @return the policyType
113      */
114     public String getPolicyType() {
115         return policyType;
116     }
117
118     /**
119      * Set the policy type.
120      *
121      * @param policyType the policyType to set
122      */
123     public void setPolicyType(String policyType) {
124         this.policyType = policyType;
125     }
126
127     /**
128      * Get the tosca model name.
129      *
130      * @return the toscaModelName
131      */
132     public String getToscaModelName() {
133         return toscaModelName;
134     }
135
136     /**
137      * Set the tosca model name.
138      *
139      * @param toscaModelName the toscaModelName to set
140      */
141     public void setToscaModelName(String toscaModelName) {
142         this.toscaModelName = toscaModelName;
143     }
144
145 }