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.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
31 import com.google.gson.annotations.Expose;
33 import java.io.Serializable;
34 import java.io.UnsupportedEncodingException;
35 import java.net.URLEncoder;
36 import java.nio.charset.StandardCharsets;
37 import java.util.HashMap;
39 import java.util.Map.Entry;
41 import javax.persistence.Column;
42 import javax.persistence.Entity;
43 import javax.persistence.FetchType;
44 import javax.persistence.Id;
45 import javax.persistence.JoinColumn;
46 import javax.persistence.ManyToOne;
47 import javax.persistence.Table;
49 import org.hibernate.annotations.Type;
50 import org.hibernate.annotations.TypeDef;
51 import org.hibernate.annotations.TypeDefs;
52 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
53 import org.onap.clamp.loop.Loop;
54 import org.onap.clamp.policy.Policy;
55 import org.yaml.snakeyaml.Yaml;
58 @Table(name = "operational_policies")
59 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
60 public class OperationalPolicy implements Serializable, Policy {
62 * The serial version ID.
64 private static final long serialVersionUID = 6117076450841538255L;
68 @Column(nullable = false, name = "name", unique = true)
73 @Column(columnDefinition = "json", name = "configurations_json")
74 private JsonObject configurationsJson;
76 @ManyToOne(fetch = FetchType.LAZY)
77 @JoinColumn(name = "loop_id", nullable = false)
80 public OperationalPolicy() {
88 * The name of the operational policy
90 * The loop that uses this operational policy
91 * @param configurationsJson
92 * The operational policy property in the format of json
94 public OperationalPolicy(String name, Loop loop, JsonObject configurationsJson) {
97 this.configurationsJson = configurationsJson;
101 public String getName() {
105 public void setLoop(Loop loopName) {
106 this.loop = loopName;
109 public Loop getLoop() {
114 public JsonObject getJsonRepresentation() {
115 return configurationsJson;
118 public JsonObject getConfigurationsJson() {
119 return configurationsJson;
122 public void setConfigurationsJson(JsonObject configurationsJson) {
123 this.configurationsJson = configurationsJson;
127 public int hashCode() {
128 final int prime = 31;
130 result = prime * result + ((name == null) ? 0 : name.hashCode());
135 public boolean equals(Object obj) {
142 if (getClass() != obj.getClass()) {
145 OperationalPolicy other = (OperationalPolicy) obj;
147 if (other.name != null) {
150 } else if (!name.equals(other.name)) {
156 public String createPolicyPayloadYaml() {
157 JsonObject policyPayloadResult = new JsonObject();
159 policyPayloadResult.addProperty("tosca_definitions_version", "tosca_simple_yaml_1_0_0");
161 JsonObject topologyTemplateNode = new JsonObject();
162 policyPayloadResult.add("topology_template", topologyTemplateNode);
164 JsonArray policiesArray = new JsonArray();
165 topologyTemplateNode.add("policies", policiesArray);
167 JsonObject operationalPolicy = new JsonObject();
168 policiesArray.add(operationalPolicy);
170 JsonObject operationalPolicyDetails = new JsonObject();
171 operationalPolicy.add(this.name, operationalPolicyDetails);
172 operationalPolicyDetails.addProperty("type", "onap.policies.controlloop.Operational");
173 operationalPolicyDetails.addProperty("version", "1.0.0");
175 JsonObject metadata = new JsonObject();
176 operationalPolicyDetails.add("metadata", metadata);
177 metadata.addProperty("policy-id", this.name);
179 operationalPolicyDetails.add("properties", this.configurationsJson.get("operational_policy"));
181 Gson gson = new GsonBuilder().create();
182 Map<?, ?> jsonMap = gson.fromJson(gson.toJson(policyPayloadResult), Map.class);
183 return (new Yaml()).dump(jsonMap);
186 public String createPolicyPayloadYamlLegacy() {
187 Gson gson = new GsonBuilder().create();
188 Map<?, ?> jsonMap = gson.fromJson(gson.toJson(this.configurationsJson.get("operational_policy")), Map.class);
189 return (new Yaml()).dump(jsonMap);
193 public String createPolicyPayload() throws UnsupportedEncodingException {
195 // Now the Yaml payload must be injected in a json ...
196 JsonObject payload = new JsonObject();
197 payload.addProperty("policy-id", this.getName());
198 payload.addProperty("content",
199 URLEncoder.encode(createPolicyPayloadYamlLegacy(), StandardCharsets.UTF_8.toString()));
200 return new GsonBuilder().setPrettyPrinting().create().toJson(payload);
204 * Return a map containing all Guard policies indexed by Guard policy Name.
206 * @return The Guards map
208 public Map<String, String> createGuardPolicyPayloads() {
209 Map<String, String> result = new HashMap<>();
211 JsonElement guardsList = this.getConfigurationsJson().get("guard_policies");
212 if (guardsList != null) {
213 for (Entry<String, JsonElement> guardElem : guardsList.getAsJsonObject().entrySet()) {
214 JsonObject guard = new JsonObject();
215 guard.addProperty("policy-id", guardElem.getKey());
216 guard.add("content", guardElem.getValue());
217 result.put(guardElem.getKey(), new GsonBuilder().create().toJson(guard));