2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Huawei. All rights reserved.
4 * ================================================================================
5 * Modifications Copyright (C) 2019-2020 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 // TODO this class will be deleted
42 public final class SdncManager implements Runnable {
44 private String sdncUrlBase;
45 private String username;
46 private String password;
47 private SdncRequest sdncRequest;
48 private SdncCallback callback;
49 private static final Logger logger = LoggerFactory.getLogger(SdncManager.class);
51 // The REST manager used for processing REST calls for this Sdnc manager
52 private RestManager restManager;
55 public interface SdncCallback {
56 public void onCallback(SdncResponse response);
62 * @param cb Callback method
63 * @param request request
65 public SdncManager(SdncCallback cb, SdncRequest request, String url,
66 String user, String password) {
67 if (cb == null || request == null) {
68 throw new IllegalArgumentException(
69 "the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null"
73 this.sdncRequest = request;
75 throw new IllegalArgumentException(
76 "the \"url\" parameter on the SdncManager constructor may not be null"
79 this.sdncUrlBase = url;
81 this.password = password;
83 restManager = new RestManager();
89 * @param baseUrl base URL
90 * @param name username
93 public void setSdncParams(String baseUrl, String name, String pwd) {
94 sdncUrlBase = baseUrl;
101 Map<String, String> headers = new HashMap<>();
102 Pair<Integer, String> httpDetails;
104 SdncResponse responseError = new SdncResponse();
105 SdncResponseOutput responseOutput = new SdncResponseOutput();
106 responseOutput.setResponseCode("404");
107 responseError.setResponseOutput(responseOutput);
109 headers.put("Accept", "application/json");
110 String sdncUrl = sdncUrlBase + sdncRequest.getUrl();
113 String sdncRequestJson = Serialization.gsonPretty.toJson(sdncRequest);
114 NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, sdncUrl, sdncRequestJson);
115 logger.info("[OUT|{}|{}|]{}{}", CommInfrastructure.REST, sdncUrl, NetLoggerUtil.SYSTEM_LS, sdncRequestJson);
117 httpDetails = restManager.post(sdncUrl, username, password, headers, "application/json",
119 } catch (Exception e) {
120 logger.info(e.getMessage(), e);
121 this.callback.onCallback(responseError);
125 if (httpDetails == null) {
126 this.callback.onCallback(responseError);
131 SdncResponse response = Serialization.gsonPretty.fromJson(httpDetails.second, SdncResponse.class);
132 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, sdncUrl, httpDetails.second);
133 logger.info("[IN|{}|{}|]{}{}", "Sdnc", sdncUrl, NetLoggerUtil.SYSTEM_LS, httpDetails.second);
134 String body = Serialization.gsonPretty.toJson(response);
135 logger.info("Response to Sdnc Heal post:");
137 response.setRequestId(sdncRequest.getRequestId().toString());
139 if (!"200".equals(response.getResponseOutput().getResponseCode())) {
141 "Sdnc Heal Restcall failed with http error code {} {}", httpDetails.first, httpDetails.second
145 this.callback.onCallback(response);
146 } catch (JsonSyntaxException e) {
147 logger.info("Failed to deserialize into SdncResponse {}", e.getLocalizedMessage(), e);
148 } catch (Exception e) {
149 logger.info("Unknown error deserializing into SdncResponse {}", e.getLocalizedMessage(), e);
154 * Protected setter for rest manager to allow mocked rest manager to be used for testing.
155 * @param restManager the test REST manager
157 protected void setRestManager(final RestManager restManager) {
158 this.restManager = restManager;