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