Add svg support
[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.HashSet;
31 import java.util.Set;
32
33 import javax.persistence.CascadeType;
34 import javax.persistence.Column;
35 import javax.persistence.Entity;
36 import javax.persistence.EnumType;
37 import javax.persistence.Enumerated;
38 import javax.persistence.FetchType;
39 import javax.persistence.Id;
40 import javax.persistence.JoinColumn;
41 import javax.persistence.JoinTable;
42 import javax.persistence.ManyToMany;
43 import javax.persistence.OneToMany;
44 import javax.persistence.Table;
45
46 import org.hibernate.annotations.Type;
47 import org.hibernate.annotations.TypeDef;
48 import org.hibernate.annotations.TypeDefs;
49 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
50 import org.onap.clamp.loop.log.LoopLog;
51 import org.onap.clamp.policy.microservice.MicroServicePolicy;
52 import org.onap.clamp.policy.operational.OperationalPolicy;
53
54 @Entity
55 @Table(name = "loops")
56 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
57 public class Loop implements Serializable {
58
59     /**
60      *
61      */
62     private static final long serialVersionUID = -286522707701388642L;
63
64     @Id
65     @Expose
66     @Column(nullable = false, name = "name", unique = true)
67     private String name;
68
69     @Expose
70     @Column(name = "dcae_deployment_id")
71     private String dcaeDeploymentId;
72
73     @Expose
74     @Column(name = "dcae_deployment_status_url")
75     private String dcaeDeploymentStatusUrl;
76
77     @Expose
78     @Column(name = "dcae_blueprint_id")
79     private String dcaeBlueprintId;
80
81     @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
82     private String svgRepresentation;
83
84     @Expose
85     @Type(type = "json")
86     @Column(columnDefinition = "json", name = "global_properties_json")
87     private JsonObject globalPropertiesJson;
88
89     @Expose
90     @Type(type = "json")
91     @Column(columnDefinition = "json", name = "model_properties_json")
92     private JsonObject modelPropertiesJson;
93
94     @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml")
95     private String blueprint;
96
97     @Expose
98     @Column(nullable = false, name = "last_computed_state")
99     @Enumerated(EnumType.STRING)
100     private LoopState lastComputedState;
101
102     @Expose
103     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
104     private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
105
106     @Expose
107     @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
108     @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id"))
109     private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
110
111     @Expose
112     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
113     private Set<LoopLog> loopLogs = new HashSet<>();
114
115     public Loop() {
116     }
117
118     public Loop(String name, String blueprint, String svgRepresentation) {
119         this.name = name;
120         this.svgRepresentation = svgRepresentation;
121         this.blueprint = blueprint;
122         this.lastComputedState = LoopState.DESIGN;
123         this.globalPropertiesJson = new JsonObject();
124     }
125
126     public String getName() {
127         return name;
128     }
129
130     void setName(String name) {
131         this.name = name;
132     }
133
134     public String getDcaeDeploymentId() {
135         return dcaeDeploymentId;
136     }
137
138     void setDcaeDeploymentId(String dcaeDeploymentId) {
139         this.dcaeDeploymentId = dcaeDeploymentId;
140     }
141
142     public String getDcaeDeploymentStatusUrl() {
143         return dcaeDeploymentStatusUrl;
144     }
145
146     void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
147         this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
148     }
149
150     public String getSvgRepresentation() {
151         return svgRepresentation;
152     }
153
154     void setSvgRepresentation(String svgRepresentation) {
155         this.svgRepresentation = svgRepresentation;
156     }
157
158     public String getBlueprint() {
159         return blueprint;
160     }
161
162     void setBlueprint(String blueprint) {
163         this.blueprint = blueprint;
164     }
165
166     LoopState getLastComputedState() {
167         return lastComputedState;
168     }
169
170     void setLastComputedState(LoopState lastComputedState) {
171         this.lastComputedState = lastComputedState;
172     }
173
174     Set<OperationalPolicy> getOperationalPolicies() {
175         return operationalPolicies;
176     }
177
178     void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
179         this.operationalPolicies = operationalPolicies;
180     }
181
182     Set<MicroServicePolicy> getMicroServicePolicies() {
183         return microServicePolicies;
184     }
185
186     void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
187         this.microServicePolicies = microServicePolicies;
188     }
189
190     JsonObject getGlobalPropertiesJson() {
191         return globalPropertiesJson;
192     }
193
194     void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
195         this.globalPropertiesJson = globalPropertiesJson;
196     }
197
198     Set<LoopLog> getLoopLogs() {
199         return loopLogs;
200     }
201
202     void setLoopLogs(Set<LoopLog> loopLogs) {
203         this.loopLogs = loopLogs;
204     }
205
206     void addOperationalPolicy(OperationalPolicy opPolicy) {
207         operationalPolicies.add(opPolicy);
208         opPolicy.setLoop(this);
209     }
210
211     void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
212         microServicePolicies.add(microServicePolicy);
213         microServicePolicy.getUsedByLoops().add(this);
214     }
215
216     void addLog(LoopLog log) {
217         loopLogs.add(log);
218         log.setLoop(this);
219     }
220
221     String getDcaeBlueprintId() {
222         return dcaeBlueprintId;
223     }
224
225     void setDcaeBlueprintId(String dcaeBlueprintId) {
226         this.dcaeBlueprintId = dcaeBlueprintId;
227     }
228
229     JsonObject getModelPropertiesJson() {
230         return modelPropertiesJson;
231     }
232
233     void setModelPropertiesJson(JsonObject modelPropertiesJson) {
234         this.modelPropertiesJson = modelPropertiesJson;
235     }
236
237     public static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
238         String blueprintFilename) {
239         StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
240             .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", ""));
241         return buffer.toString().replace('.', '_').replaceAll(" ", "");
242     }
243
244     @Override
245     public int hashCode() {
246         final int prime = 31;
247         int result = 1;
248         result = prime * result + ((name == null) ? 0 : name.hashCode());
249         return result;
250     }
251
252     @Override
253     public boolean equals(Object obj) {
254         if (this == obj)
255             return true;
256         if (obj == null)
257             return false;
258         if (getClass() != obj.getClass())
259             return false;
260         Loop other = (Loop) obj;
261         if (name == null) {
262             if (other.name != null)
263                 return false;
264         } else if (!name.equals(other.name))
265             return false;
266         return true;
267     }
268
269 }