1b0676caac30b020f1c94c2d0015351b80eeb1d7
[nonrtric.git] / test / usecases / oruclosedlooprecovery / scriptversion / simulators / message_generator.py
1
2 #  ============LICENSE_START===============================================
3 #  Copyright (C) 2021 Nordix Foundation. 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 #  ============LICENSE_END=================================================
17 #
18
19 import json
20 import os
21 import random
22 import requests
23 import time
24
25 # Randomly, between 0 and 10 seconds sends a "CUS Link Failure" alarm event to the Message Router. The ID of the O-RU is also
26 # randomly generated between 0 and 9.
27 # When the modulo of the ID is 1, a "heartbeat" message will also be sent to MR.
28
29 mr_host = "http://localhost"
30 mr_port = "3904"
31 MR_PATH = "/events/unauthenticated.SEC_FAULT_OUTPUT"
32
33 linkFailureMessage = {
34     "event": {
35         "commonEventHeader": {
36             "domain": "fault",
37             "eventId": "nt:network-topology/nt:topology/nt:node/nt:node-id",
38             "eventName": "fault_O-RAN-RU-Fault_Alarms_CUS_Link_Failure",
39             "eventType": "O-RAN-RU-Fault",
40             "sequence": 0,
41             "priority": "Normal",
42             "reportingEntityId": "SDNR",
43             "reportingEntityName": "@controllerName@",
44             "sourceId": "",
45             "sourceName": "O-RU-ID",
46             "startEpochMicrosec": "@timestamp@",
47             "lastEpochMicrosec": "@timestamp@",
48             "nfNamingCode": "",
49             "nfVendorName": "ietf-hardware (RFC8348) /hardware/component[not(parent)][1]/mfg-name",
50             "timeZoneOffset": "+00:00",
51             "version": "4.1",
52             "vesEventListenerVersion": "7.2.1"
53         },
54         "faultFields": {
55             "faultFieldsVersion": "4.0",
56             "alarmCondition": "30",
57             "alarmInterfaceA": "o-ran-fm:alarm-notif/fault-source",
58             "eventSourceType": "ietf-hardware (RFC8348) /hardware/component[not(parent)][1]/mfg-model or \"O-RU\"",
59             "specificProblem": "",
60             "eventSeverity": "CRITICAL",
61             "vfStatus": "Active",
62             "alarmAdditionalInformation": {
63                 "eventTime": "@eventTime@",
64                 "equipType": "@type@",
65                 "vendor": "@vendor@",
66                 "model": "@model@"
67             }
68         }
69     }
70 }
71
72 heartBeatMessage = {
73    "event": {
74      "commonEventHeader": {
75        "version": 3.0,
76        "domain": "heartbeat",
77        "eventName": "Heartbeat\_vIsbcMmc",
78        "eventId": "ab305d54-85b4-a31b-7db2fb6b9e546015",
79        "sequence": 0,
80        "priority": "Normal",
81        "reportingEntityId": "cc305d54-75b4-431badb2eb6b9e541234",
82        "reportingEntityName": "EricssonOamVf",
83        "sourceId": "de305d54-75b4-431b-adb2-eb6b9e546014",
84        "sourceName": "ibcx0001vm002ssc001",
85        "nfNamingCode": "ibcx",
86        "nfcNamingCode": "ssc",
87        "startEpochMicrosec": 1413378172000000,
88        "lastEpochMicrosec": 1413378172000000
89       }
90    }
91  }
92
93 if __name__ == "__main__":
94     if os.getenv("MR-HOST") is not None:
95         mr_host = os.getenv("MR-HOST")
96         print("Using MR Host from os: " + mr_host)
97     if os.getenv("MR-PORT") is not None:
98         mr_port = os.getenv("MR-PORT")
99         print("Using MR Port from os: " + mr_port)
100
101     mr_url = mr_host + ":" + mr_port + MR_PATH
102     print(mr_url)
103     while True:
104         random_time = int(10 * random.random())
105         if (random_time % 3 == 1):
106             print("Sent heart beat")
107             requests.post(mr_url, json=heartBeatMessage);
108
109         o_ru_id = "ERICSSON-O-RU-1122" + str(random_time)
110         print("Sent link failure for O-RAN-RU: " + o_ru_id)
111         msg_as_json = json.loads(json.dumps(linkFailureMessage))
112         msg_as_json["event"]["commonEventHeader"]["sourceName"] = o_ru_id
113         requests.post(mr_url, json=msg_as_json);
114
115         time.sleep(random_time)
116