2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T 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.operational;
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.JsonElement;
32 import com.google.gson.JsonObject;
33 import com.google.gson.JsonSyntaxException;
34 import com.google.gson.annotations.Expose;
36 import java.io.IOException;
37 import java.io.Serializable;
38 import java.io.UnsupportedEncodingException;
39 import java.net.URLEncoder;
40 import java.nio.charset.StandardCharsets;
41 import java.util.HashMap;
44 import javax.persistence.Column;
45 import javax.persistence.Entity;
46 import javax.persistence.FetchType;
47 import javax.persistence.Id;
48 import javax.persistence.JoinColumn;
49 import javax.persistence.JoinColumns;
50 import javax.persistence.ManyToOne;
51 import javax.persistence.Table;
52 import javax.persistence.Transient;
54 import org.hibernate.annotations.TypeDef;
55 import org.hibernate.annotations.TypeDefs;
56 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
57 import org.onap.clamp.loop.Loop;
58 import org.onap.clamp.loop.template.PolicyModel;
59 import org.onap.clamp.policy.Policy;
60 import org.yaml.snakeyaml.DumperOptions;
61 import org.yaml.snakeyaml.Yaml;
64 @Table(name = "operational_policies")
65 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
66 public class OperationalPolicy extends Policy implements Serializable {
68 * The serial version ID.
70 private static final long serialVersionUID = 6117076450841538255L;
73 private static final EELFLogger logger = EELFManager.getInstance().getLogger(OperationalPolicy.class);
77 @Column(nullable = false, name = "name", unique = true)
80 @ManyToOne(fetch = FetchType.LAZY)
81 @JoinColumn(name = "loop_id", nullable = false)
85 @ManyToOne(fetch = FetchType.EAGER)
86 @JoinColumns({@JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"),
87 @JoinColumn(name = "policy_model_version", referencedColumnName = "version")})
88 private PolicyModel policyModel;
90 public OperationalPolicy() {
97 * @param name The name of the operational policy
98 * @param loop The loop that uses this operational policy
99 * @param configurationsJson The operational policy property in the format of
101 * @param policyModel The policy model associated if any, can be null
103 public OperationalPolicy(String name, Loop loop, JsonObject configurationsJson, PolicyModel policyModel) {
106 this.setPolicyModel(policyModel);
107 this.setConfigurationsJson(configurationsJson);
108 LegacyOperationalPolicy.preloadConfiguration(configurationsJson, loop);
110 this.setJsonRepresentation(
111 OperationalPolicyRepresentationBuilder.generateOperationalPolicySchema(loop.getModelService()));
112 } catch (JsonSyntaxException | IOException | NullPointerException e) {
113 logger.error("Unable to generate the operational policy Schema ... ", e);
114 this.setJsonRepresentation(new JsonObject());
118 public void setLoop(Loop loopName) {
119 this.loop = loopName;
122 public Loop getLoop() {
127 public String getName() {
134 * @param name the name to set
137 public void setName(String name) {
142 * policyModel getter.
144 * @return the policyModel
146 public PolicyModel getPolicyModel() {
151 * policyModel setter.
153 * @param policyModel the policyModel to set
155 public void setPolicyModel(PolicyModel policyModel) {
156 this.policyModel = policyModel;
160 public int hashCode() {
161 final int prime = 31;
163 result = prime * result + ((name == null) ? 0 : name.hashCode());
168 public boolean equals(Object obj) {
175 if (getClass() != obj.getClass()) {
178 OperationalPolicy other = (OperationalPolicy) obj;
180 if (other.name != null) {
183 } else if (!name.equals(other.name)) {
190 * Create policy Yaml from json defined here.
192 * @return A string containing Yaml
194 public String createPolicyPayloadYaml() {
195 JsonObject policyPayloadResult = new JsonObject();
197 policyPayloadResult.addProperty("tosca_definitions_version", "tosca_simple_yaml_1_0_0");
199 JsonObject topologyTemplateNode = new JsonObject();
200 policyPayloadResult.add("topology_template", topologyTemplateNode);
202 JsonArray policiesArray = new JsonArray();
203 topologyTemplateNode.add("policies", policiesArray);
205 JsonObject operationalPolicy = new JsonObject();
206 policiesArray.add(operationalPolicy);
208 JsonObject operationalPolicyDetails = new JsonObject();
209 operationalPolicy.add(this.name, operationalPolicyDetails);
210 operationalPolicyDetails.addProperty("type", "onap.policies.controlloop.Operational");
211 operationalPolicyDetails.addProperty("version", "1.0.0");
213 JsonObject metadata = new JsonObject();
214 operationalPolicyDetails.add("metadata", metadata);
215 metadata.addProperty("policy-id", this.name);
217 operationalPolicyDetails.add("properties", LegacyOperationalPolicy
218 .reworkPayloadAttributes(this.getConfigurationsJson().get("operational_policy").deepCopy()));
220 DumperOptions options = new DumperOptions();
221 options.setIndent(2);
222 options.setPrettyFlow(true);
223 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
224 Gson gson = new GsonBuilder().create();
226 return (new Yaml(options)).dump(gson.fromJson(gson.toJson(policyPayloadResult), Map.class));
230 public String createPolicyPayload() throws UnsupportedEncodingException {
231 // Now using the legacy payload fo Dublin
232 JsonObject payload = new JsonObject();
233 payload.addProperty("policy-id", this.getName());
234 payload.addProperty("content",
236 LegacyOperationalPolicy
237 .createPolicyPayloadYamlLegacy(this.getConfigurationsJson().get("operational_policy")),
238 StandardCharsets.UTF_8.toString()));
239 String opPayload = new GsonBuilder().setPrettyPrinting().create().toJson(payload);
240 logger.info("Operational policy payload: " + opPayload);
245 * Return a map containing all Guard policies indexed by Guard policy Name.
247 * @return The Guards map
249 public Map<String, String> createGuardPolicyPayloads() {
250 Map<String, String> result = new HashMap<>();
252 JsonElement guardsList = this.getConfigurationsJson().get("guard_policies");
253 if (guardsList != null) {
254 for (JsonElement guardElem : guardsList.getAsJsonArray()) {
255 result.put(guardElem.getAsJsonObject().get("policy-id").getAsString(),
256 new GsonBuilder().create().toJson(guardElem));
259 logger.info("Guard policy payload: " + result);
264 * Regenerate the Operational Policy Json Representation.
266 public void updateJsonRepresentation() {
268 this.setJsonRepresentation(
269 OperationalPolicyRepresentationBuilder.generateOperationalPolicySchema(loop.getModelService()));
270 } catch (JsonSyntaxException | IOException | NullPointerException e) {
271 logger.error("Unable to generate the operational policy Schema ... ", e);
272 this.setJsonRepresentation(new JsonObject());