Modified the code to fix various pushPolicy issues
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / CreateUpdateDictionaryService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 package org.onap.policy.pdp.rest.api.services;
21
22 import java.io.ByteArrayInputStream;
23 import java.util.UUID;
24
25 import javax.json.JsonException;
26 import javax.json.JsonObject;
27
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;
35
36 public class CreateUpdateDictionaryService {
37     private static final Logger LOGGER = FlexLogger.getLogger(CreateUpdateDictionaryService.class.getName());
38     
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;
45
46     public CreateUpdateDictionaryService(
47             DictionaryParameters dictionaryParameters, String requestID,
48             boolean updateFlag) {
49         this.updateFlag = updateFlag;
50         this.dictionaryParameters = dictionaryParameters;
51         if(dictionaryParameters.getRequestID()==null){
52             UUID requestUUID = null;
53             if (requestID != null && !requestID.isEmpty()) {
54                 try {
55                     requestUUID = UUID.fromString(requestID);
56                 } catch (IllegalArgumentException e) {
57                     requestUUID = UUID.randomUUID();
58                     LOGGER.info("Generated Random UUID: " + requestUUID.toString(),e);
59                 }
60             }else{
61                 requestUUID = UUID.randomUUID();
62                 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
63             }
64             this.dictionaryParameters.setRequestID(requestUUID);
65         }
66
67         try{
68             run();
69             specialCheck();
70         }catch(PolicyException e){
71             dictionaryResult = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
72             status = HttpStatus.BAD_REQUEST;
73         }
74     }
75
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;
83         }
84     }
85
86     private void run() throws PolicyException{
87         // Check Validation. 
88         if(!getValidation()){
89             LOGGER.error(message);
90             throw new PolicyException(message);
91         }
92         // Get Result. 
93         try{
94             status = HttpStatus.OK;
95             dictionaryResult = processResult();
96         }catch (Exception e){
97             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
98             status = HttpStatus.BAD_REQUEST;
99             throw new PolicyException(e);
100         }
101     }
102
103     private String processResult() throws PolicyException{
104         String operation = null;
105         if (updateFlag){
106             operation = "update";
107         } else {
108             operation = "create";
109         }
110         
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");
114     }
115
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.";
120             return false;
121         }
122         if(dictionaryParameters.getDictionaryType()==null || dictionaryParameters.getDictionaryType().toString().trim().isEmpty()){
123             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Dictionary Type given.";
124             return false;
125         }
126         if(dictionaryParameters.getDictionary()==null || dictionaryParameters.getDictionary().trim().isEmpty()){
127             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Dictionary given.";
128             return false;
129         }
130         if(dictionaryParameters.getDictionaryJson()==null || dictionaryParameters.getDictionaryJson().isEmpty()){
131             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Dictionary JSON given.";
132             return false;
133         } 
134         if (updateFlag && "MicroServiceDictionary".equalsIgnoreCase(dictionaryParameters.getDictionary())&& !dictionaryParameters.getDictionaryJson().contains("initialFields")){
135                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Mising the required field initialFields.";
136                 return false;
137         }
138         
139         try{
140             json = PolicyApiUtils.stringToJsonObject(dictionaryParameters.getDictionaryJson());
141             String result = PolicyApiUtils.validateDictionaryJsonFields(json.getJsonObject("dictionaryFields"), dictionaryParameters.getDictionary());
142             
143             if(!"success".equals(result)) {
144                 message = result;
145                 return false;
146             }
147             
148         }catch(JsonException| IllegalStateException e){
149             message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper Dictionary JSON object : " + dictionaryParameters.getDictionaryJson();
150             LOGGER.error(message, e);
151             return false;
152         }
153         
154         LOGGER.info("dictionary API request validation complete and valid.");
155         return true;
156     }
157
158     public String getResult() {
159         return dictionaryResult;
160     }
161
162     public HttpStatus getResponseCode() {
163         return status;
164     }
165
166 }