2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
25 package org.onap.clamp.policy.operational;
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;
53 @Table(name = "operational_policies")
54 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
55 public class OperationalPolicy extends Policy implements Serializable {
57 * The serial version ID.
59 private static final long serialVersionUID = 6117076450841538255L;
62 private static final EELFLogger logger = EELFManager.getInstance().getLogger(OperationalPolicy.class);
66 @Column(nullable = false, name = "name", unique = true)
69 @ManyToOne(fetch = FetchType.LAZY)
70 @JoinColumn(name = "loop_id", nullable = false)
74 * Constructor for serialization.
76 public OperationalPolicy() {
82 * @param name The name of the operational policy
83 * @param configurationsJson The operational policy property in the format of
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
91 public OperationalPolicy(String name, JsonObject configurationsJson,
92 JsonObject jsonRepresentation, PolicyModel policyModel,
93 LoopElementModel loopElementModel, String pdpGroup, String pdpSubgroup) {
95 this.setPolicyModel(policyModel);
96 this.setConfigurationsJson(configurationsJson);
97 this.setPdpGroup(pdpGroup);
98 this.setPdpSubgroup(pdpSubgroup);
99 this.setLoopElementModel(loopElementModel);
100 this.setJsonRepresentation(jsonRepresentation);
105 * Create an operational policy from a loop element model.
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
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);
120 this.updateJsonRepresentation(toscaConverter, service);
124 * Create an operational policy from a policy model.
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
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)),
137 new JsonObject(), policyModel, null, null, null);
139 this.updateJsonRepresentation(toscaConverter, service);
142 public void setLoop(Loop loopName) {
143 this.loop = loopName;
146 public Loop getLoop() {
151 public String getName() {
158 * @param name the name to set
161 public void setName(String name) {
166 public void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter, Service serviceModel) {
168 this.setJsonRepresentation(new JsonObject());
169 if (this.getPolicyModel() == null) {
174 this.setJsonRepresentation(toscaConverter.convertToscaToJsonSchemaObject(
175 this.getPolicyModel().getPolicyModelTosca(),
176 this.getPolicyModel().getPolicyModelType(), serviceModel));
182 public int hashCode() {
183 final int prime = 31;
185 result = prime * result + ((name == null) ? 0 : name.hashCode());
190 public boolean equals(Object obj) {
197 if (getClass() != obj.getClass()) {
200 OperationalPolicy other = (OperationalPolicy) obj;
202 if (other.name != null) {
206 if (!name.equals(other.name)) {
214 public String createPolicyPayload() throws UnsupportedEncodingException {
215 return super.createPolicyPayload();