ONAP FTC job script to run when new patchset created (Updating a1pms snapshot version...
[nonrtric.git] / 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 FAULT_ID = "28"
33
34 linkFailureMessage = {
35     "event": {
36         "commonEventHeader": {
37             "domain": "fault",
38             "eventId": "nt:network-topology/nt:topology/nt:node/nt:node-id",
39             "eventName": "fault_O-RAN-RU-Fault_Alarms_CUS_Link_Failure",
40             "eventType": "O-RAN-RU-Fault",
41             "sequence": 0,
42             "priority": "Normal",
43             "reportingEntityId": "SDNR",
44             "reportingEntityName": "@controllerName@",
45             "sourceId": "",
46             "sourceName": "O-RU-ID",
47             "startEpochMicrosec": "@timestamp@",
48             "lastEpochMicrosec": "@timestamp@",
49             "nfNamingCode": "",
50             "nfVendorName": "ietf-hardware (RFC8348) /hardware/component[not(parent)][1]/mfg-name",
51             "timeZoneOffset": "+00:00",
52             "version": "4.1",
53             "vesEventListenerVersion": "7.2.1"
54         },
55         "faultFields": {
56             "faultFieldsVersion": "4.0",
57             "alarmCondition": FAULT_ID,
58             "alarmInterfaceA": "o-ran-fm:alarm-notif/fault-source",
59             "eventSourceType": "ietf-hardware (RFC8348) /hardware/component[not(parent)][1]/mfg-model or \"O-RU\"",
60             "specificProblem": "",
61             "eventSeverity": "CRITICAL",
62             "vfStatus": "Active",
63             "alarmAdditionalInformation": {
64                 "eventTime": "@eventTime@",
65                 "equipType": "@type@",
66                 "vendor": "@vendor@",
67                 "model": "@model@"
68             }
69         }
70     }
71 }
72
73 heartBeatMessage = {
74    "event": {
75      "commonEventHeader": {
76        "version": 3.0,
77        "domain": "heartbeat",
78        "eventName": "Heartbeat\_vIsbcMmc",
79        "eventId": "ab305d54-85b4-a31b-7db2fb6b9e546015",
80        "sequence": 0,
81        "priority": "Normal",
82        "reportingEntityId": "cc305d54-75b4-431badb2eb6b9e541234",
83        "reportingEntityName": "EricssonOamVf",
84        "sourceId": "de305d54-75b4-431b-adb2-eb6b9e546014",
85        "sourceName": "ibcx0001vm002ssc001",
86        "nfNamingCode": "ibcx",
87        "nfcNamingCode": "ssc",
88        "startEpochMicrosec": 1413378172000000,
89        "lastEpochMicrosec": 1413378172000000
90       }
91    }
92  }
93
94
95 def sendPostRequest(url, msg):
96     try:
97         requests.post(url, json=msg)
98     except Exception as e:
99         print(type(e))
100         print(e.args)
101         print(e)
102
103
104 if __name__ == "__main__":
105     if os.getenv("MR-HOST") is not None:
106         mr_host = os.getenv("MR-HOST")
107         print("Using MR Host from os: " + mr_host)
108     if os.getenv("MR-PORT") is not None:
109         mr_port = os.getenv("MR-PORT")
110         print("Using MR Port from os: " + mr_port)
111
112     mr_url = mr_host + ":" + mr_port + MR_PATH
113     print(mr_url)
114     while True:
115         random_time = int(10 * random.random())
116         if (random_time % 3 == 1):
117             print("Sent heart beat")
118             sendPostRequest(mr_url, heartBeatMessage)
119
120         o_ru_id = "ERICSSON-O-RU-1122" + str(random_time)
121         print("Sent link failure for O-RAN-RU: " + o_ru_id)
122         msg_as_json = json.loads(json.dumps(linkFailureMessage))
123         msg_as_json["event"]["commonEventHeader"]["sourceName"] = o_ru_id
124         sendPostRequest(mr_url, msg_as_json)
125
126         time.sleep(random_time)
127