Remove legacy operational Policy
[clamp.git] / src / main / java / org / onap / clamp / policy / operational / OperationalPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * Modifications Copyright (C) 2020 Huawei Technologies Co., Ltd.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END============================================
21  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.policy.operational;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.google.gson.JsonObject;
30 import com.google.gson.annotations.Expose;
31 import java.io.Serializable;
32 import java.io.UnsupportedEncodingException;
33 import javax.persistence.Column;
34 import javax.persistence.Entity;
35 import javax.persistence.FetchType;
36 import javax.persistence.Id;
37 import javax.persistence.JoinColumn;
38 import javax.persistence.ManyToOne;
39 import javax.persistence.Table;
40 import javax.persistence.Transient;
41 import org.apache.commons.lang3.RandomStringUtils;
42 import org.hibernate.annotations.TypeDef;
43 import org.hibernate.annotations.TypeDefs;
44 import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport;
45 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
46 import org.onap.clamp.loop.Loop;
47 import org.onap.clamp.loop.service.Service;
48 import org.onap.clamp.loop.template.LoopElementModel;
49 import org.onap.clamp.loop.template.PolicyModel;
50 import org.onap.clamp.policy.Policy;
51
52 @Entity
53 @Table(name = "operational_policies")
54 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
55 public class OperationalPolicy extends Policy implements Serializable {
56     /**
57      * The serial version ID.
58      */
59     private static final long serialVersionUID = 6117076450841538255L;
60
61     @Transient
62     private static final EELFLogger logger = EELFManager.getInstance().getLogger(OperationalPolicy.class);
63
64     @Id
65     @Expose
66     @Column(nullable = false, name = "name", unique = true)
67     private String name;
68
69     @ManyToOne(fetch = FetchType.LAZY)
70     @JoinColumn(name = "loop_id", nullable = false)
71     private Loop loop;
72
73     /**
74      * Constructor for serialization.
75      */
76     public OperationalPolicy() {
77     }
78
79     /**
80      * The constructor.
81      *
82      * @param name               The name of the operational policy
83      * @param configurationsJson The operational policy property in the format of
84      *                           json
85      * @param jsonRepresentation The jsonObject defining the json schema
86      * @param policyModel        The policy model associated if any, can be null
87      * @param loopElementModel   The loop element from which this instance is supposed to be created
88      * @param pdpGroup           The Pdp Group info
89      * @param pdpSubgroup        The Pdp Subgroup info
90      */
91     public OperationalPolicy(String name, JsonObject configurationsJson,
92                              JsonObject jsonRepresentation, PolicyModel policyModel,
93                              LoopElementModel loopElementModel, String pdpGroup, String pdpSubgroup) {
94         this.name = name;
95         this.setPolicyModel(policyModel);
96         this.setConfigurationsJson(configurationsJson);
97         this.setPdpGroup(pdpGroup);
98         this.setPdpSubgroup(pdpSubgroup);
99         this.setLoopElementModel(loopElementModel);
100         this.setJsonRepresentation(jsonRepresentation);
101
102     }
103
104     /**
105      * Create an operational policy from a loop element model.
106      *
107      * @param loop             The parent loop
108      * @param service          The loop service
109      * @param loopElementModel The loop element model
110      * @param toscaConverter   The tosca converter that must be used to create the Json representation
111      */
112     public OperationalPolicy(Loop loop, Service service, LoopElementModel loopElementModel,
113                              ToscaConverterWithDictionarySupport toscaConverter) {
114         this(Policy.generatePolicyName("OPERATIONAL", service.getName(), service.getVersion(),
115                 loopElementModel.getPolicyModels().first().getPolicyAcronym() + '_'
116                         + loopElementModel.getPolicyModels().first().getVersion(),
117                 RandomStringUtils.randomAlphanumeric(3)), new JsonObject(),
118                 new JsonObject(), loopElementModel.getPolicyModels().first(), loopElementModel, null, null);
119         this.setLoop(loop);
120         this.updateJsonRepresentation(toscaConverter, service);
121     }
122
123     /**
124      * Create an operational policy from a policy model.
125      *
126      * @param loop           The parent loop
127      * @param service        The loop service
128      * @param policyModel    The policy model
129      * @param toscaConverter The tosca converter that must be used to create the Json representation
130      */
131     public OperationalPolicy(Loop loop, Service service, PolicyModel policyModel,
132                              ToscaConverterWithDictionarySupport toscaConverter) {
133         this(Policy.generatePolicyName("OPERATIONAL", service.getName(), service.getVersion(),
134                 policyModel.getPolicyAcronym() + '_' + policyModel.getVersion(),
135                 RandomStringUtils.randomAlphanumeric(3)),
136                 new JsonObject(),
137                 new JsonObject(), policyModel, null, null, null);
138         this.setLoop(loop);
139         this.updateJsonRepresentation(toscaConverter, service);
140     }
141
142     public void setLoop(Loop loopName) {
143         this.loop = loopName;
144     }
145
146     public Loop getLoop() {
147         return loop;
148     }
149
150     @Override
151     public String getName() {
152         return name;
153     }
154
155     /**
156      * name setter.
157      *
158      * @param name the name to set
159      */
160     @Override
161     public void setName(String name) {
162         this.name = name;
163     }
164
165     @Override
166     public void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter, Service serviceModel) {
167         {
168             this.setJsonRepresentation(new JsonObject());
169             if (this.getPolicyModel() == null) {
170                 return;
171             }
172
173             // Generic Case
174             this.setJsonRepresentation(toscaConverter.convertToscaToJsonSchemaObject(
175                     this.getPolicyModel().getPolicyModelTosca(),
176                     this.getPolicyModel().getPolicyModelType(), serviceModel));
177
178         }
179     }
180
181     @Override
182     public int hashCode() {
183         final int prime = 31;
184         int result = 1;
185         result = prime * result + ((name == null) ? 0 : name.hashCode());
186         return result;
187     }
188
189     @Override
190     public boolean equals(Object obj) {
191         if (this == obj) {
192             return true;
193         }
194         if (obj == null) {
195             return false;
196         }
197         if (getClass() != obj.getClass()) {
198             return false;
199         }
200         OperationalPolicy other = (OperationalPolicy) obj;
201         if (name == null) {
202             if (other.name != null) {
203                 return false;
204             }
205         } else {
206             if (!name.equals(other.name)) {
207                 return false;
208             }
209         }
210         return true;
211     }
212
213     @Override
214     public String createPolicyPayload() throws UnsupportedEncodingException {
215         return super.createPolicyPayload();
216
217     }
218 }