User story RICPLT-2620
[ric-app/admin.git] / src / protector-plugin / NetworkProtector.h
1 /*
2 ==================================================================================
3
4         Copyright (c) 2018-2019 AT&T Intellectual Property.
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20
21 #ifndef NETWORKPROTECTOR_H
22 #define NETWORKPROTECTOR_H
23
24 #include "sliding_window.hpp"
25 #include <mutex>
26 #include <map>
27 #include <sgnb_addition_request.hpp>  // to decode the X2AP payload
28 #include <sgnb_addition_response.hpp> // to respond
29
30 #ifdef __GNUC__
31 #define likely(x)  __builtin_expect((x), 1)
32 #define unlikely(x) __builtin_expect((x), 0)
33 #else
34 #define likely(x) (x)
35 #define unlikely(x) (x)
36 #endif
37
38 // each policy corresponds to a specific X2 subscriber  profile ID
39 // and applies sliding window logic to UEs in that class (if enforce)
40 class protector_policy {
41 public:
42   protector_policy(bool enforce=true,  int window_size=60, int threshold=10, double block_rate=10): _enforce(enforce), _window_size(window_size), _threshold(threshold), _block_rate(block_rate){
43     _counter = 0;
44     _req = 0;
45     _rej = 0;
46     _window_ref = std::make_unique<sliding_window>(_window_size);
47   };
48
49   bool _enforce;                // do we enforce policy ?
50   int _counter;                 // count the # of attaching access
51   int _window_size;                     // time in seconds window for the # of counts
52   int _threshold;                       // count above which we start enforcing if enforce set
53   double _block_rate;           // % of rejecting rate for counter > threshold
54   std::unique_ptr<sliding_window> _window_ref;
55   unsigned long int _req; // number of requests
56   unsigned long int _rej; // number of rejects
57
58 };
59
60   
61 class protector
62 {
63 public: 
64   
65   protector( bool rep=true);
66   protector(bool enforce, int window_size, int threshold, double block_rate, bool rep=true);
67   bool operator()(unsigned char *, size_t , unsigned char *, size_t *);
68   
69   bool configure(bool enforce, int windowSize_, int threshold_, double blockRate_, int id);
70   bool add_policy (bool enforce, int windowSize_, int threshold_, double blockRate_, int id);
71   bool delete_policy(int id);
72   bool query_policy(int , std::vector<double> &);
73   void get_active_policies(std::vector<int> & );
74   bool is_active(int id);
75   
76   void clear();
77   bool selectiveBlock(double);
78
79   long int get_requests(int id) const;
80   long int get_rejects(int id) const;
81   std::string get_error(void) { return error_string;};
82   
83 private:
84
85   std::map<int, protector_policy> policy_list;
86   std::unique_ptr<std::mutex> m_access;
87
88   sgnb_addition_helper sgnb_data;
89   sgnb_addition_request sgnb_req;
90   sgnb_addition_response sgnb_resp;
91
92   unsigned long int net_requests = 0;
93   unsigned long int net_rejects = 0;
94   
95   std::string error_string;
96   bool report_mode_only;
97 };
98
99 #endif