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