6464eeb689edd6d2294bf8da9592c8998d46d838
[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 PssdConfigurationParametersGroup pssdConfiguration;
42     private PluginHandlerParameters pluginHandlerParameters;
43
44     /**
45      * Constructor for instantiating ReceptionHandlerParameters.
46      *
47      * @param receptionHandlerType the reception handler type
48      * @param receptionHandlerClassName the reception handler class name
49      * @param pluginHandlerParameters the plugin handler parameters
50      */
51     public ReceptionHandlerParameters(final String receptionHandlerType, final String receptionHandlerClassName, 
52                                       final PssdConfigurationParametersGroup pssdConfiguration, 
53                                       final PluginHandlerParameters pluginHandlerParameters) {
54         this.receptionHandlerType = receptionHandlerType;
55         this.receptionHandlerClassName = receptionHandlerClassName;
56         this.pssdConfiguration = pssdConfiguration;
57         this.pluginHandlerParameters = pluginHandlerParameters;
58     }
59
60     /**
61      * Return the receptionHandlerType of this ReceptionHandlerParameters instance.
62      *
63      * @return the receptionHandlerType
64      */
65     public String getReceptionHandlerType() {
66         return receptionHandlerType;
67     }
68
69     /**
70      * Return the receptionHandlerClassName of this ReceptionHandlerParameters instance.
71      *
72      * @return the receptionHandlerClassName
73      */
74     public String getReceptionHandlerClassName() {
75         return receptionHandlerClassName;
76     }
77
78     /**
79      * Return the PssdConfigurationParametersGroup of this ReceptionHandlerParameters instance.
80      *
81      * @return the PssdConfigurationParametersGroup
82      */
83     public PssdConfigurationParametersGroup getPssdConfigurationParametersGroup() {
84         return pssdConfiguration;
85     }
86
87     /**
88      * Return the pluginHandlerParameters of this ReceptionHandlerParameters instance.
89      *
90      * @return the pluginHandlerParameters
91      */
92     public PluginHandlerParameters getPluginHandlerParameters() {
93         return pluginHandlerParameters;
94     }
95
96     @Override
97     public String getName() {
98         return name + "_" + receptionHandlerType;
99     }
100
101     /**
102      * Validate the reception handler parameters.
103      *
104      */
105     @Override
106     public GroupValidationResult validate() {
107         final GroupValidationResult validationResult = new GroupValidationResult(this);
108         if (receptionHandlerType == null || receptionHandlerType.trim().length() == 0) {
109             validationResult.setResult("receptionHandlerType", ValidationStatus.INVALID, "must be a non-blank string");
110         }
111         if (receptionHandlerClassName == null || receptionHandlerClassName.trim().length() == 0) {
112             validationResult.setResult("receptionHandlerClassName", ValidationStatus.INVALID,
113                     "must be a non-blank string containing full class name of the reception handler");
114         } else {
115             validateReceptionHandlerClass(validationResult);
116         }
117         if (pluginHandlerParameters == null) {
118             validationResult.setResult("pluginHandlerParameters", ValidationStatus.INVALID,
119                     "must have a plugin handler");
120         } else {
121             validationResult.setResult("pluginHandlerParameters", pluginHandlerParameters.validate());
122         }
123         return validationResult;
124     }
125
126     private void validateReceptionHandlerClass(final GroupValidationResult validationResult) {
127         try {
128             Class.forName(receptionHandlerClassName);
129         } catch (final ClassNotFoundException exp) {
130             LOGGER.error("reception handler class not found in classpath", exp);
131             validationResult.setResult("receptionHandlerClassName", ValidationStatus.INVALID,
132                     "reception handler class not found in classpath");
133         }
134     }
135
136     /**
137      * Set the name of this group.
138      *
139      * @param name the name to set
140      */
141     public void setName(final String name) {
142         this.name = name;
143     }
144 }