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