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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.gui.editors.apex.rest;
22 import java.util.Optional;
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;
32 public class UploadPluginConfigParameters implements ParameterGroup {
34 public static final String GROUP_NAME = "UploadParameters";
37 private boolean isEnabled;
40 public UploadPluginConfigParameters() {
41 this.name = GROUP_NAME;
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());
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()));
64 * Gets a property value based on the key and type.
66 * @param <T> represents the class type
67 * @param key the property key
68 * @return the property value if it exists
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));
75 if (key == PolicyUploadPluginConfigKey.ENABLE && type.isInstance(isEnabled)) {
76 return (Optional<T>) Optional.of(type.cast(isEnabled));
78 return Optional.empty();