Merge "CLAMP-CDS integration to display all CDS actions for blueprint in CL"
[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.hibernate.annotations.TypeDef;
41 import org.hibernate.annotations.TypeDefs;
42 import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
43 import org.onap.clamp.clds.util.JsonUtils;
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, policyModel, shared, JsonUtils.GSON_JPA_MODEL
108                 .fromJson(new ToscaYamlToJsonConvertor().parseToscaYaml(policyModel.getPolicyModelTosca(),
109                         policyModel.getPolicyModelType()), JsonObject.class), loopElementModel,null,null);
110     }
111
112     /**
113      * The constructor that does not make use of ToscaYamlToJsonConvertor but take
114      * the jsonRepresentation instead.
115      *
116      * @param name               The name of the MicroService
117      * @param policyModel        The policy model type of the MicroService
118      * @param shared             The flag indicate whether the MicroService is
119  *                           shared
120      * @param jsonRepresentation The UI representation in json format
121      * @param loopElementModel   The loop element model from which this instance should be created
122      * @param pdpGroup           The Pdp Group info
123      * @param pdpSubgroup        The Pdp Subgrouop info
124      */
125     public MicroServicePolicy(String name, PolicyModel policyModel, Boolean shared,
126                               JsonObject jsonRepresentation, LoopElementModel loopElementModel, String pdpGroup, String pdpSubgroup) {
127         this.name = name;
128         this.setPolicyModel(policyModel);
129         this.shared = shared;
130         this.setJsonRepresentation(jsonRepresentation);
131         this.setLoopElementModel(loopElementModel);
132         this.setPdpGroup(pdpGroup);
133         this.setPdpSubgroup(pdpSubgroup);
134     }
135
136     @Override
137     public String getName() {
138         return name;
139     }
140
141     /**
142      * name setter.
143      *
144      * @param name the name to set
145      */
146     @Override
147     public void setName(String name) {
148         this.name = name;
149     }
150
151     public Boolean getShared() {
152         return shared;
153     }
154
155     void setShared(Boolean shared) {
156         this.shared = shared;
157     }
158
159     public Set<Loop> getUsedByLoops() {
160         return usedByLoops;
161     }
162
163     void setUsedByLoops(Set<Loop> usedBy) {
164         this.usedByLoops = usedBy;
165     }
166
167     public String getContext() {
168         return context;
169     }
170
171     public void setContext(String context) {
172         this.context = context;
173     }
174
175     public String getDeviceTypeScope() {
176         return deviceTypeScope;
177     }
178
179     public void setDeviceTypeScope(String deviceTypeScope) {
180         this.deviceTypeScope = deviceTypeScope;
181     }
182
183     /**
184      * dcaeDeploymentId getter.
185      *
186      * @return the dcaeDeploymentId
187      */
188     public String getDcaeDeploymentId() {
189         return dcaeDeploymentId;
190     }
191
192     /**
193      * dcaeDeploymentId setter.
194      *
195      * @param dcaeDeploymentId the dcaeDeploymentId to set
196      */
197     public void setDcaeDeploymentId(String dcaeDeploymentId) {
198         this.dcaeDeploymentId = dcaeDeploymentId;
199     }
200
201     /**
202      * dcaeDeploymentStatusUrl getter.
203      *
204      * @return the dcaeDeploymentStatusUrl
205      */
206     public String getDcaeDeploymentStatusUrl() {
207         return dcaeDeploymentStatusUrl;
208     }
209
210     /**
211      * dcaeDeploymentStatusUrl setter.
212      *
213      * @param dcaeDeploymentStatusUrl the dcaeDeploymentStatusUrl to set
214      */
215     public void setDcaeDeploymentStatusUrl(String dcaeDeploymentStatusUrl) {
216         this.dcaeDeploymentStatusUrl = dcaeDeploymentStatusUrl;
217     }
218
219     /**
220      * dcaeBlueprintId getter.
221      *
222      * @return the dcaeBlueprintId
223      */
224     public String getDcaeBlueprintId() {
225         return dcaeBlueprintId;
226     }
227
228     /**
229      * dcaeBlueprintId setter.
230      *
231      * @param dcaeBlueprintId the dcaeBlueprintId to set
232      */
233     void setDcaeBlueprintId(String dcaeBlueprintId) {
234         this.dcaeBlueprintId = dcaeBlueprintId;
235     }
236
237     @Override
238     public int hashCode() {
239         final int prime = 31;
240         int result = 1;
241         result = prime * result + ((name == null) ? 0 : name.hashCode());
242         return result;
243     }
244
245     @Override
246     public boolean equals(Object obj) {
247         if (this == obj) {
248             return true;
249         }
250         if (obj == null) {
251             return false;
252         }
253         if (getClass() != obj.getClass()) {
254             return false;
255         }
256         MicroServicePolicy other = (MicroServicePolicy) obj;
257         if (name == null) {
258             if (other.name != null) {
259                 return false;
260             }
261         } else if (!name.equals(other.name)) {
262             return false;
263         }
264         return true;
265     }
266 }