a9fae3090254ec2c2330fef2f81ebff1dd7e1580
[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 / DownloadPerformanceManagementFilesHandler.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 io.reactivex.rxjava3.core.Single;
23 import java.io.ByteArrayInputStream;
24 import java.util.zip.ZipInputStream;
25 import lombok.AccessLevel;
26 import lombok.NoArgsConstructor;
27 import org.apache.hc.client5.http.async.methods.SimpleBody;
28 import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
29 import org.apache.hc.core5.http.ContentType;
30 import org.apache.hc.core5.http.HttpStatus;
31 import org.apache.hc.core5.http.message.StatusLine;
32 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.PerformanceManagementException;
33 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.pojos.Adapter;
34 import org.o.ran.oam.nf.oam.adopter.pm.sb.rest.client.DefaultHttpRestClient;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 @NoArgsConstructor(access = AccessLevel.PRIVATE)
39 public final class DownloadPerformanceManagementFilesHandler {
40
41     private static final Logger LOG = LoggerFactory.getLogger(DownloadPerformanceManagementFilesHandler.class);
42
43     /**
44      * Reads and return ZIP CSV PM Files from device.
45      */
46     public static Single<ZipInputStream> readPerformanceManagementFiles(final DefaultHttpRestClient httpSession,
47             final String pmFilesEndpoint, final Adapter adapter) {
48         LOG.info("Download PM files from RAN {}", adapter.getHostIpAddress());
49         return httpSession.get(adapter, pmFilesEndpoint)
50                        .flatMap(response -> DownloadPerformanceManagementFilesHandler
51                                                     .validateGetZipFile(adapter, response))
52                        .map(entity -> new ZipInputStream(new ByteArrayInputStream(entity.getBodyBytes())));
53     }
54
55     private static Single<SimpleBody> validateGetZipFile(final Adapter adapter, final SimpleHttpResponse response) {
56         final String statusLine = new StatusLine(response).toString();
57         final ContentType contentType = response.getContentType();
58         final SimpleBody entity = response.getBody();
59         if (response.getCode() == HttpStatus.SC_OK && entity != null && ContentType.APPLICATION_OCTET_STREAM
60             .getMimeType().equals(contentType.getMimeType())) {
61             return Single.just(entity);
62         }
63         return Single.error(new PerformanceManagementException(
64                 "Download files from " + adapter.getHostIpAddress() + " failed: " + statusLine));
65     }
66 }