e330af7f66f4a9f4e4fbe4881167e95d92077e33
[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     @Setter
36     private String name;
37     private boolean isEnabled;
38     private String url;
39
40     public UploadPluginConfigParameters() {
41         this.name = GROUP_NAME;
42         initProperties();
43     }
44
45     private void initProperties() {
46         final String isEnabledProperty = System.getProperty(PolicyUploadPluginConfigKey.ENABLE.getKey());
47         isEnabled = Boolean.parseBoolean(isEnabledProperty);
48         url = System.getProperty(PolicyUploadPluginConfigKey.URL.getKey());
49     }
50
51     @Override
52     public GroupValidationResult validate() {
53         final GroupValidationResult result = new GroupValidationResult(this);
54         if (isEnabled && StringUtils.isEmpty(url)) {
55             result.setResult("url", ValidationStatus.INVALID,
56                 String.format("The URL for the upload endpoint must be provided as the java property '%s'",
57                     PolicyUploadPluginConfigKey.URL.getKey()));
58         }
59
60         return result;
61     }
62
63     /**
64      * Gets a property value based on the key and type.
65      *
66      * @param <T> represents the class type
67      * @param key the property key
68      * @return the property value if it exists
69      */
70     public <T> Optional<T> getValue(final PolicyUploadPluginConfigKey key) {
71         final Class<?> type = key.getType();
72         if (key == PolicyUploadPluginConfigKey.URL && type.isInstance(url)) {
73             return (Optional<T>) Optional.of(type.cast(url));
74         }
75         if (key == PolicyUploadPluginConfigKey.ENABLE && type.isInstance(isEnabled)) {
76             return (Optional<T>) Optional.of(type.cast(isEnabled));
77         }
78         return Optional.empty();
79     }
80
81 }