2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2017-2019 Intel Corp. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * Modifications Copyright (C) 2018-2019 AT&T Corporation. All rights reserved.
6 * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.vfc;
24 import com.google.gson.JsonSyntaxException;
26 import java.util.HashMap;
29 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
30 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
31 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
32 import org.onap.policy.rest.RestManager;
33 import org.onap.policy.rest.RestManager.Pair;
34 import org.onap.policy.vfc.util.Serialization;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public final class VfcManager implements Runnable {
40 private String vfcUrlBase;
41 private String username;
42 private String password;
43 private VfcRequest vfcRequest;
44 private VfcCallback callback;
45 private static final Logger logger = LoggerFactory.getLogger(VfcManager.class);
47 // The REST manager used for processing REST calls for this VFC manager
48 private RestManager restManager;
51 public interface VfcCallback {
52 void onResponse(VfcResponse responseError);
58 * @param cb Callback method to call when response
59 * @param request request
60 * @param url URL to VFC component
61 * @param user username
64 public VfcManager(VfcCallback cb, VfcRequest request, String url, String user, String pwd) {
65 if (cb == null || request == null) {
66 throw new IllegalArgumentException(
67 "the parameters \"cb\" and \"request\" on the VfcManager constructor may not be null");
70 throw new IllegalArgumentException(
71 "the \"url\" parameter on the VfcManager constructor may not be null");
79 restManager = new RestManager();
85 * @param baseUrl base URL
86 * @param name username
89 public void setVfcParams(String baseUrl, String name, String pwd) {
90 vfcUrlBase = baseUrl + "/api/nslcm/v1";
97 Map<String, String> headers = new HashMap<>();
98 Pair<Integer, String> httpDetails;
100 VfcResponse responseError = new VfcResponse();
101 responseError.setResponseDescriptor(new VfcResponseDescriptor());
102 responseError.getResponseDescriptor().setStatus("error");
104 headers.put("Accept", "application/json");
105 String vfcUrl = vfcUrlBase + "/ns/" + vfcRequest.getNsInstanceId() + "/heal";
107 String vfcRequestJson = Serialization.gsonPretty.toJson(vfcRequest);
108 NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, vfcUrl, vfcRequestJson);
110 httpDetails = restManager.post(vfcUrl, username, password, headers, "application/json", vfcRequestJson);
111 } catch (Exception e) {
112 logger.error(e.getMessage(), e);
113 this.callback.onResponse(responseError);
117 if (httpDetails == null) {
118 this.callback.onResponse(responseError);
122 if (httpDetails.first != 202) {
123 logger.warn("VFC Heal Restcall failed");
128 handleVfcResponse(headers, httpDetails, vfcUrl);
129 } catch (JsonSyntaxException e) {
130 logger.error("Failed to deserialize into VfcResponse {}", e.getLocalizedMessage(), e);
131 } catch (InterruptedException e) {
132 logger.error("Interrupted exception: {}", e.getLocalizedMessage(), e);
133 Thread.currentThread().interrupt();
134 } catch (Exception e) {
135 logger.error("Unknown error deserializing into VfcResponse {}", e.getLocalizedMessage(), e);
140 * Handle a VFC response message.
142 * @param headers the headers in the response
143 * @param httpDetails the HTTP details in the response
144 * @param vfcUrl the response URL
145 * @throws InterruptedException on errors in the response
147 private void handleVfcResponse(Map<String, String> headers, Pair<Integer, String> httpDetails, String vfcUrl)
148 throws InterruptedException {
149 VfcResponse response = Serialization.gsonPretty.fromJson(httpDetails.second, VfcResponse.class);
150 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, vfcUrl, httpDetails.second);
151 String body = Serialization.gsonPretty.toJson(response);
152 logger.debug("Response to VFC Heal post:");
155 String jobId = response.getJobId();
156 int attemptsLeft = 20;
158 String urlGet = vfcUrlBase + "/jobs/" + jobId;
159 VfcResponse responseGet = null;
161 while (attemptsLeft-- > 0) {
162 NetLoggerUtil.getNetworkLogger().info("[OUT|{}|{}|]", "VFC", urlGet);
163 Pair<Integer, String> httpDetailsGet = restManager.get(urlGet, username, password, headers);
164 responseGet = Serialization.gsonPretty.fromJson(httpDetailsGet.second, VfcResponse.class);
165 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, vfcUrl, httpDetailsGet.second);
166 responseGet.setRequestId(vfcRequest.getRequestId().toString());
167 body = Serialization.gsonPretty.toJson(responseGet);
168 logger.debug("Response to VFC Heal get:");
171 String responseStatus = responseGet.getResponseDescriptor().getStatus();
172 if (httpDetailsGet.first == 200
173 && ("finished".equalsIgnoreCase(responseStatus) || "error".equalsIgnoreCase(responseStatus))) {
174 logger.debug("VFC Heal Status {}", responseGet.getResponseDescriptor().getStatus());
175 this.callback.onResponse(responseGet);
180 boolean isTimeout = (attemptsLeft <= 0) && (responseGet != null)
181 && (responseGet.getResponseDescriptor() != null);
182 isTimeout = isTimeout && (responseGet.getResponseDescriptor().getStatus() != null)
183 && (!responseGet.getResponseDescriptor().getStatus().isEmpty());
185 logger.debug("VFC timeout. Status: ({})", responseGet.getResponseDescriptor().getStatus());
186 this.callback.onResponse(responseGet);
191 * Protected setter for rest manager to allow mocked rest manager to be used for testing.
193 * @param restManager the test REST manager
195 protected void setRestManager(final RestManager restManager) {
196 this.restManager = restManager;