2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2017-2019 Intel Corp. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 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;
25 import java.util.HashMap;
27 import org.apache.commons.lang3.tuple.Pair;
28 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
29 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
30 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
31 import org.onap.policy.rest.RestManager;
32 import org.onap.policy.vfc.util.Serialization;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public final class VfcManager implements Runnable {
38 private String vfcUrlBase;
39 private String username;
40 private String password;
41 private VfcRequest vfcRequest;
42 private VfcCallback callback;
43 private static final Logger logger = LoggerFactory.getLogger(VfcManager.class);
45 // The REST manager used for processing REST calls for this VFC manager
46 private RestManager restManager;
49 public interface VfcCallback {
50 void onResponse(VfcResponse responseError);
56 * @param cb Callback method to call when response
57 * @param request request
58 * @param url URL to VFC component
59 * @param user username
62 public VfcManager(VfcCallback cb, VfcRequest request, String url, String user, String pwd) {
63 if (cb == null || request == null) {
64 throw new IllegalArgumentException(
65 "the parameters \"cb\" and \"request\" on the VfcManager constructor may not be null");
68 throw new IllegalArgumentException(
69 "the \"url\" parameter on the VfcManager constructor may not be null");
77 restManager = new RestManager();
83 * @param baseUrl base URL
84 * @param name username
87 public void setVfcParams(String baseUrl, String name, String pwd) {
88 vfcUrlBase = baseUrl + "/api/nslcm/v1";
95 Map<String, String> headers = new HashMap<>();
96 Pair<Integer, String> httpDetails;
98 VfcResponse responseError = new VfcResponse();
99 responseError.setResponseDescriptor(new VfcResponseDescriptor());
100 responseError.getResponseDescriptor().setStatus("error");
102 headers.put("Accept", "application/json");
103 String vfcUrl = vfcUrlBase + "/ns/" + vfcRequest.getNsInstanceId() + "/heal";
105 String vfcRequestJson = Serialization.gsonPretty.toJson(vfcRequest);
106 NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, vfcUrl, vfcRequestJson);
108 httpDetails = restManager.post(vfcUrl, username, password, headers, "application/json", vfcRequestJson);
109 } catch (Exception e) {
110 logger.error(e.getMessage(), e);
111 this.callback.onResponse(responseError);
115 if (httpDetails == null) {
116 this.callback.onResponse(responseError);
120 if (httpDetails.getLeft() != 202) {
121 logger.warn("VFC Heal Restcall failed");
126 handleVfcResponse(headers, httpDetails, vfcUrl);
127 } catch (JsonSyntaxException e) {
128 logger.error("Failed to deserialize into VfcResponse {}", e.getLocalizedMessage(), e);
129 } catch (InterruptedException e) {
130 logger.error("Interrupted exception: {}", e.getLocalizedMessage(), e);
131 Thread.currentThread().interrupt();
132 } catch (Exception e) {
133 logger.error("Unknown error deserializing into VfcResponse {}", e.getLocalizedMessage(), e);
138 * Handle a VFC response message.
140 * @param headers the headers in the response
141 * @param httpDetails the HTTP details in the response
142 * @param vfcUrl the response URL
143 * @throws InterruptedException on errors in the response
145 private void handleVfcResponse(Map<String, String> headers, Pair<Integer, String> httpDetails, String vfcUrl)
146 throws InterruptedException {
147 VfcResponse response = Serialization.gsonPretty.fromJson(httpDetails.getRight(), VfcResponse.class);
148 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, vfcUrl, httpDetails.getRight());
149 String body = Serialization.gsonPretty.toJson(response);
150 logger.debug("Response to VFC Heal post:");
153 String jobId = response.getJobId();
154 int attemptsLeft = 20;
156 String urlGet = vfcUrlBase + "/jobs/" + jobId;
157 VfcResponse responseGet = null;
159 while (attemptsLeft-- > 0) {
160 NetLoggerUtil.getNetworkLogger().info("[OUT|{}|{}|]", "VFC", urlGet);
161 Pair<Integer, String> httpDetailsGet = restManager.get(urlGet, username, password, headers);
162 responseGet = Serialization.gsonPretty.fromJson(httpDetailsGet.getRight(), VfcResponse.class);
163 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, vfcUrl, httpDetailsGet.getRight());
164 responseGet.setRequestId(vfcRequest.getRequestId().toString());
165 body = Serialization.gsonPretty.toJson(responseGet);
166 logger.debug("Response to VFC Heal get:");
169 String responseStatus = responseGet.getResponseDescriptor().getStatus();
170 if (httpDetailsGet.getLeft() == 200
171 && ("finished".equalsIgnoreCase(responseStatus) || "error".equalsIgnoreCase(responseStatus))) {
172 logger.debug("VFC Heal Status {}", responseGet.getResponseDescriptor().getStatus());
173 this.callback.onResponse(responseGet);
178 boolean isTimeout = (attemptsLeft <= 0) && (responseGet != null)
179 && (responseGet.getResponseDescriptor() != null);
180 isTimeout = isTimeout && (responseGet.getResponseDescriptor().getStatus() != null)
181 && (!responseGet.getResponseDescriptor().getStatus().isEmpty());
183 logger.debug("VFC timeout. Status: ({})", responseGet.getResponseDescriptor().getStatus());
184 this.callback.onResponse(responseGet);
189 * Protected setter for rest manager to allow mocked rest manager to be used for testing.
191 * @param restManager the test REST manager
193 protected void setRestManager(final RestManager restManager) {
194 this.restManager = restManager;