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;
34 import java.io.UnsupportedEncodingException;
36 import javax.persistence.Column;
37 import javax.persistence.FetchType;
38 import javax.persistence.JoinColumn;
39 import javax.persistence.JoinColumns;
40 import javax.persistence.ManyToOne;
41 import javax.persistence.MappedSuperclass;
42 import javax.persistence.Transient;
43 import org.hibernate.annotations.Type;
44 import org.hibernate.annotations.TypeDef;
45 import org.hibernate.annotations.TypeDefs;
46 import org.json.JSONObject;
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 * policyModel getter.
182 * @return the policyModel
184 public PolicyModel getPolicyModel() {
189 * policyModel setter.
191 * @param policyModel The new policyModel
193 public void setPolicyModel(PolicyModel policyModel) {
194 this.policyModel = policyModel;
198 * configurationsJson getter.
200 * @return The configurationsJson
202 public JsonObject getConfigurationsJson() {
203 return configurationsJson;
207 * configurationsJson setter.
209 * @param configurationsJson the configurationsJson to set
211 public void setConfigurationsJson(JsonObject configurationsJson) {
212 this.configurationsJson = configurationsJson;
216 * loopElementModel getter.
218 * @return the loopElementModel
220 public LoopElementModel getLoopElementModel() {
221 return loopElementModel;
225 * loopElementModel setter.
227 * @param loopElementModel the loopElementModel to set
229 public void setLoopElementModel(LoopElementModel loopElementModel) {
230 this.loopElementModel = loopElementModel;
236 * @return the pdpGroup
238 public String getPdpGroup() {
245 * @param pdpGroup the pdpGroup to set
247 public void setPdpGroup(String pdpGroup) {
248 this.pdpGroup = pdpGroup;
252 * pdpSubgroup getter.
254 * @return the pdpSubgroup
256 public String getPdpSubgroup() {
261 * pdpSubgroup setter.
263 * @param pdpSubgroup the pdpSubgroup to set
265 public void setPdpSubgroup(String pdpSubgroup) {
266 this.pdpSubgroup = pdpSubgroup;
270 * Generate the policy name.
272 * @param policyType The policy type
273 * @param serviceName The service name
274 * @param serviceVersion The service version
275 * @param resourceName The resource name
276 * @param blueprintFilename The blueprint file name
277 * @return The generated policy name
279 public static String generatePolicyName(String policyType, String serviceName, String serviceVersion,
280 String resourceName, String blueprintFilename) {
281 StringBuilder buffer = new StringBuilder(policyType).append("_").append(serviceName).append("_v")
282 .append(serviceVersion).append("_").append(resourceName).append("_")
283 .append(blueprintFilename.replaceAll(".yaml", ""));
284 return buffer.toString().replace('.', '_').replaceAll(" ", "");