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