aboutsummaryrefslogtreecommitdiffstats
path: root/so-optimization-clients/src/main/java/org/onap/so/client/sniro/SniroValidator.java
blob: 9a66f7531111a363e6f91f37f059a9dde01d385a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.client.sniro;


import static org.apache.commons.lang3.StringUtils.*;
import java.util.Map;
import org.json.JSONObject;
import org.onap.so.client.exception.BadResponseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;



@Component
public class SniroValidator {

    private static final Logger logger = LoggerFactory.getLogger(SniroValidator.class);

    private static final String MESSAGE_NOT_PROVIDED = "error message not provided";

    /**
     * Validates the synchronous homing response from sniro manager
     *
     * @throws BadResponseException
     */
    public void validateDemandsResponse(Map<String, Object> response) throws BadResponseException {
        logger.debug("Validating Sniro Managers synchronous response");
        if (!response.isEmpty()) {
            JSONObject jsonResponse = new JSONObject(response);
            if (jsonResponse.has("requestStatus")) {
                String status = jsonResponse.getString("requestStatus");
                if ("accepted".equals(status)) {
                    logger.debug("Sniro Managers synchronous response indicates accepted");
                } else {
                    String message = jsonResponse.getString("statusMessage");
                    if (isNotBlank(message)) {
                        logger.debug("Sniro Managers response indicates failed: {}", message);
                    } else {
                        logger.debug("Sniro Managers response indicates failed: no status message provided");
                        message = MESSAGE_NOT_PROVIDED;
                    }
                    throw new BadResponseException("Sniro Managers synchronous response indicates failed: " + message);
                }
            } else {
                logger.debug("Sniro Managers synchronous response does not contain: request status");
                throw new BadResponseException("Sniro Managers synchronous response does not contain: request status");
            }
        } else {
            logger.debug("Sniro Managers synchronous response is empty");
            throw new BadResponseException("Sniro Managers synchronous response is empty");
        }
    }

    /**
     * Validates the asynchronous/callback response from sniro manager which contains the homing and licensing solutions
     *
     * @throws BadResponseException
     */
    public static void validateSolution(String response) throws BadResponseException {
        logger.debug("Validating Sniro Managers asynchronous callback response");
        if (isNotBlank(response)) {
            JSONObject jsonResponse = new JSONObject(response);
            if (!jsonResponse.has("serviceException")) {
                logger.debug("Sniro Managers asynchronous response is valid");
            } else {
                String message = jsonResponse.getJSONObject("serviceException").getString("text");
                if (isNotBlank(message)) {
                    logger.debug("Sniro Managers response contains a service exception: {}", message);
                } else {
                    logger.debug(
                            "Sniro Managers response contains a service exception: no service exception text provided");
                    message = MESSAGE_NOT_PROVIDED;
                }
                throw new BadResponseException(
                        "Sniro Managers asynchronous response contains a service exception: " + message);
            }
        } else {
            logger.debug("Sniro Managers asynchronous response is empty");
            throw new BadResponseException("Sniro Managers asynchronous response is empty");
        }
    }


    /**
     * Validates the release response from sniro conductor
     *
     * @throws BadResponseException
     */
    public void validateReleaseResponse(Map<String, Object> response) throws BadResponseException {
        logger.debug("Validating Sniro Conductors response");
        if (!response.isEmpty()) {
            String status = (String) response.get("status");
            if (isNotBlank(status)) {
                if ("success".equals(status)) {
                    logger.debug("Sniro Conductors synchronous response indicates success");
                } else {
                    String message = (String) response.get("message");
                    if (isNotBlank(message)) {
                        logger.debug("Sniro Conductors response indicates failed: {}", message);
                    } else {
                        logger.debug("Sniro Conductors response indicates failed: error message not provided");
                        message = MESSAGE_NOT_PROVIDED;
                    }
                    throw new BadResponseException(
                            "Sniro Conductors synchronous response indicates failed: " + message);
                }
            } else {
                logger.debug("Sniro Managers Conductors response does not contain: status");
                throw new BadResponseException("Sniro Conductors synchronous response does not contain: status");
            }
        } else {
            logger.debug("Sniro Conductors response is empty");
            throw new BadResponseException("Sniro Conductors response is empty");
        }

    }

}