Containerization feature of SO
[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  * 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.sniro;
22
23
24 import static org.apache.commons.lang.StringUtils.*;
25
26 import java.util.LinkedHashMap;
27
28 import org.json.JSONObject;
29 import org.onap.so.client.exception.BadResponseException;
30
31 import org.onap.so.logger.MsoLogger;
32 import org.springframework.stereotype.Component;
33
34
35
36 @Component
37 public class SniroValidator {
38
39         private static final MsoLogger log = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, SniroValidator.class);
40
41         /**
42          * Validates the synchronous homing response from sniro manager
43          *
44          * @throws BadResponseException
45          */
46         public void validateDemandsResponse(LinkedHashMap<?, ?> response) throws BadResponseException {
47                 log.debug("Validating Sniro Managers synchronous response");
48                 if(!response.isEmpty()){
49                         JSONObject jsonResponse = new JSONObject(response);
50                         if(jsonResponse.has("requestStatus")){
51                                 String status = jsonResponse.getString("requestStatus");
52                                 if(status.equals("accepted")){
53                                         log.debug("Sniro Managers synchronous response indicates accepted");
54                                 }else{
55                                         String message = jsonResponse.getString("statusMessage");
56                                         if(isNotBlank(message)){
57                                                 log.debug("Sniro Managers response indicates failed: " + message);
58                                         }else{
59                                                 log.debug("Sniro Managers response indicates failed: no status message provided");
60                                                 message = "error message not provided";
61                                         }
62                                         throw new BadResponseException("Sniro Managers synchronous response indicates failed: " + message);
63                                 }
64                         }else{
65                                 log.debug("Sniro Managers synchronous response does not contain: request status");
66                                 throw new BadResponseException("Sniro Managers synchronous response does not contain: request status");
67                         }
68                 }else{
69                         log.debug("Sniro Managers synchronous response is empty");
70                         throw new BadResponseException("Sniro Managers synchronous response i is empty");
71                 }
72         }
73
74         /**
75          * Validates the asynchronous/callback response from sniro manager which
76          * contains the homing and licensing solutions
77          *
78          * @throws BadResponseException
79          */
80         public static void validateSolution(String response) throws BadResponseException{
81                 log.debug("Validating Sniro Managers asynchronous callback response");
82                 if(isNotBlank(response)) {
83                         JSONObject jsonResponse = new JSONObject(response);
84                         if(!jsonResponse.has("serviceException")){
85                                 log.debug("Sniro Managers asynchronous response is valid");
86                         }else{
87                                 String message = jsonResponse.getJSONObject("serviceException").getString("text");
88                                 if(isNotBlank(message)){
89                                         log.debug("Sniro Managers response contains a service exception: " + message);
90                                 }else{
91                                         log.debug("Sniro Managers response contains a service exception: no service exception text provided");
92                                         message = "error message not provided";
93                                 }
94                                 throw new BadResponseException("Sniro Managers asynchronous response contains a service exception: " + message);
95                         }
96                 }else{
97                         log.debug("Sniro Managers asynchronous response is empty");
98                         throw new BadResponseException("Sniro Managers asynchronous response is empty");
99                 }
100         }
101
102
103         /**
104          * Validates the release response from sniro conductor
105          *
106          * @throws BadResponseException
107          */
108         public void validateReleaseResponse(LinkedHashMap<?, ?> response) throws BadResponseException {
109                 log.debug("Validating Sniro Conductors response");
110                 if(!response.isEmpty()){
111                         String status = (String) response.get("status");
112                         if(isNotBlank(status)){
113                                 if(status.equals("success")){
114                                         log.debug("Sniro Conductors synchronous response indicates success");
115                                 }else{
116                                         String message = (String) response.get("message");
117                                         if(isNotBlank(message)){
118                                                 log.debug("Sniro Conductors response indicates failed: " + message);
119                                         }else{
120                                                 log.debug("Sniro Conductors response indicates failed: error message not provided");
121                                                 message = "error message not provided";
122                                         }
123                                         throw new BadResponseException("Sniro Conductors synchronous response indicates failed: " + message);
124                                 }
125                         }else{
126                                 log.debug("Sniro Managers Conductors response does not contain: status");
127                                 throw new BadResponseException("Sniro Conductors synchronous response does not contain: status");
128                         }
129                 }else{
130                         log.debug("Sniro Conductors response is empty");
131                         throw new BadResponseException("Sniro Conductors response is empty");
132                 }
133
134         }
135
136
137
138 }