4b17f5a887e3675b8b2b9d28703ae4e3c21f7214
[ric-app/hw.git] / 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
25 #include <errno.h>
26
27
28 SubscriptionHandler::SubscriptionHandler(unsigned int timeout_seconds):_time_out(std::chrono::seconds(timeout_seconds)){
29           _data_lock = std::make_unique<std::mutex>();
30           _cv = std::make_unique<std::condition_variable>();
31 };
32
33 void SubscriptionHandler::clear(void){
34   {
35     std::lock_guard<std::mutex> lock(*(_data_lock).get());
36     status_table.clear();
37   }
38   
39 };
40
41
42 bool SubscriptionHandler::add_request_entry(transaction_identifier id, transaction_status status){
43
44   // add entry in hash table if it does not exist
45   auto search = status_table.find(id);
46   if(search != status_table.end()){
47     return false;
48   }
49   
50   status_table[id] = status;
51   return true;
52
53 };
54
55
56
57 bool SubscriptionHandler::delete_request_entry(transaction_identifier id){
58
59   auto search = status_table.find(id);
60
61   if (!trans_table.empty()) {
62           auto search2 = trans_table.find(id);
63           if(search2 !=trans_table.end()){
64                   trans_table.erase(search2);
65           }
66   }
67
68   if (search != status_table.end()){
69     status_table.erase(search);
70     mdclog_write(MDCLOG_INFO,"Entry for Transaction ID deleted: %d",id);
71     return true;
72   }
73   mdclog_write(MDCLOG_INFO,"Entry not found in SubscriptionHandler for Transaction ID: %d",id);
74
75   return false;
76 };
77
78
79 bool SubscriptionHandler::set_request_status(transaction_identifier id, transaction_status status){
80
81   // change status of a request only if it exists.
82   auto search = status_table.find(id);
83   if(search != status_table.end()){
84     status_table[id] = status;
85     return true;
86   }
87
88   return false;
89   
90 };
91
92
93 int const SubscriptionHandler::get_request_status(transaction_identifier id){
94   auto search = status_table.find(id);
95   if (search == status_table.end()){
96     return -1;
97   }
98   
99   return search->second;
100 }
101                                    
102
103
104 bool SubscriptionHandler::is_request_entry(transaction_identifier id){
105   auto search = status_table.find(id);
106   if (search != status_table.end())
107     return true;
108   else
109     return false;
110 }
111
112 // Handles subscription responses
113 void SubscriptionHandler::manage_subscription_response(int message_type, transaction_identifier id){
114
115   bool res;
116   std::cout << "In Manage subscription" << std::endl;
117
118   // wake up all waiting users ...
119   if(is_request_entry(id)){
120           std::cout << "In Manage subscription" << std::endl;
121           set_request_status(id, request_success);
122      _cv.get()->notify_all();
123   }
124
125 }
126