Soutbound PM Rest Client
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-pm-sb-rest-client / src / main / java / org / o / ran / oam / nf / oam / adopter / pm / sb / rest / client / http / OffSetTimeZoneHandler.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  O-RAN-SC
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
11  *
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=========================================================
18  */
19
20 package org.o.ran.oam.nf.oam.adopter.pm.sb.rest.client.http;
21
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;
36
37 @NoArgsConstructor(access = AccessLevel.PRIVATE)
38 public final class OffSetTimeZoneHandler {
39
40     private static final Logger LOG = LoggerFactory.getLogger(OffSetTimeZoneHandler.class);
41
42     private static final Gson GSON = new Gson();
43
44     /**
45      * Returns time zone of the device.
46      */
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))
52                        .map(ZoneId::of);
53     }
54
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());
59         }
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");
64         }
65         return GSON.fromJson(output, TimeZoneResponse.class).getOffset();
66     }
67 }