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.lang3.StringUtils.*;
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;
37 public class SniroValidator {
39 private static final Logger logger = LoggerFactory.getLogger(SniroValidator.class);
41 private static final String MESSAGE_NOT_PROVIDED = "error message not provided";
44 * Validates the synchronous homing response from sniro manager
46 * @throws BadResponseException
48 public void validateDemandsResponse(Map<String, Object> response) throws BadResponseException {
49 logger.debug("Validating Sniro Managers synchronous response");
50 if (!response.isEmpty()) {
51 JSONObject jsonResponse = new JSONObject(response);
52 if (jsonResponse.has("requestStatus")) {
53 String status = jsonResponse.getString("requestStatus");
54 if ("accepted".equals(status)) {
55 logger.debug("Sniro Managers synchronous response indicates accepted");
57 String message = jsonResponse.getString("statusMessage");
58 if (isNotBlank(message)) {
59 logger.debug("Sniro Managers response indicates failed: {}", message);
61 logger.debug("Sniro Managers response indicates failed: no status message provided");
62 message = MESSAGE_NOT_PROVIDED;
64 throw new BadResponseException("Sniro Managers synchronous response indicates failed: " + message);
67 logger.debug("Sniro Managers synchronous response does not contain: request status");
68 throw new BadResponseException("Sniro Managers synchronous response does not contain: request status");
71 logger.debug("Sniro Managers synchronous response is empty");
72 throw new BadResponseException("Sniro Managers synchronous response is empty");
77 * Validates the asynchronous/callback response from sniro manager which contains the homing and licensing solutions
79 * @throws BadResponseException
81 public static void validateSolution(String response) throws BadResponseException {
82 logger.debug("Validating Sniro Managers asynchronous callback response");
83 if (isNotBlank(response)) {
84 JSONObject jsonResponse = new JSONObject(response);
85 if (!jsonResponse.has("serviceException")) {
86 logger.debug("Sniro Managers asynchronous response is valid");
88 String message = jsonResponse.getJSONObject("serviceException").getString("text");
89 if (isNotBlank(message)) {
90 logger.debug("Sniro Managers response contains a service exception: {}", message);
93 "Sniro Managers response contains a service exception: no service exception text provided");
94 message = MESSAGE_NOT_PROVIDED;
96 throw new BadResponseException(
97 "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(Map<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 ("success".equals(status)) {
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 = MESSAGE_NOT_PROVIDED;
126 throw new BadResponseException(
127 "Sniro Conductors synchronous response indicates failed: " + message);
130 logger.debug("Sniro Managers Conductors response does not contain: status");
131 throw new BadResponseException("Sniro Conductors synchronous response does not contain: status");
134 logger.debug("Sniro Conductors response is empty");
135 throw new BadResponseException("Sniro Conductors response is empty");