Rework the submit operation
[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"), 
109        inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id"))
110     private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
111
112     @Expose
113     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
114     private Set<LoopLog> loopLogs = new HashSet<>();
115
116     public Loop() {
117     }
118
119     /**
120      * Constructor.
121      */
122     public Loop(String name, String blueprint, String svgRepresentation) {
123         this.name = name;
124         this.svgRepresentation = svgRepresentation;
125         this.blueprint = blueprint;
126         this.lastComputedState = LoopState.DESIGN;
127         this.globalPropertiesJson = new JsonObject();
128     }
129
130     public String getName() {
131         return name;
132     }
133
134     void setName(String name) {
135         this.name = name;
136     }
137
138     public String getDcaeDeploymentId() {
139         return dcaeDeploymentId;
140     }
141
142     public void setDcaeDeploymentId(String dcaeDeploymentId) {
143         this.dcaeDeploymentId = dcaeDeploymentId;
144     }
145
146     public String getDcaeDeploymentStatusUrl() {
147         return dcaeDeploymentStatusUrl;
148     }
149
150     public void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
151         this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
152     }
153
154     public String getSvgRepresentation() {
155         return svgRepresentation;
156     }
157
158     void setSvgRepresentation(String svgRepresentation) {
159         this.svgRepresentation = svgRepresentation;
160     }
161
162     public String getBlueprint() {
163         return blueprint;
164     }
165
166     void setBlueprint(String blueprint) {
167         this.blueprint = blueprint;
168     }
169
170     public LoopState getLastComputedState() {
171         return lastComputedState;
172     }
173
174     public void setLastComputedState(LoopState lastComputedState) {
175         this.lastComputedState = lastComputedState;
176     }
177
178     Set<OperationalPolicy> getOperationalPolicies() {
179         return operationalPolicies;
180     }
181
182     void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
183         this.operationalPolicies = operationalPolicies;
184     }
185
186     public Set<MicroServicePolicy> getMicroServicePolicies() {
187         return microServicePolicies;
188     }
189
190     void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
191         this.microServicePolicies = microServicePolicies;
192     }
193
194     JsonObject getGlobalPropertiesJson() {
195         return globalPropertiesJson;
196     }
197
198     void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
199         this.globalPropertiesJson = globalPropertiesJson;
200     }
201
202     Set<LoopLog> getLoopLogs() {
203         return loopLogs;
204     }
205
206     void setLoopLogs(Set<LoopLog> loopLogs) {
207         this.loopLogs = loopLogs;
208     }
209
210     void addOperationalPolicy(OperationalPolicy opPolicy) {
211         operationalPolicies.add(opPolicy);
212         opPolicy.setLoop(this);
213     }
214
215     void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
216         microServicePolicies.add(microServicePolicy);
217         microServicePolicy.getUsedByLoops().add(this);
218     }
219
220     void addLog(LoopLog log) {
221         loopLogs.add(log);
222         log.setLoop(this);
223     }
224
225     public String getDcaeBlueprintId() {
226         return dcaeBlueprintId;
227     }
228
229     void setDcaeBlueprintId(String dcaeBlueprintId) {
230         this.dcaeBlueprintId = dcaeBlueprintId;
231     }
232
233     public JsonObject getModelPropertiesJson() {
234         return modelPropertiesJson;
235     }
236
237     void setModelPropertiesJson(JsonObject modelPropertiesJson) {
238         this.modelPropertiesJson = modelPropertiesJson;
239     }
240
241     /**
242      * Generate the loop name.
243      * @param serviceName The service name
244      * @param serviceVersion The service version
245      * @param resourceName The resource name
246      * @param blueprintFileName The blueprint file name
247      * @return The generated loop name
248      */
249     public static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
250         String blueprintFilename) {
251         StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
252             .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", ""));
253         return buffer.toString().replace('.', '_').replaceAll(" ", "");
254     }
255
256     @Override
257     public int hashCode() {
258         final int prime = 31;
259         int result = 1;
260         result = prime * result + ((name == null) ? 0 : name.hashCode());
261         return result;
262     }
263
264     @Override
265     public boolean equals(Object obj) {
266         if (this == obj) {
267             return true;
268         }
269         if (obj == null) {
270             return false;
271         }
272         if (getClass() != obj.getClass()) {
273             return false;
274         }
275         Loop other = (Loop) obj;
276         if (name == null) {
277             if (other.name != null) {
278                 return false;
279             }
280         } else if (!name.equals(other.name)) {
281             return false;
282         }
283         return true;
284     }
285
286 }