2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Intel Corp. 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.client.oof;
24 import org.json.JSONObject;
25 import org.onap.so.client.exception.BadResponseException;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.stereotype.Component;
29 import java.util.LinkedHashMap;
30 import static org.apache.commons.lang3.StringUtils.isNotBlank;
34 public class OofValidator {
36 private static final Logger logger = LoggerFactory.getLogger(OofValidator.class);
39 * Validates the synchronous homing response from oof
41 * @throws BadResponseException
43 public void validateDemandsResponse(LinkedHashMap<?, ?> response) throws BadResponseException {
44 logger.debug("Validating oofs synchronous response");
45 if (!response.isEmpty()) {
46 JSONObject jsonResponse = new JSONObject(response);
47 if (jsonResponse.has("requestStatus")) {
48 String status = jsonResponse.getString("requestStatus");
49 if (status.equals("accepted")) {
50 logger.debug("oofs synchronous response indicates accepted");
52 String message = jsonResponse.getString("statusMessage");
53 if (isNotBlank(message)) {
54 logger.debug("oofs response indicates failed: " + message);
56 logger.debug("oofs response indicates failed: no status message provided");
57 message = "error message not provided";
59 throw new BadResponseException("oofs synchronous response indicates failed: " + message);
62 logger.debug("oofs synchronous response does not contain: request status");
63 throw new BadResponseException("oofs synchronous response does not contain: request status");
66 logger.debug("oofs synchronous response is empty");
67 throw new BadResponseException("oofs synchronous response i is empty");
72 * Validates the asynchronous/callback response from oof which contains the homing and licensing solutions
74 * @throws BadResponseException
76 public void validateSolution(String response) throws BadResponseException {
77 logger.debug("Validating oofs asynchronous callback response");
78 if (isNotBlank(response)) {
79 JSONObject jsonResponse = new JSONObject(response);
80 if (!jsonResponse.has("serviceException")) {
81 logger.debug("oofs asynchronous response is valid");
83 String message = jsonResponse.getJSONObject("serviceException").getString("text");
84 if (isNotBlank(message)) {
85 logger.debug("oofs response contains a service exception: " + message);
87 logger.debug("oofs response contains a service exception: no service exception text provided");
88 message = "error message not provided";
90 throw new BadResponseException("oofs asynchronous response contains a service exception: " + message);
93 logger.debug("oofs asynchronous response is empty");
94 throw new BadResponseException("oofs asynchronous response is empty");