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.pypdp;
23 import java.io.StringReader;
24 import java.util.UUID;
26 import javax.json.Json;
27 import javax.json.JsonObject;
28 import javax.json.JsonReader;
30 import org.openecomp.policy.api.PolicyConfigException;
31 import org.openecomp.policy.pypdp.model_pojo.PepConfigFirewallPolicyRequest;
32 import org.openecomp.policy.std.StdPolicyEngine;
34 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
36 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
38 public class ConfigFirewallPolicyRequest {
40 private StdPolicyEngine pe;
41 public ConfigFirewallPolicyRequest(StdPolicyEngine pe){
45 public String run(PepConfigFirewallPolicyRequest pep, String requestID, String operation, String userID, String passcode) {
49 // construct a UUID from the request string
50 UUID requestUUID = null;
51 if (requestID != null && !requestID.isEmpty()) {
53 requestUUID = UUID.fromString(requestID);
55 catch (IllegalArgumentException e) {
56 requestUUID = UUID.randomUUID();
57 PolicyLogger.info("Generated Random UUID: " + requestUUID.toString());
61 if (pep.getPolicyName()!= null && !pep.getPolicyName().isEmpty()) {
62 if (pep.getFirewallJson() != null && !pep.getFirewallJson().isEmpty()) {
63 if (pep.getPolicyScope() != null && !pep.getPolicyScope().isEmpty()) {
66 JsonObject json = stringToJson(pep.getFirewallJson());
68 if(!json.toString().contains("errorMessage")){
69 if (operation.equalsIgnoreCase("create")) {
70 result = pe.createConfigFirewallPolicy(pep.getPolicyName(), json, pep.getPolicyScope(), requestUUID, userID, passcode,
71 pep.getRiskLevel(), pep.getRiskType(), pep.getGuard(), pep.getTtlDate());
73 result = pe.updateConfigFirewallPolicy(pep.getPolicyName(), json, pep.getPolicyScope(), requestUUID, userID, passcode,
74 pep.getRiskLevel(), pep.getRiskType(), pep.getGuard(), pep.getTtlDate());
77 result = XACMLErrorConstants.ERROR_SCHEMA_INVALID + "BAD REQUEST: Invalid Json for firewallJson: " + pep.getFirewallJson();
79 } catch (PolicyConfigException e) {
80 result = e.getMessage();
81 } catch (Exception e) {
82 // TODO Auto-generated catch block
86 result = XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST: policyScope was null or empty.";
89 result = XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST: firewallJson was null or empty.";
92 result = XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST: policyName was null or empty.";
99 private JsonObject stringToJson(String jsonString) {
101 JsonObject json = null;
102 if (jsonString != null) {
106 //Read jsonBody to JsonObject
107 StringReader in = null;
109 in = new StringReader(jsonString);
111 JsonReader jsonReader = Json.createReader(in);
112 json = jsonReader.readObject();
114 } catch (Exception e) {
115 String jsonError = "{\"errorMessage\": \"" + e.getMessage() + "\"}";
116 StringReader error = null;
117 error = new StringReader(jsonError);
118 JsonReader jsonReader = Json.createReader(error);
119 JsonObject badJson = jsonReader.readObject();