765f098de91d166fea28e3cacbc2a202d266052c
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-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.plugins.event.protocol.xml;
22
23 import java.util.Random;
24
25 /**
26  * @author Liam Fallon (liam.fallon@ericsson.com)
27  */
28 public class XMLEventGenerator {
29     private static int nextEventNo = 0;
30
31     public static String xmlEvents(final int eventCount) {
32         final StringBuilder builder = new StringBuilder();
33
34         for (int i = 0; i < eventCount; i++) {
35             if (i > 0) {
36                 builder.append("\n");
37             }
38             builder.append(xmlEvent());
39         }
40
41         return builder.toString();
42     }
43
44     public static String xmlEvent() {
45         final Random rand = new Random();
46
47         final StringBuilder builder = new StringBuilder();
48
49         int nextEventNo = rand.nextInt(2);
50         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
51         final int nextMatchCase = rand.nextInt(4);
52         final float nextTestTemperature = rand.nextFloat() * 10000;
53
54         builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
55         builder.append("<xmlApexEvent xmlns=\"http://www.onap.org/policy/apex-pdp/apexevent\">\n");
56
57         builder.append("  <name>" + eventName + "</name>\n");
58         builder.append("  <version>0.0.1</version>\n");
59         builder.append("  <nameSpace>org.onap.policy.apex.sample.events</nameSpace>\n");
60         builder.append("  <source>test</source>\n");
61         builder.append("  <target>apex</target>\n");
62         builder.append("  <data>\n");
63         builder.append("    <key>TestSlogan</key>\n");
64         builder.append("    <value>Test slogan for External Event" + (nextEventNo++) + "</value>\n");
65         builder.append("  </data>\n");
66         builder.append("  <data>\n");
67         builder.append("    <key>TestMatchCase</key>\n");
68         builder.append("        <value>" + nextMatchCase + "</value>\n");
69         builder.append("  </data>\n");
70         builder.append("  <data>\n");
71         builder.append("    <key>TestTimestamp</key>\n");
72         builder.append("    <value>" + System.currentTimeMillis() + "</value>\n");
73         builder.append("  </data>\n");
74         builder.append("  <data>\n");
75         builder.append("    <key>TestTemperature</key>\n");
76         builder.append("    <value>" + nextTestTemperature + "</value>\n");
77         builder.append("  </data>\n");
78         builder.append("</xmlApexEvent>");
79
80         return builder.toString();
81     }
82
83     public static void main(final String[] args) {
84         if (args.length != 1) {
85             System.err.println("usage EventGenerator #events");
86             return;
87         }
88
89         int eventCount = 0;
90         try {
91             eventCount = Integer.parseInt(args[0]);
92         } catch (final Exception e) {
93             System.err.println("usage EventGenerator #events");
94             e.printStackTrace();
95             return;
96         }
97
98         System.out.println(xmlEvents(eventCount));
99     }
100
101     public static int getNextEventNo() {
102         return nextEventNo;
103     }
104 }