2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdp.rest.api.services;
23 import com.att.research.xacml.api.pap.PAPException;
24 import com.att.research.xacml.util.XACMLProperties;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.nio.file.Files;
28 import java.util.Collection;
29 import java.util.List;
30 import java.util.Properties;
32 import java.util.UUID;
33 import java.util.stream.Collectors;
34 import org.onap.policy.api.ConfigNameRequest;
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.pdp.rest.XACMLPdpLoader;
38 import org.springframework.http.HttpStatus;
41 public class ListPolicyService {
43 private static Logger logger = FlexLogger.getLogger(ListPolicyService.class.getName());
45 private Collection<String> results = null;
46 private HttpStatus status = HttpStatus.BAD_REQUEST;
47 private ConfigNameRequest configRequestParameters = null;
48 private String requestId = null;
51 public ListPolicyService() {
52 // Default Constructor
56 * Instantiates a new list policy service.
58 * @param configRequestParameters the config request parameters
60 public ListPolicyService(final ConfigNameRequest configRequestParameters) {
61 requestId = UUID.randomUUID().toString();
62 this.configRequestParameters = configRequestParameters;
66 } catch (Exception e) {
67 logger.warn("ListPolicy - ERROR for request - " + requestId + ", " + e);
68 status = HttpStatus.BAD_REQUEST;
72 logger.info("Result for listPolicy - " + configRequestParameters + ", for request - " + requestId
73 + " - Response - " + results);
76 private void run() throws PAPException, IOException {
77 Properties currentProperties = new Properties();
78 try (InputStream is = Files.newInputStream(XACMLPdpLoader.getPDPPolicyCache())) {
79 currentProperties.load(is);
82 Set<String> listOfPolicies = XACMLProperties.getRootPolicyIDs(currentProperties);
83 results = filterList(listOfPolicies, configRequestParameters.getPolicyName());
84 status = HttpStatus.OK;
88 private List<String> filterList(Set<String> list, String regex) {
89 return list.stream().filter(s -> s.matches(regex)).collect(Collectors.toList());
92 public Collection<String> getResult() {
96 public HttpStatus getResponseCode() {