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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.context.impl.schema.java;
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;
37 * Event protocol parameters for JSON as an event protocol.
39 * <p>The parameters for this plugin are:
41 * <li>adaptedClass: The name of the class being adapted.
42 * <li>adapterClass: the JSON adapter class to use for the adapted class.
45 * @author Liam Fallon (liam.fallon@ericsson.com)
48 public class JavaSchemaHelperJsonAdapterParameters implements ParameterGroup {
49 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaSchemaHelperJsonAdapterParameters.class);
51 // Recurring string constants
52 private static final String ADAPTED_CLASS = "adaptedClass";
53 private static final String ADAPTOR_CLASS = "adaptorClass";
55 private String adaptedClass;
56 private String adaptorClass;
62 public String getName() {
63 return getAdaptedClass();
70 public void setName(String adaptedClass) {
71 setAdaptedClass(adaptedClass);
75 * Gets the adapted class.
77 * @return the adapted class
79 public String getAdaptedClass() {
84 * Gets the adapted class.
86 * @return the adapted class
88 public Class<?> getAdaptedClazz() {
89 if (adaptedClass == null) {
94 return Class.forName(adaptedClass);
95 } catch (final ClassNotFoundException e) {
96 LOGGER.warn("class \"" + adaptedClass + "\" not found: ", e);
102 * Sets the adapted class.
104 * @param adaptedClass the new adapted class
106 public void setAdaptedClass(String adaptedClass) {
107 this.adaptedClass = adaptedClass;
111 * Gets the adaptor class.
113 * @return the adaptor class
115 public String getAdaptorClass() {
120 * Gets the adaptor class.
122 * @return the adaptor class
124 public Class<?> getAdaptorClazz() {
125 if (adaptorClass == null) {
130 return Class.forName(adaptorClass);
131 } catch (final ClassNotFoundException e) {
132 LOGGER.warn("class \"" + adaptorClass + "\" not found: ", e);
138 * Sets the adaptor class.
140 * @param adaptorClass the new adaptor class
142 public void setAdaptorClass(String adaptorClass) {
143 this.adaptorClass = adaptorClass;
150 public BeanValidationResult validate() {
151 final BeanValidationResult result = new BeanValidator().validateTop(getClass().getSimpleName(), this);
153 getClass(ADAPTED_CLASS, adaptedClass, result);
155 Class<?> adaptorClazz = getClass(ADAPTOR_CLASS, adaptorClass, result);
157 if (adaptorClazz != null) {
158 String errorMessage = null;
160 if (TypeAdapter.class.isAssignableFrom(adaptorClazz)) {
164 if (!JsonSerializer.class.isAssignableFrom(adaptorClazz)) {
165 errorMessage = "class is not a JsonSerializer";
168 if (!JsonDeserializer.class.isAssignableFrom(adaptorClazz)) {
169 if (errorMessage == null) {
170 errorMessage = "class is not a JsonDeserializer";
172 errorMessage = "class is not a JsonSerializer or JsonDeserializer";
176 if (errorMessage != null) {
177 result.addResult(ADAPTOR_CLASS, adaptorClazz, ValidationStatus.INVALID, errorMessage);
185 * Check a class exists.
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
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");
197 // Get the class for the event protocol
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);