76af06e536a05cc14f492d46f326abcf471fb662
[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.handling;
21
22 import java.util.Map;
23 import java.util.stream.Collectors;
24 import java.util.stream.Stream;
25 import javax.ws.rs.Consumes;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.Path;
28 import javax.ws.rs.Produces;
29 import javax.ws.rs.core.MediaType;
30 import org.onap.policy.apex.model.modelapi.ApexApiResult;
31 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
32 import org.onap.policy.common.parameters.ParameterService;
33 import org.onap.policy.common.utils.coder.CoderException;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.gui.editors.apex.rest.UploadPluginConfigParameters;
36 import org.onap.policy.gui.editors.apex.rest.handling.config.PolicyUploadPluginConfigKey;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Handles endpoints for the configuration properties.
42  */
43 @Path("editor/config")
44 @Produces({MediaType.APPLICATION_JSON})
45 @Consumes({MediaType.APPLICATION_JSON})
46 public class ConfigurationRestResource {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationRestResource.class);
49     private static final StandardCoder STANDARD_CODER = new StandardCoder();
50
51     private final UploadPluginConfigParameters uploadConfigParam;
52
53     public ConfigurationRestResource() {
54         uploadConfigParam = ParameterService.get(UploadPluginConfigParameters.GROUP_NAME);
55     }
56
57     /**
58      * Gets the configured properties.
59      *
60      * @return the properties as JSON in the ApexApiResult messages list.
61      */
62     @GET
63     @Path("")
64     public ApexApiResult show() {
65         final ApexApiResult result = new ApexApiResult(Result.SUCCESS);
66
67         final Map<String, Object> configMap = Stream.of(PolicyUploadPluginConfigKey.values())
68             .filter(key -> uploadConfigParam.getValue(key).isPresent())
69             .collect(Collectors.toMap(PolicyUploadPluginConfigKey::getKey,
70                 configKey -> uploadConfigParam.getValue(configKey).get()));
71         try {
72             final String encode = STANDARD_CODER.encode(configMap);
73             result.addMessage(encode);
74         } catch (final CoderException e) {
75             result.setResult(Result.FAILED);
76             final String errorMsg = "Could not parse configuration parameters as JSON";
77             result.addMessage(errorMsg);
78             LOGGER.error(errorMsg, e);
79         }
80         return result;
81     }
82
83 }