ea6d8d86d62ee2293b5de178471a89f8f2ca0afb
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Copyright (C) 2019 Nordix Foundation.
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.distribution.reception.parameters;
23
24 import org.onap.policy.common.parameters.GroupValidationResult;
25 import org.onap.policy.common.parameters.ParameterGroup;
26 import org.onap.policy.common.parameters.ValidationStatus;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Class to hold all the reception handler parameters.
32  *
33  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
34  */
35 public class ReceptionHandlerParameters implements ParameterGroup {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(ReceptionHandlerParameters.class);
38
39     private String name;
40     private String receptionHandlerType;
41     private String receptionHandlerClassName;
42     private String receptionHandlerConfigurationName;
43     private PluginHandlerParameters pluginHandlerParameters;
44
45     /**
46      * Constructor for instantiating ReceptionHandlerParameters.
47      *
48      * @param receptionHandlerType the reception handler type
49      * @param receptionHandlerClassName the reception handler class name
50      * @param receptionHandlerConfigurationName the name of the configuration for the reception handler
51      * @param pluginHandlerParameters the plugin handler parameters
52      */
53     public ReceptionHandlerParameters(final String receptionHandlerType, final String receptionHandlerClassName,
54             final String receptionHandlerConfigurationName, final PluginHandlerParameters pluginHandlerParameters) {
55         this.receptionHandlerType = receptionHandlerType;
56         this.receptionHandlerClassName = receptionHandlerClassName;
57         this.receptionHandlerConfigurationName = receptionHandlerConfigurationName;
58         this.pluginHandlerParameters = pluginHandlerParameters;
59     }
60
61     /**
62      * Return the receptionHandlerType of this ReceptionHandlerParameters instance.
63      *
64      * @return the receptionHandlerType
65      */
66     public String getReceptionHandlerType() {
67         return receptionHandlerType;
68     }
69
70     /**
71      * Return the receptionHandlerClassName of this ReceptionHandlerParameters instance.
72      *
73      * @return the receptionHandlerClassName
74      */
75     public String getReceptionHandlerClassName() {
76         return receptionHandlerClassName;
77     }
78
79     /**
80      * Return the name of the reception handler configuration for this ReceptionHandlerParameters instance.
81      *
82      * @return the PssdConfigurationParametersGroup
83      */
84     public String getReceptionHandlerConfigurationName() {
85         return receptionHandlerConfigurationName;
86     }
87
88     /**
89      * Return the pluginHandlerParameters of this ReceptionHandlerParameters instance.
90      *
91      * @return the pluginHandlerParameters
92      */
93     public PluginHandlerParameters getPluginHandlerParameters() {
94         return pluginHandlerParameters;
95     }
96
97     @Override
98     public String getName() {
99         return name + "_" + receptionHandlerType;
100     }
101
102     /**
103      * Validate the reception handler parameters.
104      *
105      */
106     @Override
107     public GroupValidationResult validate() {
108         final GroupValidationResult validationResult = new GroupValidationResult(this);
109         if (receptionHandlerType == null || receptionHandlerType.trim().length() == 0) {
110             validationResult.setResult("receptionHandlerType", ValidationStatus.INVALID, "must be a non-blank string");
111         }
112         if (receptionHandlerClassName == null || receptionHandlerClassName.trim().length() == 0) {
113             validationResult.setResult("receptionHandlerClassName", ValidationStatus.INVALID,
114                     "must be a non-blank string containing full class name of the reception handler");
115         } else {
116             validateReceptionHandlerClass(validationResult);
117         }
118         if (pluginHandlerParameters == null) {
119             validationResult.setResult("pluginHandlerParameters", ValidationStatus.INVALID,
120                     "must have a plugin handler");
121         } else {
122             validationResult.setResult("pluginHandlerParameters", pluginHandlerParameters.validate());
123         }
124         return validationResult;
125     }
126
127     private void validateReceptionHandlerClass(final GroupValidationResult validationResult) {
128         try {
129             Class.forName(receptionHandlerClassName);
130         } catch (final ClassNotFoundException exp) {
131             LOGGER.error("reception handler class not found in classpath", exp);
132             validationResult.setResult("receptionHandlerClassName", ValidationStatus.INVALID,
133                     "reception handler class not found in classpath");
134         }
135     }
136
137     /**
138      * Set the name of this group.
139      *
140      * @param name the name to set
141      */
142     @Override
143     public void setName(final String name) {
144         this.name = name;
145     }
146 }