9b86204e0e4a49242ea1ab07b28e65429573cdcf
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / GetDictionaryService.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.util.UUID;
23
24 import javax.json.JsonException;
25 import javax.json.JsonObject;
26
27 import org.onap.policy.api.DictionaryParameters;
28 import org.onap.policy.api.DictionaryResponse;
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.std.StdDictionaryResponse;
34 import org.onap.policy.xacml.api.XACMLErrorConstants;
35 import org.springframework.http.HttpStatus;
36
37 public class GetDictionaryService {
38     private static final Logger LOGGER = FlexLogger.getLogger(GetDictionaryService.class.getName());
39     
40     private DictionaryResponse dictionaryResponse = null;
41     private HttpStatus status = HttpStatus.BAD_REQUEST;
42     private String message = null;
43     private DictionaryParameters dictionaryParameters = null;
44
45     public GetDictionaryService(DictionaryParameters dictionaryParameters,
46             String requestID) {
47         this.dictionaryParameters = dictionaryParameters;
48         if(dictionaryParameters.getRequestID()==null){
49             UUID requestUUID = null;
50             if (requestID != null && !requestID.isEmpty()) {
51                 try {
52                     requestUUID = UUID.fromString(requestID);
53                 } catch (IllegalArgumentException e) {
54                     requestUUID = UUID.randomUUID();
55                     LOGGER.info("Generated Random UUID: " + requestUUID.toString(), e);
56                 }
57             }else{
58                 requestUUID = UUID.randomUUID();
59                 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
60             }
61             this.dictionaryParameters.setRequestID(requestUUID);
62         }
63         try{
64             run();
65             specialCheck();
66         }catch(PolicyException e){
67             StdDictionaryResponse dictionaryResponse = new StdDictionaryResponse();
68             dictionaryResponse.setResponseMessage(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
69             this.dictionaryResponse = dictionaryResponse;
70             status = HttpStatus.BAD_REQUEST;
71         }
72     }
73
74     private void specialCheck() {
75         if(dictionaryResponse!=null && (dictionaryResponse.getResponseMessage()!=null && dictionaryResponse.getResponseMessage().contains("PE300"))){
76                 status = HttpStatus.BAD_REQUEST;
77         }
78     }
79
80     private void run() throws PolicyException{
81      // Check Validation. 
82         if(!getValidation()){
83             LOGGER.error(message);
84             throw new PolicyException(message);
85         }
86         // Get Result. 
87         try{
88             status = HttpStatus.OK;
89             dictionaryResponse = processResult();
90         }catch (Exception e){
91             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
92             status = HttpStatus.BAD_REQUEST;
93             throw new PolicyException(e);
94         }
95     }
96
97     private DictionaryResponse processResult() throws PolicyException{
98         StdDictionaryResponse response = new StdDictionaryResponse();
99         PAPServices papServices = new PAPServices();
100         String result = (String) papServices.callPAP(null, new String[] {"operation=get", "apiflag=api", "dictionaryType="+dictionaryParameters.getDictionary()}, dictionaryParameters.getRequestID(), "dictionaryItem");
101         
102         if (result!=null && result.contains("data")) {
103             String jsonString = formatDictionaryJson(result);
104             String responseMessage = result.substring(0, 82);
105             JsonObject json = null;
106             try{
107                 json = PolicyApiUtils.stringToJsonObject(jsonString.replace("\\\\\\", "\\"));
108                 String datas = json.getString("data").replaceFirst("\"\\[", "[");
109                 int i = datas.lastIndexOf("]");
110                 if( i>=0 ) {
111                         datas = new StringBuilder(datas).replace(i, i+2,"]").toString();
112                 }
113                 json = PolicyApiUtils.stringToJsonObject(datas);
114             } catch(JsonException| IllegalStateException e){
115                 message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper Dictionary JSON object : " + dictionaryParameters.getDictionaryJson();
116                 LOGGER.error(message, e);
117                 response.setResponseMessage(message);
118                 response.setResponseCode(400);
119                 return response;
120             }
121             response.setResponseCode(papServices.getResponseCode());
122             response.setDictionaryJson(json);
123             response.setResponseMessage(responseMessage);                     
124         } else {
125             response.setResponseCode(400);
126             response.setResponseMessage(result);
127             if(result!=null && result.contains("PE200")){
128                 status=HttpStatus.INTERNAL_SERVER_ERROR;
129             }else{
130                 status=HttpStatus.BAD_REQUEST;
131             }
132         }
133         return response;
134     }
135
136     private String formatDictionaryJson(String result) {
137         String jsonString = result.substring(82);
138         String dictionary = dictionaryParameters.getDictionary();
139         
140         switch (dictionary) {
141         case "OnapName":
142             jsonString = jsonString.replace("onapNameDictionaryDatas", "DictionaryDatas");
143             break;
144         case "Attribute":
145             jsonString = jsonString.replace("attributeDictionaryDatas", "DictionaryDatas");
146             break;
147             case "Action":
148             jsonString = jsonString.replace("actionPolicyDictionaryDatas", "DictionaryDatas");
149             break;
150         case "BRMSParamTemplate":
151             jsonString = jsonString.replace("brmsParamDictionaryDatas", "DictionaryDatas");
152             break;
153         case "VSCLAction":
154             jsonString = jsonString.replace("vsclActionDictionaryDatas", "DictionaryDatas");
155             break;
156         case "VNFType":
157             jsonString = jsonString.replace("vnfTypeDictionaryDatas", "DictionaryDatas");
158             break;
159         case "PEPOptions":
160             jsonString = jsonString.replace("pepOptionsDictionaryDatas", "DictionaryDatas");
161             break;
162         case "Varbind":
163             jsonString = jsonString.replace("varbindDictionaryDatas", "DictionaryDatas");
164             break;
165         case "Service":
166             jsonString = jsonString.replace("closedLoopServiceDictionaryDatas", "DictionaryDatas");
167             break;
168         case "Site":
169             jsonString = jsonString.replace("closedLoopSiteDictionaryDatas", "DictionaryDatas");
170             break;
171         case "Settings":
172             jsonString = jsonString.replace("settingsDictionaryDatas", "DictionaryDatas");
173             break;
174         case "RainyDayTreatments":
175                 jsonString = jsonString.replace("rainyDayDictionaryDatas", "DictionaryDatas");
176                 break;
177         case "DescriptiveScope":
178             jsonString = jsonString.replace("descriptiveScopeDictionaryDatas", "DictionaryDatas");
179             break;
180         case "Enforcer":
181             jsonString = jsonString.replace("enforcerDictionaryDatas", "DictionaryDatas");
182             break;
183         case "ActionList":
184             jsonString = jsonString.replace("actionListDictionaryDatas", "DictionaryDatas");
185             break;
186         case "ProtocolList":
187             jsonString = jsonString.replace("protocolListDictionaryDatas", "DictionaryDatas");
188             break;
189         case "Zone":
190             jsonString = jsonString.replace("zoneDictionaryDatas", "DictionaryDatas");
191             break;
192         case "SecurityZone":
193             jsonString = jsonString.replace("securityZoneDictionaryDatas", "DictionaryDatas");
194             break;
195         case "PrefixList":
196             jsonString = jsonString.replace("prefixListDictionaryDatas", "DictionaryDatas");
197             break;
198         case "AddressGroup":
199             jsonString = jsonString.replace("addressGroupDictionaryDatas", "DictionaryDatas");
200             break;
201         case "ServiceGroup":
202             jsonString = jsonString.replace("serviceGroupDictionaryDatas", "DictionaryDatas");
203             break;
204         case "ServiceList":
205             jsonString = jsonString.replace("serviceListDictionaryDatas", "DictionaryDatas");
206             break;
207         case "TermList":
208         case "RuleList":
209         case "FirewallRuleList":
210         case "Term":
211             jsonString = jsonString.replace("termListDictionaryDatas", "DictionaryDatas");
212             break;
213         case "MicroServiceLocation":
214             jsonString = jsonString.replace("microServiceLocationDictionaryDatas", "DictionaryDatas");
215             break;
216         case "MicroServiceConfigName":
217             jsonString = jsonString.replace("microServiceCongigNameDictionaryDatas", "DictionaryDatas");
218             break;
219         case "DCAEUUID":
220             jsonString = jsonString.replace("dcaeUUIDDictionaryDatas", "DictionaryDatas");
221             break;
222         case "MicroServiceModels":
223             jsonString = jsonString.replace("microServiceModelsDictionaryDatas", "DictionaryDatas");
224             break;
225         case "PolicyScopeService":
226             jsonString = jsonString.replace("psServiceDictionaryDatas", "DictionaryDatas");
227             break;
228         case "PolicyScopeResource":
229             jsonString = jsonString.replace("psResourceDictionaryDatas", "DictionaryDatas");
230             break;
231         case "PolicyScopeType":
232             jsonString = jsonString.replace("psTypeDictionaryDatas", "DictionaryDatas");
233             break;
234         case "PolicyScopeClosedLoop":
235             jsonString = jsonString.replace("psClosedLoopDictionaryDatas", "DictionaryDatas");
236             break;
237         case "GroupPolicyScopeList":
238             jsonString = jsonString.replace("groupPolicyScopeListDatas", "DictionaryDatas");
239             break;
240         case "RiskType":
241             jsonString = jsonString.replace("riskTypeDictionaryDatas", "DictionaryDatas");
242             break;
243         case "SafePolicyWarning":
244             jsonString = jsonString.replace("safePolicyWarningDatas", "DictionaryDatas");
245             break;
246         case "MicroServiceDictionary":
247             jsonString = jsonString.replace("microServiceDictionaryDatas", "DictionaryDatas");
248             break;
249         default:
250             break;
251         }
252         return jsonString;
253     }
254
255     private boolean getValidation() {
256         if(dictionaryParameters==null){
257             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Dictionary Parameters are not given.";
258             return false;
259         }
260         if(dictionaryParameters.getDictionaryType()==null || dictionaryParameters.getDictionaryType().toString().trim().isEmpty()){
261             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Dictionary Type given.";
262             return false;
263         }
264         if(dictionaryParameters.getDictionary()==null || dictionaryParameters.getDictionary().trim().isEmpty()){
265             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Dictionary given.";
266             return false;
267         }
268         return true;
269     }
270
271     public DictionaryResponse getResult() {
272         return dictionaryResponse;
273     }
274
275     public HttpStatus getResponseCode() {
276         return status;
277     }
278
279 }