New listPolicy API to check pushed policies in PDP
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / ListPolicyService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pdp.rest.api.services;
22
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;
31 import java.util.Set;
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;
39
40
41 public class ListPolicyService {
42
43     private static Logger logger = FlexLogger.getLogger(ListPolicyService.class.getName());
44
45     private Collection<String> results = null;
46     private HttpStatus status = HttpStatus.BAD_REQUEST;
47     private ConfigNameRequest configRequestParameters = null;
48     private String requestId = null;
49
50
51     public ListPolicyService() {
52         // Default Constructor
53     }
54
55     /**
56      * Instantiates a new list policy service.
57      *
58      * @param configRequestParameters the config request parameters
59      */
60     public ListPolicyService(final ConfigNameRequest configRequestParameters) {
61         requestId = UUID.randomUUID().toString();
62         this.configRequestParameters = configRequestParameters;
63
64         try {
65             run();
66         } catch (Exception e) {
67             logger.warn("ListPolicy - ERROR for request - " + requestId + ", " + e);
68             status = HttpStatus.BAD_REQUEST;
69             results = null;
70         }
71
72         logger.info("Result for listPolicy - " + configRequestParameters + ", for request - " + requestId
73                 + " - Response - " + results);
74     }
75
76     private void run() throws PAPException, IOException {
77         Properties currentProperties = new Properties();
78         try (InputStream is = Files.newInputStream(XACMLPdpLoader.getPDPPolicyCache())) {
79             currentProperties.load(is);
80         }
81
82         Set<String> listOfPolicies = XACMLProperties.getRootPolicyIDs(currentProperties);
83         results = filterList(listOfPolicies, configRequestParameters.getPolicyName());
84         status = HttpStatus.OK;
85
86     }
87
88     private List<String> filterList(Set<String> list, String regex) {
89         return list.stream().filter(s -> s.matches(regex)).collect(Collectors.toList());
90     }
91
92     public Collection<String> getResult() {
93         return results;
94     }
95
96     public HttpStatus getResponseCode() {
97         return status;
98     }
99
100 }