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>
38 // this _should_ come from the message types header, but if not ensure we have something
39 constexpr int ric_alarm_value = 110;
40 #define RIC_ALARM ric_alarm_value
45 #include "msg_component.hpp"
46 #include "message.hpp"
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"
150 //action = std::string( wbuf );
154 // --------------- builders/operators -------------------------------------
157 Create a new message wrapper for an existing RMR msg buffer.
159 msg is a message which was allocaed by the framework and thus has the
160 mrc reference embedded.
162 xapp::Alarm::Alarm( std::shared_ptr<Message> msg ) :
164 endpoint( endpoint_addr() )
168 Parameterised constructor (avoids calling setters after creation).
170 xapp::Alarm::Alarm( std::shared_ptr<Message> msg, int prob_id, const std::string& meid ) :
172 endpoint( endpoint_addr() ),
174 problem_id( prob_id )
177 xapp::Alarm::Alarm( std::shared_ptr<Message> msg, const std::string& meid ) :
179 endpoint( endpoint_addr() ),
185 // ------------------ copy and move support ---------------------------------
188 Copy builder. Given a source object instance (soi), create a copy.
189 Creating a copy should be avoided as it can be SLOW!
191 xapp::Alarm::Alarm( const Alarm& soi ) :
193 endpoint( soi.endpoint ),
196 me_id( soi.me_id ), // user stuff
197 app_id( soi.app_id ),
198 problem_id( soi.problem_id ),
199 severity( soi.severity ),
201 add_info( soi.add_info )
205 Assignment operator. Simiolar to the copycat, but "this" object exists and
206 may have data that needs to be released prior to making a copy of the soi.
208 Alarm& xapp::Alarm::operator=( const Alarm& soi ) {
209 if( this != &soi ) { // cannot do self assignment
211 endpoint = soi.endpoint;
215 problem_id = soi.problem_id;
216 severity = soi.severity;
218 add_info = soi.add_info;
225 Move builder. Given a source object instance (soi), move the information from
226 the soi ensuring that the destriction of the soi doesn't trash things from
229 xapp::Alarm::Alarm( Alarm&& soi ) :
230 msg( soi.msg ), // order must match .hpp else sonar complains
231 endpoint( soi.endpoint ),
234 app_id( soi.app_id ),
235 problem_id( soi.problem_id ),
236 severity( soi.severity ),
238 add_info( soi.add_info )
241 soi.msg = NULL; // prevent closing of RMR stuff on soi destroy
245 Move Assignment operator. Move the message data to the existing object
246 ensure the object reference is cleaned up, and ensuring that the source
247 object references are removed.
249 Alarm& xapp::Alarm::operator=( Alarm&& soi ) {
250 if( this != &soi ) { // cannot do self assignment
251 // anything that needs to be freed/delted from soi, must be done here
253 msg = soi.msg; // move pointers and values
254 endpoint = soi.endpoint;
259 problem_id = soi.problem_id;
260 severity = soi.severity;
262 add_info = soi.add_info;
264 soi.msg = NULL; // prevent bad things when source is destroyed
273 xapp::Alarm::~Alarm() {
279 // ---- setters -------------------------------------------------
281 void xapp::Alarm::Set_meid( const std::string& new_meid ) {
285 void xapp::Alarm::Set_severity( int new_sev ) {
287 case Alarm::SEV_CRIT:
288 severity = "CRITICAL";
291 case Alarm::SEV_MAJOR:
295 case Alarm::SEV_MINOR:
299 case Alarm::SEV_WARN:
300 severity = "WARNING";
303 case Alarm::SEV_CLEAR:
304 severity = "CLEARED";
308 severity = "DEFAULT";
313 void xapp::Alarm::Set_appid( const std::string& new_id ) {
317 void xapp::Alarm::Set_problem( int new_id ) {
321 void xapp::Alarm::Set_info( const std::string& new_info ) {
325 void xapp::Alarm::Set_additional( const std::string& new_info ) {
329 void xapp::Alarm::Set_whid( int new_whid ) {
333 const void xapp::Alarm::Dump() {
334 fprintf( stderr, "Alarm: prob id: %d\n", problem_id );
335 fprintf( stderr, "Alarm: meid: %s\n", me_id.c_str() );
336 fprintf( stderr, "Alarm: app: %s\n", app_id.c_str() );
337 fprintf( stderr, "Alarm: info: %s\n", info.c_str() );
338 fprintf( stderr, "Alarm: ainfo: %s\n", add_info.c_str() );
341 // ------------------- getters ------------------------------------
344 Return the enpoint address string we have.
346 const std::string xapp::Alarm::Get_endpoint( ) {
350 // ------- message sending ---------------------------------------
353 Send a raise message with the alarm contents unchanged.
355 bool xapp::Alarm::Raise( ) {
357 used = build_alarm( ACT_RAISE, msg->Get_payload(), msg->Get_available_size() );
358 msg->Wormhole_send( whid, RIC_ALARM, xapp::Message::NO_SUBID, used + 1, NULL );
362 Additional prototypes allow for avoiding some setter calls when raising alarms.
363 Severity is one of our SEV_* constants. Problem is the caller's assigned
364 problem ID. Info and addional_info are user supplied data that is just passed
367 bool xapp::Alarm::Raise( int new_severity, int problem, const std::string& info ) {
368 Set_severity( new_severity );
369 problem_id = problem;
375 bool xapp::Alarm::Raise( int new_severity, int problem, const std::string& info, const std::string& additional_info ) {
376 Set_severity( new_severity );
377 problem_id = problem;
379 this->add_info = additional_info;
385 Send a clear message with the contents of the alarm otherwise unchanged.
387 bool xapp::Alarm::Clear( ) {
390 used = build_alarm( ACT_CLEAR, msg->Get_payload(), msg->Get_available_size() );
391 msg->Wormhole_send( whid, RIC_ALARM, xapp::Message::NO_SUBID, used + 1, NULL );
395 Additional prototypes allow for avoiding some setter calls when raising alarms.
396 Severity is one of our SEV_* constants. Problem is the caller's assigned
397 problem ID. Info and addional_info are user supplied data that is just passed
400 bool xapp::Alarm::Clear( int new_severity, int problem, const std::string& info ) {
401 Set_severity( new_severity );
402 problem_id = problem;
408 bool xapp::Alarm::Clear( int new_severity, int problem, const std::string& info, const std::string& additional_info ) {
409 Set_severity( new_severity );
410 problem_id = problem;
412 this->add_info = additional_info;
419 Send a clear-all message. The contents of the alarm are unaffected.
421 bool xapp::Alarm::Clear_all( ) {
424 used = build_alarm( ACT_CLEAR_ALL, msg->Get_payload(), msg->Get_available_size() );
425 msg->Wormhole_send( whid, RIC_ALARM, xapp::Message::NO_SUBID, used + 1, NULL );
430 This is a convenience function which sends a clear message followed by a
431 raise message. Alarm contents are not adjusted.
433 bool xapp::Alarm::Raise_again( ) {