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