Merge "Update get Dcae Status flow"
[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     @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
96     private String svgRepresentation;
97
98     @Expose
99     @Type(type = "json")
100     @Column(columnDefinition = "json", name = "global_properties_json")
101     private JsonObject globalPropertiesJson;
102
103     @Expose
104     @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
105     @JoinColumn(name = "service_uuid")
106     private Service modelService;
107
108     @Expose
109     @Column(nullable = false, name = "last_computed_state")
110     @Enumerated(EnumType.STRING)
111     private LoopState lastComputedState;
112
113     @Expose
114     @Transient
115     private final Map<String, ExternalComponent> components = new HashMap<>();
116
117     @Expose
118     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop", orphanRemoval = true)
119     private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
120
121     @Expose
122     @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.EAGER)
123     @JoinTable(name = "loops_to_microservicepolicies", joinColumns = @JoinColumn(name = "loop_name"),
124             inverseJoinColumns = @JoinColumn(name = "microservicepolicy_name"))
125     private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
126
127     @Expose
128     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop", orphanRemoval = true)
129     @SortNatural
130     private SortedSet<LoopLog> loopLogs = new TreeSet<>();
131
132     @Expose
133     @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.EAGER)
134     @JoinColumn(name = "loop_template_name", nullable=false)
135     private LoopTemplate loopTemplate;
136
137     private void initializeExternalComponents() {
138         this.addComponent(new PolicyComponent());
139         this.addComponent(new DcaeComponent());
140     }
141
142     /**
143      * Public constructor.
144      */
145     public Loop() {
146         initializeExternalComponents();
147     }
148
149     /**
150      * Constructor.
151      */
152     public Loop(String name, String svgRepresentation) {
153         this.name = name;
154         this.svgRepresentation = svgRepresentation;
155         this.lastComputedState = LoopState.DESIGN;
156         this.globalPropertiesJson = new JsonObject();
157         initializeExternalComponents();
158     }
159
160     public String getName() {
161         return name;
162     }
163
164     void setName(String name) {
165         this.name = name;
166     }
167
168     public String getDcaeDeploymentId() {
169         return dcaeDeploymentId;
170     }
171
172     void setDcaeDeploymentId(String dcaeDeploymentId) {
173         this.dcaeDeploymentId = dcaeDeploymentId;
174     }
175
176     public String getDcaeDeploymentStatusUrl() {
177         return dcaeDeploymentStatusUrl;
178     }
179
180     void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
181         this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
182     }
183
184     public String getSvgRepresentation() {
185         return svgRepresentation;
186     }
187
188     void setSvgRepresentation(String svgRepresentation) {
189         this.svgRepresentation = svgRepresentation;
190     }
191
192     public LoopState getLastComputedState() {
193         return lastComputedState;
194     }
195
196     void setLastComputedState(LoopState lastComputedState) {
197         this.lastComputedState = lastComputedState;
198     }
199
200     public Set<OperationalPolicy> getOperationalPolicies() {
201         return operationalPolicies;
202     }
203
204     void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
205         this.operationalPolicies = operationalPolicies;
206     }
207
208     public Set<MicroServicePolicy> getMicroServicePolicies() {
209         return microServicePolicies;
210     }
211
212     void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
213         this.microServicePolicies = microServicePolicies;
214     }
215
216     public JsonObject getGlobalPropertiesJson() {
217         return globalPropertiesJson;
218     }
219
220     void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
221         this.globalPropertiesJson = globalPropertiesJson;
222     }
223
224     public Set<LoopLog> getLoopLogs() {
225         return loopLogs;
226     }
227
228     void setLoopLogs(SortedSet<LoopLog> loopLogs) {
229         this.loopLogs = loopLogs;
230     }
231
232     void addOperationalPolicy(OperationalPolicy opPolicy) {
233         operationalPolicies.add(opPolicy);
234         opPolicy.setLoop(this);
235     }
236
237     void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
238         microServicePolicies.add(microServicePolicy);
239         microServicePolicy.getUsedByLoops().add(this);
240     }
241
242     public void addLog(LoopLog log) {
243         log.setLoop(this);
244         this.loopLogs.add(log);
245     }
246
247     public Service getModelService() {
248         return modelService;
249     }
250
251     void setModelService(Service modelService) {
252         this.modelService = modelService;
253     }
254
255     public Map<String, ExternalComponent> getComponents() {
256         refreshDcaeComponents();
257         return components;
258     }
259
260     public ExternalComponent getComponent(String componentName) {
261         refreshDcaeComponents();
262         return this.components.get(componentName);
263     }
264
265     public void addComponent(ExternalComponent component) {
266         this.components.put(component.getComponentName(), component);
267     }
268
269     public LoopTemplate getLoopTemplate() {
270         return loopTemplate;
271     }
272
273     public void setLoopTemplate(LoopTemplate loopTemplate) {
274         this.loopTemplate = loopTemplate;
275     }
276
277     private void refreshDcaeComponents() {
278         if (!this.loopTemplate.getUniqueBlueprint()) {
279             this.components.remove("DCAE");
280             for (MicroServicePolicy policy : this.microServicePolicies) {
281                 if (!this.components.containsKey("DCAE_" + policy.getName())) {
282                     this.addComponent(new DcaeComponent(policy.getName()));
283                 }
284             }
285         }
286     }
287
288     /**
289      * Generate the loop name.
290      *
291      * @param serviceName       The service name
292      * @param serviceVersion    The service version
293      * @param resourceName      The resource name
294      * @param blueprintFileName The blueprint file name
295      * @return The generated loop name
296      */
297     public static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
298             String blueprintFileName) {
299         StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
300                 .append("_").append(resourceName).append("_").append(blueprintFileName.replaceAll(".yaml", ""));
301         return buffer.toString().replace('.', '_').replaceAll(" ", "");
302     }
303
304     @Override
305     public int hashCode() {
306         final int prime = 31;
307         int result = 1;
308         result = prime * result + ((name == null) ? 0 : name.hashCode());
309         return result;
310     }
311
312     @Override
313     public boolean equals(Object obj) {
314         if (this == obj) {
315             return true;
316         }
317         if (obj == null) {
318             return false;
319         }
320         if (getClass() != obj.getClass()) {
321             return false;
322         }
323         Loop other = (Loop) obj;
324         if (name == null) {
325             if (other.name != null) {
326                 return false;
327             }
328         } else if (!name.equals(other.name)) {
329             return false;
330         }
331         return true;
332     }
333
334 }