Remove legacy operational Policy
[clamp.git] / src / main / java / org / onap / clamp / policy / Policy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia 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;
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 import java.io.UnsupportedEncodingException;
34 import java.util.Map;
35 import javax.persistence.Column;
36 import javax.persistence.FetchType;
37 import javax.persistence.JoinColumn;
38 import javax.persistence.JoinColumns;
39 import javax.persistence.ManyToOne;
40 import javax.persistence.MappedSuperclass;
41 import javax.persistence.Transient;
42 import org.hibernate.annotations.Type;
43 import org.hibernate.annotations.TypeDef;
44 import org.hibernate.annotations.TypeDefs;
45 import org.json.JSONObject;
46 import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport;
47 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
48 import org.onap.clamp.loop.common.AuditEntity;
49 import org.onap.clamp.loop.service.Service;
50 import org.onap.clamp.loop.template.LoopElementModel;
51 import org.onap.clamp.loop.template.PolicyModel;
52 import org.yaml.snakeyaml.Yaml;
53
54 @MappedSuperclass
55 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
56 public abstract class Policy extends AuditEntity {
57
58     @Transient
59     private static final EELFLogger logger = EELFManager.getInstance().getLogger(Policy.class);
60
61     @Expose
62     @Type(type = "json")
63     @Column(columnDefinition = "json", name = "json_representation", nullable = false)
64     private JsonObject jsonRepresentation;
65
66     @Expose
67     @Type(type = "json")
68     @Column(columnDefinition = "json", name = "configurations_json")
69     private JsonObject configurationsJson;
70
71     /**
72      * This attribute can be null when the user add a policy on the loop instance, not the template.
73      * When null, It therefore indicates that this policy is not by default in the loop template.
74      */
75     @Expose
76     @ManyToOne(fetch = FetchType.EAGER)
77     @JoinColumn(name = "loop_element_model_id")
78     private LoopElementModel loopElementModel;
79
80     @Expose
81     @Column(name = "pdp_group")
82     private String pdpGroup;
83
84     @Expose
85     @Column(name = "pdp_sub_group")
86     private String pdpSubgroup;
87
88     @Expose
89     @ManyToOne(fetch = FetchType.EAGER)
90     @JoinColumns({@JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"),
91             @JoinColumn(name = "policy_model_version", referencedColumnName = "version")})
92     private PolicyModel policyModel;
93
94     private JsonObject createJsonFromPolicyTosca() {
95         Map<String, Object> map =
96                 new Yaml().load(this.getPolicyModel() != null ? this.getPolicyModel().getPolicyModelTosca() : "");
97         JSONObject jsonObject = new JSONObject(map);
98         return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
99     }
100
101     /**
102      * This method create the policy payload that must be sent to PEF.
103      *
104      * @return A String containing the payload
105      * @throws UnsupportedEncodingException In case of failure
106      */
107     public String createPolicyPayload() throws UnsupportedEncodingException {
108         JsonObject toscaJson = createJsonFromPolicyTosca();
109
110         JsonObject policyPayloadResult = new JsonObject();
111
112         policyPayloadResult.add("tosca_definitions_version", toscaJson.get("tosca_definitions_version"));
113
114         JsonObject topologyTemplateNode = new JsonObject();
115         policyPayloadResult.add("topology_template", topologyTemplateNode);
116
117         JsonArray policiesArray = new JsonArray();
118         topologyTemplateNode.add("policies", policiesArray);
119
120         JsonObject thisPolicy = new JsonObject();
121         policiesArray.add(thisPolicy);
122
123         JsonObject policyDetails = new JsonObject();
124         thisPolicy.add(this.getName(), policyDetails);
125         policyDetails.addProperty("type", this.getPolicyModel().getPolicyModelType());
126         policyDetails.addProperty("type_version", this.getPolicyModel().getVersion());
127         policyDetails.addProperty("version", this.getPolicyModel().getVersion());
128
129         JsonObject policyMetadata = new JsonObject();
130         policyDetails.add("metadata", policyMetadata);
131         policyMetadata.addProperty("policy-id", this.getName());
132
133         policyDetails.add("properties", this.getConfigurationsJson());
134
135         String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);
136         logger.info("Policy payload: " + policyPayload);
137         return policyPayload;
138     }
139
140
141     /**
142      * Name getter.
143      *
144      * @return the name
145      */
146     public abstract String getName();
147
148     /**
149      * Name setter.
150      */
151     public abstract void setName(String name);
152
153     /**
154      * jsonRepresentation getter.
155      *
156      * @return the jsonRepresentation
157      */
158     public JsonObject getJsonRepresentation() {
159         return jsonRepresentation;
160     }
161
162     /**
163      * jsonRepresentation setter.
164      *
165      * @param jsonRepresentation The jsonRepresentation to set
166      */
167     public void setJsonRepresentation(JsonObject jsonRepresentation) {
168         this.jsonRepresentation = jsonRepresentation;
169     }
170
171     /**
172      * Regenerate the Policy Json Representation.
173      *
174      * @param toscaConverter The tosca converter required to regenerate the json schema
175      * @param serviceModel The service model associated
176      */
177     public abstract void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter,
178                                                   Service serviceModel);
179
180     /**
181      * policyModel getter.
182      *
183      * @return the policyModel
184      */
185     public PolicyModel getPolicyModel() {
186         return policyModel;
187     }
188
189     /**
190      * policyModel setter.
191      *
192      * @param policyModel The new policyModel
193      */
194     public void setPolicyModel(PolicyModel policyModel) {
195         this.policyModel = policyModel;
196     }
197
198     /**
199      * configurationsJson getter.
200      *
201      * @return The configurationsJson
202      */
203     public JsonObject getConfigurationsJson() {
204         return configurationsJson;
205     }
206
207     /**
208      * configurationsJson setter.
209      *
210      * @param configurationsJson the configurationsJson to set
211      */
212     public void setConfigurationsJson(JsonObject configurationsJson) {
213         this.configurationsJson = configurationsJson;
214     }
215
216     /**
217      * loopElementModel getter.
218      *
219      * @return the loopElementModel
220      */
221     public LoopElementModel getLoopElementModel() {
222         return loopElementModel;
223     }
224
225     /**
226      * loopElementModel setter.
227      *
228      * @param loopElementModel the loopElementModel to set
229      */
230     public void setLoopElementModel(LoopElementModel loopElementModel) {
231         this.loopElementModel = loopElementModel;
232     }
233
234     /**
235      * pdpGroup getter.
236      *
237      * @return the pdpGroup
238      */
239     public String getPdpGroup() {
240         return pdpGroup;
241     }
242
243     /**
244      * pdpGroup setter.
245      *
246      * @param pdpGroup the pdpGroup to set
247      */
248     public void setPdpGroup(String pdpGroup) {
249         this.pdpGroup = pdpGroup;
250     }
251
252     /**
253      * pdpSubgroup getter.
254      *
255      * @return the pdpSubgroup
256      */
257     public String getPdpSubgroup() {
258         return pdpSubgroup;
259     }
260
261     /**
262      * pdpSubgroup setter.
263      *
264      * @param pdpSubgroup the pdpSubgroup to set
265      */
266     public void setPdpSubgroup(String pdpSubgroup) {
267         this.pdpSubgroup = pdpSubgroup;
268     }
269
270     /**
271      * Generate the policy name.
272      *
273      * @param policyType        The policy type
274      * @param serviceName       The service name
275      * @param serviceVersion    The service version
276      * @param resourceName      The resource name
277      * @param blueprintFilename The blueprint file name
278      * @return The generated policy name
279      */
280     public static String generatePolicyName(String policyType, String serviceName, String serviceVersion,
281                                             String resourceName, String blueprintFilename) {
282         StringBuilder buffer = new StringBuilder(policyType).append("_").append(serviceName).append("_v")
283                 .append(serviceVersion).append("_").append(resourceName).append("_")
284                 .append(blueprintFilename.replaceAll(".yaml", ""));
285         return buffer.toString().replace('.', '_').replaceAll(" ", "");
286     }
287 }