0c68c42a11588c22478a8a71e5e35f15e91a9696
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.context.impl.schema.java;
23
24 import com.google.gson.JsonDeserializer;
25 import com.google.gson.JsonSerializer;
26 import com.google.gson.TypeAdapter;
27 import org.onap.policy.common.parameters.GroupValidationResult;
28 import org.onap.policy.common.parameters.ParameterGroup;
29 import org.onap.policy.common.parameters.ValidationStatus;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
32
33 //@formatter:off
34 /**
35  * Event protocol parameters for JSON as an event protocol.
36  *
37  * <p>The parameters for this plugin are:
38  * <ol>
39  * <li>adaptedClass: The name of the class being adapted.
40  * <li>adapterClass: the JSON adapter class to use for the adapted class.
41  * </ol>
42  *
43  * @author Liam Fallon (liam.fallon@ericsson.com)
44  */
45 //@formatter:on
46 public class JavaSchemaHelperJsonAdapterParameters implements ParameterGroup {
47     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaSchemaHelperJsonAdapterParameters.class);
48
49     // Recurring string constants
50     private static final String ADAPTED_CLASS = "adaptedClass";
51     private static final String ADAPTOR_CLASS = "adaptorClass";
52
53     private String adaptedClass;
54     private String adaptorClass;
55
56     /**
57      * {@inheritDoc}.
58      */
59     @Override
60     public String getName() {
61         return getAdaptedClass();
62     }
63
64     /**
65      * {@inheritDoc}.
66      */
67     @Override
68     public void setName(String adaptedClass) {
69         setAdaptedClass(adaptedClass);
70     }
71
72     /**
73      * Gets the adapted class.
74      *
75      * @return the adapted class
76      */
77     public String getAdaptedClass() {
78         return adaptedClass;
79     }
80
81     /**
82      * Gets the adapted class.
83      *
84      * @return the adapted class
85      */
86     public Class<?> getAdaptedClazz() {
87         if (adaptedClass == null) {
88             return null;
89         }
90
91         try {
92             return Class.forName(adaptedClass);
93         } catch (final ClassNotFoundException e) {
94             LOGGER.warn("class \"" + adaptedClass + "\" not found: ", e);
95             return null;
96         }
97     }
98
99     /**
100      * Sets the adapted class.
101      *
102      * @param adaptedClass the new adapted class
103      */
104     public void setAdaptedClass(String adaptedClass) {
105         this.adaptedClass = adaptedClass;
106     }
107
108     /**
109      * Gets the adaptor class.
110      *
111      * @return the adaptor class
112      */
113     public String getAdaptorClass() {
114         return adaptorClass;
115     }
116
117     /**
118      * Gets the adaptor class.
119      *
120      * @return the adaptor class
121      */
122     public Class<?> getAdaptorClazz() {
123         if (adaptorClass == null) {
124             return null;
125         }
126
127         try {
128             return Class.forName(adaptorClass);
129         } catch (final ClassNotFoundException e) {
130             LOGGER.warn("class \"" + adaptorClass + "\" not found: ", e);
131             return null;
132         }
133     }
134
135     /**
136      * Sets the adaptor class.
137      *
138      * @param adaptorClass the new adaptor class
139      */
140     public void setAdaptorClass(String adaptorClass) {
141         this.adaptorClass = adaptorClass;
142     }
143
144     /**
145      * {@inheritDoc}.
146      */
147     @Override
148     public GroupValidationResult validate() {
149         final GroupValidationResult result = new GroupValidationResult(this);
150
151         getClass(ADAPTED_CLASS, adaptedClass, result);
152
153         Class<?> adaptorClazz = getClass(ADAPTOR_CLASS, adaptorClass, result);
154
155         if (adaptorClazz != null) {
156             String errorMessage = null;
157
158             if (TypeAdapter.class.isAssignableFrom(adaptorClazz)) {
159                 return result;
160             }
161
162             if (!JsonSerializer.class.isAssignableFrom(adaptorClazz)) {
163                 errorMessage = "class is not a JsonSerializer";
164             }
165
166             if (!JsonDeserializer.class.isAssignableFrom(adaptorClazz)) {
167                 if (errorMessage == null) {
168                     errorMessage = "class is not a JsonDeserializer";
169                 } else {
170                     errorMessage = "class is not a JsonSerializer or JsonDeserializer";
171                 }
172             }
173
174             if (errorMessage != null) {
175                 result.setResult(ADAPTOR_CLASS, ValidationStatus.INVALID, errorMessage);
176             }
177         }
178
179         return result;
180     }
181
182     /**
183      * Check a class exists.
184      *
185      * @param parameterName the parameter name of the class to check for existence
186      * @param classToCheck the class to check for existence
187      * @param result the result of the check
188      */
189     private Class<?> getClass(String parameterName, String classToCheck, final GroupValidationResult result) {
190         if (classToCheck == null || classToCheck.trim().length() == 0) {
191             result.setResult(parameterName, ValidationStatus.INVALID, "parameter is null or blank");
192             return null;
193         }
194
195         // Get the class for the event protocol
196         try {
197             return Class.forName(classToCheck);
198         } catch (final ClassNotFoundException e) {
199             result.setResult(parameterName, ValidationStatus.INVALID, "class not found: " + e.getMessage());
200             LOGGER.warn("class not found: ", e);
201             return null;
202         }
203     }
204 }