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.admin;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.io.UnsupportedEncodingException;
31 import java.net.HttpURLConnection;
33 import java.net.URLEncoder;
34 import java.nio.charset.StandardCharsets;
35 import java.util.Base64;
36 import java.util.Collections;
37 import java.util.HashMap;
41 import org.apache.commons.io.IOUtils;
42 import org.onap.policy.rest.XACMLRestProperties;
43 import org.onap.policy.rest.adapter.PolicyRestAdapter;
44 import org.onap.policy.xacml.api.XACMLErrorConstants;
45 import org.onap.policy.xacml.api.pap.OnapPDP;
46 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
47 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
48 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
49 import org.onap.policy.xacml.std.pap.StdPDP;
50 import org.onap.policy.xacml.std.pap.StdPDPGroup;
51 import org.onap.policy.xacml.std.pap.StdPDPItemSetChangeNotifier;
52 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
53 import org.onap.policy.xacml.std.pap.StdPDPStatus;
54 import com.att.research.xacml.api.pap.PAPException;
55 import com.att.research.xacml.api.pap.PDPPolicy;
56 import com.att.research.xacml.api.pap.PDPStatus;
57 import com.att.research.xacml.util.XACMLProperties;
58 import com.fasterxml.jackson.databind.DeserializationFeature;
59 import com.fasterxml.jackson.databind.ObjectMapper;
60 import com.fasterxml.jackson.databind.type.CollectionType;
61 import org.onap.policy.common.logging.flexlogger.FlexLogger;
62 import org.onap.policy.common.logging.flexlogger.Logger;
65 * Implementation of the PAPEngine interface that communicates with a PAP engine in a remote servlet
66 * through a RESTful interface
70 public class RESTfulPAPEngine extends StdPDPItemSetChangeNotifier implements PAPPolicyEngine {
71 private static final Logger LOGGER = FlexLogger.getLogger(RESTfulPAPEngine.class);
73 private static final String groupID = "groupId=";
76 // URL of the PAP Servlet that this Admin Console talks to
78 private String papServletURLString;
81 * Set up link with PAP Servlet and get our initial set of Groups
84 public RESTfulPAPEngine (String myURLString) throws PAPException, IOException {
86 // Get our URL to the PAP servlet
88 this.papServletURLString = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL);
89 if (this.papServletURLString == null || this.papServletURLString.length() == 0) {
90 String message = "The property 'POLICYENGINE_ADMIN_ACTIVE' was not set during installation. Admin Console cannot call PAP.";
91 LOGGER.error(message);
92 throw new PAPException(message);
96 // register this Admin Console with the PAP Servlet to get updates
98 Object newURL = sendToPAP("PUT", null, null, null, "adminConsoleURL=" + myURLString);
100 // assume this was a re-direct and try again
101 LOGGER.warn("Redirecting to '" + newURL + "'");
102 this.papServletURLString = (String)newURL;
103 newURL = sendToPAP("PUT", null, null, null, "adminConsoleURL=" + myURLString);
104 if (newURL != null) {
105 LOGGER.error("Failed to redirect to " + this.papServletURLString);
106 throw new PAPException("Failed to register with PAP");
113 // High-level commands used by the Admin Console code through the PAPEngine Interface
117 public OnapPDPGroup getDefaultGroup() throws PAPException {
118 return (OnapPDPGroup)sendToPAP("GET", null, null, StdPDPGroup.class, groupID, "default=");
122 public void setDefaultGroup(OnapPDPGroup group) throws PAPException {
123 sendToPAP("POST", null, null, null, groupID + group.getId(), "default=true");
126 @SuppressWarnings("unchecked")
128 public Set<OnapPDPGroup> getOnapPDPGroups() throws PAPException {
129 Set<OnapPDPGroup> newGroupSet;
130 newGroupSet = (Set<OnapPDPGroup>) this.sendToPAP("GET", null, Set.class, StdPDPGroup.class, groupID);
131 return Collections.unmodifiableSet(newGroupSet);
136 public OnapPDPGroup getGroup(String id) throws PAPException {
137 return (OnapPDPGroup)sendToPAP("GET", null, null, StdPDPGroup.class, groupID + id);
141 public void newGroup(String name, String description)
142 throws PAPException, NullPointerException {
143 String escapedName = null;
144 String escapedDescription = null;
146 escapedName = URLEncoder.encode(name, "UTF-8");
147 escapedDescription = URLEncoder.encode(description, "UTF-8");
148 } catch (UnsupportedEncodingException e) {
149 throw new PAPException("Unable to send name or description to PAP: " + e.getMessage() +e);
152 this.sendToPAP("POST", null, null, null, groupID, "groupName="+escapedName, "groupDescription=" + escapedDescription);
157 * Update the configuration on the PAP for a single Group.
161 * @throws PAPException
164 public void updateGroup(OnapPDPGroup group) throws PAPException {
169 // ASSUME that all of the policies mentioned in this group are already located in the correct directory on the PAP!
171 // Whenever a Policy is added to the group, that file must be automatically copied to the PAP from the Workspace.
175 // Copy all policies from the local machine's workspace to the PAP's PDPGroup directory.
176 // This is not efficient since most of the policies will already exist there.
177 // However, the policy files are (probably!) not too huge, and this is a good way to ensure that any corrupted files on the PAP get refreshed.
180 // now update the group object on the PAP
182 sendToPAP("PUT", group, null, null, groupID + group.getId());
183 } catch (Exception e) {
184 String message = "Unable to PUT policy '" + group.getId() + "', e:" + e;
185 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
186 throw new PAPException(message);
192 public void removeGroup(OnapPDPGroup group, OnapPDPGroup newGroup)
193 throws PAPException, NullPointerException {
194 String moveToGroupString = null;
195 if (newGroup != null) {
196 moveToGroupString = "movePDPsToGroupId=" + newGroup.getId();
198 sendToPAP("DELETE", null, null, null, groupID + group.getId(), moveToGroupString);
202 public OnapPDPGroup getPDPGroup(OnapPDP pdp) throws PAPException {
203 return getPDPGroup(pdp.getId());
207 public OnapPDPGroup getPDPGroup(String pdpId) throws PAPException {
208 return (OnapPDPGroup)sendToPAP("GET", null, null, StdPDPGroup.class, groupID, "pdpId=" + pdpId, "getPDPGroup=");
212 public OnapPDP getPDP(String pdpId) throws PAPException {
213 return (OnapPDP)sendToPAP("GET", null, null, StdPDP.class, groupID, "pdpId=" + pdpId);
217 public void newPDP(String id, OnapPDPGroup group, String name, String description, int jmxport) throws PAPException,
218 NullPointerException {
219 StdPDP newPDP = new StdPDP(id, name, description, jmxport);
220 sendToPAP("PUT", newPDP, null, null, groupID + group.getId(), "pdpId=" + id);
225 public void movePDP(OnapPDP pdp, OnapPDPGroup newGroup) throws PAPException {
226 sendToPAP("POST", null, null, null, groupID + newGroup.getId(), "pdpId=" + pdp.getId());
231 public void updatePDP(OnapPDP pdp) throws PAPException {
232 OnapPDPGroup group = getPDPGroup(pdp);
233 sendToPAP("PUT", pdp, null, null, groupID + group.getId(), "pdpId=" + pdp.getId());
238 public void removePDP(OnapPDP pdp) throws PAPException {
239 OnapPDPGroup group = getPDPGroup(pdp);
240 sendToPAP("DELETE", null, null, null, groupID + group.getId(), "pdpId=" + pdp.getId());
244 //Validate the Policy Data
245 public boolean validatePolicyRequest(PolicyRestAdapter policyAdapter, String policyType) throws PAPException {
246 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(policyAdapter.getPolicyName(), policyAdapter.getConfigBodyData(), policyAdapter.getConfigType(), "Base");
248 //send JSON object to PAP
249 return (Boolean) sendToPAP("PUT", newPAPPolicy, null, null, "operation=validate", "apiflag=admin", "policyType=" + policyType);
255 public void publishPolicy(String id, String name, boolean isRoot,
256 InputStream policy, OnapPDPGroup group) throws PAPException {
259 // copy the (one) file into the target directory on the PAP servlet
260 copyFile(id, group, policy);
262 // adjust the local copy of the group to include the new policy
263 PDPPolicy pdpPolicy = new StdPDPPolicy(id, isRoot, name);
264 group.getPolicies().add(pdpPolicy);
266 // tell the PAP servlet to include the policy in the configuration
273 * Copy a single Policy file from the input stream to the PAP Servlet.
274 * Either this works (silently) or it throws an exception.
280 * @throws PAPException
282 public void copyFile(String policyId, OnapPDPGroup group, InputStream policy) throws PAPException {
283 // send the policy file to the PAP Servlet
285 sendToPAP("POST", policy, null, null, groupID + group.getId(), "policyId="+policyId);
286 } catch (Exception e) {
287 String message = "Unable to PUT policy '" + policyId + "', e:" + e;
288 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
289 throw new PAPException(message);
295 public void copyPolicy(PDPPolicy policy, OnapPDPGroup group) throws PAPException {
296 if (policy == null || group == null) {
297 throw new PAPException("Null input policy="+policy+" group="+group);
299 try (InputStream is = new FileInputStream(new File(policy.getLocation())) ) {
300 copyFile(policy.getId(), group, is );
301 } catch (Exception e) {
302 String message = "Unable to PUT policy '" + policy.getId() + "', e:" + e;
303 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
304 throw new PAPException(message);
309 public void removePolicy(PDPPolicy policy, OnapPDPGroup group) throws PAPException {
310 throw new PAPException("NOT IMPLEMENTED");
316 * Special operation - Similar to the normal PAP operations but this one contacts the PDP directly
317 * to get detailed status info.
321 * @throws PAPException
324 public PDPStatus getStatus(OnapPDP pdp) throws PAPException {
325 return (StdPDPStatus)sendToPAP("GET", pdp, null, StdPDPStatus.class);
330 // Internal Operations called by the PAPEngine Interface methods
334 * Send a request to the PAP Servlet and get the response.
336 * The content is either an InputStream to be copied to the Request OutputStream
337 * OR it is an object that is to be encoded into JSON and pushed into the Request OutputStream.
339 * The Request parameters may be encoded in multiple "name=value" sets, or parameters may be combined by the caller.
342 * @param content - EITHER an InputStream OR an Object to be encoded in JSON
343 * @param collectionTypeClass
344 * @param responseContentClass
349 @SuppressWarnings({ "rawtypes", "unchecked" })
350 private Object sendToPAP(String method, Object content, Class collectionTypeClass, Class responseContentClass, String... parameters ) throws PAPException {
351 HttpURLConnection connection = null;
352 String papID = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID);
353 LOGGER.info("User Id is " + papID);
354 String papPass = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS);
355 Base64.Encoder encoder = Base64.getEncoder();
356 String encoding = encoder.encodeToString((papID+":"+papPass).getBytes(StandardCharsets.UTF_8));
357 Object contentObj = content;
358 LOGGER.info("Encoding for the PAP is: " + encoding);
360 String fullURL = papServletURLString;
361 if (parameters != null && parameters.length > 0) {
362 String queryString = "";
363 for (String p : parameters) {
364 queryString += "&" + p;
366 fullURL += "?" + queryString.substring(1);
369 // special case - Status (actually the detailed status) comes from the PDP directly, not the PAP
370 if ("GET".equals(method) && (contentObj instanceof OnapPDP) && responseContentClass == StdPDPStatus.class) {
371 // Adjust the url and properties appropriately
372 String pdpID =((OnapPDP)contentObj).getId();
373 fullURL = pdpID + "?type=Status";
375 if(CheckPDP.validateID(pdpID)){
376 encoding = CheckPDP.getEncoding(pdpID);
381 URL url = new URL(fullURL);
384 // Open up the connection
386 connection = (HttpURLConnection)url.openConnection();
388 // Setup our method and headers
390 connection.setRequestMethod(method);
391 connection.setUseCaches(false);
393 // Adding this in. It seems the HttpUrlConnection class does NOT
394 // properly forward our headers for POST re-direction. It does so
395 // for a GET re-direction.
397 // So we need to handle this ourselves.
399 connection.setInstanceFollowRedirects(false);
400 connection.setRequestProperty("Authorization", "Basic " + encoding);
401 connection.setDoOutput(true);
402 connection.setDoInput(true);
404 if (contentObj != null) {
405 if (contentObj instanceof InputStream) {
408 // Send our current policy configuration
410 try (OutputStream os = connection.getOutputStream()) {
411 int count = IOUtils.copy((InputStream)contentObj, os);
412 if (LOGGER.isDebugEnabled()) {
413 LOGGER.debug("copied to output, bytes="+count);
416 } catch (Exception e) {
417 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to write content in '" + method + "'", e);
420 // The contentObj is an object to be encoded in JSON
421 ObjectMapper mapper = new ObjectMapper();
422 mapper.writeValue(connection.getOutputStream(), contentObj);
428 connection.connect();
429 if (connection.getResponseCode() == 204) {
430 LOGGER.info("Success - no content.");
432 } else if (connection.getResponseCode() == 200) {
433 LOGGER.info("Success. We have a return object.");
434 String isValidData = connection.getHeaderField("isValidData");
435 String isSuccess = connection.getHeaderField("successMapKey");
436 Map<String, String> successMap = new HashMap<>();
437 if (isValidData != null && "true".equalsIgnoreCase(isValidData)){
438 LOGGER.info("Policy Data is valid.");
440 } else if (isValidData != null && "false".equalsIgnoreCase(isValidData)) {
441 LOGGER.info("Policy Data is invalid.");
443 } else if (isSuccess != null && "success".equalsIgnoreCase(isSuccess)) {
444 LOGGER.info("Policy Created Successfully!" );
445 String finalPolicyPath = connection.getHeaderField("finalPolicyPath");
446 successMap.put("success", finalPolicyPath);
448 } else if (isSuccess != null && "error".equalsIgnoreCase(isSuccess)) {
449 LOGGER.info("There was an error while creating the policy!");
450 successMap.put("error", "error");
453 // get the response content into a String
455 // read the inputStream into a buffer (trick found online scans entire input looking for end-of-file)
456 java.util.Scanner scanner = new java.util.Scanner(connection.getInputStream());
457 scanner.useDelimiter("\\A");
458 json = scanner.hasNext() ? scanner.next() : "";
460 LOGGER.info("JSON response from PAP: " + json);
462 // convert Object sent as JSON into local object
463 ObjectMapper mapper = new ObjectMapper();
464 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
465 if (collectionTypeClass != null) {
466 // collection of objects expected
467 final CollectionType javaType =
468 mapper.getTypeFactory().constructCollectionType(collectionTypeClass, responseContentClass);
470 Object objectFromJSON = mapper.readValue(json, javaType);
471 return objectFromJSON;
473 // single value object expected
474 Object objectFromJSON = mapper.readValue(json, responseContentClass);
475 return objectFromJSON;
479 } else if (connection.getResponseCode() >= 300 && connection.getResponseCode() <= 399) {
481 String newURL = connection.getHeaderField("Location");
482 if (newURL == null) {
483 LOGGER.error("No Location header to redirect to when response code="+connection.getResponseCode());
484 throw new IOException("No redirect Location header when response code="+connection.getResponseCode());
486 int qIndex = newURL.indexOf("?");
488 newURL = newURL.substring(0, qIndex);
490 LOGGER.info("Redirect seen. Redirecting " + fullURL + " to " + newURL);
493 LOGGER.warn("Unexpected response code: " + connection.getResponseCode() + " message: " + connection.getResponseMessage());
494 throw new IOException("Server Response: " + connection.getResponseCode() + ": " + connection.getResponseMessage());
497 } catch (Exception e) {
498 LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "HTTP Request/Response to PAP: " + e,e);
499 throw new PAPException("Request/Response threw :" + e);
501 // cleanup the connection
502 if (connection != null) {
504 // For some reason trying to get the inputStream from the connection
505 // throws an exception rather than returning null when the InputStream does not exist.
506 InputStream is = connection.getInputStream();
510 } catch (IOException ex) {
511 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to close connection: " + ex, ex);
513 connection.disconnect();