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=========================================================
20 package org.onap.policy.pdp.rest.api.services;
22 import java.io.ByteArrayInputStream;
23 import java.util.UUID;
25 import javax.json.JsonException;
26 import javax.json.JsonObject;
28 import org.onap.policy.api.DictionaryParameters;
29 import org.onap.policy.api.PolicyException;
30 import org.onap.policy.common.logging.flexlogger.FlexLogger;
31 import org.onap.policy.common.logging.flexlogger.Logger;
32 import org.onap.policy.pdp.rest.api.utils.PolicyApiUtils;
33 import org.onap.policy.xacml.api.XACMLErrorConstants;
34 import org.springframework.http.HttpStatus;
36 public class CreateUpdateDictionaryService {
37 private static final Logger LOGGER = FlexLogger.getLogger(CreateUpdateDictionaryService.class.getName());
39 private String dictionaryResult = null;
40 private HttpStatus status = HttpStatus.BAD_REQUEST;
41 private String message = null;
42 private Boolean updateFlag = false;
43 private DictionaryParameters dictionaryParameters = null;
44 private JsonObject json = null;
46 public CreateUpdateDictionaryService(
47 DictionaryParameters dictionaryParameters, String requestID,
49 this.updateFlag = updateFlag;
50 this.dictionaryParameters = dictionaryParameters;
51 if(dictionaryParameters.getRequestID()==null){
52 UUID requestUUID = null;
53 if (requestID != null && !requestID.isEmpty()) {
55 requestUUID = UUID.fromString(requestID);
56 } catch (IllegalArgumentException e) {
57 requestUUID = UUID.randomUUID();
58 LOGGER.info("Generated Random UUID: " + requestUUID.toString(),e);
61 requestUUID = UUID.randomUUID();
62 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
64 this.dictionaryParameters.setRequestID(requestUUID);
70 }catch(PolicyException e){
71 dictionaryResult = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
72 status = HttpStatus.BAD_REQUEST;
76 private void specialCheck() {
77 if(dictionaryResult== null || dictionaryResult.contains("BAD REQUEST")||dictionaryResult.contains("PE300")){
78 status = HttpStatus.BAD_REQUEST;
79 } else if (dictionaryResult.contains("Policy Exist Error")) {
80 status = HttpStatus.CONFLICT;
81 } else if (dictionaryResult.contains("PE200")){
82 status = HttpStatus.INTERNAL_SERVER_ERROR;
86 private void run() throws PolicyException{
89 LOGGER.error(message);
90 throw new PolicyException(message);
94 status = HttpStatus.OK;
95 dictionaryResult = processResult();
97 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
98 status = HttpStatus.BAD_REQUEST;
99 throw new PolicyException(e);
103 private String processResult() throws PolicyException{
104 String operation = null;
106 operation = "update";
108 operation = "create";
111 String dictionaryFields = json.toString();
112 PAPServices papServices = new PAPServices();
113 return (String) papServices.callPAP(new ByteArrayInputStream(dictionaryFields.getBytes()), new String[] {"operation="+operation, "apiflag=api", "dictionaryType="+dictionaryParameters.getDictionary()}, dictionaryParameters.getRequestID(), "dictionaryItem");
116 private boolean getValidation() {
117 LOGGER.info("Start validating create or update dictionary request.");
118 if(dictionaryParameters==null){
119 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Dictionary Parameters are not given.";
122 if(dictionaryParameters.getDictionaryType()==null || dictionaryParameters.getDictionaryType().toString().trim().isEmpty()){
123 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Dictionary Type given.";
126 if(dictionaryParameters.getDictionary()==null || dictionaryParameters.getDictionary().trim().isEmpty()){
127 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Dictionary given.";
130 if(dictionaryParameters.getDictionaryJson()==null || dictionaryParameters.getDictionaryJson().isEmpty()){
131 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Dictionary JSON given.";
134 if (updateFlag && "MicroServiceDictionary".equalsIgnoreCase(dictionaryParameters.getDictionary())&& !dictionaryParameters.getDictionaryJson().contains("initialFields")){
135 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Mising the required field initialFields.";
140 json = PolicyApiUtils.stringToJsonObject(dictionaryParameters.getDictionaryJson());
141 String result = PolicyApiUtils.validateDictionaryJsonFields(json.getJsonObject("dictionaryFields"), dictionaryParameters.getDictionary());
143 if(!"success".equals(result)) {
148 }catch(JsonException| IllegalStateException e){
149 message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper Dictionary JSON object : " + dictionaryParameters.getDictionaryJson();
150 LOGGER.error(message, e);
154 LOGGER.info("dictionary API request validation complete and valid.");
158 public String getResult() {
159 return dictionaryResult;
162 public HttpStatus getResponseCode() {