Fix loop order
[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.GsonBuilder;
27 import com.google.gson.JsonArray;
28 import com.google.gson.JsonObject;
29 import com.google.gson.annotations.Expose;
30
31 import java.io.Serializable;
32 import java.util.ArrayList;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Set;
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.OrderBy;
49 import javax.persistence.Table;
50
51 import org.hibernate.annotations.Type;
52 import org.hibernate.annotations.TypeDef;
53 import org.hibernate.annotations.TypeDefs;
54 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
55 import org.onap.clamp.loop.log.LoopLog;
56 import org.onap.clamp.policy.microservice.MicroServicePolicy;
57 import org.onap.clamp.policy.operational.OperationalPolicy;
58
59 @Entity
60 @Table(name = "loops")
61 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
62 public class Loop implements Serializable {
63
64     /**
65      * The serial version id.
66      */
67     private static final long serialVersionUID = -286522707701388642L;
68
69     @Id
70     @Expose
71     @Column(nullable = false, name = "name", unique = true)
72     private String name;
73
74     @Expose
75     @Column(name = "dcae_deployment_id")
76     private String dcaeDeploymentId;
77
78     @Expose
79     @Column(name = "dcae_deployment_status_url")
80     private String dcaeDeploymentStatusUrl;
81
82     @Expose
83     @Column(name = "dcae_blueprint_id")
84     private String dcaeBlueprintId;
85
86     @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
87     private String svgRepresentation;
88
89     @Expose
90     @Type(type = "json")
91     @Column(columnDefinition = "json", name = "global_properties_json")
92     private JsonObject globalPropertiesJson;
93
94     @Expose
95     @Type(type = "json")
96     @Column(columnDefinition = "json", name = "model_properties_json")
97     private JsonObject modelPropertiesJson;
98
99     @Column(columnDefinition = "MEDIUMTEXT", nullable = false, name = "blueprint_yaml")
100     private String blueprint;
101
102     @Expose
103     @Column(nullable = false, name = "last_computed_state")
104     @Enumerated(EnumType.STRING)
105     private LoopState lastComputedState;
106
107     @Expose
108     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
109     private Set<OperationalPolicy> operationalPolicies = new HashSet<>();
110
111     @Expose
112     @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
113     @JoinTable(name = "loops_microservicepolicies", joinColumns = @JoinColumn(name = "loop_id"), inverseJoinColumns = @JoinColumn(name = "microservicepolicy_id"))
114     private Set<MicroServicePolicy> microServicePolicies = new HashSet<>();
115
116     @Expose
117     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loop")
118     @OrderBy("id DESC")
119     private Set<LoopLog> loopLogs = new HashSet<>();
120
121     public Loop() {
122     }
123
124     /**
125      * Constructor.
126      */
127     public Loop(String name, String blueprint, String svgRepresentation) {
128         this.name = name;
129         this.svgRepresentation = svgRepresentation;
130         this.blueprint = blueprint;
131         this.lastComputedState = LoopState.DESIGN;
132         this.globalPropertiesJson = new JsonObject();
133     }
134
135     public String getName() {
136         return name;
137     }
138
139     void setName(String name) {
140         this.name = name;
141     }
142
143     public String getDcaeDeploymentId() {
144         return dcaeDeploymentId;
145     }
146
147     void setDcaeDeploymentId(String dcaeDeploymentId) {
148         this.dcaeDeploymentId = dcaeDeploymentId;
149     }
150
151     public String getDcaeDeploymentStatusUrl() {
152         return dcaeDeploymentStatusUrl;
153     }
154
155     void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
156         this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
157     }
158
159     public String getSvgRepresentation() {
160         return svgRepresentation;
161     }
162
163     void setSvgRepresentation(String svgRepresentation) {
164         this.svgRepresentation = svgRepresentation;
165     }
166
167     public String getBlueprint() {
168         return blueprint;
169     }
170
171     void setBlueprint(String blueprint) {
172         this.blueprint = blueprint;
173     }
174
175     public LoopState getLastComputedState() {
176         return lastComputedState;
177     }
178
179     void setLastComputedState(LoopState lastComputedState) {
180         this.lastComputedState = lastComputedState;
181     }
182
183     public Set<OperationalPolicy> getOperationalPolicies() {
184         return operationalPolicies;
185     }
186
187     void setOperationalPolicies(Set<OperationalPolicy> operationalPolicies) {
188         this.operationalPolicies = operationalPolicies;
189     }
190
191     public Set<MicroServicePolicy> getMicroServicePolicies() {
192         return microServicePolicies;
193     }
194
195     void setMicroServicePolicies(Set<MicroServicePolicy> microServicePolicies) {
196         this.microServicePolicies = microServicePolicies;
197     }
198
199     public JsonObject getGlobalPropertiesJson() {
200         return globalPropertiesJson;
201     }
202
203     void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
204         this.globalPropertiesJson = globalPropertiesJson;
205     }
206
207     public Set<LoopLog> getLoopLogs() {
208         return loopLogs;
209     }
210
211     void setLoopLogs(Set<LoopLog> loopLogs) {
212         this.loopLogs = loopLogs;
213     }
214
215     void addOperationalPolicy(OperationalPolicy opPolicy) {
216         operationalPolicies.add(opPolicy);
217         opPolicy.setLoop(this);
218     }
219
220     void addMicroServicePolicy(MicroServicePolicy microServicePolicy) {
221         microServicePolicies.add(microServicePolicy);
222         microServicePolicy.getUsedByLoops().add(this);
223     }
224
225     void addLog(LoopLog log) {
226         loopLogs.add(log);
227         log.setLoop(this);
228     }
229
230     public String getDcaeBlueprintId() {
231         return dcaeBlueprintId;
232     }
233
234     void setDcaeBlueprintId(String dcaeBlueprintId) {
235         this.dcaeBlueprintId = dcaeBlueprintId;
236     }
237
238     public JsonObject getModelPropertiesJson() {
239         return modelPropertiesJson;
240     }
241
242     void setModelPropertiesJson(JsonObject modelPropertiesJson) {
243         this.modelPropertiesJson = modelPropertiesJson;
244     }
245
246     /**
247      * Generate the loop name.
248      *
249      * @param serviceName
250      *        The service name
251      * @param serviceVersion
252      *        The service version
253      * @param resourceName
254      *        The resource name
255      * @param blueprintFileName
256      *        The blueprint file name
257      * @return The generated loop name
258      */
259     static String generateLoopName(String serviceName, String serviceVersion, String resourceName,
260         String blueprintFilename) {
261         StringBuilder buffer = new StringBuilder("LOOP_").append(serviceName).append("_v").append(serviceVersion)
262             .append("_").append(resourceName).append("_").append(blueprintFilename.replaceAll(".yaml", ""));
263         return buffer.toString().replace('.', '_').replaceAll(" ", "");
264     }
265
266     /**
267      * Generates the Json that must be sent to policy to add all policies to Active
268      * PDP group.
269      *
270      * @return The json, payload to send
271      */
272     public String createPoliciesPayloadPdpGroup() {
273         JsonObject jsonObject = new JsonObject();
274         JsonArray jsonArray = new JsonArray();
275         jsonObject.add("policies", jsonArray);
276
277         for (String policyName : this.listPolicyNamesPdpGroup()) {
278             JsonObject policyNode = new JsonObject();
279             jsonArray.add(policyNode);
280             policyNode.addProperty("policy-id", policyName);
281         }
282         return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
283     }
284
285     /**
286      * Generates the list of policy names that must be send/remove to/from active
287      * PDP group.
288      *
289      * @return A list of policy names
290      */
291     public List<String> listPolicyNamesPdpGroup() {
292         List<String> policyNamesList = new ArrayList<>();
293         for (OperationalPolicy opPolicy : this.getOperationalPolicies()) {
294             policyNamesList.add(opPolicy.getName());
295             for (String guardName : opPolicy.createGuardPolicyPayloads().keySet()) {
296                 policyNamesList.add(guardName);
297             }
298         }
299         for (MicroServicePolicy microServicePolicy : this.getMicroServicePolicies()) {
300             policyNamesList.add(microServicePolicy.getName());
301         }
302         return policyNamesList;
303     }
304
305     @Override
306     public int hashCode() {
307         final int prime = 31;
308         int result = 1;
309         result = prime * result + ((name == null) ? 0 : name.hashCode());
310         return result;
311     }
312
313     @Override
314     public boolean equals(Object obj) {
315         if (this == obj) {
316             return true;
317         }
318         if (obj == null) {
319             return false;
320         }
321         if (getClass() != obj.getClass()) {
322             return false;
323         }
324         Loop other = (Loop) obj;
325         if (name == null) {
326             if (other.name != null) {
327                 return false;
328             }
329         } else if (!name.equals(other.name)) {
330             return false;
331         }
332         return true;
333     }
334
335 }