9d2577426d3c4ae14947ba776bacb15266cb0478
[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
45     public CreateUpdateDictionaryService(
46             DictionaryParameters dictionaryParameters, String requestID,
47             boolean updateFlag) {
48         this.updateFlag = updateFlag;
49         this.dictionaryParameters = dictionaryParameters;
50         if(dictionaryParameters.getRequestID()==null){
51             UUID requestUUID = null;
52             if (requestID != null && !requestID.isEmpty()) {
53                 try {
54                     requestUUID = UUID.fromString(requestID);
55                 } catch (IllegalArgumentException e) {
56                     requestUUID = UUID.randomUUID();
57                     LOGGER.info("Generated Random UUID: " + requestUUID.toString(),e);
58                 }
59             }else{
60                 requestUUID = UUID.randomUUID();
61                 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
62             }
63             this.dictionaryParameters.setRequestID(requestUUID);
64         }
65         try{
66             run();
67             specialCheck();
68         }catch(PolicyException e){
69             dictionaryResult = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
70             status = HttpStatus.BAD_REQUEST;
71         }
72     }
73
74     private void specialCheck() {
75         if(dictionaryResult== null || dictionaryResult.contains("BAD REQUEST")||dictionaryResult.contains("PE300")){
76             status = HttpStatus.BAD_REQUEST;
77         } else if (dictionaryResult.contains("Policy Exist Error")) {
78             status = HttpStatus.CONFLICT;
79         } else if (dictionaryResult.contains("PE200")){
80             status = HttpStatus.INTERNAL_SERVER_ERROR;
81         }
82     }
83
84     private void run() throws PolicyException{
85         // Check Validation. 
86         if(!getValidation()){
87             LOGGER.error(message);
88             throw new PolicyException(message);
89         }
90         // Get Result. 
91         try{
92             status = HttpStatus.OK;
93             dictionaryResult = processResult();
94         }catch (Exception e){
95             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
96             status = HttpStatus.BAD_REQUEST;
97             throw new PolicyException(e);
98         }
99     }
100
101     private String processResult() throws PolicyException{
102         String operation = null;
103         if (updateFlag){
104             operation = "update";
105         } else {
106             operation = "create";
107         }
108         JsonObject json = null;
109         try{
110             json = PolicyApiUtils.stringToJsonObject(dictionaryParameters.getDictionaryJson());
111         } catch(JsonException| IllegalStateException e){
112             message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper Dictionary JSON object : " + dictionaryParameters.getDictionaryJson();
113             LOGGER.error(message, e);
114             return message;
115         }
116         String dictionaryFields = json.toString();
117         PAPServices papServices = new PAPServices();
118         return (String) papServices.callPAP(new ByteArrayInputStream(dictionaryFields.getBytes()), new String[] {"operation="+operation, "apiflag=api", "dictionaryType="+dictionaryParameters.getDictionary()}, dictionaryParameters.getRequestID(), "dictionaryItem");
119     }
120
121     private boolean getValidation() {
122         if(dictionaryParameters==null){
123             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Dictionary Parameters are not given.";
124             return false;
125         }
126         if(dictionaryParameters.getDictionaryType()==null || dictionaryParameters.getDictionaryType().toString().trim().isEmpty()){
127             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Dictionary Type given.";
128             return false;
129         }
130         if(dictionaryParameters.getDictionary()==null || dictionaryParameters.getDictionary().trim().isEmpty()){
131             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Dictionary given.";
132             return false;
133         }
134         if(dictionaryParameters.getDictionaryJson()==null || dictionaryParameters.getDictionaryJson().isEmpty()){
135             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Dictionary JSON given.";
136             return false;
137         }
138         if (updateFlag && "MicroServiceDictionary".equalsIgnoreCase(dictionaryParameters.getDictionary())&& !dictionaryParameters.getDictionaryJson().contains("initialFields")){
139                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Mising the required field initialFields.";
140                 return false;
141         }
142         return true;
143     }
144
145     public String getResult() {
146         return dictionaryResult;
147     }
148
149     public HttpStatus getResponseCode() {
150         return status;
151     }
152
153 }