Merge "Validate ids"
[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.google.gson.JsonObject;
27 import com.google.gson.annotations.Expose;
28
29 import java.io.Serializable;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.Map;
33 import java.util.Set;
34 import java.util.SortedSet;
35 import java.util.TreeSet;
36
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.OneToMany;
48 import javax.persistence.Table;
49 import javax.persistence.Transient;
50
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.dao.model.jsontype.StringJsonUserType;
56 import org.onap.clamp.loop.components.external.DcaeComponent;
57 import org.onap.clamp.loop.components.external.ExternalComponent;
58 import org.onap.clamp.loop.components.external.PolicyComponent;
59 import org.onap.clamp.loop.log.LoopLog;
60 import org.onap.clamp.policy.microservice.MicroServicePolicy;
61 import org.onap.clamp.policy.operational.OperationalPolicy;
62
63 @Entity
64 @Table(name = "loops")
65 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
66 public class Loop implements Serializable {
67
68     /**
69      * The serial version id.
70      */
71     private static final long serialVersionUID = -286522707701388642L;
72
73     @Id
74     @Expose
75     @Column(nullable = false, name = "name", unique = true)
76     private String name;
77
78     @Expose
79     @Column(name = "dcae_deployment_id")
80     private String dcaeDeploymentId;
81
82     @Expose
83     @Column(name = "dcae_deployment_status_url")
84     private String dcaeDeploymentStatusUrl;
85
86     @Expose
87     @Column(name = "dcae_blueprint_id")
88     private String dcaeBlueprintId;
89
90     @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
91     private String svgRepresentation;
92
93     @Expose
94     @Type(type = "json")
95     @Column(columnDefinition = "json", name = "global_properties_json")
96     private JsonObject globalPropertiesJson;
97
98     @Expose
99     @Type(type = "json")
100     @Column(columnDefinition = "json", name = "model_properties_json")
101     private JsonObject modelPropertiesJson;
102
103     @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml")
104     private String blueprint;
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")
117     private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
118
119     @Expose
120     @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
121     @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id"))
122     private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
123
124     @Expose
125     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
126     @SortNatural
127     private SortedSet<LoopLog> loopLogs = new TreeSet<>();
128
129     private void initializeExternalComponents() {
130         this.addComponent(new PolicyComponent());
131         this.addComponent(new DcaeComponent());
132     }
133
134     public Loop() {
135         initializeExternalComponents();
136     }
137
138     /**
139      * Constructor.
140      */
141     public Loop(String name, String blueprint, String svgRepresentation) {
142         this.name = name;
143         this.svgRepresentation = svgRepresentation;
144         this.blueprint = blueprint;
145         this.lastComputedState = LoopState.DESIGN;
146         this.globalPropertiesJson = new JsonObject();
147         initializeExternalComponents();
148     }
149
150     public String getName() {
151         return name;
152     }
153
154     void setName(String name) {
155         this.name = name;
156     }
157
158     public String getDcaeDeploymentId() {
159         return dcaeDeploymentId;
160     }
161
162     void setDcaeDeploymentId(String dcaeDeploymentId) {
163         this.dcaeDeploymentId = dcaeDeploymentId;
164     }
165
166     public String getDcaeDeploymentStatusUrl() {
167         return dcaeDeploymentStatusUrl;
168     }
169
170     void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
171         this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
172     }
173
174     public String getSvgRepresentation() {
175         return svgRepresentation;
176     }
177
178     void setSvgRepresentation(String svgRepresentation) {
179         this.svgRepresentation = svgRepresentation;
180     }
181
182     public String getBlueprint() {
183         return blueprint;
184     }
185
186     void setBlueprint(String blueprint) {
187         this.blueprint = blueprint;
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     void addOperationalPolicy(OperationalPolicy opPolicy) {
231         operationalPolicies.add(opPolicy);
232         opPolicy.setLoop(this);
233     }
234
235     void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
236         microServicePolicies.add(microServicePolicy);
237         microServicePolicy.getUsedByLoops().add(this);
238     }
239
240     public void addLog(LoopLog log) {
241         log.setLoop(this);
242         this.loopLogs.add(log);
243     }
244
245     public String getDcaeBlueprintId() {
246         return dcaeBlueprintId;
247     }
248
249     void setDcaeBlueprintId(String dcaeBlueprintId) {
250         this.dcaeBlueprintId = dcaeBlueprintId;
251     }
252
253     public JsonObject getModelPropertiesJson() {
254         return modelPropertiesJson;
255     }
256
257     void setModelPropertiesJson(JsonObject modelPropertiesJson) {
258         this.modelPropertiesJson = modelPropertiesJson;
259     }
260
261     public Map<String, ExternalComponent> getComponents() {
262         return components;
263     }
264
265     public ExternalComponent getComponent(String componentName) {
266         return this.components.get(componentName);
267     }
268
269     public void addComponent(ExternalComponent component) {
270         this.components.put(component.getComponentName(), component);
271     }
272
273     /**
274      * Generate the loop name.
275      *
276      * @param serviceName
277      *        The service name
278      * @param serviceVersion
279      *        The service version
280      * @param resourceName
281      *        The resource name
282      * @param blueprintFileName
283      *        The blueprint file name
284      * @return The generated loop name
285      */
286     static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
287         String blueprintFilename) {
288         StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
289             .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", ""));
290         return buffer.toString().replace('.', '_').replaceAll(" ", "");
291     }
292
293     @Override
294     public int hashCode() {
295         final int prime = 31;
296         int result = 1;
297         result = prime * result + ((name == null) ? 0 : name.hashCode());
298         return result;
299     }
300
301     @Override
302     public boolean equals(Object obj) {
303         if (this == obj) {
304             return true;
305         }
306         if (obj == null) {
307             return false;
308         }
309         if (getClass() != obj.getClass()) {
310             return false;
311         }
312         Loop other = (Loop) obj;
313         if (name == null) {
314             if (other.name != null) {
315                 return false;
316             }
317         } else if (!name.equals(other.name)) {
318             return false;
319         }
320         return true;
321     }
322
323 }