2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.o.ran.oam.nf.oam.adopter.pm.sb.rest.client.http;
22 import com.google.gson.Gson;
23 import io.reactivex.rxjava3.core.Single;
24 import java.time.ZoneId;
25 import lombok.AccessLevel;
26 import lombok.NoArgsConstructor;
27 import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
28 import org.apache.hc.core5.http.HttpStatus;
29 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.PerformanceManagementEmptyOutputException;
30 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.PerformanceManagementException;
31 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.pojos.Adapter;
32 import org.o.ran.oam.nf.oam.adopter.pm.sb.rest.client.DefaultHttpRestClient;
33 import org.o.ran.oam.nf.oam.adopter.pm.sb.rest.client.pojos.TimeZoneResponse;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 @NoArgsConstructor(access = AccessLevel.PRIVATE)
38 public final class OffSetTimeZoneHandler {
40 private static final Logger LOG = LoggerFactory.getLogger(OffSetTimeZoneHandler.class);
42 private static final Gson GSON = new Gson();
45 * Returns time zone of the device.
47 public static Single<ZoneId> readTimeZone(final DefaultHttpRestClient httpRestClient, final String offsetEndpoint,
48 final Adapter adapter) {
49 LOG.debug("Read Time Zone from adapter {}", adapter.getHostIpAddress());
50 return httpRestClient.get(adapter, offsetEndpoint)
51 .map(response -> validateGet(response, adapter))
55 private static String validateGet(final SimpleHttpResponse response, final Adapter adapter) {
56 if (response.getCode() != HttpStatus.SC_OK) {
57 throw new PerformanceManagementException(
58 "Get Zone offset failed for " + adapter.getHostIpAddress() + " Code: " + response.getCode());
60 final String output = response.getBody().getBodyText();
61 if (output.isEmpty()) {
62 throw new PerformanceManagementEmptyOutputException(
63 "Get Zone offset failed for " + adapter.getHostIpAddress() + " . Empty output received");
65 return GSON.fromJson(output, TimeZoneResponse.class).getOffset();