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