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.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;
35 * Event protocol parameters for JSON as an event protocol.
37 * <p>The parameters for this plugin are:
39 * <li>adaptedClass: The name of the class being adapted.
40 * <li>adapterClass: the JSON adapter class to use for the adapted class.
43 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class JavaSchemaHelperJsonAdapterParameters implements ParameterGroup {
47 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaSchemaHelperJsonAdapterParameters.class);
49 // Recurring string constants
50 private static final String ADAPTED_CLASS = "adaptedClass";
51 private static final String ADAPTOR_CLASS = "adaptorClass";
53 private String adaptedClass;
54 private String adaptorClass;
60 public String getName() {
61 return getAdaptedClass();
68 public void setName(String adaptedClass) {
69 setAdaptedClass(adaptedClass);
73 * Gets the adapted class.
75 * @return the adapted class
77 public String getAdaptedClass() {
82 * Gets the adapted class.
84 * @return the adapted class
86 public Class<?> getAdaptedClazz() {
87 if (adaptedClass == null) {
92 return Class.forName(adaptedClass);
93 } catch (final ClassNotFoundException e) {
94 LOGGER.warn("class \"" + adaptedClass + "\" not found: ", e);
100 * Sets the adapted class.
102 * @param adaptedClass the new adapted class
104 public void setAdaptedClass(String adaptedClass) {
105 this.adaptedClass = adaptedClass;
109 * Gets the adaptor class.
111 * @return the adaptor class
113 public String getAdaptorClass() {
118 * Gets the adaptor class.
120 * @return the adaptor class
122 public Class<?> getAdaptorClazz() {
123 if (adaptorClass == null) {
128 return Class.forName(adaptorClass);
129 } catch (final ClassNotFoundException e) {
130 LOGGER.warn("class \"" + adaptorClass + "\" not found: ", e);
136 * Sets the adaptor class.
138 * @param adaptorClass the new adaptor class
140 public void setAdaptorClass(String adaptorClass) {
141 this.adaptorClass = adaptorClass;
148 public GroupValidationResult validate() {
149 final GroupValidationResult result = new GroupValidationResult(this);
151 getClass(ADAPTED_CLASS, adaptedClass, result);
153 Class<?> adaptorClazz = getClass(ADAPTOR_CLASS, adaptorClass, result);
155 if (adaptorClazz != null) {
156 String errorMessage = null;
158 if (TypeAdapter.class.isAssignableFrom(adaptorClazz)) {
162 if (!JsonSerializer.class.isAssignableFrom(adaptorClazz)) {
163 errorMessage = "class is not a JsonSerializer";
166 if (!JsonDeserializer.class.isAssignableFrom(adaptorClazz)) {
167 if (errorMessage == null) {
168 errorMessage = "class is not a JsonDeserializer";
170 errorMessage = "class is not a JsonSerializer or JsonDeserializer";
174 if (errorMessage != null) {
175 result.setResult(ADAPTOR_CLASS, ValidationStatus.INVALID, errorMessage);
183 * Check a class exists.
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
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");
195 // Get the class for the event protocol
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);