Changes for checkstyle 8.32
[policy/apex-pdp.git] / testsuites / performance / performance-benchmark-test / src / main / java / org / onap / policy / apex / testsuites / performance / benchmark / eventgenerator / EventBatch.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
22
23 import java.util.Map;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.atomic.AtomicInteger;
26 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.InputEvent;
27 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.OutputEvent;
28
29 /**
30  * This class keeps track of a batch of events sent to an Apex instance.
31  */
32 public class EventBatch {
33     private static AtomicInteger nextBatchNumber = new AtomicInteger();
34
35     private final int batchNumber = nextBatchNumber.getAndIncrement();
36     private final Map<Integer, InputEvent>  inputEventMap  = new ConcurrentHashMap<>();
37     private final Map<Integer, OutputEvent> outputEventMap = new ConcurrentHashMap<>();
38
39     private final int batchSize;
40     private final String apexClient;
41
42     /**
43      * Create an event batch.
44      *
45      * @param batchSize the size of the batch
46      * @param apexClient the apex client to which the event batch will be sent
47      */
48     public EventBatch(final int batchSize, final String apexClient) {
49         this.batchSize = batchSize;
50         this.apexClient = apexClient;
51
52         // Create the events for the batch of events
53         for (int eventNumber = 0; eventNumber < batchSize; eventNumber++) {
54             InputEvent inputEvent = new InputEvent();
55             inputEvent.setTestSlogan(getEventSlogan(eventNumber));
56             inputEventMap.put(eventNumber, inputEvent);
57         }
58     }
59
60     /**
61      * Get the batch of events as a JSON string.
62      *
63      * @return the JSON string representation of the batch of events.
64      */
65     public String getBatchAsJsonString() {
66         if (batchSize == 1) {
67             return inputEventMap.get(0).asJson();
68         }
69
70         StringBuilder jsonBuilder = new StringBuilder();
71         jsonBuilder.append("[\n");
72         boolean first = true;
73         for (InputEvent inputEvent : inputEventMap.values()) {
74             if (first) {
75                 first = false;
76             } else {
77                 jsonBuilder.append(",\n");
78             }
79             jsonBuilder.append(inputEvent.asJson());
80         }
81         jsonBuilder.append("\n]\n");
82
83         return jsonBuilder.toString();
84     }
85
86     public int getBatchNumber() {
87         return batchNumber;
88     }
89
90     public int getBatchSize() {
91         return batchSize;
92     }
93
94     public String getApexClient() {
95         return apexClient;
96     }
97
98     /**
99      * Get the event slogan.
100      *
101      * @param eventNumber the number of this event
102      * @return the event slogan
103      */
104     private String getEventSlogan(final int eventNumber) {
105         StringBuilder testSloganBuilder = new StringBuilder();
106         testSloganBuilder.append(batchNumber);
107         testSloganBuilder.append('-');
108         testSloganBuilder.append(eventNumber);
109         testSloganBuilder.append(": ");
110         testSloganBuilder.append(apexClient);
111
112         return testSloganBuilder.toString();
113     }
114
115     /**
116      * Handle a response event.
117      *
118      * @param responseEvent the response event
119      */
120     public void handleResponse(OutputEvent responseEvent) {
121         outputEventMap.put(responseEvent.findEventNumber(), responseEvent);
122     }
123
124     /**
125      * Get the statistics on this event batch.
126      * @return the event batch statistics
127      */
128     public EventBatchStats getStats() {
129         return new EventBatchStats(this);
130     }
131
132     /**
133      * Get an input event for an event number.
134      * @param eventNo the event number
135      * @return the event
136      */
137     public InputEvent getInputEvent(int eventNo) {
138         return inputEventMap.get(eventNo);
139     }
140
141     /**
142      * Get an output event for an event number.
143      * @param eventNo the event number
144      * @return the event
145      */
146     public OutputEvent getOutputEvent(int eventNo) {
147         return outputEventMap.get(eventNo);
148     }
149 }