Create get Pdp Groups flow
[clamp.git] / src / main / java / org / onap / clamp / policy / Policy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia 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;
25
26 import com.google.gson.JsonObject;
27 import com.google.gson.annotations.Expose;
28
29 import java.io.UnsupportedEncodingException;
30
31 import javax.persistence.Column;
32 import javax.persistence.FetchType;
33 import javax.persistence.JoinColumn;
34 import javax.persistence.ManyToOne;
35 import javax.persistence.MappedSuperclass;
36
37 import org.hibernate.annotations.Type;
38 import org.hibernate.annotations.TypeDef;
39 import org.hibernate.annotations.TypeDefs;
40 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
41 import org.onap.clamp.loop.common.AuditEntity;
42 import org.onap.clamp.loop.template.LoopElementModel;
43
44 @MappedSuperclass
45 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
46 public abstract class Policy extends AuditEntity {
47
48     @Expose
49     @Type(type = "json")
50     @Column(columnDefinition = "json", name = "json_representation", nullable = false)
51     private JsonObject jsonRepresentation;
52
53     @Expose
54     @Type(type = "json")
55     @Column(columnDefinition = "json", name = "configurations_json")
56     private JsonObject configurationsJson;
57
58     /**
59      * This attribute can be null when the user add a policy on the loop instance, not the template.
60      * When null, It therefore indicates that this policy is not by default in the loop template.
61      */
62     @Expose
63     @ManyToOne(fetch = FetchType.EAGER)
64     @JoinColumn(name = "loop_element_model_id")
65     private LoopElementModel loopElementModel;
66
67     @Expose
68     @Column(name = "pdp_group")
69     private String pdpGroup;
70
71     @Expose
72     @Column(name = "pdp_sub_group")
73     private String pdpSubGroup;
74
75     public abstract String createPolicyPayload() throws UnsupportedEncodingException;
76
77     /**
78      * Name getter.
79      *
80      * @return the name
81      */
82     public abstract String getName();
83
84     /**
85      * Name setter.
86      */
87     public abstract void setName(String name);
88
89     /**
90      * jsonRepresentation getter.
91      *
92      * @return the jsonRepresentation
93      */
94     public JsonObject getJsonRepresentation() {
95         return jsonRepresentation;
96     }
97
98     /**
99      * jsonRepresentation setter.
100      *
101      * @param jsonRepresentation The jsonRepresentation to set
102      */
103     public void setJsonRepresentation(JsonObject jsonRepresentation) {
104         this.jsonRepresentation = jsonRepresentation;
105     }
106
107     /**
108      * configurationsJson getter.
109      *
110      * @return The configurationsJson
111      */
112     public JsonObject getConfigurationsJson() {
113         return configurationsJson;
114     }
115
116     /**
117      * configurationsJson setter.
118      *
119      * @param configurationsJson the configurationsJson to set
120      */
121     public void setConfigurationsJson(JsonObject configurationsJson) {
122         this.configurationsJson = configurationsJson;
123     }
124
125     /**
126      * loopElementModel getter.
127      *
128      * @return the loopElementModel
129      */
130     public LoopElementModel getLoopElementModel() {
131         return loopElementModel;
132     }
133
134     /**
135      * loopElementModel setter.
136      *
137      * @param loopElementModel the loopElementModel to set
138      */
139     public void setLoopElementModel(LoopElementModel loopElementModel) {
140         this.loopElementModel = loopElementModel;
141     }
142
143     /**
144      * pdpGroup getter.
145      *
146      * @return the pdpGroup
147      */
148     public String getPdpGroup() {
149         return pdpGroup;
150     }
151
152     /**
153      * pdpGroup setter.
154      *
155      * @param pdpGroup the pdpGroup to set
156      */
157     public void setPdpGroup(String pdpGroup) {
158         this.pdpGroup = pdpGroup;
159     }
160
161     /**
162      * pdpSubGroup getter.
163      * 
164      * @return the pdpSubGroup
165      */
166     public String getPdpSubGroup() {
167         return pdpSubGroup;
168     }
169
170     /**
171      * pdpSubGroup setter.
172      * 
173      * @param pdpSubGroup the pdpSubGroup to set
174      */
175     public void setPdpSubGroup(String pdpSubGroup) {
176         this.pdpSubGroup = pdpSubGroup;
177     }
178
179     /**
180      * Generate the policy name.
181      *
182      * @param policyType        The policy type
183      * @param serviceName       The service name
184      * @param serviceVersion    The service version
185      * @param resourceName      The resource name
186      * @param blueprintFilename The blueprint file name
187      * @return The generated policy name
188      */
189     public static String generatePolicyName(String policyType, String serviceName, String serviceVersion,
190                                             String resourceName, String blueprintFilename) {
191         StringBuilder buffer = new StringBuilder(policyType).append("_").append(serviceName).append("_v")
192                 .append(serviceVersion).append("_").append(resourceName).append("_")
193                 .append(blueprintFilename.replaceAll(".yaml", ""));
194         return buffer.toString().replace('.', '_').replaceAll(" ", "");
195     }
196
197 }