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;
30 import java.util.LinkedHashMap;
32 import static org.apache.commons.lang.StringUtils.isNotBlank;
36 public class OofValidator {
38 private static final Logger logger = LoggerFactory.getLogger(OofValidator.class);
41 * Validates the synchronous homing response from oof
43 * @throws BadResponseException
45 public void validateDemandsResponse(LinkedHashMap<?, ?> response) throws BadResponseException {
46 logger.debug("Validating oofs synchronous response");
47 if(!response.isEmpty()){
48 JSONObject jsonResponse = new JSONObject(response);
49 if(jsonResponse.has("requestStatus")){
50 String status = jsonResponse.getString("requestStatus");
51 if(status.equals("accepted")){
52 logger.debug("oofs synchronous response indicates accepted");
54 String message = jsonResponse.getString("statusMessage");
55 if(isNotBlank(message)){
56 logger.debug("oofs response indicates failed: " + message);
58 logger.debug("oofs response indicates failed: no status message provided");
59 message = "error message not provided";
61 throw new BadResponseException("oofs synchronous response indicates failed: " + message);
64 logger.debug("oofs synchronous response does not contain: request status");
65 throw new BadResponseException("oofs synchronous response does not contain: request status");
68 logger.debug("oofs synchronous response is empty");
69 throw new BadResponseException("oofs synchronous response i is empty");
74 * Validates the asynchronous/callback response from oof which
75 * contains the homing and licensing solutions
77 * @throws BadResponseException
79 public void validateSolution(String response) throws BadResponseException{
80 logger.debug("Validating oofs asynchronous callback response");
81 if(isNotBlank(response)) {
82 JSONObject jsonResponse = new JSONObject(response);
83 if(!jsonResponse.has("serviceException")){
84 logger.debug("oofs asynchronous response is valid");
86 String message = jsonResponse.getJSONObject("serviceException").getString("text");
87 if(isNotBlank(message)){
88 logger.debug("oofs response contains a service exception: " + message);
90 logger.debug("oofs response contains a service exception: no service exception text provided");
91 message = "error message not provided";
93 throw new BadResponseException("oofs asynchronous response contains a service exception: " + message);
96 logger.debug("oofs asynchronous response is empty");
97 throw new BadResponseException("oofs asynchronous response is empty");