Addressing Technical Debt for POLICY-SDK-APP
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / admin / CheckPDP.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 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.admin;
22
23
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Base64;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Properties;
39
40 import org.onap.policy.common.logging.flexlogger.FlexLogger;
41 import org.onap.policy.common.logging.flexlogger.Logger;
42 import org.onap.policy.rest.XACMLRestProperties;
43 import org.onap.policy.xacml.api.XACMLErrorConstants;
44
45 import com.att.research.xacml.util.XACMLProperties;
46
47 public class CheckPDP {
48         private static Path pdpPath = null;
49         private static Long oldModified = null;
50         private static HashMap<String, String> pdpMap = null;
51         private static final Logger LOGGER = FlexLogger.getLogger(CheckPDP.class);
52         
53         private CheckPDP(){
54                 //default constructor
55         }
56         
57         public static Map<String, String> getPdpMap() {
58                 return pdpMap;
59         }
60
61         public static boolean validateID(String id) {
62                 // ReadFile
63                 try {
64                         readFile();
65                 } catch (Exception e) {
66                         LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
67                         return false;
68                 }
69                 // Check ID
70                 return pdpMap.containsKey(id);
71         }
72
73         private static void readFile(){
74                 String pdpFile = null;
75                 try{
76                         pdpFile = XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_IDFILE);     
77                 }catch (Exception e){
78                         LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Cannot read the PDP ID File" + e);
79                         return;
80                 }
81                 if (pdpFile == null) {
82                         LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PDP File name not Valid : " + pdpFile);
83                 }
84                 if (pdpPath == null) {
85                         pdpPath = Paths.get(pdpFile);
86                         if (!pdpPath.toFile().exists()) {
87                                 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "File doesn't exist in the specified Path : "     + pdpPath.toString());
88
89                         } 
90                         if (pdpPath.toString().endsWith(".properties")) {
91                                 readProps();
92                         } else {
93                                 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Not a .properties file " + pdpFile);
94                         }
95                 }
96                 // Check if File is updated recently
97                 else {
98                         Long newModified = pdpPath.toFile().lastModified();
99                         if (!newModified.equals(oldModified)) {
100                                 // File has been updated.
101                                 readProps();
102                         }
103                 }
104         }
105
106         @SuppressWarnings({ "unchecked", "rawtypes" })
107         private static void readProps() {
108                 Properties pdpProp;
109                 pdpProp = new Properties();
110                 try {
111                         InputStream in = new FileInputStream(pdpPath.toFile());
112                         oldModified = pdpPath.toFile().lastModified();
113                         pdpProp.load(in);
114                         // Read the Properties and Load the PDPs and encoding.
115                         pdpMap = new HashMap<>();
116                         // Check the Keys for PDP_URLs
117                         Collection<Object> unsorted = pdpProp.keySet();
118                         List<String> sorted = new ArrayList(unsorted);
119                         Collections.sort(sorted);
120                         for (String propKey : sorted) {
121                                 loadPDPProperties(propKey, pdpProp);
122                         }
123                         if (pdpMap == null || pdpMap.isEmpty()) {
124                                 LOGGER.debug(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Cannot Proceed without PDP_URLs");
125                         }
126                         in.close();
127                 } catch (IOException e) {
128                         LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
129                 }
130         }
131         
132         private static void loadPDPProperties(String propKey, Properties pdpProp){
133                 if (propKey.startsWith("PDP_URL")) {
134                         String checkVal = pdpProp.getProperty(propKey);
135                         if (checkVal == null) {
136                                 LOGGER.error("Properties file doesn't have the PDP_URL parameter");
137                         }
138                         if (checkVal != null && checkVal.contains(";")) {
139                                 List<String> pdpDefault = new ArrayList<>(Arrays.asList(checkVal.split("\\s*;\\s*")));
140                                 int pdpCount = 0;
141                                 while (pdpCount < pdpDefault.size()) {
142                                         String pdpVal = pdpDefault.get(pdpCount);
143                                         readPDPParam(pdpVal);
144                                         pdpCount++;
145                                 }
146                         }
147                 }
148         }
149
150         private static void readPDPParam(String pdpVal){
151                 if(pdpVal.contains(",")){
152                         List<String> pdpValues = new ArrayList<>(Arrays.asList(pdpVal.split("\\s*,\\s*")));
153                         if(pdpValues.size()==3){
154                                 // 1:2 will be UserID:Password
155                                 String userID = pdpValues.get(1);
156                                 String pass = pdpValues.get(2);
157                                 Base64.Encoder encoder = Base64.getEncoder();
158                                 // 0 - PDPURL
159                                 pdpMap.put(pdpValues.get(0), encoder.encodeToString((userID+":"+pass).getBytes(StandardCharsets.UTF_8)));
160                         }else{
161                                 LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS + "No Credentials to send Request: " + pdpValues);
162                         }
163                 }else{
164                         LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS + "No Credentials to send Request: " + pdpVal);
165                 }
166         }
167         
168         public static String getEncoding(String pdpID){
169                 try {
170                         readFile();
171                 } catch (Exception e) {
172                         LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
173                 }
174                 String encoding = null;
175                 if(pdpMap!=null && (!pdpMap.isEmpty())){
176                         try{
177                                 encoding = pdpMap.get(pdpID);
178                         } catch(Exception e){
179                                 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
180                         }
181                         return encoding;
182                 }else{
183                         return null;
184                 }
185         }
186 }