99f480b6ce7553b0458aea416fef9fd5f525234e
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
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  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
23
24 import java.util.Map;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.atomic.AtomicInteger;
27 import lombok.Getter;
28 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.InputEvent;
29 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.OutputEvent;
30
31 /**
32  * This class keeps track of a batch of events sent to an Apex instance.
33  */
34 public class EventBatch {
35     private static AtomicInteger nextBatchNumber = new AtomicInteger();
36
37     @Getter
38     private final int batchNumber = nextBatchNumber.getAndIncrement();
39     private final Map<Integer, InputEvent>  inputEventMap  = new ConcurrentHashMap<>();
40     private final Map<Integer, OutputEvent> outputEventMap = new ConcurrentHashMap<>();
41
42     @Getter
43     private final int batchSize;
44     @Getter
45     private final String apexClient;
46
47     /**
48      * Create an event batch.
49      *
50      * @param batchSize the size of the batch
51      * @param apexClient the apex client to which the event batch will be sent
52      */
53     public EventBatch(final int batchSize, final String apexClient) {
54         this.batchSize = batchSize;
55         this.apexClient = apexClient;
56
57         // Create the events for the batch of events
58         for (int eventNumber = 0; eventNumber < batchSize; eventNumber++) {
59             InputEvent inputEvent = new InputEvent();
60             inputEvent.setTestSlogan(getEventSlogan(eventNumber));
61             inputEventMap.put(eventNumber, inputEvent);
62         }
63     }
64
65     /**
66      * Get the batch of events as a JSON string.
67      *
68      * @return the JSON string representation of the batch of events.
69      */
70     public String getBatchAsJsonString() {
71         if (batchSize == 1) {
72             return inputEventMap.get(0).asJson();
73         }
74
75         StringBuilder jsonBuilder = new StringBuilder();
76         jsonBuilder.append("[\n");
77         boolean first = true;
78         for (InputEvent inputEvent : inputEventMap.values()) {
79             if (first) {
80                 first = false;
81             } else {
82                 jsonBuilder.append(",\n");
83             }
84             jsonBuilder.append(inputEvent.asJson());
85         }
86         jsonBuilder.append("\n]\n");
87
88         return jsonBuilder.toString();
89     }
90
91     /**
92      * Get the event slogan.
93      *
94      * @param eventNumber the number of this event
95      * @return the event slogan
96      */
97     private String getEventSlogan(final int eventNumber) {
98         StringBuilder testSloganBuilder = new StringBuilder();
99         testSloganBuilder.append(batchNumber);
100         testSloganBuilder.append('-');
101         testSloganBuilder.append(eventNumber);
102         testSloganBuilder.append(": ");
103         testSloganBuilder.append(apexClient);
104
105         return testSloganBuilder.toString();
106     }
107
108     /**
109      * Handle a response event.
110      *
111      * @param responseEvent the response event
112      */
113     public void handleResponse(OutputEvent responseEvent) {
114         outputEventMap.put(responseEvent.findEventNumber(), responseEvent);
115     }
116
117     /**
118      * Get the statistics on this event batch.
119      * @return the event batch statistics
120      */
121     public EventBatchStats getStats() {
122         return new EventBatchStats(this);
123     }
124
125     /**
126      * Get an input event for an event number.
127      * @param eventNo the event number
128      * @return the event
129      */
130     public InputEvent getInputEvent(int eventNo) {
131         return inputEventMap.get(eventNo);
132     }
133
134     /**
135      * Get an output event for an event number.
136      * @param eventNo the event number
137      * @return the event
138      */
139     public OutputEvent getOutputEvent(int eventNo) {
140         return outputEventMap.get(eventNo);
141     }
142 }