974436aab06ab2cece144fa10ed2cc6649cce9f3
[policy/distribution.git] /
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.distribution.reception.parameters;
22
23 import org.onap.policy.common.logging.flexlogger.FlexLogger;
24 import org.onap.policy.common.logging.flexlogger.Logger;
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
29 /**
30  * Class to hold all the reception handler parameters.
31  *
32  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
33  */
34 public class ReceptionHandlerParameters implements ParameterGroup {
35
36     private static final Logger LOGGER = FlexLogger.getLogger(ReceptionHandlerParameters.class);
37
38     private String name;
39     private String receptionHandlerType;
40     private String receptionHandlerClassName;
41     private PluginHandlerParameters pluginHandlerParameters;
42
43     /**
44      * Constructor for instantiating ReceptionHandlerParameters.
45      *
46      * @param receptionHandlerType the reception handler type
47      * @param receptionHandlerClassName the reception handler class name
48      * @param pluginHandlerParameters the plugin handler parameters
49      */
50     public ReceptionHandlerParameters(final String receptionHandlerType, final String receptionHandlerClassName,
51             final PluginHandlerParameters pluginHandlerParameters) {
52         this.receptionHandlerType = receptionHandlerType;
53         this.receptionHandlerClassName = receptionHandlerClassName;
54         this.pluginHandlerParameters = pluginHandlerParameters;
55     }
56
57     /**
58      * Return the receptionHandlerType of this ReceptionHandlerParameters instance.
59      *
60      * @return the receptionHandlerType
61      */
62     public String getReceptionHandlerType() {
63         return receptionHandlerType;
64     }
65
66     /**
67      * Return the receptionHandlerClassName of this ReceptionHandlerParameters instance.
68      *
69      * @return the receptionHandlerClassName
70      */
71     public String getReceptionHandlerClassName() {
72         return receptionHandlerClassName;
73     }
74
75     /**
76      * Return the pluginHandlerParameters of this ReceptionHandlerParameters instance.
77      *
78      * @return the pluginHandlerParameters
79      */
80     public PluginHandlerParameters getPluginHandlerParameters() {
81         return pluginHandlerParameters;
82     }
83
84     @Override
85     public String getName() {
86         return name + "_" + receptionHandlerType;
87     }
88
89     /**
90      * Validate the reception handler parameters.
91      *
92      */
93     @Override
94     public GroupValidationResult validate() {
95         final GroupValidationResult validationResult = new GroupValidationResult(this);
96         if (receptionHandlerType == null || receptionHandlerType.trim().length() == 0) {
97             validationResult.setResult("receptionHandlerType", ValidationStatus.INVALID, "must be a non-blank string");
98         }
99         if (receptionHandlerClassName == null || receptionHandlerClassName.trim().length() == 0) {
100             validationResult.setResult("receptionHandlerClassName", ValidationStatus.INVALID,
101                     "must be a non-blank string containing full class name of the reception handler");
102         } else {
103             validateReceptionHandlerClass(validationResult);
104         }
105         if (pluginHandlerParameters == null) {
106             validationResult.setResult("pluginHandlerParameters", ValidationStatus.INVALID,
107                     "must have a plugin handler");
108         } else {
109             validationResult.setResult("pluginHandlerParameters", pluginHandlerParameters.validate());
110         }
111         return validationResult;
112     }
113
114     private void validateReceptionHandlerClass(final GroupValidationResult validationResult) {
115         try {
116             Class.forName(receptionHandlerClassName);
117         } catch (final ClassNotFoundException exp) {
118             LOGGER.error("reception handler class not found in classpath", exp);
119             validationResult.setResult("receptionHandlerClassName", ValidationStatus.INVALID,
120                     "reception handler class not found in classpath");
121         }
122     }
123
124     /**
125      * Set the name of this group.
126      *
127      * @param name the name to set
128      */
129     public void setName(final String name) {
130         this.name = name;
131     }
132 }