Remove GroupValidationResult
[policy/apex-pdp.git] / context / context-management / src / main / java / org / onap / policy / apex / context / impl / schema / java / JavaSchemaHelperJsonAdapterParameters.java
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.apache.commons.lang3.StringUtils;
28 import org.onap.policy.common.parameters.BeanValidationResult;
29 import org.onap.policy.common.parameters.BeanValidator;
30 import org.onap.policy.common.parameters.ParameterGroup;
31 import org.onap.policy.common.parameters.ValidationStatus;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
34
35 //@formatter:off
36 /**
37  * Event protocol parameters for JSON as an event protocol.
38  *
39  * <p>The parameters for this plugin are:
40  * <ol>
41  * <li>adaptedClass: The name of the class being adapted.
42  * <li>adapterClass: the JSON adapter class to use for the adapted class.
43  * </ol>
44  *
45  * @author Liam Fallon (liam.fallon@ericsson.com)
46  */
47 //@formatter:on
48 public class JavaSchemaHelperJsonAdapterParameters implements ParameterGroup {
49     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaSchemaHelperJsonAdapterParameters.class);
50
51     // Recurring string constants
52     private static final String ADAPTED_CLASS = "adaptedClass";
53     private static final String ADAPTOR_CLASS = "adaptorClass";
54
55     private String adaptedClass;
56     private String adaptorClass;
57
58     /**
59      * {@inheritDoc}.
60      */
61     @Override
62     public String getName() {
63         return getAdaptedClass();
64     }
65
66     /**
67      * {@inheritDoc}.
68      */
69     @Override
70     public void setName(String adaptedClass) {
71         setAdaptedClass(adaptedClass);
72     }
73
74     /**
75      * Gets the adapted class.
76      *
77      * @return the adapted class
78      */
79     public String getAdaptedClass() {
80         return adaptedClass;
81     }
82
83     /**
84      * Gets the adapted class.
85      *
86      * @return the adapted class
87      */
88     public Class<?> getAdaptedClazz() {
89         if (adaptedClass == null) {
90             return null;
91         }
92
93         try {
94             return Class.forName(adaptedClass);
95         } catch (final ClassNotFoundException e) {
96             LOGGER.warn("class \"" + adaptedClass + "\" not found: ", e);
97             return null;
98         }
99     }
100
101     /**
102      * Sets the adapted class.
103      *
104      * @param adaptedClass the new adapted class
105      */
106     public void setAdaptedClass(String adaptedClass) {
107         this.adaptedClass = adaptedClass;
108     }
109
110     /**
111      * Gets the adaptor class.
112      *
113      * @return the adaptor class
114      */
115     public String getAdaptorClass() {
116         return adaptorClass;
117     }
118
119     /**
120      * Gets the adaptor class.
121      *
122      * @return the adaptor class
123      */
124     public Class<?> getAdaptorClazz() {
125         if (adaptorClass == null) {
126             return null;
127         }
128
129         try {
130             return Class.forName(adaptorClass);
131         } catch (final ClassNotFoundException e) {
132             LOGGER.warn("class \"" + adaptorClass + "\" not found: ", e);
133             return null;
134         }
135     }
136
137     /**
138      * Sets the adaptor class.
139      *
140      * @param adaptorClass the new adaptor class
141      */
142     public void setAdaptorClass(String adaptorClass) {
143         this.adaptorClass = adaptorClass;
144     }
145
146     /**
147      * {@inheritDoc}.
148      */
149     @Override
150     public BeanValidationResult validate() {
151         final BeanValidationResult result = new BeanValidator().validateTop(getClass().getSimpleName(), this);
152
153         getClass(ADAPTED_CLASS, adaptedClass, result);
154
155         Class<?> adaptorClazz = getClass(ADAPTOR_CLASS, adaptorClass, result);
156
157         if (adaptorClazz != null) {
158             String errorMessage = null;
159
160             if (TypeAdapter.class.isAssignableFrom(adaptorClazz)) {
161                 return result;
162             }
163
164             if (!JsonSerializer.class.isAssignableFrom(adaptorClazz)) {
165                 errorMessage = "class is not a JsonSerializer";
166             }
167
168             if (!JsonDeserializer.class.isAssignableFrom(adaptorClazz)) {
169                 if (errorMessage == null) {
170                     errorMessage = "class is not a JsonDeserializer";
171                 } else {
172                     errorMessage = "class is not a JsonSerializer or JsonDeserializer";
173                 }
174             }
175
176             if (errorMessage != null) {
177                 result.addResult(ADAPTOR_CLASS, adaptorClazz, ValidationStatus.INVALID, errorMessage);
178             }
179         }
180
181         return result;
182     }
183
184     /**
185      * Check a class exists.
186      *
187      * @param parameterName the parameter name of the class to check for existence
188      * @param classToCheck the class to check for existence
189      * @param result the result of the check
190      */
191     private Class<?> getClass(String parameterName, String classToCheck, final BeanValidationResult result) {
192         if (StringUtils.isBlank(classToCheck)) {
193             result.addResult(parameterName, classToCheck, ValidationStatus.INVALID, "parameter is null or blank");
194             return null;
195         }
196
197         // Get the class for the event protocol
198         try {
199             return Class.forName(classToCheck);
200         } catch (final ClassNotFoundException e) {
201             result.addResult(parameterName, classToCheck, ValidationStatus.INVALID,
202                             "class not found: " + e.getMessage());
203             LOGGER.warn("class not found: ", e);
204             return null;
205         }
206     }
207 }