Merge "Update cadi to 2.1.10 and updated keystore"
[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.dao.model.jsontype.StringJsonUserType;
50 import org.onap.clamp.loop.log.LoopLog;
51 import org.onap.clamp.policy.microservice.MicroServicePolicy;
52 import org.onap.clamp.policy.operational.OperationalPolicy;
53
54 @Entity
55 @Table(name = "loops")
56 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
57 public class Loop implements Serializable {
58
59     /**
60      * The serial version id.
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(columnDefinition = "MEDIUMTEXT", 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     @Expose
90     @Type(type = "json")
91     @Column(columnDefinition = "json", name = "model_properties_json")
92     private JsonObject modelPropertiesJson;
93
94     @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml")
95     private String blueprint;
96
97     @Expose
98     @Column(nullable = false, name = "last_computed_state")
99     @Enumerated(EnumType.STRING)
100     private LoopState lastComputedState;
101
102     @Expose
103     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
104     private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
105
106     @Expose
107     @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
108     @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id"))
109     private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
110
111     @Expose
112     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
113     private Set<LoopLog> loopLogs = new HashSet<>();
114
115     public Loop() {
116     }
117
118     /**
119      * Constructor.
120      */
121     public Loop(String name, String blueprint, String svgRepresentation) {
122         this.name = name;
123         this.svgRepresentation = svgRepresentation;
124         this.blueprint = blueprint;
125         this.lastComputedState = LoopState.DESIGN;
126         this.globalPropertiesJson = new JsonObject();
127     }
128
129     public String getName() {
130         return name;
131     }
132
133     void setName(String name) {
134         this.name = name;
135     }
136
137     public String getDcaeDeploymentId() {
138         return dcaeDeploymentId;
139     }
140
141     void setDcaeDeploymentId(String dcaeDeploymentId) {
142         this.dcaeDeploymentId = dcaeDeploymentId;
143     }
144
145     public String getDcaeDeploymentStatusUrl() {
146         return dcaeDeploymentStatusUrl;
147     }
148
149     void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
150         this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
151     }
152
153     public String getSvgRepresentation() {
154         return svgRepresentation;
155     }
156
157     void setSvgRepresentation(String svgRepresentation) {
158         this.svgRepresentation = svgRepresentation;
159     }
160
161     public String getBlueprint() {
162         return blueprint;
163     }
164
165     void setBlueprint(String blueprint) {
166         this.blueprint = blueprint;
167     }
168
169     public LoopState getLastComputedState() {
170         return lastComputedState;
171     }
172
173     void setLastComputedState(LoopState lastComputedState) {
174         this.lastComputedState = lastComputedState;
175     }
176
177     public Set<OperationalPolicy> getOperationalPolicies() {
178         return operationalPolicies;
179     }
180
181     void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
182         this.operationalPolicies = operationalPolicies;
183     }
184
185     public Set<MicroServicePolicy> getMicroServicePolicies() {
186         return microServicePolicies;
187     }
188
189     void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
190         this.microServicePolicies = microServicePolicies;
191     }
192
193     public JsonObject getGlobalPropertiesJson() {
194         return globalPropertiesJson;
195     }
196
197     void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
198         this.globalPropertiesJson = globalPropertiesJson;
199     }
200
201     public Set<LoopLog> getLoopLogs() {
202         return loopLogs;
203     }
204
205     void setLoopLogs(Set<LoopLog> loopLogs) {
206         this.loopLogs = loopLogs;
207     }
208
209     void addOperationalPolicy(OperationalPolicy opPolicy) {
210         operationalPolicies.add(opPolicy);
211         opPolicy.setLoop(this);
212     }
213
214     void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
215         microServicePolicies.add(microServicePolicy);
216         microServicePolicy.getUsedByLoops().add(this);
217     }
218
219     void addLog(LoopLog log) {
220         loopLogs.add(log);
221         log.setLoop(this);
222     }
223
224     public String getDcaeBlueprintId() {
225         return dcaeBlueprintId;
226     }
227
228     void setDcaeBlueprintId(String dcaeBlueprintId) {
229         this.dcaeBlueprintId = dcaeBlueprintId;
230     }
231
232     public JsonObject getModelPropertiesJson() {
233         return modelPropertiesJson;
234     }
235
236     void setModelPropertiesJson(JsonObject modelPropertiesJson) {
237         this.modelPropertiesJson = modelPropertiesJson;
238     }
239
240     /**
241      * Generate the loop name.
242      *
243      * @param serviceName
244      *        The service name
245      * @param serviceVersion
246      *        The service version
247      * @param resourceName
248      *        The resource name
249      * @param blueprintFileName
250      *        The blueprint file name
251      * @return The generated loop name
252      */
253     static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
254         String blueprintFilename) {
255         StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
256             .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", ""));
257         return buffer.toString().replace('.', '_').replaceAll(" ", "");
258     }
259
260     @Override
261     public int hashCode() {
262         final int prime = 31;
263         int result = 1;
264         result = prime * result + ((name == null) ? 0 : name.hashCode());
265         return result;
266     }
267
268     @Override
269     public boolean equals(Object obj) {
270         if (this == obj) {
271             return true;
272         }
273         if (obj == null) {
274             return false;
275         }
276         if (getClass() != obj.getClass()) {
277             return false;
278         }
279         Loop other = (Loop) obj;
280         if (name == null) {
281             if (other.name != null) {
282                 return false;
283             }
284         } else if (!name.equals(other.name)) {
285             return false;
286         }
287         return true;
288     }
289
290 }