aboutsummaryrefslogtreecommitdiffstats
path: root/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/helpers/SDCClientHelper.java
blob: 7aa6e0f8319e5ab9c34fd2cba776234c58134012 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 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.apihandlerinfra.tenantisolation.helpers;

import java.net.URL;
import java.util.UUID;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import org.apache.http.HttpStatus;
import org.json.JSONException;
import org.json.JSONObject;
import org.onap.so.apihandler.common.ErrorNumbers;
import org.onap.so.apihandlerinfra.exceptions.ApiException;
import org.onap.so.apihandlerinfra.exceptions.ValidateException;
import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
import org.onap.so.client.HttpClient;
import org.onap.so.client.HttpClientFactory;
import org.onap.logging.filter.base.ErrorCode;
import org.onap.so.logger.MessageEnum;
import org.onap.so.utils.CryptoUtils;
import org.onap.logging.filter.base.ONAPComponents;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class SDCClientHelper {

    private static Logger logger = LoggerFactory.getLogger(SDCClientHelper.class);
    private static final String SDC_CONTENT_TYPE = "application/json";
    private static final String SDC_ACCEPT_TYPE = "application/json";
    private static final String PARTIAL_SDC_URI = "/sdc/v1/catalog/services/";

    private static final String MESSAGE_UNDEFINED_ERROR = "Undefined Error Message!";
    private static final String MESSAGE_UNEXPECTED_FORMAT = "Unexpected response format from SDC.";
    private final HttpClientFactory httpClientFactory = new HttpClientFactory();

    private static final String STATUS_CODE = "statusCode";
    private static final String MESSAGE = "message";
    private static final String MESSAGE_ID = "messageId";
    private static final String REQUEST_ERROR = "requestError";
    private static final String SERVICE_EXCEPTION = "serviceException";
    private static final String POLICY_EXCEPTION = "policyException";

    @Value("${mso.sdc.endpoint}")
    private String sdcEndpoint;
    @Value("${mso.sdc.activate.userid}")
    private String sdcActivateUserId;
    @Value("${mso.sdc.activate.instanceid}")
    private String sdcActivateInstanceId;
    @Value("${mso.sdc.client.auth}")
    private String sdcClientAuth;
    @Value("${mso.msoKey}")
    private String msoKey;

    /**
     * Send POST request to SDC for operational activation
     * 
     * @param serviceModelVersionI - String
     * @param operationalEnvironmentId - String
     * @param workloadContext - String
     * @return sdcResponseJsonObj - JSONObject object
     * @throws JSONException
     */
    public JSONObject postActivateOperationalEnvironment(String serviceModelVersionId, String operationalEnvironmentId,
            String workloadContext) throws ApiException {
        JSONObject sdcResponseJsonObj = new JSONObject();

        try {
            String urlString = this.buildUriBuilder(serviceModelVersionId, operationalEnvironmentId);
            String jsonPayload = this.buildJsonWorkloadContext(workloadContext);
            String basicAuthCred = getBasicAuth();

            if (basicAuthCred == null || "".equals(basicAuthCred)) {
                ErrorLoggerInfo errorLoggerInfo =
                        new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcessError)
                                .build();

                throw new ValidateException.Builder(
                        " SDC credentials 'mso.sdc.client.auth' not setup in properties file!",
                        HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo)
                                .build();
            }

            URL url = new URL(urlString);

            HttpClient httpClient = httpClientFactory.newJsonClient(url, ONAPComponents.SDC);
            httpClient.addBasicAuthHeader(sdcClientAuth, msoKey);
            httpClient.addAdditionalHeader("X-ECOMP-InstanceID", sdcActivateInstanceId);
            httpClient.addAdditionalHeader("Content-Type", SDCClientHelper.SDC_CONTENT_TYPE);
            httpClient.addAdditionalHeader("Accept", SDCClientHelper.SDC_ACCEPT_TYPE);
            httpClient.addAdditionalHeader("USER_ID", sdcActivateUserId);

            Response apiResponse = setHttpPostResponse(httpClient, jsonPayload);
            int statusCode = apiResponse.getStatus();;

            String responseData = apiResponse.readEntity(String.class);
            sdcResponseJsonObj = enhanceJsonResponse(new JSONObject(responseData), statusCode);

        } catch (Exception ex) {
            logger.debug("calling SDC Exception message:", ex);
            String errorMessage = " Encountered Error while calling SDC POST Activate. " + ex.getMessage();
            logger.debug(errorMessage);
            sdcResponseJsonObj.put(STATUS_CODE, String.valueOf(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
            sdcResponseJsonObj.put(MESSAGE_ID, "");
            sdcResponseJsonObj.put(MESSAGE, errorMessage);

        }
        return sdcResponseJsonObj;
    }

    /**
     * set HttpPostResponse
     * 
     * @param config - RESTConfig object
     * @param jsonPayload - String
     * @return client - RestClient object
     */
    public Response setHttpPostResponse(HttpClient client, String jsonPayload) throws ApiException {
        try {
            return client.post(jsonPayload);
        } catch (Exception ex) {
            ErrorLoggerInfo errorLoggerInfo =
                    new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcessError)
                            .build();

            throw new ValidateException.Builder("Bad request could not post payload", HttpStatus.SC_BAD_REQUEST,
                    ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(ex).errorInfo(errorLoggerInfo).build();
        }
    }

    /**
     * enhance Response
     * 
     * @param sdcResponseJsonObj - JSONObject object
     * @param statusCode - int
     * @return enhancedAsdcResponseJsonObj - JSONObject object
     */
    public JSONObject enhanceJsonResponse(JSONObject sdcResponseJsonObj, int statusCode) {

        JSONObject enhancedAsdcResponseJsonObj = new JSONObject();

        String message = "";
        String messageId = "";

        if (statusCode == Response.Status.ACCEPTED.getStatusCode()) { // Accepted
            enhancedAsdcResponseJsonObj.put("distributionId", sdcResponseJsonObj.get("distributionId"));
            enhancedAsdcResponseJsonObj.put(STATUS_CODE, Integer.toString(statusCode));
            enhancedAsdcResponseJsonObj.put(MESSAGE_ID, "");
            enhancedAsdcResponseJsonObj.put(MESSAGE, "Success");

        } else { // error
            if (sdcResponseJsonObj.has(REQUEST_ERROR)) {
                JSONObject requestErrorObj = sdcResponseJsonObj.getJSONObject(REQUEST_ERROR);
                if (sdcResponseJsonObj.getJSONObject(REQUEST_ERROR).has(SERVICE_EXCEPTION)) {
                    message = requestErrorObj.getJSONObject(SERVICE_EXCEPTION).getString("text");
                    messageId = requestErrorObj.getJSONObject(SERVICE_EXCEPTION).getString(MESSAGE_ID);
                }
                if (sdcResponseJsonObj.getJSONObject(REQUEST_ERROR).has(POLICY_EXCEPTION)) {
                    message = requestErrorObj.getJSONObject(POLICY_EXCEPTION).getString("text");
                    messageId = requestErrorObj.getJSONObject(POLICY_EXCEPTION).getString(MESSAGE_ID);
                }
                enhancedAsdcResponseJsonObj.put(STATUS_CODE, Integer.toString(statusCode));
                enhancedAsdcResponseJsonObj.put(MESSAGE_ID, messageId);
                enhancedAsdcResponseJsonObj.put(MESSAGE, message);

            } else {
                // unexpected format
                enhancedAsdcResponseJsonObj.put(STATUS_CODE,
                        String.valueOf(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
                enhancedAsdcResponseJsonObj.put(MESSAGE_ID, MESSAGE_UNDEFINED_ERROR);
                enhancedAsdcResponseJsonObj.put(MESSAGE, MESSAGE_UNEXPECTED_FORMAT);
            }
        }
        return enhancedAsdcResponseJsonObj;

    }

    /**
     * Build Uri
     * 
     * @param serviceModelVersionId - String
     * @param operationalEnvironmentId - String
     * @return uriBuilder - String
     */
    public String buildUriBuilder(String serviceModelVersionId, String operationalEnvironmentId) {
        String path = serviceModelVersionId + "/distribution/" + operationalEnvironmentId + "/activate";
        UriBuilder uriBuilder = UriBuilder.fromPath(sdcEndpoint + SDCClientHelper.PARTIAL_SDC_URI).path(path);
        return uriBuilder.build().toString();
    }

    /**
     * Build JSON context
     * 
     * @param workloadContext - String
     * @return String json
     * @throws JSONException
     */
    public String buildJsonWorkloadContext(String workloadContext) {
        return new JSONObject().put("workloadContext", workloadContext).toString();

    }

    /**
     * decrypt value
     * 
     * @param toDecrypt - String
     * @param msokey - String
     * @return result - String
     */
    public synchronized String decrypt(String toDecrypt, String msokey) {
        String result = null;
        try {
            result = CryptoUtils.decrypt(toDecrypt, msokey);

        } catch (Exception e) {
            logger.debug("Failed to decrypt credentials: {}", toDecrypt, e);
        }
        return result;
    }

    private String getBasicAuth() {
        return decrypt(sdcClientAuth, msoKey);
    }
}