b8093ccf1fa7be7870e240eea58a78b2b62b3373
[clamp.git] / src / main / java / org / onap / clamp / policy / microservice / MicroServicePolicy.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.policy.microservice;
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.annotations.Expose;
30 import java.io.IOException;
31 import java.io.Serializable;
32 import java.util.HashSet;
33 import java.util.Set;
34 import javax.persistence.Column;
35 import javax.persistence.Entity;
36 import javax.persistence.FetchType;
37 import javax.persistence.Id;
38 import javax.persistence.ManyToMany;
39 import javax.persistence.Table;
40 import javax.persistence.Transient;
41 import org.hibernate.annotations.TypeDef;
42 import org.hibernate.annotations.TypeDefs;
43 import org.onap.clamp.clds.tosca.update.UnknownComponentException;
44 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
45 import org.onap.clamp.loop.Loop;
46 import org.onap.clamp.loop.template.LoopElementModel;
47 import org.onap.clamp.loop.template.PolicyModel;
48 import org.onap.clamp.policy.Policy;
49
50 @Entity
51 @Table(name = "micro_service_policies")
52 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
53 public class MicroServicePolicy extends Policy implements Serializable {
54     /**
55      * The serial version ID.
56      */
57     private static final long serialVersionUID = 6271238288583332616L;
58
59     @Transient
60     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MicroServicePolicy.class);
61
62     @Expose
63     @Id
64     @Column(nullable = false, name = "name", unique = true)
65     private String name;
66
67     @Expose
68     @Column(name = "context")
69     private String context;
70
71     @Expose
72     @Column(name = "device_type_scope")
73     private String deviceTypeScope;
74
75     @Expose
76     @Column(name = "shared", nullable = false)
77     private Boolean shared;
78
79     @ManyToMany(mappedBy = "microServicePolicies", fetch = FetchType.EAGER)
80     private Set<Loop> usedByLoops = new HashSet<>();
81
82     @Expose
83     @Column(name = "dcae_deployment_id")
84     private String dcaeDeploymentId;
85
86     @Expose
87     @Column(name = "dcae_deployment_status_url")
88     private String dcaeDeploymentStatusUrl;
89
90     @Expose
91     @Column(name = "dcae_blueprint_id")
92     private String dcaeBlueprintId;
93
94     public MicroServicePolicy() {
95         // serialization
96     }
97
98     /**
99      * The constructor that creates the json representation from the policyTosca
100      * using the ToscaYamlToJsonConvertor.
101      *
102      * @param name        The name of the MicroService
103      * @param policyModel The policy model of the MicroService
104      * @param shared      The flag indicate whether the MicroService is shared
105      */
106     public MicroServicePolicy(String name, PolicyModel policyModel, Boolean shared, LoopElementModel loopElementModel) {
107         this.name = name;
108         this.setPolicyModel(policyModel);
109         this.shared = shared;
110         try {
111             this.setJsonRepresentation(
112                     Policy.generateJsonRepresentationFromToscaModel(policyModel.getPolicyModelTosca(),
113                             policyModel.getPolicyModelType()));
114         } catch (UnknownComponentException | NullPointerException | IOException e) {
115             logger.error("Unable to generate the microservice policy Schema ... ", e);
116             this.setJsonRepresentation(new JsonObject());
117         }
118         this.setLoopElementModel(loopElementModel);
119     }
120
121     /**
122      * The constructor that does not make use of ToscaYamlToJsonConvertor but take
123      * the jsonRepresentation instead.
124      *
125      * @param name               The name of the MicroService
126      * @param policyModel        The policy model type of the MicroService
127      * @param shared             The flag indicate whether the MicroService is
128      *                           shared
129      * @param jsonRepresentation The UI representation in json format
130      * @param loopElementModel   The loop element model from which this instance should be created
131      * @param pdpGroup           The Pdp Group info
132      * @param pdpSubgroup        The Pdp Subgrouop info
133      */
134     public MicroServicePolicy(String name, PolicyModel policyModel, Boolean shared,
135                               JsonObject jsonRepresentation, LoopElementModel loopElementModel, String pdpGroup,
136                               String pdpSubgroup) {
137         this.name = name;
138         this.setPolicyModel(policyModel);
139         this.shared = shared;
140         this.setJsonRepresentation(jsonRepresentation);
141         this.setLoopElementModel(loopElementModel);
142         this.setPdpGroup(pdpGroup);
143         this.setPdpSubgroup(pdpSubgroup);
144     }
145
146     @Override
147     public String getName() {
148         return name;
149     }
150
151     /**
152      * name setter.
153      *
154      * @param name the name to set
155      */
156     @Override
157     public void setName(String name) {
158         this.name = name;
159     }
160
161     public Boolean getShared() {
162         return shared;
163     }
164
165     void setShared(Boolean shared) {
166         this.shared = shared;
167     }
168
169     public Set<Loop> getUsedByLoops() {
170         return usedByLoops;
171     }
172
173     void setUsedByLoops(Set<Loop> usedBy) {
174         this.usedByLoops = usedBy;
175     }
176
177     public String getContext() {
178         return context;
179     }
180
181     public void setContext(String context) {
182         this.context = context;
183     }
184
185     public String getDeviceTypeScope() {
186         return deviceTypeScope;
187     }
188
189     public void setDeviceTypeScope(String deviceTypeScope) {
190         this.deviceTypeScope = deviceTypeScope;
191     }
192
193     /**
194      * dcaeDeploymentId getter.
195      *
196      * @return the dcaeDeploymentId
197      */
198     public String getDcaeDeploymentId() {
199         return dcaeDeploymentId;
200     }
201
202     /**
203      * dcaeDeploymentId setter.
204      *
205      * @param dcaeDeploymentId the dcaeDeploymentId to set
206      */
207     public void setDcaeDeploymentId(String dcaeDeploymentId) {
208         this.dcaeDeploymentId = dcaeDeploymentId;
209     }
210
211     /**
212      * dcaeDeploymentStatusUrl getter.
213      *
214      * @return the dcaeDeploymentStatusUrl
215      */
216     public String getDcaeDeploymentStatusUrl() {
217         return dcaeDeploymentStatusUrl;
218     }
219
220     /**
221      * dcaeDeploymentStatusUrl setter.
222      *
223      * @param dcaeDeploymentStatusUrl the dcaeDeploymentStatusUrl to set
224      */
225     public void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
226         this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
227     }
228
229     /**
230      * dcaeBlueprintId getter.
231      *
232      * @return the dcaeBlueprintId
233      */
234     public String getDcaeBlueprintId() {
235         return dcaeBlueprintId;
236     }
237
238     /**
239      * dcaeBlueprintId setter.
240      *
241      * @param dcaeBlueprintId the dcaeBlueprintId to set
242      */
243     void setDcaeBlueprintId(String dcaeBlueprintId) {
244         this.dcaeBlueprintId = dcaeBlueprintId;
245     }
246
247     @Override
248     public int hashCode() {
249         final int prime = 31;
250         int result = 1;
251         result = prime * result + ((name == null) ? 0 : name.hashCode());
252         return result;
253     }
254
255     @Override
256     public boolean equals(Object obj) {
257         if (this == obj) {
258             return true;
259         }
260         if (obj == null) {
261             return false;
262         }
263         if (getClass() != obj.getClass()) {
264             return false;
265         }
266         MicroServicePolicy other = (MicroServicePolicy) obj;
267         if (name == null) {
268             if (other.name != null) {
269                 return false;
270             }
271         }
272         else if (!name.equals(other.name)) {
273             return false;
274         }
275         return true;
276     }
277 }