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