2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 * Modifications Copyright (C) 2019 Bell Canada
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.admin;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.nio.charset.StandardCharsets;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Base64;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.Properties;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
44 import org.onap.policy.rest.XACMLRestProperties;
45 import org.onap.policy.xacml.api.XACMLErrorConstants;
47 import com.att.research.xacml.util.XACMLProperties;
50 * What is not good about this class is that once a value has been set for pdpProperties path you cannot change it. That
51 * may be ok for a highly controlled production environment in which nothing changes, but not a very good
54 * The reset() method has been added to assist with the above problem in order to acquire >80% JUnit code coverage.
56 * This static class doesn't really check a PDP, it simply loads a properties file and tried to ensure that a valid URL
57 * exists for a PDP along with user/password.
59 public class CheckPDP {
61 private static Path pdpPath = null;
62 private static Long oldModified = null;
63 private static HashMap<String, String> pdpMap = null;
64 private static final Logger LOGGER = FlexLogger.getLogger(CheckPDP.class);
70 public static Map<String, String> getPdpMap() {
74 private static void reset() {
80 public static boolean validateID(String id) {
84 } catch (Exception e) {
85 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
92 return pdpMap.containsKey(id);
95 private static void readFile() {
98 pdpFile = XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_IDFILE);
99 } catch (Exception e) {
100 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Cannot read the PDP ID File" + e);
103 if (pdpFile == null) {
104 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PDP File name not Valid : " + pdpFile);
105 } else if (pdpPath == null) {
106 pdpPath = Paths.get(pdpFile);
107 if (!pdpPath.toString().endsWith(".properties") || !pdpPath.toFile().exists()) {
109 XACMLErrorConstants.ERROR_SYSTEM_ERROR + "File doesn't exist in the specified Path : " + pdpPath
115 } else { // Check if File is updated recently
116 Long newModified = pdpPath.toFile().lastModified();
117 if (!newModified.equals(oldModified)) {
118 // File has been updated.
124 @SuppressWarnings({"unchecked", "rawtypes"})
125 private static void readProps() {
127 pdpProp = new Properties();
128 try (InputStream in = new FileInputStream(pdpPath.toFile())) {
129 oldModified = pdpPath.toFile().lastModified();
131 // Read the Properties and Load the PDPs and encoding.
132 pdpMap = new HashMap<>();
133 // Check the Keys for PDP_URLs
134 Collection<Object> unsorted = pdpProp.keySet();
135 List<String> sorted = new ArrayList(unsorted);
136 Collections.sort(sorted);
137 for (String propKey : sorted) {
138 loadPDPProperties(propKey, pdpProp);
140 } catch (IOException e) {
141 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
143 if (pdpMap == null || pdpMap.isEmpty()) {
144 LOGGER.debug(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Cannot Proceed without PDP_URLs");
149 private static void loadPDPProperties(String propKey, Properties pdpProp) {
150 if (propKey.startsWith("PDP_URL")) {
151 String checkVal = pdpProp.getProperty(propKey);
152 if (checkVal == null) {
153 LOGGER.error("Properties file doesn't have the PDP_URL parameter");
155 if (checkVal != null && checkVal.contains(";")) {
156 List<String> pdpDefault = new ArrayList<>(Arrays.asList(checkVal.split("\\s*;\\s*")));
158 while (pdpCount < pdpDefault.size()) {
159 String pdpVal = pdpDefault.get(pdpCount);
160 readPDPParam(pdpVal);
167 private static void readPDPParam(String pdpVal) {
168 if (pdpVal.contains(",")) {
169 List<String> pdpValues = new ArrayList<>(Arrays.asList(pdpVal.split("\\s*,\\s*")));
170 if (pdpValues.size() == 3) {
171 // 1:2 will be UserID:Password
172 String userID = pdpValues.get(1);
173 String pass = pdpValues.get(2);
174 Base64.Encoder encoder = Base64.getEncoder();
176 pdpMap.put(pdpValues.get(0),
177 encoder.encodeToString((userID + ":" + pass).getBytes(StandardCharsets.UTF_8)));
179 LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS + "No Credentials to send Request: " + pdpValues);
182 LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS + "No Credentials to send Request: " + pdpVal);
186 public static String getEncoding(String pdpID) {
189 } catch (Exception e) {
190 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
192 String encoding = null;
193 if (pdpMap != null && (!pdpMap.isEmpty())) {
195 encoding = pdpMap.get(pdpID);
196 } catch (Exception e) {
197 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);