Storage of PM Data
[nonrtric.git] / pmlog / src / main / java / org / oran / pmlog / DataFromKafkaTopic.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2023 Nordix Foundation
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  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.oran.pmlog;
22
23 import java.io.ByteArrayInputStream;
24 import java.util.zip.GZIPInputStream;
25
26 import lombok.ToString;
27
28 import org.apache.kafka.common.header.Header;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 @ToString
33 public class DataFromKafkaTopic {
34     private final byte[] key;
35     private final byte[] value;
36     private String stringValue = null;
37     private static final Logger logger = LoggerFactory.getLogger(DataFromKafkaTopic.class);
38
39     public final Iterable<Header> headers;
40
41     private static byte[] noBytes = new byte[0];
42
43     public DataFromKafkaTopic(Iterable<Header> headers, byte[] key, byte[] value) {
44         this.key = key == null ? noBytes : key;
45         this.value = value == null ? noBytes : value;
46         this.headers = headers;
47     }
48
49     public String valueAsString() {
50         if (stringValue == null) {
51             if (isZipped()) {
52                 stringValue = unzip(this.value);
53             } else {
54                 stringValue = new String(this.value);
55             }
56
57         }
58         return this.stringValue;
59     }
60
61     private String unzip(byte[] bytes) {
62         try (final GZIPInputStream gzipInput = new GZIPInputStream(new ByteArrayInputStream(bytes))) {
63             return new String(gzipInput.readAllBytes());
64         } catch (Exception e) {
65             logger.error("Could not unzip received info, reason: {}, typeId: {}", e.getMessage(),
66                     getTypeIdFromHeaders());
67             return "";
68         }
69     }
70
71     public static final String ZIPPED_PROPERTY = "gzip";
72     public static final String TYPE_ID_PROPERTY = "type-id";
73
74     public boolean isZipped() {
75         if (headers == null) {
76             return false;
77         }
78         for (Header h : headers) {
79             if (h.key().equals(ZIPPED_PROPERTY)) {
80                 return true;
81             }
82         }
83         return false;
84     }
85
86     public String getTypeIdFromHeaders() {
87         if (headers == null) {
88             return "";
89         }
90         for (Header h : headers) {
91             if (h.key().equals(TYPE_ID_PROPERTY)) {
92                 return new String(h.value());
93             }
94         }
95         return "";
96     }
97 }