2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.pap.xacml.rest;
23 import com.att.research.xacml.api.pap.PAPException;
24 import com.att.research.xacml.api.pap.PDPStatus;
25 import java.io.OutputStream;
26 import java.net.HttpURLConnection;
28 import java.util.List;
29 import java.util.Properties;
30 import java.util.UUID;
31 import org.onap.policy.common.logging.ONAPLoggingContext;
32 import org.onap.policy.common.logging.eelf.MessageCodes;
33 import org.onap.policy.common.logging.eelf.PolicyLogger;
34 import org.onap.policy.common.logging.flexlogger.FlexLogger;
35 import org.onap.policy.common.logging.flexlogger.Logger;
36 import org.onap.policy.pap.xacml.restAuth.CheckPDP;
37 import org.onap.policy.xacml.api.pap.OnapPDP;
39 public class UpdatePdpThread implements Runnable {
41 private static final Logger LOGGER = FlexLogger.getLogger(UpdatePdpThread.class);
42 private ONAPLoggingContext baseLoggingContext = new XACMLPapServlet().getBaseLoggingContext();
43 private static final Logger auditLogger = FlexLogger.getLogger("auditLogger");
44 private static final String XACMLPAPSERVLET = "XACMLPapServlet";
45 private static final String MESSAGE = " message: ";
48 private String requestId;
49 private ONAPLoggingContext loggingContext;
50 private List<Properties> properties;
53 public UpdatePdpThread(OnapPDP pdp, List<Properties> properties) {
55 this.properties = properties;
59 * Instantiates a new update pdp thread.
62 * @param loggingContext the logging context
63 * @param properties the properties
65 public UpdatePdpThread(OnapPDP pdp, ONAPLoggingContext loggingContext, List<Properties> properties) {
67 if (loggingContext != null
68 && (loggingContext.getRequestID() != null || "".equals(loggingContext.getRequestID()))) {
69 this.requestId = loggingContext.getRequestID();
71 this.loggingContext = loggingContext;
72 this.properties = properties;
77 // send the current configuration to one PDP
78 HttpURLConnection connection = null;
79 // get a new logging context for the thread
81 if (this.loggingContext == null) {
82 loggingContext = new ONAPLoggingContext(baseLoggingContext);
84 } catch (Exception e) {
85 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, XACMLPAPSERVLET,
86 " Failed to send property file to " + pdp.getId());
87 // Since this is a server-side error, it probably does not
88 // reflect a problem on the client,
89 // so do not change the PDP status.
93 loggingContext.setServiceName("PAP:PDP.putConfig");
94 // If a requestId was provided, use it, otherwise generate one;
95 // post to loggingContext to be used later when calling PDP
96 if (requestId == null || "".equals(requestId)) {
97 UUID requestId = UUID.randomUUID();
98 loggingContext.setRequestID(requestId.toString());
100 .info("requestID not provided in call to XACMLPapSrvlet (UpdatePDPThread) so we generated one: "
101 + loggingContext.getRequestID());
103 loggingContext.setRequestID(requestId);
104 PolicyLogger.info("requestID was provided in call to XACMLPapSrvlet (UpdatePDPThread): "
105 + loggingContext.getRequestID());
107 loggingContext.transactionStarted();
108 // the Id of the PDP is its URL
109 if (LOGGER.isDebugEnabled()) {
110 LOGGER.debug("creating url for id '" + pdp.getId() + "'");
112 URL url = new URL(pdp.getId() + "?cache=all");
113 // Open up the connection
114 connection = (HttpURLConnection) url.openConnection();
115 // Setup our method and headers
116 connection.setRequestMethod("PUT");
118 String encoding = CheckPDP.getEncoding(pdp.getId());
119 if (encoding != null) {
120 connection.setRequestProperty("Authorization", "Basic " + encoding);
122 connection.setRequestProperty("Content-Type", "text/x-java-properties");
123 connection.setRequestProperty("X-ECOMP-RequestID", loggingContext.getRequestID());
124 connection.setInstanceFollowRedirects(true);
125 connection.setDoOutput(true);
126 if (!writePropertiesToStream(connection)) {
130 loggingContext.metricStarted();
131 connection.connect();
132 loggingContext.metricEnded();
133 PolicyLogger.metrics("XACMLPapServlet UpdatePDPThread connection connect");
134 if (connection.getResponseCode() == 204) {
135 LOGGER.info("Success. We are configured correctly - " + pdp.getId());
136 loggingContext.transactionEnded();
137 auditLogger.info("Success. PDP is configured correctly.");
138 PolicyLogger.audit("Transaction Success. PDP is configured correctly.");
139 getPapInstance().setPDPSummaryStatus(pdp, PDPStatus.Status.UP_TO_DATE);
140 } else if (connection.getResponseCode() == 200) {
141 LOGGER.info("Success. PDP needs to update its configuration - " + pdp.getId());
142 loggingContext.transactionEnded();
143 auditLogger.info("Success. PDP needs to update its configuration.");
144 PolicyLogger.audit("Transaction Success. PDP is configured correctly.");
145 getPapInstance().setPDPSummaryStatus(pdp, PDPStatus.Status.OUT_OF_SYNCH);
147 LOGGER.warn("Failed: " + connection.getResponseCode() + MESSAGE + connection.getResponseMessage()
149 loggingContext.transactionEnded();
150 auditLogger.warn("Failed: " + connection.getResponseCode() + MESSAGE + connection.getResponseMessage());
151 PolicyLogger.audit("Transaction Failed: " + connection.getResponseCode() + MESSAGE
152 + connection.getResponseMessage());
153 getPapInstance().setPDPSummaryStatus(pdp, PDPStatus.Status.UNKNOWN);
155 } catch (Exception e) {
157 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, XACMLPAPSERVLET,
158 " Unable to sync config with PDP '" + pdp.getId() + "'");
159 loggingContext.transactionEnded();
160 PolicyLogger.audit("Transaction Failed: Unable to sync config with PDP '" + pdp.getId() + "': " + e);
161 LOGGER.info("Transaction Failed: Unable to sync config with PDP '" + pdp.getId() + "': " + e);
163 getPapInstance().setPDPSummaryStatus(pdp, PDPStatus.Status.UNKNOWN);
164 } catch (PAPException e1) {
167 .audit("Transaction Failed: Unable to set status of PDP " + pdp.getId() + " to UNKNOWN: " + e);
168 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, XACMLPAPSERVLET,
169 " Unable to set status of PDP '" + pdp.getId() + "' to UNKNOWN");
172 // cleanup the connection
173 if (connection != null) {
174 connection.disconnect();
179 private boolean writePropertiesToStream(HttpURLConnection connection) {
180 try (OutputStream os = connection.getOutputStream()) {
182 properties.get(0).store(os, "");
184 properties.get(1).store(os, "");
186 if (properties.size() == 3) {
187 properties.get(2).store(os, "");
189 } catch (Exception e) {
190 PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, e, XACMLPAPSERVLET,
191 " Failed to send property file to " + pdp.getId());
192 // Since this is a server-side error, it probably does not
193 // reflect a problem on the client,
194 // so do not change the PDP status.
200 private XACMLPapServlet getPapInstance() {
201 return new XACMLPapServlet();