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