252ff0d6a6fce9eb17a19b0046d01ebcbaf01ceb
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / oof / OofValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Intel Corp. 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
21 package org.onap.so.client.oof;
22
23
24 import org.json.JSONObject;
25 import org.onap.so.client.exception.BadResponseException;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.stereotype.Component;
29
30 import java.util.LinkedHashMap;
31
32 import static org.apache.commons.lang.StringUtils.isNotBlank;
33
34
35 @Component
36 public class OofValidator {
37
38     private static final Logger logger = LoggerFactory.getLogger(OofValidator.class);
39
40     /**
41      * Validates the synchronous homing response from oof
42      *
43      * @throws BadResponseException
44      */
45     public void validateDemandsResponse(LinkedHashMap<?, ?> response) throws BadResponseException {
46         logger.debug("Validating oofs synchronous response");
47         if(!response.isEmpty()){
48             JSONObject jsonResponse = new JSONObject(response);
49             if(jsonResponse.has("requestStatus")){
50                 String status = jsonResponse.getString("requestStatus");
51                 if(status.equals("accepted")){
52                     logger.debug("oofs synchronous response indicates accepted");
53                 }else{
54                     String message = jsonResponse.getString("statusMessage");
55                     if(isNotBlank(message)){
56                         logger.debug("oofs response indicates failed: " + message);
57                     }else{
58                         logger.debug("oofs response indicates failed: no status message provided");
59                         message = "error message not provided";
60                     }
61                     throw new BadResponseException("oofs synchronous response indicates failed: " + message);
62                 }
63             }else{
64                 logger.debug("oofs synchronous response does not contain: request status");
65                 throw new BadResponseException("oofs synchronous response does not contain: request status");
66             }
67         }else{
68             logger.debug("oofs synchronous response is empty");
69             throw new BadResponseException("oofs synchronous response i is empty");
70         }
71     }
72
73     /**
74      * Validates the asynchronous/callback response from oof which
75      * contains the homing and licensing solutions
76      *
77      * @throws BadResponseException
78      */
79     public void validateSolution(String response) throws BadResponseException{
80         logger.debug("Validating oofs asynchronous callback response");
81         if(isNotBlank(response)) {
82             JSONObject jsonResponse = new JSONObject(response);
83             if(!jsonResponse.has("serviceException")){
84                 logger.debug("oofs asynchronous response is valid");
85             }else{
86                 String message = jsonResponse.getJSONObject("serviceException").getString("text");
87                 if(isNotBlank(message)){
88                     logger.debug("oofs response contains a service exception: " + message);
89                 }else{
90                     logger.debug("oofs response contains a service exception: no service exception text provided");
91                     message = "error message not provided";
92                 }
93                 throw new BadResponseException("oofs asynchronous response contains a service exception: " + message);
94             }
95         }else{
96             logger.debug("oofs asynchronous response is empty");
97             throw new BadResponseException("oofs asynchronous response is empty");
98         }
99     }
100
101 }