Revision of copyright
[ric-app/kpimon.git] / src / KPI-Monitoring / kpi_db.cc
1 /*\r
2 ==================================================================================\r
3         Copyright (c) 2018-2019 SAMSUNG Intellectual Property.\r
4 \r
5    Licensed under the Apache License, Version 2.0 (the "License");\r
6    you may not use this file except in compliance with the License.\r
7    You may obtain a copy of the License at\r
8 \r
9        http://www.apache.org/licenses/LICENSE-2.0\r
10 \r
11    Unless required by applicable law or agreed to in writing, software\r
12    distributed under the License is distributed on an "AS IS" BASIS,\r
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14    See the License for the specific language governing permissions and\r
15    limitations under the License.\r
16 ==================================================================================\r
17 */\r
18 \r
19 #include <kpi_db.hpp>\r
20 \r
21 // Destructor\r
22 kpi_db_resp::~kpi_db_resp(void){\r
23     freeReplyObject(redis_reply);\r
24 };\r
25 \r
26 // Push the data to the DB\r
27 void kpi_db_resp::Rpush(redisContext *c, std::string key,std::string value){\r
28   redis_reply = (redisReply*)redisCommand(c, "RPUSH %s %s",key,value);\r
29 }\r
30 \r
31 // Getting val corresponding to key\r
32 void kpi_db_resp::Get(redisContext *c, std::string key){\r
33   redis_reply= (redisReply*)redisCommand(c, "GET %s",key);\r
34 }\r
35 \r
36 // Delete the key and value pair from the DB\r
37 void kpi_db_resp::Del(redisContext *c, std::string key){\r
38   redis_reply= (redisReply*)redisCommand(c, "DEL %s",key);\r
39 }\r
40 \r
41 // Check whether the Key exists in the DB\r
42 void kpi_db_resp::Exists(redisContext *c, std::string key){\r
43   redis_reply= (redisReply*)redisCommand(c, "EXISTS %s",key);\r
44 }\r
45 \r
46 // Called whenever the KPI information is received from the RIC Indication message\r
47 void kpi_db_resp::UpdateDB(redisContext* c,cucp_resource_helper *kpi_data){\r
48 \r
49     if(kpi_data == NULL || c==NULL) {\r
50         mdclog_write(MDCLOG_INFO, "Reddis Context or KPI_DATA pointer NULL\n");\r
51         return;\r
52     }\r
53     std::string gnb_id(reinterpret_cast<char*>(kpi_data->gnbId));\r
54     Exists(c,gnb_id);\r
55     if(redis_reply->type==REDIS_REPLY_STATUS){\r
56       if(redis_reply->integer==1){\r
57         //Key already exists ---> delete the data and insert again\r
58         Del(c,gnb_id);\r
59         Rpush(c,gnb_id,std::to_string(kpi_data->cuCpResourceStatus.bhca));\r
60         Rpush(c,gnb_id,std::to_string(kpi_data->cuCpResourceStatus.numberOfUEs));\r
61       }\r
62       else{\r
63         //Key does not exists --> Simply insert\r
64         Rpush(c,gnb_id,std::to_string(kpi_data->cuCpResourceStatus.bhca));\r
65         Rpush(c,gnb_id,std::to_string(kpi_data->cuCpResourceStatus.numberOfUEs));\r
66       }\r
67     }\r
68     mdclog_write(MDCLOG_INFO, "GNB_ID : %s\n\tBHCA : %d\n\tUE_Count : %d",gnb_id, kpi_data->cuCpResourceStatus.bhca, kpi_data->cuCpResourceStatus.numberOfUEs);\r
69 }