2 * ============LICENSE_START=======================================================
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
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);
61 * Helper static class that does the work to write a policy set to a file on disk and notify PAP
65 public static Path writePolicyFile(Path filename, PolicySetType policySet) {
66 if(LOGGER.isDebugEnabled()){
67 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicySetType policySet)"
68 + "\nfilename = " + filename
69 + "\npolicySet = " + policySet);
72 Path path = XACMLPolicyWriter.writePolicyFile(filename, policySet);
76 if(notifyPapOfCreateUpdate(filename.toAbsolutePath().toString())){
79 //write to DB failed. So, delete the file
81 Files.deleteIfExists(path);
82 }catch(DirectoryNotEmptyException e){
83 //We are trying to delete a directory and it is not empty
84 LOGGER.error("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicySetType policySet): Files.deleteIfExists(path)"
85 + "\nDirectoryNotEmptyException for path = " + path
86 + "\nException message = " + e);
87 }catch(IOException e) {
88 // File permission problems are caught here.
89 LOGGER.error("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicySetType policySet): Files.deleteIfExists(path)"
90 + "\nIOException for path = " + path
91 + "\nException message = " + e);
93 LOGGER.error("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicySetType policySet): Files.deleteIfExists(path)"
94 + "\nException for path = " + path
95 + "\nException message = " + e);
106 * Helper static class that does the work to write a policy set to an output stream and notify PAP
110 public static void writePolicyFile(OutputStream os, PolicySetType policySet) {
111 if(LOGGER.isDebugEnabled()){
112 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(OutputStream os, PolicySetType policySet)"
114 + "\npolicySet = " + policySet);
116 //Only used for writing a byte array output stream for a message. No file is written
117 XACMLPolicyWriter.writePolicyFile(os, policySet);
121 * Helper static class that does the work to write a policy to a file on disk.
125 public static Path writePolicyFile(Path filename, PolicyType policy) {
126 if(LOGGER.isDebugEnabled()){
127 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicyType policy)"
128 + "\nfilename = " + filename
129 + "\npolicy = " + policy);
133 Path path = XACMLPolicyWriter.writePolicyFile(filename, policy);
137 if(notifyPapOfCreateUpdate(filename.toAbsolutePath().toString())){
140 //write to DB failed so delete the file
142 Files.deleteIfExists(path);
143 }catch(DirectoryNotEmptyException e){
144 //We are trying to delete a directory and it is not empty
145 LOGGER.error("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicySetType policySet)Files.deleteIfExists(path) :"
146 + "\nDirectoryNotEmptyException for path = " + path
147 + "\nException message = " + e);
148 }catch(IOException e) {
149 // File permission problems are caught here.
150 LOGGER.error("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicySetType policySet): Files.deleteIfExists(path)"
151 + "\nIOException for path = " + path
152 + "\nException message = " + e);
154 LOGGER.error("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(Path filename, PolicySetType policySet): Files.deleteIfExists(path)"
155 + "\nException for path = " + path
156 + "\nException message = " + e);
168 * Helper static class that does the work to write a policy to a file on disk.
172 public static InputStream getXmlAsInputStream(PolicyType policy) {
173 if(LOGGER.isDebugEnabled()){
174 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.getXmlAsInputStream(PolicyType policy)"
175 + "\npolicy = " + policy);
177 return XACMLPolicyWriter.getXmlAsInputStream(policy);
180 * Helper static class that does the work to write a policy set to an output stream.
184 public static void writePolicyFile(OutputStream os, PolicyType policy) {
185 if(LOGGER.isDebugEnabled()){
186 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.writePolicyFile(OutputStream os, PolicyType policy)"
188 + "\npolicy = " + policy);
190 //There are no references to this and if there were, it would most likely be used in an http message
191 XACMLPolicyWriter.writePolicyFile(os, policy);
194 public static String changeFileNameInXmlWhenRenamePolicy(Path filename) {
195 if(LOGGER.isDebugEnabled()){
196 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.changeFileNameInXmlWhenRenamePolicy(Path filename)"
197 + "\nfilename = " + filename);
199 return XACMLPolicyWriter.changeFileNameInXmlWhenRenamePolicy(filename);
202 public static boolean notifyPapOfPolicyRename(String oldPolicyName, String newPolicyName){
203 if(LOGGER.isDebugEnabled()){
204 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.notifyPapOfCreateUpdate(String policyToCreateUpdate) "
205 + "\npolicyToCreateUpdate = " + " ");
207 Base64.Encoder encoder = Base64.getEncoder();
208 String encoding = encoder.encodeToString((XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID)+":"+XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS)).getBytes(StandardCharsets.UTF_8));
209 HttpURLConnection connection = null;
210 UUID requestID = UUID.randomUUID();
213 url = new URL(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL)+"?oldPolicyName="+ URLEncoder.encode(oldPolicyName, "UTF-8")+"&newPolicyName="+URLEncoder.encode(newPolicyName,"UTF-8"));
214 if(LOGGER.isDebugEnabled()){
215 LOGGER.debug("\nnotifyPapOfCreateUpdate: URL = " + url);
217 } catch (MalformedURLException e) {
218 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
219 + "\nMalformedURLException message = " + e);
222 } catch (UnsupportedEncodingException e) {
223 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
224 + "\nUnsupportedEncodingException message = " + e);
229 // Open up the connection
232 connection = (HttpURLConnection)url.openConnection();
233 } catch (IOException e) {
234 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
235 + "\nurl.openConnection() IOException message = " + e);
239 // Setup our method and headers
242 connection.setRequestMethod("PUT");
243 } catch (ProtocolException e) {
244 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
245 + "\nconnection.setRequestMethod(PUT) ProtocolException message = " + e);
246 connection.disconnect();
249 connection.setRequestProperty("Authorization", "Basic " + encoding);
250 connection.setRequestProperty("Accept", "text/x-java-properties");
251 connection.setRequestProperty("Content-Type", "text/x-java-properties");
252 connection.setRequestProperty("requestID", requestID.toString());
253 connection.setUseCaches(false);
255 // Adding this in. It seems the HttpUrlConnection class does NOT
256 // properly forward our headers for POST re-direction. It does so
257 // for a GET re-direction.
259 // So we need to handle this ourselves.
261 connection.setInstanceFollowRedirects(false);
262 connection.setDoOutput(true);
263 connection.setDoInput(true);
265 connection.connect();
266 } catch (IOException e) {
267 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
268 + "\nconnection.connect() IOException message = " + e);
269 connection.disconnect();
273 int responseCode = connection.getResponseCode();
274 if(LOGGER.isDebugEnabled()){
275 LOGGER.debug("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
276 + "\nconnection.getResponseCode() = " + responseCode);
278 if (responseCode == 200) {
279 connection.disconnect();
282 connection.disconnect();
285 } catch (IOException e) {
286 LOGGER.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
287 + "\nconnection.getResponseCode() IOException message = " + e);
288 connection.disconnect();
293 public static boolean notifyPapOfDelete(String policyToDelete){
294 Base64.Encoder encoder = Base64.getEncoder();
295 String encoding = encoder.encodeToString((XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID)+":"+XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS)).getBytes(StandardCharsets.UTF_8));
296 HttpURLConnection connection = null;
297 UUID requestID = UUID.randomUUID();
298 //loggingContext.setRequestID(requestID.toString());
299 //loggingContext.transactionStarted();
300 String papUrl = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL);
302 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE +
303 "PAP url property does not exist");
306 String urlString = "";
308 urlString = papUrl+"?groupId=0&isDeleteNotify=1&policyToDelete="+ URLEncoder.encode(policyToDelete, "UTF-8");
309 } catch(UnsupportedEncodingException e){
310 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE +
311 "Invalid encoding: UTF-8", e);
316 url = new URL(urlString);
317 } catch (MalformedURLException e) {
318 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW +
319 "Error parsing PAP url: "
325 // Open up the connection
328 connection = (HttpURLConnection)url.openConnection();
329 } catch (IOException e) {
330 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +
331 "Error opening HttpURLConnection to: "
337 // Setup our method and headers
340 connection.setRequestMethod("DELETE");
341 } catch (ProtocolException e) {
342 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE +
343 "Invalid request method: DELETE", e);
344 connection.disconnect();
347 connection.setRequestProperty("Authorization", "Basic " + encoding);
348 connection.setRequestProperty("Accept", "text/x-java-properties");
349 connection.setRequestProperty("Content-Type", "text/x-java-properties");
350 connection.setRequestProperty("requestID", requestID.toString());
351 connection.setUseCaches(false);
353 // Adding this in. It seems the HttpUrlConnection class does NOT
354 // properly forward our headers for POST re-direction. It does so
355 // for a GET re-direction.
357 // So we need to handle this ourselves.
359 connection.setInstanceFollowRedirects(false);
360 connection.setDoOutput(true);
361 connection.setDoInput(true);
363 connection.connect();
364 } catch (IOException e) {
365 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +
366 "Error connecting HttpURLConnection to: "
367 + connection.getURL().toString()
369 connection.disconnect();
373 if (connection.getResponseCode() == 200) {
374 connection.disconnect();
378 connection.disconnect();
381 } catch (IOException e) {
382 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +
383 "Error getting HttpUrlConnection response code for: "
384 + connection.getURL().toString()
386 connection.disconnect();
391 public static boolean notifyPapOfCreateUpdate(String policyToCreateUpdate){
392 if(LOGGER.isDebugEnabled()){
393 LOGGER.debug("\nXACMLPolicyWriterWithPapNotify.notifyPapOfCreateUpdate(String policyToCreateUpdate) "
394 + "\npolicyToCreateUpdate = " + policyToCreateUpdate);
396 Base64.Encoder encoder = Base64.getEncoder();
397 String encoding = encoder.encodeToString((XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID)+":"+XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS)).getBytes(StandardCharsets.UTF_8));
398 HttpURLConnection connection = null;
399 UUID requestID = UUID.randomUUID();
400 //loggingContext.setRequestID(requestID.toString());
401 //loggingContext.transactionStarted();
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();