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