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.service.Service;
50 import org.onap.clamp.loop.template.LoopElementModel;
51 import org.onap.clamp.loop.template.PolicyModel;
52 import org.yaml.snakeyaml.Yaml;
55 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
56 public abstract class Policy extends AuditEntity {
59 private static final EELFLogger logger = EELFManager.getInstance().getLogger(Policy.class);
63 @Column(columnDefinition = "json", name = "json_representation", nullable = false)
64 private JsonObject jsonRepresentation;
68 @Column(columnDefinition = "json", name = "configurations_json")
69 private JsonObject configurationsJson;
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.
76 @ManyToOne(fetch = FetchType.EAGER)
77 @JoinColumn(name = "loop_element_model_id")
78 private LoopElementModel loopElementModel;
81 @Column(name = "pdp_group")
82 private String pdpGroup;
85 @Column(name = "pdp_sub_group")
86 private String pdpSubgroup;
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;
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);
102 * This method create the policy payload that must be sent to PEF.
104 * @return A String containing the payload
105 * @throws UnsupportedEncodingException In case of failure
107 public String createPolicyPayload() throws UnsupportedEncodingException {
108 JsonObject toscaJson = createJsonFromPolicyTosca();
110 JsonObject policyPayloadResult = new JsonObject();
112 policyPayloadResult.add("tosca_definitions_version", toscaJson.get("tosca_definitions_version"));
114 JsonObject topologyTemplateNode = new JsonObject();
115 policyPayloadResult.add("topology_template", topologyTemplateNode);
117 JsonArray policiesArray = new JsonArray();
118 topologyTemplateNode.add("policies", policiesArray);
120 JsonObject thisPolicy = new JsonObject();
121 policiesArray.add(thisPolicy);
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());
129 JsonObject policyMetadata = new JsonObject();
130 policyDetails.add("metadata", policyMetadata);
131 policyMetadata.addProperty("policy-id", this.getName());
133 policyDetails.add("properties", this.getConfigurationsJson());
135 String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);
136 logger.info("Policy payload: " + policyPayload);
137 return policyPayload;
146 public abstract String getName();
151 public abstract void setName(String name);
154 * jsonRepresentation getter.
156 * @return the jsonRepresentation
158 public JsonObject getJsonRepresentation() {
159 return jsonRepresentation;
163 * jsonRepresentation setter.
165 * @param jsonRepresentation The jsonRepresentation to set
167 public void setJsonRepresentation(JsonObject jsonRepresentation) {
168 this.jsonRepresentation = jsonRepresentation;
172 * Regenerate the Policy Json Representation.
174 * @param toscaConverter The tosca converter required to regenerate the json schema
175 * @param serviceModel The service model associated
177 public abstract void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter,
178 Service serviceModel);
181 * policyModel getter.
183 * @return the policyModel
185 public PolicyModel getPolicyModel() {
190 * policyModel setter.
192 * @param policyModel The new policyModel
194 public void setPolicyModel(PolicyModel policyModel) {
195 this.policyModel = policyModel;
199 * configurationsJson getter.
201 * @return The configurationsJson
203 public JsonObject getConfigurationsJson() {
204 return configurationsJson;
208 * configurationsJson setter.
210 * @param configurationsJson the configurationsJson to set
212 public void setConfigurationsJson(JsonObject configurationsJson) {
213 this.configurationsJson = configurationsJson;
217 * loopElementModel getter.
219 * @return the loopElementModel
221 public LoopElementModel getLoopElementModel() {
222 return loopElementModel;
226 * loopElementModel setter.
228 * @param loopElementModel the loopElementModel to set
230 public void setLoopElementModel(LoopElementModel loopElementModel) {
231 this.loopElementModel = loopElementModel;
237 * @return the pdpGroup
239 public String getPdpGroup() {
246 * @param pdpGroup the pdpGroup to set
248 public void setPdpGroup(String pdpGroup) {
249 this.pdpGroup = pdpGroup;
253 * pdpSubgroup getter.
255 * @return the pdpSubgroup
257 public String getPdpSubgroup() {
262 * pdpSubgroup setter.
264 * @param pdpSubgroup the pdpSubgroup to set
266 public void setPdpSubgroup(String pdpSubgroup) {
267 this.pdpSubgroup = pdpSubgroup;
271 * Generate the policy name.
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
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(" ", "");