f904581600b3451cc84a6e9623ef6825f2279f1d
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Intel. 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.handling.file;
22
23 import java.io.File;
24
25 import org.onap.policy.common.parameters.GroupValidationResult;
26 import org.onap.policy.common.parameters.ValidationStatus;
27 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
28 import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParameterGroup;
29
30 /**
31  * This class handles reading, parsing and validating of the Policy SDC Service Distribution parameters from Json
32  * format, which strictly adheres to the interface:IConfiguration, defined by SDC SDK.
33  */
34 public class FileSystemReceptionHandlerConfigurationParameterGroup extends ReceptionHandlerConfigurationParameterGroup {
35
36     private String watchPath;
37     private int maxThread;
38
39     /**
40      * The constructor for instantiating {@link FileSystemReceptionHandlerConfigurationParameterGroup} class.
41      *
42      * @param builder the SDC configuration builder
43      */
44     public FileSystemReceptionHandlerConfigurationParameterGroup(
45             final FileSystemReceptionHandlerConfigurationParameterBuilder builder) {
46         watchPath = builder.getWatchPath();
47         maxThread = builder.getMaxThread();
48     }
49
50     public String getWatchPath() {
51         return watchPath;
52     }
53
54     public int getMaxThread() {
55         return maxThread;
56     }
57
58     /**
59      * {@inheritDoc}.
60      */
61     @Override
62     public GroupValidationResult validate() {
63         final GroupValidationResult validationResult = new GroupValidationResult(this);
64         validatePathElement(validationResult, watchPath, "watchPath");
65         if (!ParameterValidationUtils.validateIntParameter(maxThread)) {
66             validationResult.setResult("maxThread", ValidationStatus.INVALID, "must be a positive integer");
67         }
68         return validationResult;
69     }
70
71
72     /**
73      * Validate the string element.
74      *
75      * @param validationResult the result object
76      * @param element the element to validate
77      * @param elementName the element name for error message
78      */
79     private void validatePathElement(final GroupValidationResult validationResult, final String element,
80             final String elementName) {
81         boolean valid = false;
82         if (element != null) {
83             File file = new File(element);
84             if (file.exists() && file.isDirectory()) {
85                 valid = true;
86             }
87         }
88         if (!valid) {
89             validationResult.setResult(elementName, ValidationStatus.INVALID,
90                     elementName + " must be a valid directory");
91         }
92     }
93 }
94