2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.policy;
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;
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.template.LoopElementModel;
50 import org.onap.clamp.loop.template.PolicyModel;
51 import org.yaml.snakeyaml.Yaml;
54 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
55 public abstract class Policy extends AuditEntity {
58 private static final EELFLogger logger = EELFManager.getInstance().getLogger(Policy.class);
62 @Column(columnDefinition = "json", name = "json_representation", nullable = false)
63 private JsonObject jsonRepresentation;
67 @Column(columnDefinition = "json", name = "configurations_json")
68 private JsonObject configurationsJson;
71 * This attribute can be null when the user add a policy on the loop instance, not the template.
72 * When null, It therefore indicates that this policy is not by default in the loop template.
75 @ManyToOne(fetch = FetchType.EAGER)
76 @JoinColumn(name = "loop_element_model_id")
77 private LoopElementModel loopElementModel;
80 @Column(name = "pdp_group")
81 private String pdpGroup;
84 @Column(name = "pdp_sub_group")
85 private String pdpSubgroup;
88 @ManyToOne(fetch = FetchType.EAGER)
89 @JoinColumns({@JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"),
90 @JoinColumn(name = "policy_model_version", referencedColumnName = "version")})
91 private PolicyModel policyModel;
93 private JsonObject createJsonFromPolicyTosca() {
94 Map<String, Object> map =
95 new Yaml().load(this.getPolicyModel() != null ? this.getPolicyModel().getPolicyModelTosca() : "");
96 JSONObject jsonObject = new JSONObject(map);
97 return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
100 private String getModelPropertyNameFromTosca(JsonObject object, String policyModelType) {
101 return object.getAsJsonObject("policy_types").getAsJsonObject(policyModelType)
104 .keySet().toArray(new String[1])[0];
108 * This method create the policy payload that must be sent to PEF.
110 * @return A String containing the payload
111 * @throws UnsupportedEncodingException In case of failure
113 public String createPolicyPayload() throws UnsupportedEncodingException {
114 JsonObject toscaJson = createJsonFromPolicyTosca();
116 JsonObject policyPayloadResult = new JsonObject();
118 policyPayloadResult.add("tosca_definitions_version", toscaJson.get("tosca_definitions_version"));
120 JsonObject topologyTemplateNode = new JsonObject();
121 policyPayloadResult.add("topology_template", topologyTemplateNode);
123 JsonArray policiesArray = new JsonArray();
124 topologyTemplateNode.add("policies", policiesArray);
126 JsonObject thisPolicy = new JsonObject();
127 policiesArray.add(thisPolicy);
129 JsonObject policyDetails = new JsonObject();
130 thisPolicy.add(this.getName(), policyDetails);
131 policyDetails.addProperty("type", this.getPolicyModel().getPolicyModelType());
132 policyDetails.addProperty("version", this.getPolicyModel().getVersion());
134 JsonObject policyMetadata = new JsonObject();
135 policyDetails.add("metadata", policyMetadata);
136 policyMetadata.addProperty("policy-id", this.getName());
138 JsonObject policyProperties = new JsonObject();
139 policyDetails.add("properties", policyProperties);
141 .add(this.getModelPropertyNameFromTosca(toscaJson, this.getPolicyModel().getPolicyModelType()),
142 this.getConfigurationsJson());
143 String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);
144 logger.info("Policy payload: " + policyPayload);
145 return policyPayload;
154 public abstract String getName();
159 public abstract void setName(String name);
162 * jsonRepresentation getter.
164 * @return the jsonRepresentation
166 public JsonObject getJsonRepresentation() {
167 return jsonRepresentation;
171 * jsonRepresentation setter.
173 * @param jsonRepresentation The jsonRepresentation to set
175 public void setJsonRepresentation(JsonObject jsonRepresentation) {
176 this.jsonRepresentation = jsonRepresentation;
180 * Regenerate the Policy Json Representation.
182 * @param toscaConverter The tosca converter required to regenerate the json schema
184 public abstract void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter);
187 * policyModel getter.
189 * @return the policyModel
191 public PolicyModel getPolicyModel() {
196 * policyModel setter.
198 * @param policyModel The new policyModel
200 public void setPolicyModel(PolicyModel policyModel) {
201 this.policyModel = policyModel;
205 * configurationsJson getter.
207 * @return The configurationsJson
209 public JsonObject getConfigurationsJson() {
210 return configurationsJson;
214 * configurationsJson setter.
216 * @param configurationsJson the configurationsJson to set
218 public void setConfigurationsJson(JsonObject configurationsJson) {
219 this.configurationsJson = configurationsJson;
223 * loopElementModel getter.
225 * @return the loopElementModel
227 public LoopElementModel getLoopElementModel() {
228 return loopElementModel;
232 * loopElementModel setter.
234 * @param loopElementModel the loopElementModel to set
236 public void setLoopElementModel(LoopElementModel loopElementModel) {
237 this.loopElementModel = loopElementModel;
243 * @return the pdpGroup
245 public String getPdpGroup() {
252 * @param pdpGroup the pdpGroup to set
254 public void setPdpGroup(String pdpGroup) {
255 this.pdpGroup = pdpGroup;
259 * pdpSubgroup getter.
261 * @return the pdpSubgroup
263 public String getPdpSubgroup() {
268 * pdpSubgroup setter.
270 * @param pdpSubgroup the pdpSubgroup to set
272 public void setPdpSubgroup(String pdpSubgroup) {
273 this.pdpSubgroup = pdpSubgroup;
277 * Generate the policy name.
279 * @param policyType The policy type
280 * @param serviceName The service name
281 * @param serviceVersion The service version
282 * @param resourceName The resource name
283 * @param blueprintFilename The blueprint file name
284 * @return The generated policy name
286 public static String generatePolicyName(String policyType, String serviceName, String serviceVersion,
287 String resourceName, String blueprintFilename) {
288 StringBuilder buffer = new StringBuilder(policyType).append("_").append(serviceName).append("_v")
289 .append(serviceVersion).append("_").append(resourceName).append("_")
290 .append(blueprintFilename.replaceAll(".yaml", ""));
291 return buffer.toString().replace('.', '_').replaceAll(" ", "");