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 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;
38 @NoArgsConstructor(access = AccessLevel.PRIVATE)
39 public final class DownloadPerformanceManagementFilesHandler {
41 private static final Logger LOG = LoggerFactory.getLogger(DownloadPerformanceManagementFilesHandler.class);
44 * Reads and return ZIP CSV PM Files from device.
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())));
55 private static Single<SimpleBody> validateGetZipFile(final Adapter adapter, final SimpleHttpResponse response) {
56 final var statusLine = new StatusLine(response).toString();
57 final var 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);
63 return Single.error(new PerformanceManagementException(
64 "Download files from " + adapter.getHostIpAddress() + " failed: " + statusLine));