2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Huawei. All rights reserved.
4 * ================================================================================
5 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd.
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.policy.sdnc;
25 import com.google.gson.JsonSyntaxException;
27 import java.util.HashMap;
30 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
31 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
32 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
33 import org.onap.policy.rest.RestManager;
34 import org.onap.policy.rest.RestManager.Pair;
35 import org.onap.policy.sdnc.util.Serialization;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public final class SdncManager implements Runnable {
42 private String sdncUrlBase;
43 private String username;
44 private String password;
45 private SdncRequest sdncRequest;
46 private SdncCallback callback;
47 private static final Logger logger = LoggerFactory.getLogger(SdncManager.class);
49 // The REST manager used for processing REST calls for this Sdnc manager
50 private RestManager restManager;
52 public interface SdncCallback {
53 public void onCallback(SdncResponse response);
59 * @param cb Callback method
60 * @param request request
62 public SdncManager(SdncCallback cb, SdncRequest request, String url,
63 String user, String password) {
64 if (cb == null || request == null) {
65 throw new IllegalArgumentException(
66 "the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null"
70 this.sdncRequest = request;
72 throw new IllegalArgumentException(
73 "the \"url\" parameter on the SdncManager constructor may not be null"
76 this.sdncUrlBase = url;
78 this.password = password;
80 restManager = new RestManager();
86 * @param baseUrl base URL
87 * @param name username
90 public void setSdncParams(String baseUrl, String name, String pwd) {
91 sdncUrlBase = baseUrl;
98 Map<String, String> headers = new HashMap<>();
99 Pair<Integer, String> httpDetails;
101 SdncResponse responseError = new SdncResponse();
102 SdncResponseOutput responseOutput = new SdncResponseOutput();
103 responseOutput.setResponseCode("404");
104 responseError.setResponseOutput(responseOutput);
106 headers.put("Accept", "application/json");
107 String sdncUrl = sdncUrlBase + sdncRequest.getUrl();
110 String sdncRequestJson = Serialization.gsonPretty.toJson(sdncRequest);
111 NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, sdncUrl, sdncRequestJson);
112 logger.info("[OUT|{}|{}|]{}{}", CommInfrastructure.REST, sdncUrl, NetLoggerUtil.SYSTEM_LS, sdncRequestJson);
114 httpDetails = restManager.post(sdncUrl, username, password, headers, "application/json",
116 } catch (Exception e) {
117 logger.info(e.getMessage(), e);
118 this.callback.onCallback(responseError);
122 if (httpDetails == null) {
123 this.callback.onCallback(responseError);
128 SdncResponse response = Serialization.gsonPretty.fromJson(httpDetails.second, SdncResponse.class);
129 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, sdncUrl, httpDetails.second);
130 logger.info("[IN|{}|{}|]{}{}", "Sdnc", sdncUrl, NetLoggerUtil.SYSTEM_LS, httpDetails.second);
131 String body = Serialization.gsonPretty.toJson(response);
132 logger.info("Response to Sdnc Heal post:");
134 response.setRequestId(sdncRequest.getRequestId().toString());
136 if (!response.getResponseOutput().getResponseCode().equals("200")) {
138 "Sdnc Heal Restcall failed with http error code {} {}", httpDetails.first, httpDetails.second
142 this.callback.onCallback(response);
143 } catch (JsonSyntaxException e) {
144 logger.info("Failed to deserialize into SdncResponse {}", e.getLocalizedMessage(), e);
145 } catch (Exception e) {
146 logger.info("Unknown error deserializing into SdncResponse {}", e.getLocalizedMessage(), e);
151 * Protected setter for rest manager to allow mocked rest manager to be used for testing.
152 * @param restManager the test REST manager
154 protected void setRestManager(final RestManager restManager) {
155 this.restManager = restManager;