fc161254334d2ee02efc4922409795772e3ce7a2
[so.git] / so-optimization-clients / 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.lang3.StringUtils.*;
27 import java.util.LinkedHashMap;
28 import org.json.JSONObject;
29 import org.onap.so.client.exception.BadResponseException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Component;
33
34
35
36 @Component
37 public class SniroValidator {
38
39     private static final Logger logger = LoggerFactory.getLogger(SniroValidator.class);
40
41     /**
42      * Validates the synchronous homing response from sniro manager
43      *
44      * @throws BadResponseException
45      */
46     public void validateDemandsResponse(LinkedHashMap<String, Object> response) throws BadResponseException {
47         logger.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 ("accepted".equals(status)) {
53                     logger.debug("Sniro Managers synchronous response indicates accepted");
54                 } else {
55                     String message = jsonResponse.getString("statusMessage");
56                     if (isNotBlank(message)) {
57                         logger.debug("Sniro Managers response indicates failed: " + message);
58                     } else {
59                         logger.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                 logger.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             logger.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 contains the homing and licensing solutions
76      *
77      * @throws BadResponseException
78      */
79     public static void validateSolution(String response) throws BadResponseException {
80         logger.debug("Validating Sniro Managers asynchronous callback response");
81         if (isNotBlank(response)) {
82             JSONObject jsonResponse = new JSONObject(response);
83             if (!jsonResponse.has("serviceException")) {
84                 logger.debug("Sniro Managers asynchronous response is valid");
85             } else {
86                 String message = jsonResponse.getJSONObject("serviceException").getString("text");
87                 if (isNotBlank(message)) {
88                     logger.debug("Sniro Managers response contains a service exception: " + message);
89                 } else {
90                     logger.debug(
91                             "Sniro Managers response contains a service exception: no service exception text provided");
92                     message = "error message not provided";
93                 }
94                 throw new BadResponseException(
95                         "Sniro Managers asynchronous response contains a service exception: " + message);
96             }
97         } else {
98             logger.debug("Sniro Managers asynchronous response is empty");
99             throw new BadResponseException("Sniro Managers asynchronous response is empty");
100         }
101     }
102
103
104     /**
105      * Validates the release response from sniro conductor
106      *
107      * @throws BadResponseException
108      */
109     public void validateReleaseResponse(LinkedHashMap<String, Object> response) throws BadResponseException {
110         logger.debug("Validating Sniro Conductors response");
111         if (!response.isEmpty()) {
112             String status = (String) response.get("status");
113             if (isNotBlank(status)) {
114                 if ("success".equals(status)) {
115                     logger.debug("Sniro Conductors synchronous response indicates success");
116                 } else {
117                     String message = (String) response.get("message");
118                     if (isNotBlank(message)) {
119                         logger.debug("Sniro Conductors response indicates failed: " + message);
120                     } else {
121                         logger.debug("Sniro Conductors response indicates failed: error message not provided");
122                         message = "error message not provided";
123                     }
124                     throw new BadResponseException(
125                             "Sniro Conductors synchronous response indicates failed: " + message);
126                 }
127             } else {
128                 logger.debug("Sniro Managers Conductors response does not contain: status");
129                 throw new BadResponseException("Sniro Conductors synchronous response does not contain: status");
130             }
131         } else {
132             logger.debug("Sniro Conductors response is empty");
133             throw new BadResponseException("Sniro Conductors response is empty");
134         }
135
136     }
137
138 }