e056339213a47159d46dd5116a7b0248673a4806
[policy/gui.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
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  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.gui.editors.apex.rest;
21
22 import java.util.Optional;
23 import lombok.Getter;
24 import lombok.Setter;
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.policy.common.parameters.GroupValidationResult;
27 import org.onap.policy.common.parameters.ParameterGroup;
28 import org.onap.policy.common.parameters.ValidationStatus;
29 import org.onap.policy.gui.editors.apex.rest.handling.config.PolicyUploadPluginConfigKey;
30
31 @Getter
32 public class UploadPluginConfigParameters implements ParameterGroup {
33
34     public static final String GROUP_NAME = "UploadParameters";
35
36     @Setter
37     private String name;
38     private boolean isEnabled;
39     private String url;
40
41     public UploadPluginConfigParameters() {
42         this.name = GROUP_NAME;
43         initProperties();
44     }
45
46     private void initProperties() {
47         final String isEnabledProperty = System.getProperty(PolicyUploadPluginConfigKey.ENABLE.getKey());
48         isEnabled = Boolean.parseBoolean(isEnabledProperty);
49         url = System.getProperty(PolicyUploadPluginConfigKey.URL.getKey());
50     }
51
52     @Override
53     public GroupValidationResult validate() {
54         final GroupValidationResult result = new GroupValidationResult(this);
55         if (isEnabled && StringUtils.isEmpty(url)) {
56             result.setResult("url", ValidationStatus.INVALID,
57                 String.format("The URL for the upload endpoint must be provided as the java property '%s'",
58                     PolicyUploadPluginConfigKey.URL.getKey()));
59         }
60
61         return result;
62     }
63
64     /**
65      * Gets a property value based on the key and type.
66      *
67      * @param <T> represents the class type
68      * @param key the property key
69      * @return the property value if it exists
70      */
71     public <T> Optional<T> getValue(final PolicyUploadPluginConfigKey key) {
72         final Class<?> type = key.getType();
73         if (key == PolicyUploadPluginConfigKey.URL && type.isInstance(url)) {
74             return (Optional<T>) Optional.of(type.cast(url));
75         }
76         if (key == PolicyUploadPluginConfigKey.ENABLE && type.isInstance(isEnabled)) {
77             return (Optional<T>) Optional.of(type.cast(isEnabled));
78         }
79         return Optional.empty();
80     }
81
82 }