User story RICPLT-2620
[ric-app/admin.git] / src / protector-plugin / admission_policy.hpp
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 /* The control  wrapper class around the plugin
22    The wrapper class is responsible for control operations of the plugin :
23    set policy
24    get policy
25    report metrics
26
27    the plugin handles X2AP messages 
28 */
29 #pragma once
30 #ifndef ADMISSION_CTRL
31 #define ADMISSION_CTRL
32
33 #include <rapidjson/document.h>
34 #include <rapidjson/writer.h>
35 #include <rapidjson/stringbuffer.h>
36 #include <rapidjson/schema.h>
37
38 #include <iostream>
39 #include <vector>
40 #include <map>
41 #include <plugin-interface.hpp>
42 #include <mdclog/mdclog.h>
43 #include <NetworkProtector.h>
44 #include <chrono>
45 #include <sstream>
46
47 #define MAX_INSTANCES 10
48
49 class admission: virtual public Policy
50 {
51 public:
52   admission(std::string, std::string, std::string, unsigned int, std::string, bool report_only=true);
53   ~admission(void);
54   protector * get_protector_instance(unsigned int);
55   bool setPolicy(const char *, int , std::string & );
56   bool getPolicy(const char * , int, std::string & );
57   std::string getName(void);
58   int getMetrics(std::vector<std::string> & ) ;
59   std::string get_error(void) const {return error_string;};
60   unsigned int get_num_policies(void) {return policy_table.size();};
61 private:
62   void storePolicy(void);
63   void init_log(void);
64   void setPolicyVars(void);
65   void instantiate_protector_plugin(bool);
66   bool load_file(std::string, std::string &) ;
67   bool load_schema(const std::string & , const std::string & , std::unique_ptr<rapidjson::SchemaDocument> & , std::unique_ptr<rapidjson::SchemaValidator> &);
68   void process_counters(int , std::string & );
69
70   std::vector<protector> _plugin_instances;
71   std::map<std::string, int> policy_table;
72   std::map<std::string, rapidjson::Pointer> window_policy_vars;
73   std::map<std::string, rapidjson::Pointer> generic_policy_vars;
74   std::map<std::string, rapidjson::Pointer> metric_vars;
75
76  
77   std::unique_ptr<rapidjson::SchemaDocument> downstream_schema_ref_, notify_schema_ref_, metrics_schema_ref_;
78   std::unique_ptr<rapidjson::SchemaValidator> downstream_validator_, notify_validator_, metrics_validator_;
79   rapidjson::Document notify_message_, metrics_message_;
80
81   std::string _xapp_id;
82   
83   // stores past count of requests, reject and time stamp
84   std::map<int, std::vector<long int>> counters;
85   std::string error_string;
86   unsigned long int prev_time_stamp;
87 };
88
89 #endif