c1e075daa304bf7a99e010ddab9e99a9e5993100
[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.JsonObject;
27 import com.google.gson.annotations.Expose;
28 import java.io.Serializable;
29
30 import javax.persistence.Column;
31 import javax.persistence.Convert;
32 import javax.persistence.Entity;
33 import javax.persistence.FetchType;
34 import javax.persistence.Id;
35 import javax.persistence.JoinColumn;
36 import javax.persistence.ManyToOne;
37 import javax.persistence.Table;
38 import org.hibernate.annotations.Type;
39 import org.hibernate.annotations.TypeDef;
40 import org.hibernate.annotations.TypeDefs;
41 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
42 import org.onap.clamp.loop.Loop;
43 import org.onap.clamp.policy.Policy;
44
45 @Entity
46 @Table(name = "operational_policies")
47 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
48 public class OperationalPolicy implements Serializable, Policy {
49     /**
50      * The serial version ID.
51      */
52     private static final long serialVersionUID = 6117076450841538255L;
53
54     @Id
55     @Expose
56     @Column(nullable = false, name = "name", unique = true)
57     private String name;
58
59     @Expose
60     @Type(type = "json")
61     @Column(columnDefinition = "json", name = "configurations_json")
62     private JsonObject configurationsJson;
63
64     @ManyToOne(fetch = FetchType.LAZY)
65     @JoinColumn(name = "loop_id", nullable = false)
66     private Loop loop;
67
68     public OperationalPolicy() {
69         //Serialization
70     }
71
72     /**
73      * The constructor.
74      * @param name The name of the operational policy
75      * @param loop The loop that uses this operational policy
76      * @param configurationsJson The operational policy property in the format of json
77      */
78     public OperationalPolicy(String name, Loop loop, JsonObject configurationsJson) {
79         this.name = name;
80         this.loop = loop;
81         this.configurationsJson = configurationsJson;
82     }
83
84     public String getName() {
85         return name;
86     }
87
88     public void setLoop(Loop loopName) {
89         this.loop = loopName;
90     }
91
92     public Loop getLoop() {
93         return loop;
94     }
95
96     @Override
97     public JsonObject getJsonRepresentation() {
98         return configurationsJson;
99     }
100
101     public JsonObject getConfigurationsJson() {
102         return configurationsJson;
103     }
104
105     public void setConfigurationsJson(JsonObject configurationsJson) {
106         this.configurationsJson = configurationsJson;
107     }
108
109     @Override
110     public int hashCode() {
111         final int prime = 31;
112         int result = 1;
113         result = prime * result + ((name == null) ? 0 : name.hashCode());
114         return result;
115     }
116
117     @Override
118     public boolean equals(Object obj) {
119         if (this == obj) {
120             return true;
121         }
122         if (obj == null) {
123             return false;
124         }
125         if (getClass() != obj.getClass()) {
126             return false;
127         }
128         OperationalPolicy other = (OperationalPolicy) obj;
129         if (name == null) {
130             if (other.name != null) {
131                 return false;
132             }
133         } else if (!name.equals(other.name)) {
134             return false;
135         }
136         return true;
137     }
138
139 }