f85d9119ece29c2d2a4f66304e42fa5b383ff87a
[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  * The Class XmlEventGenerator.
27  */
28 public class XmlEventGenerator {
29     private static int nextEventNo = 0;
30
31     /**
32      * Xml events.
33      *
34      * @param eventCount the event count
35      * @return the string
36      */
37     public static String xmlEvents(final int eventCount) {
38         final StringBuilder builder = new StringBuilder();
39
40         for (int i = 0; i < eventCount; i++) {
41             if (i > 0) {
42                 builder.append("\n");
43             }
44             builder.append(xmlEvent());
45         }
46
47         return builder.toString();
48     }
49
50     /**
51      * Xml event.
52      *
53      * @return the string
54      */
55     public static String xmlEvent() {
56         final Random rand = new Random();
57
58         final StringBuilder builder = new StringBuilder();
59
60         int nextEventNo = rand.nextInt(2);
61         final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100");
62         final int nextMatchCase = rand.nextInt(4);
63         final float nextTestTemperature = rand.nextFloat() * 10000;
64
65         builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
66         builder.append("<xmlApexEvent xmlns=\"http://www.onap.org/policy/apex-pdp/apexevent\">\n");
67
68         builder.append("  <name>" + eventName + "</name>\n");
69         builder.append("  <version>0.0.1</version>\n");
70         builder.append("  <nameSpace>org.onap.policy.apex.sample.events</nameSpace>\n");
71         builder.append("  <source>test</source>\n");
72         builder.append("  <target>apex</target>\n");
73         builder.append("  <data>\n");
74         builder.append("    <key>TestSlogan</key>\n");
75         builder.append("    <value>Test slogan for External Event" + (nextEventNo++) + "</value>\n");
76         builder.append("  </data>\n");
77         builder.append("  <data>\n");
78         builder.append("    <key>TestMatchCase</key>\n");
79         builder.append("    <value>" + nextMatchCase + "</value>\n");
80         builder.append("  </data>\n");
81         builder.append("  <data>\n");
82         builder.append("    <key>TestTimestamp</key>\n");
83         builder.append("    <value>" + System.currentTimeMillis() + "</value>\n");
84         builder.append("  </data>\n");
85         builder.append("  <data>\n");
86         builder.append("    <key>TestTemperature</key>\n");
87         builder.append("    <value>" + nextTestTemperature + "</value>\n");
88         builder.append("  </data>\n");
89         builder.append("</xmlApexEvent>");
90
91         return builder.toString();
92     }
93
94     /**
95      * The main method.
96      *
97      * @param args the arguments
98      */
99     public static void main(final String[] args) {
100         if (args.length != 1) {
101             System.err.println("usage EventGenerator #events");
102             return;
103         }
104
105         int eventCount = 0;
106         try {
107             eventCount = Integer.parseInt(args[0]);
108         } catch (final Exception e) {
109             System.err.println("usage EventGenerator #events");
110             e.printStackTrace();
111             return;
112         }
113
114         System.out.println(xmlEvents(eventCount));
115     }
116
117     /**
118      * Gets the next event no.
119      *
120      * @return the next event no
121      */
122     public static int getNextEventNo() {
123         return nextEventNo;
124     }
125 }