Merge "Add template and tosca model entities and repositories"
[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.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.JsonObject;
29 import com.google.gson.annotations.Expose;
30
31 import java.io.Serializable;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.SortedSet;
37 import java.util.TreeSet;
38
39 import javax.persistence.CascadeType;
40 import javax.persistence.Column;
41 import javax.persistence.Entity;
42 import javax.persistence.EnumType;
43 import javax.persistence.Enumerated;
44 import javax.persistence.FetchType;
45 import javax.persistence.Id;
46 import javax.persistence.JoinColumn;
47 import javax.persistence.JoinTable;
48 import javax.persistence.ManyToMany;
49 import javax.persistence.ManyToOne;
50 import javax.persistence.OneToMany;
51 import javax.persistence.Table;
52 import javax.persistence.Transient;
53
54 import org.hibernate.annotations.SortNatural;
55 import org.hibernate.annotations.Type;
56 import org.hibernate.annotations.TypeDef;
57 import org.hibernate.annotations.TypeDefs;
58 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
59 import org.onap.clamp.loop.common.AuditEntity;
60 import org.onap.clamp.loop.components.external.DcaeComponent;
61 import org.onap.clamp.loop.components.external.ExternalComponent;
62 import org.onap.clamp.loop.components.external.PolicyComponent;
63 import org.onap.clamp.loop.log.LoopLog;
64 import org.onap.clamp.loop.service.Service;
65 import org.onap.clamp.loop.template.LoopTemplate;
66 import org.onap.clamp.policy.microservice.MicroServicePolicy;
67 import org.onap.clamp.policy.operational.OperationalPolicy;
68
69 @Entity
70 @Table(name = "loops")
71 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
72 public class Loop extends AuditEntity implements Serializable {
73
74     /**
75      * The serial version id.
76      */
77     private static final long serialVersionUID = -286522707701388642L;
78
79     @Transient
80     private static final EELFLogger logger = EELFManager.getInstance().getLogger(Loop.class);
81
82     @Id
83     @Expose
84     @Column(nullable = false, name = "name", unique = true)
85     private String name;
86
87     @Expose
88     @Column(name = "dcae_deployment_id")
89     private String dcaeDeploymentId;
90
91     @Expose
92     @Column(name = "dcae_deployment_status_url")
93     private String dcaeDeploymentStatusUrl;
94
95     @Expose
96     @Column(name = "dcae_blueprint_id")
97     private String dcaeBlueprintId;
98
99     @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
100     private String svgRepresentation;
101
102     @Expose
103     @Type(type = "json")
104     @Column(columnDefinition = "json", name = "global_properties_json")
105     private JsonObject globalPropertiesJson;
106
107     @Expose
108     @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
109     @JoinColumn(name = "service_uuid")
110     private Service modelService;
111
112     @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml")
113     private String blueprint;
114
115     @Expose
116     @Column(nullable = false, name = "last_computed_state")
117     @Enumerated(EnumType.STRING)
118     private LoopState lastComputedState;
119
120     @Expose
121     @Transient
122     private final Map<String, ExternalComponent> components = new HashMap<>();
123
124     @Expose
125     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop", orphanRemoval = true)
126     private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
127
128     @Expose
129     @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.EAGER)
130     @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id"))
131     private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
132
133     @Expose
134     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop", orphanRemoval = true)
135     @SortNatural
136     private SortedSet<LoopLog> loopLogs = new TreeSet<>();
137
138     @Expose
139     @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.EAGER)
140     @JoinColumn(name = "loop_template_name")
141     private LoopTemplate loopTemplate;
142
143     private void initializeExternalComponents() {
144         this.addComponent(new PolicyComponent());
145         this.addComponent(new DcaeComponent());
146     }
147
148     /**
149      * Public constructor.
150      */
151     public Loop() {
152         initializeExternalComponents();
153     }
154
155     /**
156      * Constructor.
157      */
158     public Loop(String name, String blueprint, String svgRepresentation) {
159         this.name = name;
160         this.svgRepresentation = svgRepresentation;
161         this.blueprint = blueprint;
162         this.lastComputedState = LoopState.DESIGN;
163         this.globalPropertiesJson = new JsonObject();
164         initializeExternalComponents();
165     }
166
167     public String getName() {
168         return name;
169     }
170
171     void setName(String name) {
172         this.name = name;
173     }
174
175     public String getDcaeDeploymentId() {
176         return dcaeDeploymentId;
177     }
178
179     void setDcaeDeploymentId(String dcaeDeploymentId) {
180         this.dcaeDeploymentId = dcaeDeploymentId;
181     }
182
183     public String getDcaeDeploymentStatusUrl() {
184         return dcaeDeploymentStatusUrl;
185     }
186
187     void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
188         this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
189     }
190
191     public String getSvgRepresentation() {
192         return svgRepresentation;
193     }
194
195     void setSvgRepresentation(String svgRepresentation) {
196         this.svgRepresentation = svgRepresentation;
197     }
198
199     public String getBlueprint() {
200         return blueprint;
201     }
202
203     void setBlueprint(String blueprint) {
204         this.blueprint = blueprint;
205     }
206
207     public LoopState getLastComputedState() {
208         return lastComputedState;
209     }
210
211     void setLastComputedState(LoopState lastComputedState) {
212         this.lastComputedState = lastComputedState;
213     }
214
215     public Set<OperationalPolicy> getOperationalPolicies() {
216         return operationalPolicies;
217     }
218
219     void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
220         this.operationalPolicies = operationalPolicies;
221     }
222
223     public Set<MicroServicePolicy> getMicroServicePolicies() {
224         return microServicePolicies;
225     }
226
227     void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
228         this.microServicePolicies = microServicePolicies;
229     }
230
231     public JsonObject getGlobalPropertiesJson() {
232         return globalPropertiesJson;
233     }
234
235     void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
236         this.globalPropertiesJson = globalPropertiesJson;
237     }
238
239     public Set<LoopLog> getLoopLogs() {
240         return loopLogs;
241     }
242
243     void setLoopLogs(SortedSet<LoopLog> loopLogs) {
244         this.loopLogs = loopLogs;
245     }
246
247     void addOperationalPolicy(OperationalPolicy opPolicy) {
248         operationalPolicies.add(opPolicy);
249         opPolicy.setLoop(this);
250     }
251
252     void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
253         microServicePolicies.add(microServicePolicy);
254         microServicePolicy.getUsedByLoops().add(this);
255     }
256
257     public void addLog(LoopLog log) {
258         log.setLoop(this);
259         this.loopLogs.add(log);
260     }
261
262     public String getDcaeBlueprintId() {
263         return dcaeBlueprintId;
264     }
265
266     void setDcaeBlueprintId(String dcaeBlueprintId) {
267         this.dcaeBlueprintId = dcaeBlueprintId;
268     }
269
270     public Service getModelService() {
271         return modelService;
272     }
273
274     void setModelService(Service modelService) {
275         this.modelService = modelService;
276     }
277
278     public Map<String, ExternalComponent> getComponents() {
279         return components;
280     }
281
282     public ExternalComponent getComponent(String componentName) {
283         return this.components.get(componentName);
284     }
285
286     public void addComponent(ExternalComponent component) {
287         this.components.put(component.getComponentName(), component);
288     }
289
290     public LoopTemplate getLoopTemplate() {
291         return loopTemplate;
292     }
293
294     public void setLoopTemplate(LoopTemplate loopTemplate) {
295         this.loopTemplate = loopTemplate;
296     }
297
298     /**
299      * Generate the loop name.
300      *
301      * @param serviceName       The service name
302      * @param serviceVersion    The service version
303      * @param resourceName      The resource name
304      * @param blueprintFileName The blueprint file name
305      * @return The generated loop name
306      */
307     static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
308             String blueprintFilename) {
309         StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
310                 .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", ""));
311         return buffer.toString().replace('.', '_').replaceAll(" ", "");
312     }
313
314     @Override
315     public int hashCode() {
316         final int prime = 31;
317         int result = 1;
318         result = prime * result + ((name == null) ? 0 : name.hashCode());
319         return result;
320     }
321
322     @Override
323     public boolean equals(Object obj) {
324         if (this == obj) {
325             return true;
326         }
327         if (obj == null) {
328             return false;
329         }
330         if (getClass() != obj.getClass()) {
331             return false;
332         }
333         Loop other = (Loop) obj;
334         if (name == null) {
335             if (other.name != null) {
336                 return false;
337             }
338         } else if (!name.equals(other.name)) {
339             return false;
340         }
341         return true;
342     }
343
344 }