ISSUE ID:- (RICAPP-176).
[ric-app/bouncer.git] / Bouncer / src / xapp-mgmt / subs_mgmt.cc
1 /*
2 ==================================================================================
3         Copyright (c) 2019-2020 AT&T Intellectual Property.
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 ==================================================================================
17 */
18 /*
19  * subs_mgmt.cc
20  * Created on: 2019
21  * Author: Ashwin Shridharan, Shraboni Jana
22  */
23 #include "subs_mgmt.hpp"
24 #include <thread>
25 #include <errno.h>
26
27 SubscriptionHandler::SubscriptionHandler(unsigned int timeout_seconds):_time_out(std::chrono::seconds(timeout_seconds)){
28           _data_lock = std::make_unique<std::mutex>();
29           _cv = std::make_unique<std::condition_variable>();
30 };
31
32 void SubscriptionHandler::clear(void){
33   {
34     std::lock_guard<std::mutex> lock(*(_data_lock).get());
35     status_table.clear();
36   }
37   
38 };
39
40
41 bool SubscriptionHandler::add_request_entry(transaction_identifier id, transaction_status status){
42
43   // add entry in hash table if it does not exist
44   auto search = status_table.find(id);
45   if(search != status_table.end()){
46     return false;
47   }
48   
49   status_table[id] = status;
50   return true;
51
52 };
53
54
55
56 bool SubscriptionHandler::delete_request_entry(transaction_identifier id){
57
58   auto search = status_table.find(id);
59
60   if (!trans_table.empty()) {
61           auto search2 = trans_table.find(id);
62           if(search2 !=trans_table.end()){
63                   trans_table.erase(search2);
64           }
65   }
66
67   if (search != status_table.end()){
68     status_table.erase(search);
69     mdclog_write(MDCLOG_INFO,"Entry for Transaction ID deleted: %d",id);
70     return true;
71   }
72   mdclog_write(MDCLOG_INFO,"Entry not found in SubscriptionHandler for Transaction ID: %d",id);
73
74   return false;
75 };
76
77
78 bool SubscriptionHandler::set_request_status(transaction_identifier id, transaction_status status){
79
80   // change status of a request only if it exists.
81         for(auto &it:status_table){
82                         if(strcmp(it.first.c_str(), id.c_str())==0) {
83                                 it.second = status;
84                                 return true;
85                         }
86                 }
87   return false;
88
89 };
90
91
92 int SubscriptionHandler::get_request_status(transaction_identifier id){
93
94         for(auto it:status_table){
95                 if(strcmp(it.first.c_str(), id.c_str())==0) {
96                         return it.second;
97                 }
98         }
99
100
101   return -1;
102 }
103                                    
104
105
106 bool SubscriptionHandler::is_request_entry(transaction_identifier id){
107         for(auto it:status_table){
108                         if(strcmp(it.first.c_str(), id.c_str())==0) {
109                                 return true;
110                         }
111                 }
112     return false;
113 }
114
115
116
117
118 // Handles subscription responses
119 void SubscriptionHandler::manage_subscription_response(int message_type, transaction_identifier id)
120 {
121         // Make This Thread sleep for 1 Second
122         std::this_thread::sleep_for(std::chrono::milliseconds(1000));
123         {
124                 std::unique_lock<std::mutex> _local_lock(*(_data_lock.get()));
125                 mdclog_write(MDCLOG_INFO,"Subscription Handler: Status for meid %s WAS: %d",id.c_str(),this->get_request_status(id));
126
127                 //from the message type we can know if its a success/failure etc.
128                 if(message_type==RIC_SUB_RESP)
129                         this->set_request_status(id, request_success);
130          
131                 if(message_type==RIC_SUB_DEL_RESP)
132                         this->set_request_status(id, request_success);
133
134                 if(message_type==RIC_SUB_FAILURE)
135                         this->set_request_status(id,request_failed);
136           
137                 if(message_type==RIC_SUB_DEL_FAILURE)
138                         this->set_request_status(id,request_failed);
139           
140                 mdclog_write(MDCLOG_INFO,"Subscription Handler: Status for meid %s IS: %d",id.c_str(),this->get_request_status(id));
141
142
143                 //this->print_subscription_status();
144         }
145         //_cv.get()->notify_all();
146
147 }
148