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