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