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