LoopLog repository
[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.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonObject;
30 import com.google.gson.annotations.Expose;
31
32 import java.io.Serializable;
33 import java.util.HashSet;
34 import java.util.Map;
35 import java.util.Set;
36
37 import javax.persistence.Column;
38 import javax.persistence.Entity;
39 import javax.persistence.FetchType;
40 import javax.persistence.Id;
41 import javax.persistence.ManyToMany;
42 import javax.persistence.Table;
43
44 import org.hibernate.annotations.Type;
45 import org.hibernate.annotations.TypeDef;
46 import org.hibernate.annotations.TypeDefs;
47 import org.json.JSONObject;
48 import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
49 import org.onap.clamp.clds.util.JsonUtils;
50 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
51 import org.onap.clamp.loop.Loop;
52 import org.onap.clamp.policy.Policy;
53 import org.yaml.snakeyaml.Yaml;
54
55 @Entity
56 @Table(name = "micro_service_policies")
57 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
58 public class MicroServicePolicy implements Serializable, Policy {
59     /**
60      * The serial version ID.
61      */
62     private static final long serialVersionUID = 6271238288583332616L;
63
64     @Expose
65     @Id
66     @Column(nullable = false, name = "name", unique = true)
67     private String name;
68
69     @Expose
70     @Column(nullable = false, name = "model_type")
71     private String modelType;
72
73     @Expose
74     @Type(type = "json")
75     @Column(columnDefinition = "json", name = "properties")
76     private JsonObject properties;
77
78     @Expose
79     @Column(name = "shared", nullable = false)
80     private Boolean shared;
81
82     @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca", nullable = false)
83     private String policyTosca;
84
85     @Expose
86     @Type(type = "json")
87     @Column(columnDefinition = "json", name = "json_representation", nullable = false)
88     private JsonObject jsonRepresentation;
89
90     @ManyToMany(mappedBy = "microServicePolicies", fetch = FetchType.EAGER)
91     private Set<Loop> usedByLoops = new HashSet<>();
92
93     public MicroServicePolicy() {
94         // serialization
95     }
96
97     /**
98      * The constructor that create the json representation from the policyTosca
99      * using the ToscaYamlToJsonConvertor.
100      *
101      * @param name
102      *        The name of the MicroService
103      * @param type
104      *        The model type of the MicroService
105      * @param policyTosca
106      *        The policy Tosca of the MicroService
107      * @param shared
108      *        The flag indicate whether the MicroService is shared
109      * @param usedByLoops
110      *        The list of loops that uses this MicroService
111      */
112     public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared,
113         Set<Loop> usedByLoops) {
114         this.name = name;
115         this.modelType = modelType;
116         this.policyTosca = policyTosca;
117         this.shared = shared;
118         this.jsonRepresentation = JsonUtils.GSON_JPA_MODEL
119             .fromJson(new ToscaYamlToJsonConvertor(null).parseToscaYaml(policyTosca), JsonObject.class);
120         this.usedByLoops = usedByLoops;
121     }
122
123     private JsonObject createJsonFromPolicyTosca() {
124         Map<String, Object> map = new Yaml().load(this.getPolicyTosca());
125         JSONObject jsonObject = new JSONObject(map);
126         return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
127     }
128
129     /**
130      * The constructor that does not make use of ToscaYamlToJsonConvertor but take
131      * the jsonRepresentation instead.
132      *
133      * @param name
134      *        The name of the MicroService
135      * @param type
136      *        The model type of the MicroService
137      * @param policyTosca
138      *        The policy Tosca of the MicroService
139      * @param shared
140      *        The flag indicate whether the MicroService is shared
141      * @param jsonRepresentation
142      *        The UI representation in json format
143      * @param usedByLoops
144      *        The list of loops that uses this MicroService
145      */
146     public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared,
147         JsonObject jsonRepresentation, Set<Loop> usedByLoops) {
148         this.name = name;
149         this.modelType = modelType;
150         this.policyTosca = policyTosca;
151         this.shared = shared;
152         this.usedByLoops = usedByLoops;
153         this.jsonRepresentation = jsonRepresentation;
154     }
155
156     @Override
157     public String getName() {
158         return name;
159     }
160
161     public String getModelType() {
162         return modelType;
163     }
164
165     void setModelType(String modelType) {
166         this.modelType = modelType;
167     }
168
169     public JsonObject getProperties() {
170         return properties;
171     }
172
173     public void setProperties(JsonObject properties) {
174         this.properties = properties;
175     }
176
177     public Boolean getShared() {
178         return shared;
179     }
180
181     void setShared(Boolean shared) {
182         this.shared = shared;
183     }
184
185     public String getPolicyTosca() {
186         return policyTosca;
187     }
188
189     void setPolicyTosca(String policyTosca) {
190         this.policyTosca = policyTosca;
191     }
192
193     @Override
194     public JsonObject getJsonRepresentation() {
195         return jsonRepresentation;
196     }
197
198     void setJsonRepresentation(JsonObject jsonRepresentation) {
199         this.jsonRepresentation = jsonRepresentation;
200     }
201
202     public Set<Loop> getUsedByLoops() {
203         return usedByLoops;
204     }
205
206     void setUsedByLoops(Set<Loop> usedBy) {
207         this.usedByLoops = usedBy;
208     }
209
210     @Override
211     public int hashCode() {
212         final int prime = 31;
213         int result = 1;
214         result = prime * result + ((name == null) ? 0 : name.hashCode());
215         return result;
216     }
217
218     @Override
219     public boolean equals(Object obj) {
220         if (this == obj) {
221             return true;
222         }
223         if (obj == null) {
224             return false;
225         }
226         if (getClass() != obj.getClass()) {
227             return false;
228         }
229         MicroServicePolicy other = (MicroServicePolicy) obj;
230         if (name == null) {
231             if (other.name != null) {
232                 return false;
233             }
234         } else if (!name.equals(other.name)) {
235             return false;
236         }
237         return true;
238     }
239
240     private String getMicroServicePropertyNameFromTosca(JsonObject object) {
241         return object.getAsJsonObject("policy_types").getAsJsonObject(this.modelType).getAsJsonObject("properties")
242             .keySet().toArray(new String[1])[0];
243     }
244
245     @Override
246     public String createPolicyPayload() {
247         JsonObject toscaJson = createJsonFromPolicyTosca();
248
249         JsonObject policyPayloadResult = new JsonObject();
250
251         policyPayloadResult.add("tosca_definitions_version", toscaJson.get("tosca_definitions_version"));
252
253         JsonObject topologyTemplateNode = new JsonObject();
254         policyPayloadResult.add("topology_template", topologyTemplateNode);
255
256         JsonArray policiesArray = new JsonArray();
257         topologyTemplateNode.add("policies", policiesArray);
258
259         JsonObject thisPolicy = new JsonObject();
260         policiesArray.add(thisPolicy);
261
262         JsonObject policyDetails = new JsonObject();
263         thisPolicy.add(this.getName(), policyDetails);
264         policyDetails.addProperty("type", this.getModelType());
265         policyDetails.addProperty("version", "1.0.0");
266
267         JsonObject policyMetadata = new JsonObject();
268         policyDetails.add("metadata", policyMetadata);
269         policyMetadata.addProperty("policy-id", this.getName());
270
271         JsonObject policyProperties = new JsonObject();
272         policyDetails.add("properties", policyProperties);
273         policyProperties.add(this.getMicroServicePropertyNameFromTosca(toscaJson), this.getProperties());
274         return new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);
275     }
276
277 }