2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.client.sniro;
26 import static org.apache.commons.lang.StringUtils.*;
28 import java.util.LinkedHashMap;
30 import org.json.JSONObject;
31 import org.onap.so.client.exception.BadResponseException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.stereotype.Component;
40 public class SniroValidator {
42 private static final Logger logger = LoggerFactory.getLogger(SniroValidator.class);
45 * Validates the synchronous homing response from sniro manager
47 * @throws BadResponseException
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");
58 String message = jsonResponse.getString("statusMessage");
59 if(isNotBlank(message)){
60 logger.debug("Sniro Managers response indicates failed: " + message);
62 logger.debug("Sniro Managers response indicates failed: no status message provided");
63 message = "error message not provided";
65 throw new BadResponseException("Sniro Managers synchronous response indicates failed: " + message);
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");
72 logger.debug("Sniro Managers synchronous response is empty");
73 throw new BadResponseException("Sniro Managers synchronous response i is empty");
78 * Validates the asynchronous/callback response from sniro manager which
79 * contains the homing and licensing solutions
81 * @throws BadResponseException
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");
90 String message = jsonResponse.getJSONObject("serviceException").getString("text");
91 if(isNotBlank(message)){
92 logger.debug("Sniro Managers response contains a service exception: " + message);
94 logger.debug("Sniro Managers response contains a service exception: no service exception text provided");
95 message = "error message not provided";
97 throw new BadResponseException("Sniro Managers asynchronous response contains a service exception: " + message);
100 logger.debug("Sniro Managers asynchronous response is empty");
101 throw new BadResponseException("Sniro Managers asynchronous response is empty");
107 * Validates the release response from sniro conductor
109 * @throws BadResponseException
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");
119 String message = (String) response.get("message");
120 if(isNotBlank(message)){
121 logger.debug("Sniro Conductors response indicates failed: " + message);
123 logger.debug("Sniro Conductors response indicates failed: error message not provided");
124 message = "error message not provided";
126 throw new BadResponseException("Sniro Conductors synchronous response indicates failed: " + message);
129 logger.debug("Sniro Managers Conductors response does not contain: status");
130 throw new BadResponseException("Sniro Conductors synchronous response does not contain: status");
133 logger.debug("Sniro Conductors response is empty");
134 throw new BadResponseException("Sniro Conductors response is empty");