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);
101 * This method create the policy payload that must be sent to PEF.
103 * @return A String containing the payload
104 * @throws UnsupportedEncodingException In case of failure
106 public String createPolicyPayload() throws UnsupportedEncodingException {
107 JsonObject toscaJson = createJsonFromPolicyTosca();
109 JsonObject policyPayloadResult = new JsonObject();
111 policyPayloadResult.add("tosca_definitions_version", toscaJson.get("tosca_definitions_version"));
113 JsonObject topologyTemplateNode = new JsonObject();
114 policyPayloadResult.add("topology_template", topologyTemplateNode);
116 JsonArray policiesArray = new JsonArray();
117 topologyTemplateNode.add("policies", policiesArray);
119 JsonObject thisPolicy = new JsonObject();
120 policiesArray.add(thisPolicy);
122 JsonObject policyDetails = new JsonObject();
123 thisPolicy.add(this.getName(), policyDetails);
124 policyDetails.addProperty("type", this.getPolicyModel().getPolicyModelType());
125 policyDetails.addProperty("type_version", this.getPolicyModel().getVersion());
126 policyDetails.addProperty("version", this.getPolicyModel().getVersion());
128 JsonObject policyMetadata = new JsonObject();
129 policyDetails.add("metadata", policyMetadata);
130 policyMetadata.addProperty("policy-id", this.getName());
132 policyDetails.add("properties", this.getConfigurationsJson());
134 String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);
135 logger.info("Policy payload: " + policyPayload);
136 return policyPayload;
145 public abstract String getName();
150 public abstract void setName(String name);
153 * jsonRepresentation getter.
155 * @return the jsonRepresentation
157 public JsonObject getJsonRepresentation() {
158 return jsonRepresentation;
162 * jsonRepresentation setter.
164 * @param jsonRepresentation The jsonRepresentation to set
166 public void setJsonRepresentation(JsonObject jsonRepresentation) {
167 this.jsonRepresentation = jsonRepresentation;
171 * Regenerate the Policy Json Representation.
173 * @param toscaConverter The tosca converter required to regenerate the json schema
175 public abstract void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter);
178 * policyModel getter.
180 * @return the policyModel
182 public PolicyModel getPolicyModel() {
187 * policyModel setter.
189 * @param policyModel The new policyModel
191 public void setPolicyModel(PolicyModel policyModel) {
192 this.policyModel = policyModel;
196 * configurationsJson getter.
198 * @return The configurationsJson
200 public JsonObject getConfigurationsJson() {
201 return configurationsJson;
205 * configurationsJson setter.
207 * @param configurationsJson the configurationsJson to set
209 public void setConfigurationsJson(JsonObject configurationsJson) {
210 this.configurationsJson = configurationsJson;
214 * loopElementModel getter.
216 * @return the loopElementModel
218 public LoopElementModel getLoopElementModel() {
219 return loopElementModel;
223 * loopElementModel setter.
225 * @param loopElementModel the loopElementModel to set
227 public void setLoopElementModel(LoopElementModel loopElementModel) {
228 this.loopElementModel = loopElementModel;
234 * @return the pdpGroup
236 public String getPdpGroup() {
243 * @param pdpGroup the pdpGroup to set
245 public void setPdpGroup(String pdpGroup) {
246 this.pdpGroup = pdpGroup;
250 * pdpSubgroup getter.
252 * @return the pdpSubgroup
254 public String getPdpSubgroup() {
259 * pdpSubgroup setter.
261 * @param pdpSubgroup the pdpSubgroup to set
263 public void setPdpSubgroup(String pdpSubgroup) {
264 this.pdpSubgroup = pdpSubgroup;
268 * Generate the policy name.
270 * @param policyType The policy type
271 * @param serviceName The service name
272 * @param serviceVersion The service version
273 * @param resourceName The resource name
274 * @param blueprintFilename The blueprint file name
275 * @return The generated policy name
277 public static String generatePolicyName(String policyType, String serviceName, String serviceVersion,
278 String resourceName, String blueprintFilename) {
279 StringBuilder buffer = new StringBuilder(policyType).append("_").append(serviceName).append("_v")
280 .append(serviceVersion).append("_").append(resourceName).append("_")
281 .append(blueprintFilename.replaceAll(".yaml", ""));
282 return buffer.toString().replace('.', '_').replaceAll(" ", "");