Fix: update the correct pre-built script to cmake-sonar.sh
[ric-app/mc.git] / schemaparser / schemaparser.h
1 /* ------------------------------------------------
2 Copyright 2020 AT&T Intellectual Property
3    Licensed under the Apache License, Version 2.0 (the "License");
4    you may not use this file except in compliance with the License.
5    You may obtain a copy of the License at
6
7      http://www.apache.org/licenses/LICENSE-2.0
8
9    Unless required by applicable law or agreed to in writing, software
10    distributed under the License is distributed on an "AS IS" BASIS,
11    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12    See the License for the specific language governing permissions and
13    limitations under the License.
14  ------------------------------------------- */
15
16
17
18 #ifndef __SCHEMAPARSER_INCLUDED__
19 #define __SCHEMAPARSER_INCLUDED__
20
21 #include<string>
22 #include<map>
23 #include<algorithm>
24 #include<vector>
25
26
27 //              If an mc_string is returned, it points to text in the input buffer.
28 struct mc_string{
29         unsigned int length;
30         char *data;
31 };
32
33 struct mc_ipv6_str{
34         unsigned int v[4];
35 };
36
37
38
39 ///////////////////////////////////////////////////
40 //              Type functions
41
42 #define UNDEFINED_TYPE 0
43 #define UINT_TYPE 1
44 #define INT_TYPE 2
45 #define ULLONG_TYPE 3
46 #define LLONG_TYPE 4
47 #define USHORT_TYPE 5
48 #define FLOAT_TYPE 6
49 #define BOOL_TYPE 7
50 #define VSTR_TYPE 8
51 #define TIMEVAL_TYPE 9
52 #define IP_TYPE 10
53 #define FSTRING_TYPE 11
54 #define IPV6_TYPE 12
55
56 namespace mc_schema{
57
58 //              Number of bytes used by a field
59 int type_size(int dt);
60 //              string to integer
61 int assign_type_from_string(std::string st);
62 //              integer to string
63 std::string type_to_string(int typ);
64 //              there is a casting function
65 bool is_castable(int src, int dst);
66 //              cast without loss of information
67 bool is_compatible(int src, int dst);
68
69         
70 ////////////////////////////////////////////
71 //              Represent the parsed schema of a query stream.
72 //              Created by mc_schema
73 //              will be returned on request from mc_schema
74
75 //      field_handle simplifies some of the field access functions
76 struct field_handle{
77         int type;
78         int offset;
79 };
80
81
82 class query_rep{
83 protected:
84         query_rep() {
85         };
86 public:
87         virtual int finalize() = 0;
88
89 //              Return a text representation
90         virtual std::string to_string() = 0;
91 //              Number of fields
92         virtual int get_num_fields() = 0;
93 //              name of ith field (starting at zero)
94         virtual std::string get_field_name(int i) = 0;
95 //              lookup field index by name, -1 if not found
96         virtual int get_index_of_field(std::string name) = 0;
97 //              lookup field handle by name, -1 if not found
98         virtual field_handle get_handle_of_field(std::string name) = 0;
99 //              data type of ith field.
100         virtual int get_type(int i) = 0;
101 //              string representation of data type of ith field
102         virtual std::string get_type_name(int i) = 0;
103 //              byte offset of ith field in a data block
104         virtual int get_offset(int i) = 0;
105 //              handle of ith field in a data block
106         virtual field_handle get_handle(int i) = 0;
107 };
108
109 //              This class parses a json representation of the streams and manages them
110 class mc_schemas{
111 protected:
112 //              n is a char buffer holding the json description of the stream schemas
113         mc_schemas(){ }
114
115 public:
116         virtual ~mc_schemas(){};
117         virtual void init(const char *n) = 0;
118 //              true if there are any errors.
119         virtual bool has_errors() = 0;
120
121 //              string with the error reports.  empty if there is no error.
122         virtual std::string get_errors() = 0;
123 //              return the names of the parsed streams.
124         virtual std::vector<std::string> get_streams() = 0;
125 //              return the names of the unsucessfully parsed streams
126         virtual std::vector<std::string> get_error_streams() = 0;
127 //              number of sucessfully and unsucessfully parsed streams
128         virtual bool stream_found(std::string s) = 0;
129 //              true if there is a stream with name s which parsed successfully
130         virtual bool stream_ok(std::string s) = 0;
131 //              return the error associated with a stream, if any.
132         virtual std::string get_stream_error(std::string s) = 0;
133 //              Get the query representation of a successfully parsed stream
134         virtual query_rep *get_query_rep(std::string s) = 0;
135
136 };      
137
138 //              Factory function.  They do not GC the input.
139 mc_schemas *new_mc_schemas(std::string s);
140 mc_schemas *new_mc_schemas(const char *s);
141                 
142 //              Universal return type
143 struct access_result {
144         int field_data_type; // as defined
145         union {
146                 int i;
147                 unsigned int ui;
148                 long long int l;
149                 unsigned long long int ul;
150                 double f;
151                 struct timeval t;               // defined in sys/time.h
152                 mc_string vs;
153                 mc_ipv6_str ip6;
154         } r;
155 };
156
157 ////////////////////////////////////////////
158 //                      Direct tuple access functions.
159 //                      No casting is done.  use query_rep to get the offset
160 unsigned int unpack_uint(void *data, int len, int offset, int *problem);
161 unsigned int unpack_ushort(void *data, int len, int offset, int *problem);
162 unsigned int unpack_bool(void *data, int len, int offset, int *problem);
163 int unpack_int(void *data, int len, int offset, int *problem);
164 unsigned long long int unpack_ullong(void *data, int len, int offset, int *problem);
165 long long int unpack_llong(void *data, int len, int offset, int *problem);
166 double unpack_float(void *data, int len, int offset, int *problem);
167 timeval unpack_timeval(void *data, int len, int offset, int *problem);
168 mc_string unpack_vstr(void *data,  int len, int offset, int *problem);
169 struct mc_ipv6_str unpack_ipv6(void *data,  int len, int offset, int *problem);
170
171 //              Result returned in universal return type.
172 //                      will lookup type and offset in query_rep
173 access_result get_field_by_index(query_rep *qr, int index, void *data, int len);
174 //                      user must lookup field_handle type in query_rep
175 access_result get_field_by_handle(field_handle f, void * data, int len);
176
177 //              These access functions perform casting,
178 //              use is_castable and is_compatible to determine if a field can
179 //              be cast to the return type.  Everything can cast to string.
180 //                      user must lookup offset and type in query_rep
181 int get_field_int(field_handle f, void * data, int len);
182 unsigned int get_field_uint(field_handle f, void * data, int len);
183 long long int get_field_llong(field_handle f, void * data, int len);
184 unsigned long long int get_field_ullong(field_handle f, void * data, int len);
185 double get_field_float(field_handle f, void * data, int len);
186 timeval get_field_timeval(field_handle f, void * data, int len);
187 mc_ipv6_str get_field_ipv6(field_handle f, void * data, int len);
188 std::string get_field_string(field_handle f, void * data, int len);
189                 
190 }       
191
192 #endif
193