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