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