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.HashMap;
36 import java.util.Map.Entry;
38 import javax.persistence.Column;
39 import javax.persistence.Entity;
40 import javax.persistence.FetchType;
41 import javax.persistence.Id;
42 import javax.persistence.JoinColumn;
43 import javax.persistence.ManyToOne;
44 import javax.persistence.Table;
46 import org.hibernate.annotations.Type;
47 import org.hibernate.annotations.TypeDef;
48 import org.hibernate.annotations.TypeDefs;
49 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
50 import org.onap.clamp.loop.Loop;
51 import org.onap.clamp.policy.Policy;
52 import org.yaml.snakeyaml.Yaml;
55 @Table(name = "operational_policies")
56 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
57 public class OperationalPolicy implements Serializable, Policy {
59 * The serial version ID.
61 private static final long serialVersionUID = 6117076450841538255L;
65 @Column(nullable = false, name = "name", unique = true)
70 @Column(columnDefinition = "json", name = "configurations_json")
71 private JsonObject configurationsJson;
73 @ManyToOne(fetch = FetchType.LAZY)
74 @JoinColumn(name = "loop_id", nullable = false)
77 public OperationalPolicy() {
85 * The name of the operational policy
87 * The loop that uses this operational policy
88 * @param configurationsJson
89 * The operational policy property in the format of json
91 public OperationalPolicy(String name, Loop loop, JsonObject configurationsJson) {
94 this.configurationsJson = configurationsJson;
98 public String getName() {
102 public void setLoop(Loop loopName) {
103 this.loop = loopName;
106 public Loop getLoop() {
111 public JsonObject getJsonRepresentation() {
112 return configurationsJson;
115 public JsonObject getConfigurationsJson() {
116 return configurationsJson;
119 public void setConfigurationsJson(JsonObject configurationsJson) {
120 this.configurationsJson = configurationsJson;
124 public int hashCode() {
125 final int prime = 31;
127 result = prime * result + ((name == null) ? 0 : name.hashCode());
132 public boolean equals(Object obj) {
139 if (getClass() != obj.getClass()) {
142 OperationalPolicy other = (OperationalPolicy) obj;
144 if (other.name != null) {
147 } else if (!name.equals(other.name)) {
154 public String createPolicyPayload() {
155 JsonObject policyPayloadResult = new JsonObject();
157 policyPayloadResult.addProperty("tosca_definitions_version", "tosca_simple_yaml_1_0_0");
159 JsonObject topologyTemplateNode = new JsonObject();
160 policyPayloadResult.add("topology_template", topologyTemplateNode);
162 JsonArray policiesArray = new JsonArray();
163 topologyTemplateNode.add("policies", policiesArray);
165 JsonObject operationalPolicy = new JsonObject();
166 policiesArray.add(operationalPolicy);
168 JsonObject operationalPolicyDetails = new JsonObject();
169 operationalPolicy.add(this.name, operationalPolicyDetails);
170 operationalPolicyDetails.addProperty("type", "onap.policies.controlloop.Operational");
171 operationalPolicyDetails.addProperty("version", "1.0.0");
173 JsonObject metadata = new JsonObject();
174 operationalPolicyDetails.add("metadata", metadata);
175 metadata.addProperty("policy-id", this.name);
177 operationalPolicyDetails.add("properties", this.configurationsJson.get("operational_policy"));
179 Gson gson = new GsonBuilder().create();
180 Map<?, ?> jsonMap = gson.fromJson(gson.toJson(policyPayloadResult), Map.class);
181 return (new Yaml()).dump(jsonMap);
185 * Return a map containing all Guard policies indexed by Guard policy Name.
187 * @return The Guards map
189 public Map<String, String> createGuardPolicyPayloads() {
190 Map<String, String> result = new HashMap<>();
192 JsonElement guardsList = this.getConfigurationsJson().get("guard_policies");
193 if (guardsList != null) {
194 for (Entry<String, JsonElement> guardElem : guardsList.getAsJsonObject().entrySet()) {
195 JsonObject guard = new JsonObject();
196 guard.addProperty("policy-id", guardElem.getKey());
197 guard.add("contents", guardElem.getValue());
198 result.put(guardElem.getKey(), new GsonBuilder().create().toJson(guard));