Merge "LoopLog repository"
[clamp.git] / src / main / java / org / onap / clamp / policy / operational / OperationalPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.policy.operational;
25
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;
30
31 import java.io.Serializable;
32
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;
40
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;
47
48 @Entity
49 @Table(name = "operational_policies")
50 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
51 public class OperationalPolicy implements Serializable, Policy {
52     /**
53      * The serial version ID.
54      */
55     private static final long serialVersionUID = 6117076450841538255L;
56
57     @Id
58     @Expose
59     @Column(nullable = false, name = "name", unique = true)
60     private String name;
61
62     @Expose
63     @Type(type = "json")
64     @Column(columnDefinition = "json", name = "configurations_json")
65     private JsonObject configurationsJson;
66
67     @ManyToOne(fetch = FetchType.LAZY)
68     @JoinColumn(name = "loop_id", nullable = false)
69     private Loop loop;
70
71     public OperationalPolicy() {
72         // Serialization
73     }
74
75     /**
76      * The constructor.
77      *
78      * @param name
79      *        The name of the operational policy
80      * @param loop
81      *        The loop that uses this operational policy
82      * @param configurationsJson
83      *        The operational policy property in the format of json
84      */
85     public OperationalPolicy(String name, Loop loop, JsonObject configurationsJson) {
86         this.name = name;
87         this.loop = loop;
88         this.configurationsJson = configurationsJson;
89     }
90
91     @Override
92     public String getName() {
93         return name;
94     }
95
96     public void setLoop(Loop loopName) {
97         this.loop = loopName;
98     }
99
100     public Loop getLoop() {
101         return loop;
102     }
103
104     @Override
105     public JsonObject getJsonRepresentation() {
106         return configurationsJson;
107     }
108
109     public JsonObject getConfigurationsJson() {
110         return configurationsJson;
111     }
112
113     public void setConfigurationsJson(JsonObject configurationsJson) {
114         this.configurationsJson = configurationsJson;
115     }
116
117     @Override
118     public int hashCode() {
119         final int prime = 31;
120         int result = 1;
121         result = prime * result + ((name == null) ? 0 : name.hashCode());
122         return result;
123     }
124
125     @Override
126     public boolean equals(Object obj) {
127         if (this == obj) {
128             return true;
129         }
130         if (obj == null) {
131             return false;
132         }
133         if (getClass() != obj.getClass()) {
134             return false;
135         }
136         OperationalPolicy other = (OperationalPolicy) obj;
137         if (name == null) {
138             if (other.name != null) {
139                 return false;
140             }
141         } else if (!name.equals(other.name)) {
142             return false;
143         }
144         return true;
145     }
146
147     @Override
148     public String createPolicyPayload() {
149         JsonObject policyPayloadResult = new JsonObject();
150
151         policyPayloadResult.addProperty("tosca_definitions_version", "tosca_simple_yaml_1_0_0");
152
153         JsonObject topologyTemplateNode = new JsonObject();
154         policyPayloadResult.add("topology_template", topologyTemplateNode);
155
156         JsonArray policiesArray = new JsonArray();
157         topologyTemplateNode.add("policies", policiesArray);
158
159         return new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult);
160     }
161
162     public String createGuardPolicyPayload() {
163         return null;
164     }
165
166 }