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