dcccd319c6de8d528a4f88a561940b0b4adef21b
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / sniro / SniroValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.client.sniro;
24
25
26 import static org.apache.commons.lang.StringUtils.*;
27
28 import java.util.LinkedHashMap;
29
30 import org.json.JSONObject;
31 import org.onap.so.client.exception.BadResponseException;
32
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.stereotype.Component;
36
37
38
39 @Component
40 public class SniroValidator {
41
42         private static final Logger logger = LoggerFactory.getLogger(SniroValidator.class);
43
44         /**
45          * Validates the synchronous homing response from sniro manager
46          *
47          * @throws BadResponseException
48          */
49         public void validateDemandsResponse(LinkedHashMap<String, Object> response) throws BadResponseException {
50                 logger.debug("Validating Sniro Managers synchronous response");
51                 if(!response.isEmpty()){
52                         JSONObject jsonResponse = new JSONObject(response);
53                         if(jsonResponse.has("requestStatus")){
54                                 String status = jsonResponse.getString("requestStatus");
55                                 if(status.equals("accepted")){
56                                         logger.debug("Sniro Managers synchronous response indicates accepted");
57                                 }else{
58                                         String message = jsonResponse.getString("statusMessage");
59                                         if(isNotBlank(message)){
60                                                 logger.debug("Sniro Managers response indicates failed: " + message);
61                                         }else{
62                                                 logger.debug("Sniro Managers response indicates failed: no status message provided");
63                                                 message = "error message not provided";
64                                         }
65                                         throw new BadResponseException("Sniro Managers synchronous response indicates failed: " + message);
66                                 }
67                         }else{
68                                 logger.debug("Sniro Managers synchronous response does not contain: request status");
69                                 throw new BadResponseException("Sniro Managers synchronous response does not contain: request status");
70                         }
71                 }else{
72                         logger.debug("Sniro Managers synchronous response is empty");
73                         throw new BadResponseException("Sniro Managers synchronous response i is empty");
74                 }
75         }
76
77         /**
78          * Validates the asynchronous/callback response from sniro manager which
79          * contains the homing and licensing solutions
80          *
81          * @throws BadResponseException
82          */
83         public static void validateSolution(String response) throws BadResponseException{
84                 logger.debug("Validating Sniro Managers asynchronous callback response");
85                 if(isNotBlank(response)) {
86                         JSONObject jsonResponse = new JSONObject(response);
87                         if(!jsonResponse.has("serviceException")){
88                                 logger.debug("Sniro Managers asynchronous response is valid");
89                         }else{
90                                 String message = jsonResponse.getJSONObject("serviceException").getString("text");
91                                 if(isNotBlank(message)){
92                                         logger.debug("Sniro Managers response contains a service exception: " + message);
93                                 }else{
94                                         logger.debug("Sniro Managers response contains a service exception: no service exception text provided");
95                                         message = "error message not provided";
96                                 }
97                                 throw new BadResponseException("Sniro Managers asynchronous response contains a service exception: " + message);
98                         }
99                 }else{
100                         logger.debug("Sniro Managers asynchronous response is empty");
101                         throw new BadResponseException("Sniro Managers asynchronous response is empty");
102                 }
103         }
104
105
106         /**
107          * Validates the release response from sniro conductor
108          *
109          * @throws BadResponseException
110          */
111         public void validateReleaseResponse(LinkedHashMap<String, Object> response) throws BadResponseException {
112                 logger.debug("Validating Sniro Conductors response");
113                 if(!response.isEmpty()){
114                         String status = (String) response.get("status");
115                         if(isNotBlank(status)){
116                                 if(status.equals("success")){
117                                         logger.debug("Sniro Conductors synchronous response indicates success");
118                                 }else{
119                                         String message = (String) response.get("message");
120                                         if(isNotBlank(message)){
121                                                 logger.debug("Sniro Conductors response indicates failed: " + message);
122                                         }else{
123                                                 logger.debug("Sniro Conductors response indicates failed: error message not provided");
124                                                 message = "error message not provided";
125                                         }
126                                         throw new BadResponseException("Sniro Conductors synchronous response indicates failed: " + message);
127                                 }
128                         }else{
129                                 logger.debug("Sniro Managers Conductors response does not contain: status");
130                                 throw new BadResponseException("Sniro Conductors synchronous response does not contain: status");
131                         }
132                 }else{
133                         logger.debug("Sniro Conductors response is empty");
134                         throw new BadResponseException("Sniro Conductors response is empty");
135                 }
136
137         }
138
139 }