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.util.ArrayList;
35 import java.util.List;
37 import java.util.Map.Entry;
39 import javax.persistence.Column;
40 import javax.persistence.Entity;
41 import javax.persistence.FetchType;
42 import javax.persistence.Id;
43 import javax.persistence.JoinColumn;
44 import javax.persistence.ManyToOne;
45 import javax.persistence.Table;
47 import org.hibernate.annotations.Type;
48 import org.hibernate.annotations.TypeDef;
49 import org.hibernate.annotations.TypeDefs;
50 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
51 import org.onap.clamp.loop.Loop;
52 import org.onap.clamp.policy.Policy;
53 import org.yaml.snakeyaml.Yaml;
56 @Table(name = "operational_policies")
57 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
58 public class OperationalPolicy implements Serializable, Policy {
60 * The serial version ID.
62 private static final long serialVersionUID = 6117076450841538255L;
66 @Column(nullable = false, name = "name", unique = true)
71 @Column(columnDefinition = "json", name = "configurations_json")
72 private JsonObject configurationsJson;
74 @ManyToOne(fetch = FetchType.LAZY)
75 @JoinColumn(name = "loop_id", nullable = false)
78 public OperationalPolicy() {
86 * The name of the operational policy
88 * The loop that uses this operational policy
89 * @param configurationsJson
90 * The operational policy property in the format of json
92 public OperationalPolicy(String name, Loop loop, JsonObject configurationsJson) {
95 this.configurationsJson = configurationsJson;
99 public String getName() {
103 public void setLoop(Loop loopName) {
104 this.loop = loopName;
107 public Loop getLoop() {
112 public JsonObject getJsonRepresentation() {
113 return configurationsJson;
116 public JsonObject getConfigurationsJson() {
117 return configurationsJson;
120 public void setConfigurationsJson(JsonObject configurationsJson) {
121 this.configurationsJson = configurationsJson;
125 public int hashCode() {
126 final int prime = 31;
128 result = prime * result + ((name == null) ? 0 : name.hashCode());
133 public boolean equals(Object obj) {
140 if (getClass() != obj.getClass()) {
143 OperationalPolicy other = (OperationalPolicy) obj;
145 if (other.name != null) {
148 } else if (!name.equals(other.name)) {
155 public String createPolicyPayload() {
156 JsonObject policyPayloadResult = new JsonObject();
158 policyPayloadResult.addProperty("tosca_definitions_version", "tosca_simple_yaml_1_0_0");
160 JsonObject topologyTemplateNode = new JsonObject();
161 policyPayloadResult.add("topology_template", topologyTemplateNode);
163 JsonArray policiesArray = new JsonArray();
164 topologyTemplateNode.add("policies", policiesArray);
166 JsonObject operationalPolicy = new JsonObject();
167 policiesArray.add(operationalPolicy);
169 JsonObject operationalPolicyDetails = new JsonObject();
170 operationalPolicy.add(this.name, operationalPolicyDetails);
171 operationalPolicyDetails.addProperty("type", "onap.policies.controlloop.Operational");
172 operationalPolicyDetails.addProperty("version", "1.0.0");
174 JsonObject metadata = new JsonObject();
175 operationalPolicyDetails.add("metadata", metadata);
176 metadata.addProperty("policy-id", this.name);
178 operationalPolicyDetails.add("properties", this.configurationsJson.get("operational_policy"));
180 Gson gson = new GsonBuilder().create();
181 Map<?, ?> jsonMap = gson.fromJson(gson.toJson(policyPayloadResult), Map.class);
182 return (new Yaml()).dump(jsonMap);
185 public List<String> createGuardPolicyPayloads() {
186 List<String> result = new ArrayList<>();
188 JsonObject guard = new JsonObject();
189 JsonElement guardsList = this.getConfigurationsJson().get("guard_policies");
190 for (Entry<String, JsonElement> guardElem : guardsList.getAsJsonObject().entrySet()) {
191 guard.addProperty("policy-id", guardElem.getKey());
192 guard.add("contents", guardElem.getValue());
193 result.add(new GsonBuilder().create().toJson(guard));