3 ==================================================================================
4 Copyright (c) 2020 Nokia
5 Copyright (c) 2020 AT&T Intellectual Property.
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 ==================================================================================
23 Abstract: This class provides an API to the alarm collector/reporter.
24 An object is insanced by the user xAPP allowing the xAPP
25 to send, clear, or resend the alarm as is necessary.
29 Author: E. Scott Daniels
37 #include <rmr/RIC_message_types.h>
39 #include "msg_component.hpp"
40 #include "message.hpp"
44 // this _should_ come from the message types header, but if not ensure we have something
45 constexpr int ric_alarm_value = 110;
46 #define RIC_ALARM ric_alarm_value
49 extern const char* __progname; // runtime lib supplied since we don't get argv[0]
55 // ------ private ----------------------------------------------
58 Suss out the alarm target from environment.
60 We expect two variables in the environment:
61 ALARM_MGR_SERVICE_NAME
62 ALARM_MGR_SERVICE_PORT
64 If name is not given, localhost is assumed. If port is not given
65 then we assume 4560 (the defacto RMR listen port).
67 static std::string endpoint_addr( ) {
68 const char* et; // environment token
69 std::string addr = "localhost";
70 std::string port = "4560";
72 if( (et = getenv( "ALARM_MGR_SERVICE_NAME" )) != NULL ) {
73 addr = std::string( et );
76 if( (et = getenv( "ALARM_MGR_SERVICE_PORT" )) != NULL ) {
77 port = std::string( et );
80 return addr + ":" + port;
85 Return the current time in milliseconds past the UNIX epoch.
87 static long long now( void ) {
91 clock_gettime( CLOCK_REALTIME, &ts );
92 now = (ts.tv_sec * 1000000) + (ts.tv_nsec/1000000); // convert nano to milli and bang onto seconds
99 Build the alarm json message with the current data.
100 Returns the length of the payload inserted.
102 int xapp::Alarm::build_alarm( int action_id, xapp::Msg_component payload, int payload_len ) {
103 std::string maction; // message action is a text string
106 if( app_id.compare( "" ) == 0 ) {
107 app_id = std::string( __progname ); // see comment for extern above
110 if( severity.compare( "" ) == 0 ) {
111 Set_severity( Alarm::SEV_WARN );
114 switch( action_id ) {
115 case Alarm::ACT_CLEAR:
119 case Alarm::ACT_CLEAR_ALL:
120 maction = "CLEARALL";
128 used = snprintf( (char *) payload.get(), payload_len,
130 "\"managedObjectId\": \"%s\", "
131 "\"applicationId\": \"%s\", "
132 "\"specificProblem\": %d, "
133 "\"perceivedSeverity\": \"%s\", "
134 "\"identifyingInfo\": \"%s\", "
135 "\"additionalInfo\": \"%s\", "
136 "\"AlarmAction\": \"%s\", "
137 "\"AlarmTime\": %lld"
153 // --------------- builders/operators -------------------------------------
156 Create a new message wrapper for an existing RMR msg buffer.
158 msg is a message which was allocaed by the framework and thus has the
159 mrc reference embedded.
161 xapp::Alarm::Alarm( std::shared_ptr<Message> msg ) :
163 endpoint( endpoint_addr() )
167 Parameterised constructor (avoids calling setters after creation).
169 xapp::Alarm::Alarm( std::shared_ptr<Message> msg, int prob_id, const std::string& meid ) :
171 endpoint( endpoint_addr() ),
173 problem_id( prob_id )
176 xapp::Alarm::Alarm( std::shared_ptr<Message> msg, const std::string& meid ) :
178 endpoint( endpoint_addr() ),
184 // ------------------ copy and move support ---------------------------------
187 Copy builder. Given a source object instance (soi), create a copy.
188 Creating a copy should be avoided as it can be SLOW!
190 xapp::Alarm::Alarm( const Alarm& soi ) :
192 endpoint( soi.endpoint ),
195 me_id( soi.me_id ), // user stuff
196 app_id( soi.app_id ),
197 problem_id( soi.problem_id ),
198 severity( soi.severity ),
200 add_info( soi.add_info )
204 Assignment operator. Simiolar to the copycat, but "this" object exists and
205 may have data that needs to be released prior to making a copy of the soi.
207 Alarm& xapp::Alarm::operator=( const Alarm& soi ) {
208 if( this != &soi ) { // cannot do self assignment
210 endpoint = soi.endpoint;
214 problem_id = soi.problem_id;
215 severity = soi.severity;
217 add_info = soi.add_info;
224 Move builder. Given a source object instance (soi), move the information from
225 the soi ensuring that the destriction of the soi doesn't trash things from
228 xapp::Alarm::Alarm( Alarm&& soi ) :
229 msg( soi.msg ), // order must match .hpp else sonar complains
230 endpoint( soi.endpoint ),
233 app_id( soi.app_id ),
234 problem_id( soi.problem_id ),
235 severity( soi.severity ),
237 add_info( soi.add_info )
240 soi.msg = NULL; // prevent closing of RMR stuff on soi destroy
244 Move Assignment operator. Move the message data to the existing object
245 ensure the object reference is cleaned up, and ensuring that the source
246 object references are removed.
248 Alarm& xapp::Alarm::operator=( Alarm&& soi ) noexcept {
249 if( this != &soi ) { // cannot do self assignment
250 // anything that needs to be freed/delted from soi, must be done here
252 msg = soi.msg; // move pointers and values
253 endpoint = soi.endpoint;
258 problem_id = soi.problem_id;
259 severity = soi.severity;
261 add_info = soi.add_info;
263 soi.msg = NULL; // prevent bad things when source is destroyed
272 xapp::Alarm::~Alarm() {
278 // ---- setters -------------------------------------------------
280 void xapp::Alarm::Set_meid( const std::string& new_meid ) {
284 void xapp::Alarm::Set_severity( int new_sev ) {
286 case Alarm::SEV_CRIT:
287 severity = "CRITICAL";
290 case Alarm::SEV_MAJOR:
294 case Alarm::SEV_MINOR:
298 case Alarm::SEV_WARN:
299 severity = "WARNING";
302 case Alarm::SEV_CLEAR:
303 severity = "CLEARED";
307 severity = "DEFAULT";
312 void xapp::Alarm::Set_appid( const std::string& new_id ) {
316 void xapp::Alarm::Set_problem( int new_id ) {
320 void xapp::Alarm::Set_info( const std::string& new_info ) {
324 void xapp::Alarm::Set_additional( const std::string& new_info ) {
328 void xapp::Alarm::Set_whid( int new_whid ) {
332 void xapp::Alarm::Dump() const {
333 fprintf( stderr, "Alarm: prob id: %d\n", problem_id );
334 fprintf( stderr, "Alarm: meid: %s\n", me_id.c_str() );
335 fprintf( stderr, "Alarm: app: %s\n", app_id.c_str() );
336 fprintf( stderr, "Alarm: info: %s\n", info.c_str() );
337 fprintf( stderr, "Alarm: ainfo: %s\n", add_info.c_str() );
340 // ------------------- getters ------------------------------------
343 Return the enpoint address string we have.
345 std::string xapp::Alarm::Get_endpoint( ) const {
349 // ------- message sending ---------------------------------------
352 Send a raise message with the alarm contents unchanged.
354 bool xapp::Alarm::Raise( ) {
356 used = build_alarm( ACT_RAISE, msg->Get_payload(), msg->Get_available_size() );
357 msg->Wormhole_send( whid, RIC_ALARM, xapp::Message::NO_SUBID, used + 1, NULL );
361 Additional prototypes allow for avoiding some setter calls when raising alarms.
362 Severity is one of our SEV_* constants. Problem is the caller's assigned
363 problem ID. Info and addional_info are user supplied data that is just passed
366 bool xapp::Alarm::Raise( int new_severity, int problem, const std::string& cinfo ) {
367 Set_severity( new_severity );
368 problem_id = problem;
374 bool xapp::Alarm::Raise( int new_severity, int problem, const std::string& cinfo, const std::string& additional_info ) {
375 Set_severity( new_severity );
376 problem_id = problem;
378 this->add_info = additional_info;
384 Send a clear message with the contents of the alarm otherwise unchanged.
386 bool xapp::Alarm::Clear( ) {
389 used = build_alarm( ACT_CLEAR, msg->Get_payload(), msg->Get_available_size() );
390 msg->Wormhole_send( whid, RIC_ALARM, xapp::Message::NO_SUBID, used + 1, NULL );
394 Additional prototypes allow for avoiding some setter calls when raising alarms.
395 Severity is one of our SEV_* constants. Problem is the caller's assigned
396 problem ID. Info and addional_info are user supplied data that is just passed
399 bool xapp::Alarm::Clear( int new_severity, int problem, const std::string& cinfo ) {
400 Set_severity( new_severity );
401 problem_id = problem;
407 bool xapp::Alarm::Clear( int new_severity, int problem, const std::string& cinfo, const std::string& additional_info ) {
408 Set_severity( new_severity );
409 problem_id = problem;
411 this->add_info = additional_info;
418 Send a clear-all message. The contents of the alarm are unaffected.
420 bool xapp::Alarm::Clear_all( ) {
423 used = build_alarm( ACT_CLEAR_ALL, msg->Get_payload(), msg->Get_available_size() );
424 msg->Wormhole_send( whid, RIC_ALARM, xapp::Message::NO_SUBID, used + 1, NULL );
429 This is a convenience function which sends a clear message followed by a
430 raise message. Alarm contents are not adjusted.
432 bool xapp::Alarm::Raise_again( ) {