1fad28d1c3b62121bc772823f5ffced7e6f85f9e
[nonrtric/plt/ranpm.git] / influxlogger / src / test / java / org / oran / pmlog / Integration.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.nio.charset.Charset;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import org.apache.kafka.clients.producer.ProducerConfig;
30 import org.apache.kafka.clients.producer.ProducerRecord;
31 import org.apache.kafka.common.serialization.ByteArraySerializer;
32 import org.junit.jupiter.api.AfterEach;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.oran.pmlog.configuration.ApplicationConfig;
36 import org.oran.pmlog.configuration.ConsumerJobInfo;
37 import org.oran.pmlog.configuration.ConsumerJobInfo.PmFilterData;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
43 import org.springframework.boot.test.context.TestConfiguration;
44 import org.springframework.boot.test.web.server.LocalServerPort;
45 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
46 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
47 import org.springframework.context.annotation.Bean;
48 import org.springframework.test.context.TestPropertySource;
49
50 import reactor.core.publisher.Flux;
51 import reactor.kafka.sender.KafkaSender;
52 import reactor.kafka.sender.SenderOptions;
53 import reactor.kafka.sender.SenderRecord;
54
55 @SuppressWarnings("java:S3577") // Rename class
56 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
57 @TestPropertySource(properties = { //
58         "server.ssl.key-store=./config/keystore.jks", //
59         "app.webclient.trust-store=./config/truststore.jks", //
60         "app.configuration-filepath=./src/test/resources/test_application_configuration.json", //
61         "app.pm-files-path=./src/test/resources/", //
62         "app.auth-token-file=src/test/resources/jwtToken.b64", //
63         "app.kafka.use-oath-token=false" //
64 }) //
65 class Integration {
66
67     @Autowired
68     private ApplicationConfig applicationConfig;
69
70     private static com.google.gson.Gson gson = new com.google.gson.GsonBuilder().disableHtmlEscaping().create();
71
72     private final Logger logger = LoggerFactory.getLogger(Integration.class);
73
74     @LocalServerPort
75     int localServerHttpPort;
76
77     static class TestApplicationConfig extends ApplicationConfig {
78         String thisProcessUrl() {
79             final String url = "https://localhost:" + getLocalServerHttpsPort();
80             return url;
81         }
82     }
83
84     /**
85      * Overrides the BeanFactory.
86      */
87     @TestConfiguration
88     static class TestBeanFactory extends BeanFactory {
89
90         @Override
91         @Bean
92         public ServletWebServerFactory servletContainer() {
93             return new TomcatServletWebServerFactory();
94         }
95
96         @Override
97         @Bean
98         public ApplicationConfig getApplicationConfig() {
99             TestApplicationConfig cfg = new TestApplicationConfig();
100             return cfg;
101         }
102     }
103
104     @BeforeEach
105     void init() {}
106
107     @AfterEach
108     void reset() {}
109
110     private SenderOptions<byte[], byte[]> kafkaSenderOptions() {
111         String bootstrapServers = this.applicationConfig.getKafkaBootStrapServers();
112
113         Map<String, Object> props = new HashMap<>();
114         props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
115         // props.put(ProducerConfig.CLIENT_ID_CONFIG, "sample-producerx");
116         props.put(ProducerConfig.ACKS_CONFIG, "all");
117         props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class);
118         props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class);
119         this.applicationConfig.addKafkaSecurityProps(props);
120         return SenderOptions.create(props);
121     }
122
123     private SenderRecord<byte[], byte[], Integer> kafkaSenderRecord(String data, String key) {
124         String topic = this.applicationConfig.getKafkaInputTopic();
125         int correlationMetadata = 2;
126         return SenderRecord.create(new ProducerRecord<>(topic, key.getBytes(), data.getBytes()), correlationMetadata);
127     }
128
129     private void sendDataToKafka(Flux<SenderRecord<byte[], byte[], Integer>> dataToSend) {
130         final KafkaSender<byte[], byte[]> sender = KafkaSender.create(kafkaSenderOptions());
131
132         sender.send(dataToSend) //
133                 .doOnError(e -> logger.error("Send failed", e)) //
134                 .blockLast();
135
136         sender.close();
137     }
138
139     @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
140     private static void waitForKafkaListener() throws InterruptedException {
141         Thread.sleep(4000);
142     }
143
144     private String generateCounterValue(int sequenceValue, int noOfObjects, String counterName, String resourceFdn) {
145         long value = (random.nextInt() % 100) * sequenceValue + (counterName.hashCode() % 5000);
146         return Long.toString(value);
147     }
148
149     static java.util.Random random = new java.util.Random(System.currentTimeMillis());
150
151     private long currentEpochMicroSeconds() {
152         return java.util.concurrent.TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis());
153     }
154
155     private String measType(PmReport.MeasResult measResult, PmReport.MeasInfoList measInfoList) {
156         return measInfoList.getMeasTypes().getMeasType(measResult.getP());
157     }
158
159     // Set end time. Now - (GP * sequenceValue)
160     private void setEndTime(PmReport report, int sequenceValue, int noOfObjects) {
161         long gpMicro = report.event.getPerf3gppFields().getMeasDataCollection().getGranularityPeriod() * 1000 * 1000;
162         long endTime = currentEpochMicroSeconds() - ((noOfObjects - sequenceValue - 1) * gpMicro);
163         report.event.getCommonEventHeader().setLastEpochMicrosec(endTime);
164
165     }
166
167     final String PM_REPORT_FILE_BIG = "./src/test/resources/A20000626.2315+0200-2330+0200_HTTPS-6-73.json";
168     final String PM_REPORT_FILE = "./src/test/resources/pm_report.json";
169
170     String pmReport(int sequenceValue, int noOfObjects) {
171         try {
172             String str = Files.readString(Path.of(PM_REPORT_FILE), Charset.defaultCharset());
173             PmReport report = gson.fromJson(str, PmReport.class);
174             PmReport.MeasDataCollection measDataCollection = report.event.getPerf3gppFields().getMeasDataCollection();
175
176             setEndTime(report, sequenceValue, noOfObjects);
177
178             // Fill it with generated values
179             for (PmReport.MeasInfoList measInfoList : measDataCollection.getMeasInfoList()) {
180                 for (PmReport.MeasValuesList measValueList : measInfoList.getMeasValuesList()) {
181                     for (PmReport.MeasResult measResult : measValueList.getMeasResults()) {
182                         String value = this.generateCounterValue(sequenceValue, noOfObjects,
183                                 measType(measResult, measInfoList), report.fullDistinguishedName(measValueList));
184                         measResult.setSValue(value);
185                     }
186                 }
187             }
188             return gson.toJson(report);
189         } catch (Exception e) {
190             logger.error("Could not loadPM report {}", e.getMessage(), e);
191             return null;
192         }
193
194     }
195
196     @Test
197     void testStoreReportsInflux() throws Exception {
198         final int NO_OF_OBJECTS = 24 * 4;
199         InfluxStore influxStore = new InfluxStore(this.applicationConfig);
200
201         Flux<DataFromKafkaTopic> input = Flux.range(0, NO_OF_OBJECTS) //
202                 .map(i -> pmReport(i, NO_OF_OBJECTS)) //
203                 .map(str -> new DataFromKafkaTopic(null, null, str.getBytes()));
204
205         influxStore.start(input);
206
207     }
208
209     @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
210     @Test
211     void sendPmReportsThroughKafka() throws Exception {
212         waitForKafkaListener();
213
214         final int NO_OF_OBJECTS = 20;
215
216         var dataToSend = Flux.range(0, NO_OF_OBJECTS).map(i -> kafkaSenderRecord(pmReport(i, NO_OF_OBJECTS), "key"));
217         sendDataToKafka(dataToSend);
218
219         Thread.sleep(1000 * 1000);
220     }
221
222     @Test
223     void printConfiguration() {
224         PmFilterData f = new PmFilterData();
225         f.getMeasObjInstIds().add("measObj");
226         PmFilterData.MeasTypeSpec spec = new PmFilterData.MeasTypeSpec();
227         spec.setMeasuredObjClass("measuredObjClass");
228         spec.getMeasTypes().add("measType");
229         f.getMeasTypeSpecs().add(spec);
230         f.getSourceNames().add("sourceName");
231         ConsumerJobInfo.KafkaDeliveryInfo deliveryInfo = ConsumerJobInfo.KafkaDeliveryInfo.builder() //
232                 .topic("topic").bootStrapServers("bootStrapServers") //
233                 .build();
234         ConsumerJobInfo.PmJobParameters params = ConsumerJobInfo.PmJobParameters.builder() //
235                 .filter(f) //
236                 .deliveryInfo(deliveryInfo).build();
237
238         ConsumerJobInfo info = new ConsumerJobInfo("type", params, "owner");
239         String str = gson.toJson(info);
240         System.out.print(str);
241     }
242
243     @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
244     @Test
245     void tet() throws Exception {
246         Thread.sleep(1000 * 1000);
247     }
248
249 }