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.openecomp.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.openecomp.policy.rest.XACMLRestProperties;
42 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
43 import org.openecomp.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.openecomp.policy.common.logging.flexlogger.FlexLogger;
50 import org.openecomp.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();
211 //loggingContext.setRequestID(requestID.toString());
212 //loggingContext.transactionStarted();
215 url = new URL(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL)+"?oldPolicyName="+ URLEncoder.encode(oldPolicyName, "UTF-8")+"&newPolicyName="+URLEncoder.encode(newPolicyName,"UTF-8"));
216 if(logger.isDebugEnabled()){
217 logger.debug("\nnotifyPapOfCreateUpdate: URL = " + url);
219 } catch (MalformedURLException e) {
220 logger.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
221 + "\nMalformedURLException message = " + e);
224 } catch (UnsupportedEncodingException e) {
225 logger.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
226 + "\nUnsupportedEncodingException message = " + e);
231 // Open up the connection
234 connection = (HttpURLConnection)url.openConnection();
235 } catch (IOException e) {
236 logger.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
237 + "\nurl.openConnection() IOException message = " + e);
241 // Setup our method and headers
244 connection.setRequestMethod("PUT");
245 } catch (ProtocolException e) {
246 logger.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
247 + "\nconnection.setRequestMethod(PUT) ProtocolException message = " + e);
248 connection.disconnect();
251 connection.setRequestProperty("Authorization", "Basic " + encoding);
252 connection.setRequestProperty("Accept", "text/x-java-properties");
253 connection.setRequestProperty("Content-Type", "text/x-java-properties");
254 connection.setRequestProperty("requestID", requestID.toString());
255 connection.setUseCaches(false);
257 // Adding this in. It seems the HttpUrlConnection class does NOT
258 // properly forward our headers for POST re-direction. It does so
259 // for a GET re-direction.
261 // So we need to handle this ourselves.
263 connection.setInstanceFollowRedirects(false);
264 connection.setDoOutput(true);
265 connection.setDoInput(true);
267 connection.connect();
268 } catch (IOException e) {
269 logger.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
270 + "\nconnection.connect() IOException message = " + e);
271 connection.disconnect();
275 int responseCode = connection.getResponseCode();
276 if(logger.isDebugEnabled()){
277 logger.debug("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
278 + "\nconnection.getResponseCode() = " + responseCode);
280 if (responseCode == 200) {
281 connection.disconnect();
284 connection.disconnect();
286 //System.out.println(connection.getResponseMessage());
287 //System.out.println(connection.getResponseCode());
288 //System.out.println(connection.g);
290 } catch (IOException e) {
291 logger.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
292 + "\nconnection.getResponseCode() IOException message = " + e);
293 connection.disconnect();
298 public static boolean notifyPapOfDelete(String policyToDelete){
299 Base64.Encoder encoder = Base64.getEncoder();
300 String encoding = encoder.encodeToString((XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID)+":"+XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS)).getBytes(StandardCharsets.UTF_8));
301 HttpURLConnection connection = null;
302 UUID requestID = UUID.randomUUID();
303 //loggingContext.setRequestID(requestID.toString());
304 //loggingContext.transactionStarted();
305 String papUrl = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL);
307 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE +
308 "PAP url property does not exist");
311 String urlString = "";
313 urlString = papUrl+"?groupId=0&isDeleteNotify=1&policyToDelete="+ URLEncoder.encode(policyToDelete, "UTF-8");
314 } catch(UnsupportedEncodingException e){
315 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE +
316 "Invalid encoding: UTF-8", e);
321 url = new URL(urlString);
322 } catch (MalformedURLException e) {
323 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW +
324 "Error parsing PAP url: "
330 // Open up the connection
333 connection = (HttpURLConnection)url.openConnection();
334 } catch (IOException e) {
335 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +
336 "Error opening HttpURLConnection to: "
342 // Setup our method and headers
345 connection.setRequestMethod("DELETE");
346 } catch (ProtocolException e) {
347 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE +
348 "Invalid request method: DELETE", e);
349 connection.disconnect();
352 connection.setRequestProperty("Authorization", "Basic " + encoding);
353 connection.setRequestProperty("Accept", "text/x-java-properties");
354 connection.setRequestProperty("Content-Type", "text/x-java-properties");
355 connection.setRequestProperty("requestID", requestID.toString());
356 connection.setUseCaches(false);
358 // Adding this in. It seems the HttpUrlConnection class does NOT
359 // properly forward our headers for POST re-direction. It does so
360 // for a GET re-direction.
362 // So we need to handle this ourselves.
364 connection.setInstanceFollowRedirects(false);
365 connection.setDoOutput(true);
366 connection.setDoInput(true);
368 connection.connect();
369 } catch (IOException e) {
370 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +
371 "Error connecting HttpURLConnection to: "
372 + connection.getURL().toString()
374 connection.disconnect();
378 if (connection.getResponseCode() == 200) {
379 connection.disconnect();
383 connection.disconnect();
385 //System.out.println(connection.getResponseMessage());
386 //System.out.println(connection.getResponseCode());
387 //System.out.println(connection.g);
389 } catch (IOException e) {
390 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR +
391 "Error getting HttpUrlConnection response code for: "
392 + connection.getURL().toString()
394 connection.disconnect();
399 public static boolean notifyPapOfCreateUpdate(String policyToCreateUpdate){
400 if(logger.isDebugEnabled()){
401 logger.debug("\nXACMLPolicyWriterWithPapNotify.notifyPapOfCreateUpdate(String policyToCreateUpdate) "
402 + "\npolicyToCreateUpdate = " + policyToCreateUpdate);
404 Base64.Encoder encoder = Base64.getEncoder();
405 String encoding = encoder.encodeToString((XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID)+":"+XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS)).getBytes(StandardCharsets.UTF_8));
406 HttpURLConnection connection = null;
407 UUID requestID = UUID.randomUUID();
408 //loggingContext.setRequestID(requestID.toString());
409 //loggingContext.transactionStarted();
412 url = new URL(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL)+"?policyToCreateUpdate="+ URLEncoder.encode(policyToCreateUpdate, "UTF-8"));
413 if(logger.isDebugEnabled()){
414 logger.debug("\nnotifyPapOfCreateUpdate: URL = " + url);
416 } catch (MalformedURLException e) {
417 logger.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
418 + "\nMalformedURLException message = " + e);
421 } catch (UnsupportedEncodingException e) {
422 logger.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
423 + "\nUnsupportedEncodingException message = " + e);
428 // Open up the connection
431 connection = (HttpURLConnection)url.openConnection();
432 } catch (IOException e) {
433 logger.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
434 + "\nurl.openConnection() IOException message = " + e);
438 // Setup our method and headers
441 connection.setRequestMethod("PUT");
442 } catch (ProtocolException e) {
443 logger.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
444 + "\nconnection.setRequestMethod(PUT) ProtocolException message = " + e);
445 connection.disconnect();
448 connection.setRequestProperty("Authorization", "Basic " + encoding);
449 connection.setRequestProperty("Accept", "text/x-java-properties");
450 connection.setRequestProperty("Content-Type", "text/x-java-properties");
451 connection.setRequestProperty("requestID", requestID.toString());
452 connection.setUseCaches(false);
454 // Adding this in. It seems the HttpUrlConnection class does NOT
455 // properly forward our headers for POST re-direction. It does so
456 // for a GET re-direction.
458 // So we need to handle this ourselves.
460 connection.setInstanceFollowRedirects(false);
461 connection.setDoOutput(true);
462 connection.setDoInput(true);
464 connection.connect();
465 } catch (IOException e) {
466 logger.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
467 + "\nconnection.connect() IOException message = " + e);
468 connection.disconnect();
472 int responseCode = connection.getResponseCode();
473 if(logger.isDebugEnabled()){
474 logger.debug("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
475 + "\nconnection.getResponseCode() = " + responseCode);
477 if (responseCode == 200) {
478 connection.disconnect();
481 connection.disconnect();
483 //System.out.println(connection.getResponseMessage());
484 //System.out.println(connection.getResponseCode());
485 //System.out.println(connection.g);
487 } catch (IOException e) {
488 logger.error("\nnotifyPapOfCreateUpdate(String policyToCreateUpdate)"
489 + "\nconnection.getResponseCode() IOException message = " + e);
490 connection.disconnect();