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.IOException;
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.clds.tosca.update.TemplateManagement;
48 import org.onap.clamp.clds.tosca.update.UnknownComponentException;
49 import org.onap.clamp.clds.util.ResourceFileUtil;
50 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
51 import org.onap.clamp.loop.common.AuditEntity;
52 import org.onap.clamp.loop.template.LoopElementModel;
53 import org.onap.clamp.loop.template.PolicyModel;
54 import org.yaml.snakeyaml.Yaml;
57 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
58 public abstract class Policy extends AuditEntity {
61 private static final EELFLogger logger = EELFManager.getInstance().getLogger(Policy.class);
65 @Column(columnDefinition = "json", name = "json_representation", nullable = false)
66 private JsonObject jsonRepresentation;
70 @Column(columnDefinition = "json", name = "configurations_json")
71 private JsonObject configurationsJson;
74 * This attribute can be null when the user add a policy on the loop instance, not the template.
75 * When null, It therefore indicates that this policy is not by default in the loop template.
78 @ManyToOne(fetch = FetchType.EAGER)
79 @JoinColumn(name = "loop_element_model_id")
80 private LoopElementModel loopElementModel;
83 @Column(name = "pdp_group")
84 private String pdpGroup;
87 @Column(name = "pdp_sub_group")
88 private String pdpSubgroup;
91 @ManyToOne(fetch = FetchType.EAGER)
92 @JoinColumns({@JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"),
93 @JoinColumn(name = "policy_model_version", referencedColumnName = "version")})
94 private PolicyModel policyModel;
96 private JsonObject createJsonFromPolicyTosca() {
97 Map<String, Object> map =
98 new Yaml().load(this.getPolicyModel() != null ? this.getPolicyModel().getPolicyModelTosca() : "");
99 JSONObject jsonObject = new JSONObject(map);
100 return new Gson().fromJson(jsonObject.toString(), JsonObject.class);
103 private String getModelPropertyNameFromTosca(JsonObject object, String policyModelType) {
104 return object.getAsJsonObject("policy_types").getAsJsonObject(policyModelType)
107 .keySet().toArray(new String[1])[0];
111 * This method create the policy payload that must be sent to PEF.
113 * @return A String containing the payload
114 * @throws UnsupportedEncodingException In case of failure
116 public String createPolicyPayload() throws UnsupportedEncodingException {
117 JsonObject toscaJson = createJsonFromPolicyTosca();
119 JsonObject policyPayloadResult = new JsonObject();
121 policyPayloadResult.add("tosca_definitions_version", toscaJson.get("tosca_definitions_version"));
123 JsonObject topologyTemplateNode = new JsonObject();
124 policyPayloadResult.add("topology_template", topologyTemplateNode);
126 JsonArray policiesArray = new JsonArray();
127 topologyTemplateNode.add("policies", policiesArray);
129 JsonObject thisPolicy = new JsonObject();
130 policiesArray.add(thisPolicy);
132 JsonObject policyDetails = new JsonObject();
133 thisPolicy.add(this.getName(), policyDetails);
134 policyDetails.addProperty("type", this.getPolicyModel().getPolicyModelType());
135 policyDetails.addProperty("version", this.getPolicyModel().getVersion());
137 JsonObject policyMetadata = new JsonObject();
138 policyDetails.add("metadata", policyMetadata);
139 policyMetadata.addProperty("policy-id", this.getName());
141 JsonObject policyProperties = new JsonObject();
142 policyDetails.add("properties", policyProperties);
144 .add(this.getModelPropertyNameFromTosca(toscaJson, this.getPolicyModel().getPolicyModelType()),
145 this.getConfigurationsJson());
146 String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);
147 logger.info("Policy payload: " + policyPayload);
148 return policyPayload;
157 public abstract String getName();
162 public abstract void setName(String name);
165 * jsonRepresentation getter.
167 * @return the jsonRepresentation
169 public JsonObject getJsonRepresentation() {
170 return jsonRepresentation;
174 * jsonRepresentation setter.
176 * @param jsonRepresentation The jsonRepresentation to set
178 public void setJsonRepresentation(JsonObject jsonRepresentation) {
179 this.jsonRepresentation = jsonRepresentation;
183 * policyModel getter.
185 * @return the policyModel
187 public PolicyModel getPolicyModel() {
192 * policyModel setter.
194 * @param policyModel The new policyModel
196 public void setPolicyModel(PolicyModel policyModel) {
197 this.policyModel = policyModel;
201 * configurationsJson getter.
203 * @return The configurationsJson
205 public JsonObject getConfigurationsJson() {
206 return configurationsJson;
210 * configurationsJson setter.
212 * @param configurationsJson the configurationsJson to set
214 public void setConfigurationsJson(JsonObject configurationsJson) {
215 this.configurationsJson = configurationsJson;
219 * loopElementModel getter.
221 * @return the loopElementModel
223 public LoopElementModel getLoopElementModel() {
224 return loopElementModel;
228 * loopElementModel setter.
230 * @param loopElementModel the loopElementModel to set
232 public void setLoopElementModel(LoopElementModel loopElementModel) {
233 this.loopElementModel = loopElementModel;
239 * @return the pdpGroup
241 public String getPdpGroup() {
248 * @param pdpGroup the pdpGroup to set
250 public void setPdpGroup(String pdpGroup) {
251 this.pdpGroup = pdpGroup;
255 * pdpSubgroup getter.
257 * @return the pdpSubgroup
259 public String getPdpSubgroup() {
264 * pdpSubgroup setter.
266 * @param pdpSubgroup the pdpSubgroup to set
268 public void setPdpSubgroup(String pdpSubgroup) {
269 this.pdpSubgroup = pdpSubgroup;
273 * Generate the policy name.
275 * @param policyType The policy type
276 * @param serviceName The service name
277 * @param serviceVersion The service version
278 * @param resourceName The resource name
279 * @param blueprintFilename The blueprint file name
280 * @return The generated policy name
282 public static String generatePolicyName(String policyType, String serviceName, String serviceVersion,
283 String resourceName, String blueprintFilename) {
284 StringBuilder buffer = new StringBuilder(policyType).append("_").append(serviceName).append("_v")
285 .append(serviceVersion).append("_").append(resourceName).append("_")
286 .append(blueprintFilename.replaceAll(".yaml", ""));
287 return buffer.toString().replace('.', '_').replaceAll(" ", "");
291 * This method can be used to generate the json Schema used by the UI.
293 * @param policyToscaModel The tosca model as String that must be converted
294 * @param policyModelType The tosca model type (the policy_type entry in the tosca) that will used to create the
296 * @return THe Json Schema as JsonObject
297 * @throws IOException In case of failure when opening the templates.json file
298 * @throws UnknownComponentException If the policyModelType is not found in the tosca model
300 public static JsonObject generateJsonRepresentationFromToscaModel(String policyToscaModel,
301 String policyModelType)
302 throws IOException, UnknownComponentException {
303 return new TemplateManagement(policyToscaModel,ResourceFileUtil.getResourceAsString(
304 "clds/tosca_update/defaultToscaTypes.yaml"),
305 ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json"))
306 .launchTranslation(policyModelType);