Merge "Update deploy-loop route"
[clamp.git] / src / main / java / org / onap / clamp / policy / microservice / MicroServicePolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 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.policy.microservice;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import com.google.gson.JsonArray;
31 import com.google.gson.JsonObject;
32 import com.google.gson.annotations.Expose;
33
34 import java.io.Serializable;
35 import java.util.HashSet;
36 import java.util.Map;
37 import java.util.Set;
38
39 import javax.persistence.Column;
40 import javax.persistence.Entity;
41 import javax.persistence.FetchType;
42 import javax.persistence.Id;
43 import javax.persistence.ManyToMany;
44 import javax.persistence.Table;
45 import javax.persistence.Transient;
46
47 import org.hibernate.annotations.TypeDef;
48 import org.hibernate.annotations.TypeDefs;
49 import org.json.JSONObject;
50 import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
51 import org.onap.clamp.clds.util.JsonUtils;
52 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
53 import org.onap.clamp.loop.Loop;
54 import org.onap.clamp.policy.Policy;
55 import org.yaml.snakeyaml.Yaml;
56
57 @Entity
58 @Table(name = "micro_service_policies")
59 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
60 public class MicroServicePolicy extends Policy implements Serializable {
61     /**
62      * The serial version ID.
63      */
64     private static final long serialVersionUID = 6271238288583332616L;
65
66     @Transient
67     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MicroServicePolicy.class);
68
69     @Expose
70     @Id
71     @Column(nullable = false, name = "name", unique = true)
72     private String name;
73
74     @Expose
75     @Column(nullable = false, name = "policy_model_type")
76     private String modelType;
77
78     @Expose
79     @Column(name = "context")
80     private String context;
81
82     @Expose
83     @Column(name = "device_type_scope")
84     private String deviceTypeScope;
85
86     @Expose
87     @Column(name = "shared", nullable = false)
88     private Boolean shared;
89
90     @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca", nullable = false)
91     private String policyTosca;
92
93     @ManyToMany(mappedBy = "microServicePolicies", fetch = FetchType.EAGER)
94     private Set<Loop> usedByLoops = new HashSet<>();
95
96     @Expose
97     @Column(name = "dcae_deployment_id")
98     private String dcaeDeploymentId;
99
100     @Expose
101     @Column(name = "dcae_deployment_status_url")
102     private String dcaeDeploymentStatusUrl;
103
104     @Expose
105     @Column(name = "dcae_blueprint_id")
106     private String dcaeBlueprintId;
107
108     public MicroServicePolicy() {
109         // serialization
110     }
111
112     /**
113      * The constructor that create the json representation from the policyTosca
114      * using the ToscaYamlToJsonConvertor.
115      *
116      * @param name        The name of the MicroService
117      * @param modelType   The model type of the MicroService
118      * @param policyTosca The policy Tosca of the MicroService
119      * @param shared      The flag indicate whether the MicroService is shared
120      * @param usedByLoops The list of loops that uses this MicroService
121      */
122     public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared,
123             Set<Loop> usedByLoops) {
124         this.name = name;
125         this.modelType = modelType;
126         this.policyTosca = policyTosca;
127         this.shared = shared;
128         this.setJsonRepresentation(JsonUtils.GSON_JPA_MODEL
129                 .fromJson(new ToscaYamlToJsonConvertor().parseToscaYaml(policyTosca, modelType), JsonObject.class));
130         this.usedByLoops = usedByLoops;
131     }
132
133     private JsonObject createJsonFromPolicyTosca() {
134         Map<String, Object> map = new Yaml().load(this.getPolicyTosca());
135         JSONObject jsonObject = new JSONObject(map);
136         return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
137     }
138
139     /**
140      * The constructor that does not make use of ToscaYamlToJsonConvertor but take
141      * the jsonRepresentation instead.
142      *
143      * @param name               The name of the MicroService
144      * @param modelType          The model type of the MicroService
145      * @param policyTosca        The policy Tosca of the MicroService
146      * @param shared             The flag indicate whether the MicroService is
147      *                           shared
148      * @param jsonRepresentation The UI representation in json format
149      * @param usedByLoops        The list of loops that uses this MicroService
150      */
151     public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared,
152             JsonObject jsonRepresentation, Set<Loop> usedByLoops) {
153         this.name = name;
154         this.modelType = modelType;
155         this.policyTosca = policyTosca;
156         this.shared = shared;
157         this.usedByLoops = usedByLoops;
158         this.setJsonRepresentation(jsonRepresentation);
159     }
160
161     @Override
162     public String getName() {
163         return name;
164     }
165
166     /**
167      * name setter.
168      * 
169      * @param name the name to set
170      */
171     @Override
172     public void setName(String name) {
173         this.name = name;
174     }
175
176     public String getModelType() {
177         return modelType;
178     }
179
180     void setModelType(String modelType) {
181         this.modelType = modelType;
182     }
183
184     public Boolean getShared() {
185         return shared;
186     }
187
188     void setShared(Boolean shared) {
189         this.shared = shared;
190     }
191
192     public String getPolicyTosca() {
193         return policyTosca;
194     }
195
196     void setPolicyTosca(String policyTosca) {
197         this.policyTosca = policyTosca;
198     }
199
200     public Set<Loop> getUsedByLoops() {
201         return usedByLoops;
202     }
203
204     void setUsedByLoops(Set<Loop> usedBy) {
205         this.usedByLoops = usedBy;
206     }
207
208     public String getContext() {
209         return context;
210     }
211
212     public void setContext(String context) {
213         this.context = context;
214     }
215
216     public String getDeviceTypeScope() {
217         return deviceTypeScope;
218     }
219
220     public void setDeviceTypeScope(String deviceTypeScope) {
221         this.deviceTypeScope = deviceTypeScope;
222     }
223
224     /**
225      * dcaeDeploymentId getter.
226      * 
227      * @return the dcaeDeploymentId
228      */
229     public String getDcaeDeploymentId() {
230         return dcaeDeploymentId;
231     }
232
233     /**
234      * dcaeDeploymentId setter.
235      * 
236      * @param dcaeDeploymentId the dcaeDeploymentId to set
237      */
238     public void setDcaeDeploymentId(String dcaeDeploymentId) {
239         this.dcaeDeploymentId = dcaeDeploymentId;
240     }
241
242     /**
243      * dcaeDeploymentStatusUrl getter.
244      * 
245      * @return the dcaeDeploymentStatusUrl
246      */
247     public String getDcaeDeploymentStatusUrl() {
248         return dcaeDeploymentStatusUrl;
249     }
250
251     /**
252      * dcaeDeploymentStatusUrl setter.
253      * 
254      * @param dcaeDeploymentStatusUrl the dcaeDeploymentStatusUrl to set
255      */
256     public void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
257         this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
258     }
259
260     /**
261      * dcaeBlueprintId getter.
262      * 
263      * @return the dcaeBlueprintId
264      */
265     public String getDcaeBlueprintId() {
266         return dcaeBlueprintId;
267     }
268
269     /**
270      * dcaeBlueprintId setter.
271      * 
272      * @param dcaeBlueprintId the dcaeBlueprintId to set
273      */
274     void setDcaeBlueprintId(String dcaeBlueprintId) {
275         this.dcaeBlueprintId = dcaeBlueprintId;
276     }
277
278     @Override
279     public int hashCode() {
280         final int prime = 31;
281         int result = 1;
282         result = prime * result + ((name == null) ? 0 : name.hashCode());
283         return result;
284     }
285
286     @Override
287     public boolean equals(Object obj) {
288         if (this == obj) {
289             return true;
290         }
291         if (obj == null) {
292             return false;
293         }
294         if (getClass() != obj.getClass()) {
295             return false;
296         }
297         MicroServicePolicy other = (MicroServicePolicy) obj;
298         if (name == null) {
299             if (other.name != null) {
300                 return false;
301             }
302         } else if (!name.equals(other.name)) {
303             return false;
304         }
305         return true;
306     }
307
308     private String getMicroServicePropertyNameFromTosca(JsonObject object) {
309         return object.getAsJsonObject("policy_types").getAsJsonObject(this.modelType).getAsJsonObject("properties")
310                 .keySet().toArray(new String[1])[0];
311     }
312
313     @Override
314     public String createPolicyPayload() {
315         JsonObject toscaJson = createJsonFromPolicyTosca();
316
317         JsonObject policyPayloadResult = new JsonObject();
318
319         policyPayloadResult.add("tosca_definitions_version", toscaJson.get("tosca_definitions_version"));
320
321         JsonObject topologyTemplateNode = new JsonObject();
322         policyPayloadResult.add("topology_template", topologyTemplateNode);
323
324         JsonArray policiesArray = new JsonArray();
325         topologyTemplateNode.add("policies", policiesArray);
326
327         JsonObject thisPolicy = new JsonObject();
328         policiesArray.add(thisPolicy);
329
330         JsonObject policyDetails = new JsonObject();
331         thisPolicy.add(this.getName(), policyDetails);
332         policyDetails.addProperty("type", this.getModelType());
333         policyDetails.addProperty("version", "1.0.0");
334
335         JsonObject policyMetadata = new JsonObject();
336         policyDetails.add("metadata", policyMetadata);
337         policyMetadata.addProperty("policy-id", this.getName());
338
339         JsonObject policyProperties = new JsonObject();
340         policyDetails.add("properties", policyProperties);
341         policyProperties.add(this.getMicroServicePropertyNameFromTosca(toscaJson), this.getConfigurationsJson());
342         String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);
343         logger.info("Micro service policy payload: " + policyPayload);
344         return policyPayload;
345     }
346
347 }