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