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.GsonBuilder;
27 import com.google.gson.JsonArray;
28 import com.google.gson.JsonObject;
29 import com.google.gson.annotations.Expose;
31 import java.io.Serializable;
33 import javax.persistence.Column;
34 import javax.persistence.Entity;
35 import javax.persistence.FetchType;
36 import javax.persistence.Id;
37 import javax.persistence.JoinColumn;
38 import javax.persistence.ManyToOne;
39 import javax.persistence.Table;
41 import org.hibernate.annotations.Type;
42 import org.hibernate.annotations.TypeDef;
43 import org.hibernate.annotations.TypeDefs;
44 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
45 import org.onap.clamp.loop.Loop;
46 import org.onap.clamp.policy.Policy;
49 @Table(name = "operational_policies")
50 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
51 public class OperationalPolicy implements Serializable, Policy {
53 * The serial version ID.
55 private static final long serialVersionUID = 6117076450841538255L;
59 @Column(nullable = false, name = "name", unique = true)
64 @Column(columnDefinition = "json", name = "configurations_json")
65 private JsonObject configurationsJson;
67 @ManyToOne(fetch = FetchType.LAZY)
68 @JoinColumn(name = "loop_id", nullable = false)
71 public OperationalPolicy() {
79 * The name of the operational policy
81 * The loop that uses this operational policy
82 * @param configurationsJson
83 * The operational policy property in the format of json
85 public OperationalPolicy(String name, Loop loop, JsonObject configurationsJson) {
88 this.configurationsJson = configurationsJson;
92 public String getName() {
96 public void setLoop(Loop loopName) {
100 public Loop getLoop() {
105 public JsonObject getJsonRepresentation() {
106 return configurationsJson;
109 public JsonObject getConfigurationsJson() {
110 return configurationsJson;
113 public void setConfigurationsJson(JsonObject configurationsJson) {
114 this.configurationsJson = configurationsJson;
118 public int hashCode() {
119 final int prime = 31;
121 result = prime * result + ((name == null) ? 0 : name.hashCode());
126 public boolean equals(Object obj) {
133 if (getClass() != obj.getClass()) {
136 OperationalPolicy other = (OperationalPolicy) obj;
138 if (other.name != null) {
141 } else if (!name.equals(other.name)) {
148 public String createPolicyPayload() {
149 JsonObject policyPayloadResult = new JsonObject();
151 policyPayloadResult.addProperty("tosca_definitions_version", "tosca_simple_yaml_1_0_0");
153 JsonObject topologyTemplateNode = new JsonObject();
154 policyPayloadResult.add("topology_template", topologyTemplateNode);
156 JsonArray policiesArray = new JsonArray();
157 topologyTemplateNode.add("policies", policiesArray);
159 return new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);
162 public String createGuardPolicyPayload() {