Added quantiling UDAFs
[com/gs-lite.git] / src / ftacmp / iface_q.h
1 /* ------------------------------------------------\r
2 Copyright 2014 AT&T Intellectual Property\r
3    Licensed under the Apache License, Version 2.0 (the "License");\r
4    you may not use this file except in compliance with the License.\r
5    You may obtain a copy of the License at\r
6 \r
7      http://www.apache.org/licenses/LICENSE-2.0\r
8 \r
9    Unless required by applicable law or agreed to in writing, software\r
10    distributed under the License is distributed on an "AS IS" BASIS,\r
11    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12    See the License for the specific language governing permissions and\r
13    limitations under the License.\r
14  ------------------------------------------- */\r
15 #ifndef __IFACE_Q_DEFINED_\r
16 #define __IFACE_Q_DEFINED_\r
17 \r
18 #include <stdio.h>\r
19 //#include "xmlparse.h"\r
20 #include<vector>\r
21 #include<string>\r
22 #include<map>\r
23 #include<set>\r
24 #include<algorithm>\r
25 \r
26 #include"parse_fta.h"\r
27 \r
28 \r
29 //                      Represent an interface.\r
30 class iface_t{\r
31 public:\r
32         int lineno;\r
33         std::map<std::string, std::vector<std::string> > vals;\r
34 \r
35         iface_t(int l){\r
36                 lineno = l;\r
37         }\r
38 \r
39         void add_property(const char *name, const char *att);\r
40         void add_property(const char *name, const char **atts);\r
41         void add_property(const char *name, std::vector<std::string> &val_vec);\r
42 \r
43         std::vector<std::string> get_property(std::string p){\r
44                 std::vector<std::string> ret;\r
45 \r
46         /* convert property name to lcase */\r
47         std::transform(p.begin(), p.end(), p.begin(), (int(*)(int))std::tolower);\r
48 \r
49 /*\r
50 printf("get_property %s :\n",p.c_str());\r
51 std::map<std::string, std::vector<std::string> >::iterator msvsi;\r
52 for(msvsi=vals.begin(); msvsi!=vals.end();++msvsi){\r
53   printf("\t%s : ",(*msvsi).first.c_str());\r
54   std::vector<std::string>  vs = (*msvsi).second;\r
55   int i;\r
56   for(i=0;i<vs.size();++i){\r
57     printf(" %s",vs[i].c_str());\r
58   }\r
59   printf("\n");\r
60 }\r
61 */\r
62                 if(vals.count(p)) return vals[p];\r
63                 else return ret;\r
64         }\r
65 \r
66         std::vector<std::string> get_properties(){\r
67                 std::vector<std::string> ret;\r
68 \r
69                 std::map<std::string, std::vector<std::string> >::iterator iter;\r
70                 for (iter = vals.begin(); iter != vals.end(); iter++) {\r
71                         ret.push_back((*iter).first);\r
72                 }\r
73                 return ret;\r
74         }\r
75 \r
76         int finalize(std::string &errs);\r
77         std::string to_string();\r
78         std::string get_name();\r
79         std::string get_host();\r
80         bool eval_Contains(std::string prop, std::string val);\r
81         bool eval_Equals(std::string prop, std::string val);\r
82         bool eval_Exists(std::string prop);\r
83 };\r
84 \r
85 //                      Represent an interface.\r
86 class gs_host_t{\r
87 public:\r
88         int lineno;\r
89         std::map<std::string, std::vector<std::string> > vals;\r
90 \r
91         gs_host_t(int l){\r
92                 lineno = l;\r
93         }\r
94 \r
95         void add_property(const char *name, const char *att);\r
96         void add_property(const char *name, std::vector<std::string> &val_vec);\r
97         std::vector<std::string> get_property(std::string p){\r
98                 std::vector<std::string> ret;\r
99                 if(vals.count(p)) return vals[p];\r
100                 else return ret;\r
101         }\r
102         int finalize(std::string &errs);\r
103 \r
104         void add_interface(iface_t* it) {ifaces.push_back(it);}\r
105 \r
106         void propagate_name() {\r
107                 for(int i=0;i<ifaces.size();i++)\r
108                         ifaces[i]->add_property("host", vals["name"]);\r
109         }\r
110 \r
111         std::string to_string();\r
112         std::string get_name();\r
113 \r
114         std::vector<iface_t *> ifaces;\r
115 };\r
116 \r
117 \r
118 \r
119 //              A collection of resources (interfaces)\r
120 \r
121 class resparse_data{\r
122 public:\r
123         std::vector<std::string> level;\r
124         bool in_Resources, in_Iface, in_Host;\r
125         iface_t *curr_iface;\r
126         gs_host_t *curr_host;\r
127         bool failure;\r
128     std::vector<iface_t *> ifaces;\r
129         std::vector<gs_host_t *> hosts;\r
130         bool use_live_hosts_file;\r
131         std::set<std::string> *live_hosts;\r
132         bool distributed_mode;\r
133 \r
134 //      XML_Parser parser;\r
135 \r
136 //      resparse_data(XML_Parser p){\r
137 //              parser = p;\r
138         resparse_data(bool use_live_hosts_file, std::set<std::string> *live_hosts, bool distributed_mode){\r
139                 in_Resources = false;\r
140                 in_Iface = false;\r
141                 curr_iface = NULL;\r
142                 failure = false;\r
143                 this->use_live_hosts_file = use_live_hosts_file;\r
144                 this->live_hosts = live_hosts;\r
145                 this->distributed_mode = distributed_mode;\r
146         };\r
147 \r
148         void new_iface(int l){\r
149                 curr_iface = new iface_t(l);\r
150         };\r
151         void new_host(int l){\r
152                 curr_host = new gs_host_t(l);\r
153         };\r
154         void add_property(const char *name, const char *att){\r
155                 curr_iface->add_property(name, att);\r
156         };\r
157         void add_property(const char *name, const char **atts){\r
158                 curr_iface->add_property(name, atts);\r
159         };\r
160         void add_property(const char *name, std::vector<std::string> &val_vec){\r
161                 curr_iface->add_property(name, val_vec);\r
162         };\r
163 \r
164 \r
165         std::vector<std::string> get_property(int i, std::string property){\r
166                 return ifaces[i]->get_property(property);\r
167         };\r
168         std::vector<std::string> get_properties(int i){\r
169                 return ifaces[i]->get_properties();\r
170         };\r
171 \r
172         int finalize_iface(std::string &errs);\r
173         int finalize_host(std::string &errs);\r
174         bool failed(){return failure;};\r
175         std::string to_string();\r
176         std::vector<std::pair<std::string,std::string> > find_ifaces(predicate_t *pr);\r
177         std::vector<int> get_ifaces_by_Name(std::string host_name, std::string if_name);\r
178 private:\r
179         bool eval_pred(int i, predicate_t *pr);\r
180 };\r
181 \r
182 \r
183 class ifq_t{\r
184 public:\r
185         resparse_data *rpd;\r
186         std::map<std::string, predicate_t *> ifq_map;\r
187 \r
188         ifq_t(){ rpd = NULL;};\r
189 \r
190         int load_ifaces(std::string fname, bool use_live_hosts_file, bool distributed_mode, std::string &err);\r
191         int load_ifqs(std::string fname, std::string &err);\r
192 \r
193         std::vector<std::pair<std::string,std::string> > eval(std::string qname, int &errnbr);\r
194         std::vector<std::string> get_iface_vals(std::string host_name, std::string if_name, std::string property, int &errnbr, std::string &err_str);\r
195         std::vector<std::string> get_iface_properties(std::string host_name, std::string basic_if_name, int &err_no, std::string &err_str);\r
196 };\r
197 \r
198 \r
199 /////////////////////////\r
200 //      for resource parser\r
201 \r
202 void startElement(void *userData, const char *name, std::vector<std::string> &attr_vec, std::vector<std::string> &val_vec);\r
203 void endElement(void *userData, const char *name) ;\r
204 \r
205 \r
206 \r
207 \r
208 #endif\r