Move jsonRepresentation
[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.components.external.DcaeComponent;
60 import org.onap.clamp.loop.components.external.ExternalComponent;
61 import org.onap.clamp.loop.components.external.PolicyComponent;
62 import org.onap.clamp.loop.log.LoopLog;
63 import org.onap.clamp.loop.service.Service;
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 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     @Expose
94     @Column(name = "dcae_blueprint_id")
95     private String dcaeBlueprintId;
96
97     @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
98     private String svgRepresentation;
99
100     @Expose
101     @Type(type = "json")
102     @Column(columnDefinition = "json", name = "global_properties_json")
103     private JsonObject globalPropertiesJson;
104
105     @Expose
106     @ManyToOne(fetch = FetchType.EAGER)
107     @JoinColumn(name = "service_uuid")
108     private Service modelService;
109
110     @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml")
111     private String blueprint;
112
113     @Expose
114     @Column(nullable = false, name = "last_computed_state")
115     @Enumerated(EnumType.STRING)
116     private LoopState lastComputedState;
117
118     @Expose
119     @Transient
120     private final Map<String, ExternalComponent> components = new HashMap<>();
121
122     @Expose
123     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
124     private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
125
126     @Expose
127     @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
128     @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id"))
129     private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
130
131     @Expose
132     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
133     @SortNatural
134     private SortedSet<LoopLog> loopLogs = new TreeSet<>();
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 blueprint, String svgRepresentation) {
152         this.name = name;
153         this.svgRepresentation = svgRepresentation;
154         this.blueprint = blueprint;
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 String getBlueprint() {
193         return blueprint;
194     }
195
196     void setBlueprint(String blueprint) {
197         this.blueprint = blueprint;
198     }
199
200     public LoopState getLastComputedState() {
201         return lastComputedState;
202     }
203
204     void setLastComputedState(LoopState lastComputedState) {
205         this.lastComputedState = lastComputedState;
206     }
207
208     public Set<OperationalPolicy> getOperationalPolicies() {
209         return operationalPolicies;
210     }
211
212     void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
213         this.operationalPolicies = operationalPolicies;
214     }
215
216     public Set<MicroServicePolicy> getMicroServicePolicies() {
217         return microServicePolicies;
218     }
219
220     void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
221         this.microServicePolicies = microServicePolicies;
222     }
223
224     public JsonObject getGlobalPropertiesJson() {
225         return globalPropertiesJson;
226     }
227
228     void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
229         this.globalPropertiesJson = globalPropertiesJson;
230     }
231
232     public Set<LoopLog> getLoopLogs() {
233         return loopLogs;
234     }
235
236     void setLoopLogs(SortedSet<LoopLog> loopLogs) {
237         this.loopLogs = loopLogs;
238     }
239
240     void addOperationalPolicy(OperationalPolicy opPolicy) {
241         operationalPolicies.add(opPolicy);
242         opPolicy.setLoop(this);
243     }
244
245     void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
246         microServicePolicies.add(microServicePolicy);
247         microServicePolicy.getUsedByLoops().add(this);
248     }
249
250     public void addLog(LoopLog log) {
251         log.setLoop(this);
252         this.loopLogs.add(log);
253     }
254
255     public String getDcaeBlueprintId() {
256         return dcaeBlueprintId;
257     }
258
259     void setDcaeBlueprintId(String dcaeBlueprintId) {
260         this.dcaeBlueprintId = dcaeBlueprintId;
261     }
262
263     public Service getModelService() {
264         return modelService;
265     }
266
267     void setModelService(Service modelService) {
268         this.modelService = modelService;
269     }
270
271     public Map<String, ExternalComponent> getComponents() {
272         return components;
273     }
274
275     public ExternalComponent getComponent(String componentName) {
276         return this.components.get(componentName);
277     }
278
279     public void addComponent(ExternalComponent component) {
280         this.components.put(component.getComponentName(), component);
281     }
282
283     /**
284      * Generate the loop name.
285      *
286      * @param serviceName       The service name
287      * @param serviceVersion    The service version
288      * @param resourceName      The resource name
289      * @param blueprintFileName The blueprint file name
290      * @return The generated loop name
291      */
292     static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
293             String blueprintFilename) {
294         StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
295                 .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", ""));
296         return buffer.toString().replace('.', '_').replaceAll(" ", "");
297     }
298
299     @Override
300     public int hashCode() {
301         final int prime = 31;
302         int result = 1;
303         result = prime * result + ((name == null) ? 0 : name.hashCode());
304         return result;
305     }
306
307     @Override
308     public boolean equals(Object obj) {
309         if (this == obj) {
310             return true;
311         }
312         if (obj == null) {
313             return false;
314         }
315         if (getClass() != obj.getClass()) {
316             return false;
317         }
318         Loop other = (Loop) obj;
319         if (name == null) {
320             if (other.name != null) {
321                 return false;
322             }
323         } else if (!name.equals(other.name)) {
324             return false;
325         }
326         return true;
327     }
328
329 }