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