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