Fix check style 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 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
40     /**
41      * Construct.
42      */
43     public CldsToscaModel() {
44     }
45
46     /**
47      * Creates or updates Tosca Model to DB.
48      *
49      * @param cldsDao The cldsDao
50      * @param userId The user Id
51      */
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);
57             }
58         });
59
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);
70             }
71             cldsToscaModel = cldsDao.updateToscaModelWithNewVersion(this, userId);
72         } else {
73             this.setVersion(1);
74             if (refProp.getStringValue("import.tosca.model").equalsIgnoreCase("true")) {
75                 policyClient.importToscaModel(this);
76             }
77             cldsToscaModel = cldsDao.insToscaModel(this, userId);
78         }
79         return cldsToscaModel;
80     }
81
82     private double incrementVersion(double curVersion) {
83         return curVersion + 1;
84     }
85
86     /**
87      * Get the Id.
88      * @return the id
89      */
90     public String getId() {
91         return id;
92     }
93
94     /**
95      * Set the id.
96      * @param id
97      *        the id to set
98      */
99     public void setId(String id) {
100         this.id = id;
101     }
102
103     /**
104      * Get the policy type.
105      * @return the policyType
106      */
107     public String getPolicyType() {
108         return policyType;
109     }
110
111     /**
112      * Set the policy type.
113      * @param policyType
114      *        the policyType to set
115      */
116     public void setPolicyType(String policyType) {
117         this.policyType = policyType;
118     }
119
120     /**
121      * Get the tosca model name.
122      * @return the toscaModelName
123      */
124     public String getToscaModelName() {
125         return toscaModelName;
126     }
127
128     /**
129      * Set the tosca model name.
130      * @param toscaModelName
131      *        the toscaModelName to set
132      */
133     public void setToscaModelName(String toscaModelName) {
134         this.toscaModelName = toscaModelName;
135     }
136
137 }