57d04248403e802c2c1500c32d7e5681a8fe0da3
[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.forwarding.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 policy forwarder parameters.
31  *
32  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
33  */
34 public class PolicyForwarderParameters implements ParameterGroup {
35
36     private static final Logger LOGGER = FlexLogger.getLogger(PolicyForwarderParameters.class);
37
38     private String forwarderType;
39     private String forwarderClassName;
40
41     /**
42      * Constructor for instantiating PolicyForwarderParameters.
43      *
44      * @param forwarderType the policy forwarder type
45      * @param forwarderClassName the policy forwarder class name
46      */
47     public PolicyForwarderParameters(final String forwarderType, final String forwarderClassName) {
48         this.forwarderType = forwarderType;
49         this.forwarderClassName = forwarderClassName;
50     }
51
52     /**
53      * Return the forwarderType of this PolicyForwarderParameters instance.
54      *
55      * @return the forwarderType
56      */
57     public String getForwarderType() {
58         return forwarderType;
59     }
60
61     /**
62      * Return the forwarderClassName of this PolicyForwarderParameters instance.
63      *
64      * @return the forwarderClassName
65      */
66     public String getForwarderClassName() {
67         return forwarderClassName;
68     }
69
70     @Override
71     public String getName() {
72         return null;
73     }
74
75     /**
76      * Validate the policy forwarder parameters.
77      *
78      */
79     @Override
80     public GroupValidationResult validate() {
81         final GroupValidationResult validationResult = new GroupValidationResult(this);
82         if (forwarderType == null || forwarderType.trim().length() == 0) {
83             validationResult.setResult("forwarderType", ValidationStatus.INVALID, "must be a non-blank string");
84         }
85         if (forwarderClassName == null || forwarderClassName.trim().length() == 0) {
86             validationResult.setResult("forwarderClassName", ValidationStatus.INVALID,
87                     "must be a non-blank string containing full class name of the forwarder");
88         } else {
89             validatePolicyForwarderClass(validationResult);
90         }
91         return validationResult;
92     }
93
94     private void validatePolicyForwarderClass(final GroupValidationResult validationResult) {
95         try {
96             Class.forName(forwarderClassName);
97         } catch (final ClassNotFoundException exp) {
98             LOGGER.error("policy forwarder class not found in classpath", exp);
99             validationResult.setResult("forwarderClassName", ValidationStatus.INVALID,
100                     "policy forwarder class not found in classpath");
101         }
102     }
103 }