Merge "Add missing INFO.yaml blocks"
[clamp.git] / src / main / java / org / onap / clamp / loop / Loop.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.loop;
25
26 import com.google.gson.JsonObject;
27 import com.google.gson.annotations.Expose;
28
29 import java.io.Serializable;
30 import java.util.HashSet;
31 import java.util.Set;
32
33 import javax.persistence.CascadeType;
34 import javax.persistence.Column;
35 import javax.persistence.Entity;
36 import javax.persistence.EnumType;
37 import javax.persistence.Enumerated;
38 import javax.persistence.FetchType;
39 import javax.persistence.Id;
40 import javax.persistence.JoinColumn;
41 import javax.persistence.JoinTable;
42 import javax.persistence.ManyToMany;
43 import javax.persistence.OneToMany;
44 import javax.persistence.Table;
45
46 import org.hibernate.annotations.Type;
47 import org.hibernate.annotations.TypeDef;
48 import org.hibernate.annotations.TypeDefs;
49 import org.onap.clamp.loop.log.LoopLog;
50 import org.onap.clamp.policy.microservice.MicroServicePolicy;
51 import org.onap.clamp.policy.operational.OperationalPolicy;
52 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
53
54 @Entity
55 @Table(name = "loops")
56 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
57 public class Loop implements Serializable {
58
59     /**
60      *
61      */
62     private static final long serialVersionUID = -286522707701388642L;
63
64     @Id
65     @Expose
66     @Column(nullable = false, name = "name", unique = true)
67     private String name;
68
69     @Expose
70     @Column(name = "dcae_deployment_id")
71     private String dcaeDeploymentId;
72
73     @Expose
74     @Column(name = "dcae_deployment_status_url")
75     private String dcaeDeploymentStatusUrl;
76
77     @Expose
78     @Column(name = "dcae_blueprint_id")
79     private String dcaeBlueprintId;
80
81     @Column(name = "svg_representation")
82     private String svgRepresentation;
83
84     @Expose
85     @Type(type = "json")
86     @Column(columnDefinition = "json", name = "global_properties_json")
87     private JsonObject globalPropertiesJson;
88
89     @Column(nullable = false, name = "blueprint_yaml")
90     private String blueprint;
91
92     @Expose
93     @Column(nullable = false, name = "last_computed_state")
94     @Enumerated(EnumType.STRING)
95     private LoopState lastComputedState;
96
97     @Expose
98     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
99     private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
100
101     @Expose
102     @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
103     @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id"))
104     private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
105
106     @Expose
107     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
108     private Set<LoopLog> loopLogs = new HashSet<>();
109
110     public Loop() {
111     }
112
113     public Loop(String name, String blueprint, String svgRepresentation) {
114         this.name = name;
115         this.svgRepresentation = svgRepresentation;
116         this.blueprint = blueprint;
117         this.lastComputedState = LoopState.DESIGN;
118         this.globalPropertiesJson = new JsonObject();
119     }
120
121     public String getName() {
122         return name;
123     }
124
125     void setName(String name) {
126         this.name = name;
127     }
128
129     public String getDcaeDeploymentId() {
130         return dcaeDeploymentId;
131     }
132
133     void setDcaeDeploymentId(String dcaeDeploymentId) {
134         this.dcaeDeploymentId = dcaeDeploymentId;
135     }
136
137     public String getDcaeDeploymentStatusUrl() {
138         return dcaeDeploymentStatusUrl;
139     }
140
141     void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
142         this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
143     }
144
145     public String getSvgRepresentation() {
146         return svgRepresentation;
147     }
148
149     void setSvgRepresentation(String svgRepresentation) {
150         this.svgRepresentation = svgRepresentation;
151     }
152
153     public String getBlueprint() {
154         return blueprint;
155     }
156
157     void setBlueprint(String blueprint) {
158         this.blueprint = blueprint;
159     }
160
161     LoopState getLastComputedState() {
162         return lastComputedState;
163     }
164
165     void setLastComputedState(LoopState lastComputedState) {
166         this.lastComputedState = lastComputedState;
167     }
168
169     Set<OperationalPolicy> getOperationalPolicies() {
170         return operationalPolicies;
171     }
172
173     void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
174         this.operationalPolicies = operationalPolicies;
175     }
176
177     Set<MicroServicePolicy> getMicroServicePolicies() {
178         return microServicePolicies;
179     }
180
181     void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
182         this.microServicePolicies = microServicePolicies;
183     }
184
185     JsonObject getGlobalPropertiesJson() {
186         return globalPropertiesJson;
187     }
188
189     void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
190         this.globalPropertiesJson = globalPropertiesJson;
191     }
192
193     Set<LoopLog> getLoopLogs() {
194         return loopLogs;
195     }
196
197     void setLoopLogs(Set<LoopLog> loopLogs) {
198         this.loopLogs = loopLogs;
199     }
200
201     void addOperationalPolicy(OperationalPolicy opPolicy) {
202         operationalPolicies.add(opPolicy);
203         opPolicy.setLoop(this);
204     }
205
206     void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
207         microServicePolicies.add(microServicePolicy);
208         microServicePolicy.getUsedByLoops().add(this);
209     }
210
211     void addLog(LoopLog log) {
212         loopLogs.add(log);
213         log.setLoop(this);
214     }
215
216     public String getDcaeBlueprintId() {
217         return dcaeBlueprintId;
218     }
219
220     public void setDcaeBlueprintId(String dcaeBlueprintId) {
221         this.dcaeBlueprintId = dcaeBlueprintId;
222     }
223
224     @Override
225     public int hashCode() {
226         final int prime = 31;
227         int result = 1;
228         result = prime * result + ((name == null) ? 0 : name.hashCode());
229         return result;
230     }
231
232     @Override
233     public boolean equals(Object obj) {
234         if (this == obj)
235             return true;
236         if (obj == null)
237             return false;
238         if (getClass() != obj.getClass())
239             return false;
240         Loop other = (Loop) obj;
241         if (name == null) {
242             if (other.name != null)
243                 return false;
244         } else if (!name.equals(other.name))
245             return false;
246         return true;
247     }
248
249 }