[POLICY-73] replace openecomp for policy-engine
[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))? true: false;
65         }
66
67         private static void readFile(){
68                 String pdpFile = null;
69                 Long newModified = null;
70                 try{
71                         pdpFile = XACMLProperties.getProperty(XACMLRestProperties.PROP_PDP_IDFILE);     
72                 }catch (Exception e){
73                         LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Cannot read the PDP ID File" + e);
74                         return;
75                 }
76                 if (pdpFile == null) {
77                         LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "PDP File name not Valid : " + pdpFile);
78                 }
79                 if (pdpPath == null) {
80                         pdpPath = Paths.get(pdpFile);
81                         if (!pdpPath.toFile().exists()) {
82                                 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "File doesn't exist in the specified Path : "     + pdpPath.toString());
83
84                         } 
85                         if (pdpPath.toString().endsWith(".properties")) {
86                                 readProps();
87                         } else {
88                                 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Not a .properties file " + pdpFile);
89                         }
90                 }
91                 // Check if File is updated recently
92                 else {
93                         newModified = pdpPath.toFile().lastModified();
94                         if (!newModified.equals(oldModified)) {
95                                 // File has been updated.
96                                 readProps();
97                         }
98                 }
99         }
100
101         @SuppressWarnings({ "unchecked", "rawtypes" })
102         private static void readProps() {
103                 Properties pdpProp = null;
104                 pdpProp = new Properties();
105                 try {
106                         InputStream in = new FileInputStream(pdpPath.toFile());
107                         oldModified = pdpPath.toFile().lastModified();
108                         pdpProp.load(in);
109                         // Read the Properties and Load the PDPs and encoding.
110                         pdpMap = new HashMap<>();
111                         // Check the Keys for PDP_URLs
112                         Collection<Object> unsorted = pdpProp.keySet();
113                         List<String> sorted = new ArrayList(unsorted);
114                         Collections.sort(sorted);
115                         for (String propKey : sorted) {
116                                 loadPDPProperties(propKey, pdpProp);
117                         }
118                         if (pdpMap == null || pdpMap.isEmpty()) {
119                                 LOGGER.debug(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Cannot Proceed without PDP_URLs");
120                         }
121                         in.close();
122                 } catch (IOException e) {
123                         LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
124                 }
125         }
126         
127         private static void loadPDPProperties(String propKey, Properties pdpProp){
128                 if (propKey.startsWith("PDP_URL")) {
129                         String check_val = pdpProp.getProperty(propKey);
130                         if (check_val == null) {
131                                 LOGGER.error("Properties file doesn't have the PDP_URL parameter");
132                         }
133                         if (check_val != null && check_val.contains(";")) {
134                                 List<String> pdp_default = new ArrayList<>(Arrays.asList(check_val.split("\\s*;\\s*")));
135                                 int pdpCount = 0;
136                                 while (pdpCount < pdp_default.size()) {
137                                         String pdpVal = pdp_default.get(pdpCount);
138                                         readPDPParam(pdpVal);
139                                         pdpCount++;
140                                 }
141                         }
142                 }
143         }
144
145         private static void readPDPParam(String pdpVal){
146                 if(pdpVal.contains(",")){
147                         List<String> pdpValues = new ArrayList<>(Arrays.asList(pdpVal.split("\\s*,\\s*")));
148                         if(pdpValues.size()==3){
149                                 // 1:2 will be UserID:Password
150                                 String userID = pdpValues.get(1);
151                                 String pass = pdpValues.get(2);
152                                 Base64.Encoder encoder = Base64.getEncoder();
153                                 // 0 - PDPURL
154                                 pdpMap.put(pdpValues.get(0), encoder.encodeToString((userID+":"+pass).getBytes(StandardCharsets.UTF_8)));
155                         }else{
156                                 LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS + "No Credentials to send Request: " + pdpValues);
157                         }
158                 }else{
159                         LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS + "No Credentials to send Request: " + pdpVal);
160                 }
161         }
162         
163         public static String getEncoding(String pdpID){
164                 try {
165                         readFile();
166                 } catch (Exception e) {
167                         LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
168                 }
169                 String encoding = null;
170                 if(pdpMap!=null && (!pdpMap.isEmpty())){
171                         try{
172                                 encoding = pdpMap.get(pdpID);
173                         } catch(Exception e){
174                                 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
175                         }
176                         return encoding;
177                 }else{
178                         return null;
179                 }
180         }
181 }