Operational policy modification
[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     public abstract String createPolicyPayload() throws UnsupportedEncodingException;
72
73     /**
74      * Name getter.
75      *
76      * @return the name
77      */
78     public abstract String getName();
79
80     /**
81      * Name setter.
82      */
83     public abstract void setName(String name);
84
85     /**
86      * jsonRepresentation getter.
87      *
88      * @return the jsonRepresentation
89      */
90     public JsonObject getJsonRepresentation() {
91         return jsonRepresentation;
92     }
93
94     /**
95      * jsonRepresentation setter.
96      *
97      * @param jsonRepresentation The jsonRepresentation to set
98      */
99     public void setJsonRepresentation(JsonObject jsonRepresentation) {
100         this.jsonRepresentation = jsonRepresentation;
101     }
102
103     /**
104      * configurationsJson getter.
105      *
106      * @return The configurationsJson
107      */
108     public JsonObject getConfigurationsJson() {
109         return configurationsJson;
110     }
111
112     /**
113      * configurationsJson setter.
114      *
115      * @param configurationsJson the configurationsJson to set
116      */
117     public void setConfigurationsJson(JsonObject configurationsJson) {
118         this.configurationsJson = configurationsJson;
119     }
120
121     /**
122      * loopElementModel getter.
123      *
124      * @return the loopElementModel
125      */
126     public LoopElementModel getLoopElementModel() {
127         return loopElementModel;
128     }
129
130     /**
131      * loopElementModel setter.
132      *
133      * @param loopElementModel the loopElementModel to set
134      */
135     public void setLoopElementModel(LoopElementModel loopElementModel) {
136         this.loopElementModel = loopElementModel;
137     }
138
139     /**
140      * pdpGroup getter.
141      *
142      * @return the pdpGroup
143      */
144     public String getPdpGroup() {
145         return pdpGroup;
146     }
147
148     /**
149      * pdpGroup setter.
150      *
151      * @param pdpGroup the pdpGroup to set
152      */
153     public void setPdpGroup(String pdpGroup) {
154         this.pdpGroup = pdpGroup;
155     }
156
157     /**
158      * Generate the policy name.
159      *
160      * @param policyType        The policy type
161      * @param serviceName       The service name
162      * @param serviceVersion    The service version
163      * @param resourceName      The resource name
164      * @param blueprintFilename The blueprint file name
165      * @return The generated policy name
166      */
167     public static String generatePolicyName(String policyType, String serviceName, String serviceVersion,
168                                             String resourceName, String blueprintFilename) {
169         StringBuilder buffer = new StringBuilder(policyType).append("_").append(serviceName).append("_v")
170                 .append(serviceVersion).append("_").append(resourceName).append("_")
171                 .append(blueprintFilename.replaceAll(".yaml", ""));
172         return buffer.toString().replace('.', '_').replaceAll(" ", "");
173     }
174
175 }