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
36 #include <rmr/RIC_message_types.h>
43 #include "msg_component.hpp"
44 #include "message.hpp"
47 extern char* __progname; // runtime lib supplied since we don't get argv[0]
53 // ------ private ----------------------------------------------
56 Suss out the alarm target from environment.
58 We expect two variables in the environment:
59 ALARM_MGR_SERVICE_NAME
60 ALARM_MGR_SERVICE_PORT
62 If name is not given, localhost is assumed. If port is not given
63 then we assume 4560 (the defacto RMR listen port).
65 static std::string endpoint_addr( ) {
66 char* et; // environment token
67 std::string addr = "localhost";
68 std::string port = "4560";
70 if( (et = getenv( "ALARM_MGR_SERVICE_NAME" )) != NULL ) {
71 addr = std::string( et );
74 if( (et = getenv( "ALARM_MGR_SERVICE_PORT" )) != NULL ) {
75 port = std::string( et );
78 return addr + ":" + port;
83 Return the current time in milliseconds past the UNIX epoch.
85 static long long now( void ) {
89 clock_gettime( CLOCK_REALTIME, &ts );
90 now = (ts.tv_sec * 1000000) + (ts.tv_nsec/1000000); // convert nano to milli and bang onto seconds
97 Build the alarm json message with the current data.
98 Returns the length of the payload inserted.
100 int xapp::Alarm::build_alarm( int action_id, xapp::Msg_component payload, int payload_len ) {
105 if( app_id.compare( "" ) == 0 ) {
106 app_id = std::string( __progname ); // see comment for extern above
109 if( severity.compare( "" ) == 0 ) {
110 Set_severity( Alarm::SEV_WARN );
113 switch( action_id ) {
114 case Alarm::ACT_CLEAR:
118 case Alarm::ACT_CLEAR_ALL:
127 //memset( wbuf, 0, sizeof( wbuf ) );
128 //snprintf( wbuf, sizeof( wbuf ),
129 used = snprintf( (char *) payload.get(), payload_len,
131 "\"managedObjectId\": \"%s\", "
132 "\"applicationId\": \"%s\", "
133 "\"specificProblem\": %d, "
134 "\"perceivedSeverity\": \"%s\", "
135 "\"identifyingInfo\": \"%s\", "
136 "\"additionalInfo\": \"%s\", "
137 "\"AlarmAction\": \"%s\", "
138 "\"AlarmTime\": %lld"
151 //action = std::string( wbuf );
155 // --------------- builders/operators -------------------------------------
158 Create a new message wrapper for an existing RMR msg buffer.
160 msg is a message which was allocaed by the framework and thus has the
161 mrc reference embedded.
163 xapp::Alarm::Alarm( std::shared_ptr<Message> msg ) :
165 endpoint( endpoint_addr() ),
176 Parameterised constructor (avoids calling setters after creation).
178 xapp::Alarm::Alarm( std::shared_ptr<Message> msg, int prob_id, std::string meid ) :
180 endpoint( endpoint_addr() ),
184 problem_id( prob_id ),
190 xapp::Alarm::Alarm( std::shared_ptr<Message> msg, std::string meid ) :
192 endpoint( endpoint_addr() ),
204 // ------------------ copy and move support ---------------------------------
207 Copy builder. Given a source object instance (soi), create a copy.
208 Creating a copy should be avoided as it can be SLOW!
210 xapp::Alarm::Alarm( const Alarm& soi ) {
212 endpoint = soi.endpoint;
215 me_id = soi.me_id; // user stuff
217 problem_id = soi.problem_id;
218 severity = soi.severity;
220 add_info = soi.add_info;
224 Assignment operator. Simiolar to the copycat, but "this" object exists and
225 may have data that needs to be released prior to making a copy of the soi.
227 Alarm& xapp::Alarm::operator=( const Alarm& soi ) {
228 if( this != &soi ) { // cannot do self assignment
230 endpoint = soi.endpoint;
235 problem_id = soi.problem_id;
236 severity = soi.severity;
238 add_info = soi.add_info;
245 Move builder. Given a source object instance (soi), move the information from
246 the soi ensuring that the destriction of the soi doesn't trash things from
249 xapp::Alarm::Alarm( Alarm&& soi ) {
250 msg = soi.msg; // capture pointers and copy data before setting soruce things to nil
251 endpoint = soi.endpoint;
256 problem_id = soi.problem_id;
257 severity = soi.severity;
259 add_info = soi.add_info;
261 soi.msg = NULL; // prevent closing of RMR stuff on soi destroy
265 Move Assignment operator. Move the message data to the existing object
266 ensure the object reference is cleaned up, and ensuring that the source
267 object references are removed.
269 Alarm& xapp::Alarm::operator=( Alarm&& soi ) {
270 if( this != &soi ) { // cannot do self assignment
271 // anything that needs to be freed/delted from soi, must be done here
273 msg = soi.msg; // move pointers and values
274 endpoint = soi.endpoint;
279 problem_id = soi.problem_id;
280 severity = soi.severity;
282 add_info = soi.add_info;
284 soi.msg = NULL; // prevent bad things when source is destroyed
293 xapp::Alarm::~Alarm() {
299 // ---- setters -------------------------------------------------
301 void xapp::Alarm::Set_meid( std::string new_meid ) {
305 void xapp::Alarm::Set_severity( int new_sev ) {
307 case Alarm::SEV_CRIT:
308 severity = "CRITICAL";
311 case Alarm::SEV_MAJOR:
315 case Alarm::SEV_MINOR:
319 case Alarm::SEV_WARN:
320 severity = "WARNING";
323 case Alarm::SEV_CLEAR:
324 severity = "CLEARED";
328 severity = "DEFAULT";
333 void xapp::Alarm::Set_appid( std::string new_id ) {
337 void xapp::Alarm::Set_problem( int new_id ) {
341 void xapp::Alarm::Set_info( std::string new_info ) {
345 void xapp::Alarm::Set_additional( std::string new_info ) {
349 void xapp::Alarm::Set_whid( int new_whid ) {
353 void xapp::Alarm::Dump() {
354 fprintf( stderr, "Alarm: prob id: %d\n", problem_id );
355 fprintf( stderr, "Alarm: meid: %s\n", me_id.c_str() );
356 fprintf( stderr, "Alarm: app: %s\n", app_id.c_str() );
357 fprintf( stderr, "Alarm: info: %s\n", info.c_str() );
358 fprintf( stderr, "Alarm: ainfo: %s\n", add_info.c_str() );
361 // ------------------- getters ------------------------------------
364 Return the enpoint address string we have.
366 std::string xapp::Alarm::Get_endpoint( ) {
370 // ------- message sending ---------------------------------------
373 Send a raise message with the alarm contents unchanged.
375 bool xapp::Alarm::Raise( ) {
377 used = build_alarm( ACT_RAISE, msg->Get_payload(), msg->Get_available_size() );
378 msg->Wormhole_send( whid, RIC_ALARM, xapp::Message::NO_SUBID, used + 1, NULL );
382 Additional prototypes allow for avoiding some setter calls when raising alarms.
383 Severity is one of our SEV_* constants. Problem is the caller's assigned
384 problem ID. Info and addional_info are user supplied data that is just passed
387 bool xapp::Alarm::Raise( int severity, int problem, std::string info ) {
388 this->severity = severity;
389 problem_id = problem;
395 bool xapp::Alarm::Raise( int severity, int problem, std::string info, std::string additional_info ) {
396 this->severity = severity;
397 problem_id = problem;
399 this->add_info = additional_info;
405 Send a clear message with the contents of the alarm otherwise unchanged.
407 bool xapp::Alarm::Clear( ) {
410 used = build_alarm( ACT_CLEAR, msg->Get_payload(), msg->Get_available_size() );
411 msg->Wormhole_send( whid, RIC_ALARM, xapp::Message::NO_SUBID, used + 1, NULL );
415 Additional prototypes allow for avoiding some setter calls when raising alarms.
416 Severity is one of our SEV_* constants. Problem is the caller's assigned
417 problem ID. Info and addional_info are user supplied data that is just passed
420 bool xapp::Alarm::Clear( int severity, int problem, std::string info ) {
421 this->severity = severity;
422 problem_id = problem;
428 bool xapp::Alarm::Clear( int severity, int problem, std::string info, std::string additional_info ) {
429 this->severity = severity;
430 problem_id = problem;
432 this->add_info = additional_info;
439 Send a clear-all message. The contents of the alarm are unaffected.
441 bool xapp::Alarm::Clear_all( ) {
444 used = build_alarm( ACT_CLEAR_ALL, msg->Get_payload(), msg->Get_available_size() );
445 msg->Wormhole_send( whid, RIC_ALARM, xapp::Message::NO_SUBID, used + 1, NULL );
450 This is a convenience function which sends a clear message followed by a
451 raise message. Alarm contents are not adjusted.
453 bool xapp::Alarm::Raise_again( ) {