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