2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.utils;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.io.UnsupportedEncodingException;
28 import java.net.HttpURLConnection;
29 import java.net.MalformedURLException;
30 import java.net.ProtocolException;
32 import java.net.URLEncoder;
33 import java.nio.charset.StandardCharsets;
34 import java.nio.file.DirectoryNotEmptyException;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.util.Base64;
38 import java.util.UUID;
40 import org.onap.policy.rest.XACMLRestProperties;
42 import org.onap.policy.xacml.api.XACMLErrorConstants;
43 import org.onap.policy.xacml.util.XACMLPolicyWriter;
44 import com.att.research.xacml.util.XACMLProperties;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
49 import org.onap.policy.common.logging.flexlogger.FlexLogger;
50 import org.onap.policy.common.logging.flexlogger.Logger;
53 * Helper static class that wraps XACMLPolicyWriter
57 public class XACMLPolicyWriterWithPapNotify{
58 private static final Logger LOGGER = FlexLogger.getLogger(XACMLPolicyWriterWithPapNotify.class);
60 private XACMLPolicyWriterWithPapNotify() {
61 // Add private constructor to hide the implicit public one
65 * Helper static class that does the work to write a policy set to a file on disk and notify PAP
69 public static Path writePolicyFile(Path filename, PolicySetType policySet) {
70 if(LOGGER.isDebugEnabled()){
71 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicySetType policySet)"
72 + "\nfilename = " + filename
73 + "\npolicySet = " + policySet);
76 Path path = XACMLPolicyWriter.writePolicyFile(filename, policySet);
80 if(notifyPapOfCreateUpdate(filename.toAbsolutePath().toString())){
83 //write to DB failed. So, delete the file
85 Files.deleteIfExists(path);
86 }catch(DirectoryNotEmptyException e){
87 //We are trying to delete a directory and it is not empty
88 LOGGER.error("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicySetType policySet): Files.deleteIfExists(path)"
89 + "\nDirectoryNotEmptyException for path = " + path
90 + "\nException message = " + e);
91 }catch(IOException e) {
92 // File permission problems are caught here.
93 LOGGER.error("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicySetType policySet): Files.deleteIfExists(path)"
94 + "\nIOException for path = " + path
95 + "\nException message = " + e);
97 LOGGER.error("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicySetType policySet): Files.deleteIfExists(path)"
98 + "\nException for path = " + path
99 + "\nException message = " + e);
110 * Helper static class that does the work to write a policy set to an output stream and notify PAP
114 public static void writePolicyFile(OutputStream os, PolicySetType policySet) {
115 if(LOGGER.isDebugEnabled()){
116 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(OutputStream os, PolicySetType policySet)"
118 + "\npolicySet = " + policySet);
120 //Only used for writing a byte array output stream for a message. No file is written
121 XACMLPolicyWriter.writePolicyFile(os, policySet);
125 * Helper static class that does the work to write a policy to a file on disk.
129 public static Path writePolicyFile(Path filename, PolicyType policy) {
130 if(LOGGER.isDebugEnabled()){
131 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicyType policy)"
132 + "\nfilename = " + filename
133 + "\npolicy = " + policy);
137 Path path = XACMLPolicyWriter.writePolicyFile(filename, policy);
141 if(notifyPapOfCreateUpdate(filename.toAbsolutePath().toString())){
144 //write to DB failed so delete the file
146 Files.deleteIfExists(path);
147 }catch(DirectoryNotEmptyException e){
148 //We are trying to delete a directory and it is not empty
149 LOGGER.error("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicySetType policySet)Files.deleteIfExists(path) :"
150 + "\nDirectoryNotEmptyException for path = " + path
151 + "\nException message = " + e);
152 }catch(IOException e) {
153 // File permission problems are caught here.
154 LOGGER.error("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicySetType policySet): Files.deleteIfExists(path)"
155 + "\nIOException for path = " + path
156 + "\nException message = " + e);
158 LOGGER.error("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicySetType policySet): Files.deleteIfExists(path)"
159 + "\nException for path = " + path
160 + "\nException message = " + e);
172 * Helper static class that does the work to write a policy to a file on disk.
176 public static InputStream getXmlAsInputStream(PolicyType policy) {
177 if(LOGGER.isDebugEnabled()){
178 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.getXmlAsInputStream(PolicyType policy)"
179 + "\npolicy = " + policy);
181 return XACMLPolicyWriter.getXmlAsInputStream(policy);
184 * Helper static class that does the work to write a policy set to an output stream.
188 public static void writePolicyFile(OutputStream os, PolicyType policy) {
189 if(LOGGER.isDebugEnabled()){
190 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(OutputStream os, PolicyType policy)"
192 + "\npolicy = " + policy);
194 //There are no references to this and if there were, it would most likely be used in an http message
195 XACMLPolicyWriter.writePolicyFile(os, policy);
198 public static String changeFileNameInXmlWhenRenamePolicy(Path filename) {
199 if(LOGGER.isDebugEnabled()){
200 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.changeFileNameInXmlWhenRenamePolicy(Path filename)"
201 + "\nfilename = " + filename);
203 return XACMLPolicyWriter.changeFileNameInXmlWhenRenamePolicy(filename);
206 public static boolean notifyPapOfPolicyRename(String oldPolicyName, String newPolicyName){
207 if(LOGGER.isDebugEnabled()){
208 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.notifyPapOfCreateUpdate(String policyToCreateUpdate) "
209 + "\npolicyToCreateUpdate = " + " ");
211 Base64.Encoder encoder = Base64.getEncoder();
212 String encoding = encoder.encodeToString((XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID)+":"+CryptoUtils.decryptTxtNoExStr(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS))).getBytes(StandardCharsets.UTF_8));
213 HttpURLConnection connection;
214 UUID requestID = UUID.randomUUID();
217 url = new URL(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL)+"?oldPolicyName="+ URLEncoder.encode(oldPolicyName, "UTF-8")+"&newPolicyName="+URLEncoder.encode(newPolicyName,"UTF-8"));
218 if(LOGGER.isDebugEnabled()){
219 LOGGER.debug("\nnotifyPapOfCreateUpdate: URL = " + url);
221 } catch (MalformedURLException e) {
222 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
223 + "\nMalformedURLException message = " + e);
226 } catch (UnsupportedEncodingException e) {
227 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
228 + "\nUnsupportedEncodingException message = " + e);
233 // Open up the connection
236 connection = (HttpURLConnection)url.openConnection();
237 } catch (IOException e) {
238 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
239 + "\nurl.openConnection() IOException message = " + e);
243 // Setup our method and headers
246 connection.setRequestMethod("PUT");
247 } catch (ProtocolException e) {
248 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
249 + "\nconnection.setRequestMethod(PUT) ProtocolException message = " + e);
250 connection.disconnect();
253 connection.setRequestProperty("Authorization", "Basic " + encoding);
254 connection.setRequestProperty("Accept", "text/x-java-properties");
255 connection.setRequestProperty("Content-Type", "text/x-java-properties");
256 connection.setRequestProperty("requestID", requestID.toString());
257 connection.setUseCaches(false);
259 // Adding this in. It seems the HttpUrlConnection class does NOT
260 // properly forward our headers for POST re-direction. It does so
261 // for a GET re-direction.
263 // So we need to handle this ourselves.
265 connection.setInstanceFollowRedirects(false);
266 connection.setDoOutput(true);
267 connection.setDoInput(true);
269 connection.connect();
270 } catch (IOException e) {
271 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
272 + "\nconnection.connect() IOException message = " + e);
273 connection.disconnect();
277 int responseCode = connection.getResponseCode();
278 if(LOGGER.isDebugEnabled()){
279 LOGGER.debug("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
280 + "\nconnection.getResponseCode() = " + responseCode);
282 if (responseCode == 200) {
283 connection.disconnect();
286 connection.disconnect();
289 } catch (IOException e) {
290 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
291 + "\nconnection.getResponseCode() IOException message = " + e);
292 connection.disconnect();
297 public static boolean notifyPapOfDelete(String policyToDelete){
298 Base64.Encoder encoder = Base64.getEncoder();
299 String encoding = encoder.encodeToString((XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID)+":"+CryptoUtils.decryptTxtNoExStr(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS))).getBytes(StandardCharsets.UTF_8));
300 HttpURLConnection connection;
301 UUID requestID = UUID.randomUUID();
302 String papUrl = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL);
304 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE +
305 "PAP url property does not exist");
308 String urlString = "";
310 urlString = papUrl+"?groupId=0&isDeleteNotify=1&policyToDelete="+ URLEncoder.encode(policyToDelete, "UTF-8");
311 } catch(UnsupportedEncodingException e){
312 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE +
313 "Invalid encoding: UTF-8", e);
318 url = new URL(urlString);
319 } catch (MalformedURLException e) {
320 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW +
321 "Error parsing PAP url: "
327 // Open up the connection
330 connection = (HttpURLConnection)url.openConnection();
331 } catch (IOException e) {
332 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +
333 "Error opening HttpURLConnection to: "
339 // Setup our method and headers
342 connection.setRequestMethod("DELETE");
343 } catch (ProtocolException e) {
344 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE +
345 "Invalid request method: DELETE", e);
346 connection.disconnect();
349 connection.setRequestProperty("Authorization", "Basic " + encoding);
350 connection.setRequestProperty("Accept", "text/x-java-properties");
351 connection.setRequestProperty("Content-Type", "text/x-java-properties");
352 connection.setRequestProperty("requestID", requestID.toString());
353 connection.setUseCaches(false);
355 // Adding this in. It seems the HttpUrlConnection class does NOT
356 // properly forward our headers for POST re-direction. It does so
357 // for a GET re-direction.
359 // So we need to handle this ourselves.
361 connection.setInstanceFollowRedirects(false);
362 connection.setDoOutput(true);
363 connection.setDoInput(true);
365 connection.connect();
366 } catch (IOException e) {
367 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +
368 "Error connecting HttpURLConnection to: "
369 + connection.getURL().toString()
371 connection.disconnect();
375 if (connection.getResponseCode() == 200) {
376 connection.disconnect();
380 connection.disconnect();
383 } catch (IOException e) {
384 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +
385 "Error getting HttpUrlConnection response code for: "
386 + connection.getURL().toString()
388 connection.disconnect();
393 public static boolean notifyPapOfCreateUpdate(String policyToCreateUpdate){
394 if(LOGGER.isDebugEnabled()){
395 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.notifyPapOfCreateUpdate(String policyToCreateUpdate) "
396 + "\npolicyToCreateUpdate = " + policyToCreateUpdate);
398 Base64.Encoder encoder = Base64.getEncoder();
399 String encoding = encoder.encodeToString((XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID)+":"+CryptoUtils.decryptTxtNoExStr(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS))).getBytes(StandardCharsets.UTF_8));
400 HttpURLConnection connection;
401 UUID requestID = UUID.randomUUID();
404 url = new URL(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL)+"?policyToCreateUpdate="+ URLEncoder.encode(policyToCreateUpdate, "UTF-8"));
405 if(LOGGER.isDebugEnabled()){
406 LOGGER.debug("\nnotifyPapOfCreateUpdate: URL = " + url);
408 } catch (MalformedURLException e) {
409 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
410 + "\nMalformedURLException message = " + e);
413 } catch (UnsupportedEncodingException e) {
414 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
415 + "\nUnsupportedEncodingException message = " + e);
420 // Open up the connection
423 connection = (HttpURLConnection)url.openConnection();
424 } catch (IOException e) {
425 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
426 + "\nurl.openConnection() IOException message = " + e);
430 // Setup our method and headers
433 connection.setRequestMethod("PUT");
434 } catch (ProtocolException e) {
435 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
436 + "\nconnection.setRequestMethod(PUT) ProtocolException message = " + e);
437 connection.disconnect();
440 connection.setRequestProperty("Authorization", "Basic " + encoding);
441 connection.setRequestProperty("Accept", "text/x-java-properties");
442 connection.setRequestProperty("Content-Type", "text/x-java-properties");
443 connection.setRequestProperty("requestID", requestID.toString());
444 connection.setUseCaches(false);
446 // Adding this in. It seems the HttpUrlConnection class does NOT
447 // properly forward our headers for POST re-direction. It does so
448 // for a GET re-direction.
450 // So we need to handle this ourselves.
452 connection.setInstanceFollowRedirects(false);
453 connection.setDoOutput(true);
454 connection.setDoInput(true);
456 connection.connect();
457 } catch (IOException e) {
458 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
459 + "\nconnection.connect() IOException message = " + e);
460 connection.disconnect();
464 int responseCode = connection.getResponseCode();
465 if(LOGGER.isDebugEnabled()){
466 LOGGER.debug("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
467 + "\nconnection.getResponseCode() = " + responseCode);
469 if (responseCode == 200) {
470 connection.disconnect();
473 connection.disconnect();
476 } catch (IOException e) {
477 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
478 + "\nconnection.getResponseCode() IOException message = " + e);
479 connection.disconnect();