Add new udafs and RMR support to gsprintconsole_ves
[com/gs-lite.git] / src / ftacmp / iface_q.h
1 /* ------------------------------------------------
2 Copyright 2014 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 #ifndef __IFACE_Q_DEFINED_
16 #define __IFACE_Q_DEFINED_
17
18 #include <stdio.h>
19 //#include "xmlparse.h"
20 #include<vector>
21 #include<string>
22 #include<map>
23 #include<set>
24 #include<algorithm>
25
26 #include"parse_fta.h"
27
28
29 //                      Represent an interface.
30 //      TODO Add in some more consistency checks, beyond having an interface name
31 //      TODO More intelligence about interfaces, e.g. the libraries to
32 //              link to
33 class iface_t{
34 public:
35         int lineno;
36         std::map<std::string, std::vector<std::string> > vals;
37
38         iface_t(int l){
39                 lineno = l;
40         }
41
42         void add_property(const char *name, const char *att);
43         void add_property(const char *name, const char **atts);
44         void add_property(const char *name, std::vector<std::string> &val_vec);
45
46         std::vector<std::string> get_property(std::string p){
47                 std::vector<std::string> ret;
48
49         /* convert property name to lcase */
50         std::transform(p.begin(), p.end(), p.begin(), (int(*)(int))std::tolower);
51
52 /*
53 printf("get_property %s :\n",p.c_str());
54 std::map<std::string, std::vector<std::string> >::iterator msvsi;
55 for(msvsi=vals.begin(); msvsi!=vals.end();++msvsi){
56   printf("\t%s : ",(*msvsi).first.c_str());
57   std::vector<std::string>  vs = (*msvsi).second;
58   int i;
59   for(i=0;i<vs.size();++i){
60     printf(" %s",vs[i].c_str());
61   }
62   printf("\n");
63 }
64 */
65                 if(vals.count(p)) return vals[p];
66                 else return ret;
67         }
68
69         std::vector<std::string> get_properties(){
70                 std::vector<std::string> ret;
71
72                 std::map<std::string, std::vector<std::string> >::iterator iter;
73                 for (iter = vals.begin(); iter != vals.end(); iter++) {
74                         ret.push_back((*iter).first);
75                 }
76                 return ret;
77         }
78
79 // Add in more consistency checks
80         int finalize(std::string &errs);
81
82         std::string to_string();
83         std::string get_name();
84         std::string get_host();
85         bool has_multiple_schemas();            // depends on InterfaceType
86         
87
88         bool eval_Contains(std::string prop, std::string val);
89         bool eval_Equals(std::string prop, std::string val);
90         bool eval_Exists(std::string prop);
91 };
92
93 //                      Represent an interface.
94 class gs_host_t{
95 public:
96         int lineno;
97         std::map<std::string, std::vector<std::string> > vals;
98
99         gs_host_t(int l){
100                 lineno = l;
101         }
102
103         void add_property(const char *name, const char *att);
104         void add_property(const char *name, std::vector<std::string> &val_vec);
105         std::vector<std::string> get_property(std::string p){
106                 std::vector<std::string> ret;
107                 if(vals.count(p)) return vals[p];
108                 else return ret;
109         }
110         int finalize(std::string &errs);
111
112         void add_interface(iface_t* it) {ifaces.push_back(it);}
113
114         void propagate_name() {
115                 for(int i=0;i<ifaces.size();i++)
116                         ifaces[i]->add_property("host", vals["name"]);
117         }
118
119         std::string to_string();
120         std::string get_name();
121
122         std::vector<iface_t *> ifaces;
123 };
124
125
126
127 //              A collection of resources (interfaces)
128
129 class resparse_data{
130 public:
131         std::vector<std::string> level;
132         bool in_Resources, in_Iface, in_Host;
133         iface_t *curr_iface;
134         gs_host_t *curr_host;
135         bool failure;
136     std::vector<iface_t *> ifaces;
137         std::vector<gs_host_t *> hosts;
138         bool use_live_hosts_file;
139         std::set<std::string> *live_hosts;
140         bool distributed_mode;
141
142 //      XML_Parser parser;
143
144 //      resparse_data(XML_Parser p){
145 //              parser = p;
146         resparse_data(bool use_live_hosts_file, std::set<std::string> *live_hosts, bool distributed_mode){
147                 in_Resources = false;
148                 in_Iface = false;
149                 curr_iface = NULL;
150                 failure = false;
151                 this->use_live_hosts_file = use_live_hosts_file;
152                 this->live_hosts = live_hosts;
153                 this->distributed_mode = distributed_mode;
154         };
155
156         void new_iface(int l){
157                 curr_iface = new iface_t(l);
158         };
159         iface_t *get_interface(int l){
160                 return ifaces[l];
161         }
162
163
164         void new_host(int l){
165                 curr_host = new gs_host_t(l);
166         };
167         void add_property(const char *name, const char *att){
168                 curr_iface->add_property(name, att);
169         };
170         void add_property(const char *name, const char **atts){
171                 curr_iface->add_property(name, atts);
172         };
173         void add_property(const char *name, std::vector<std::string> &val_vec){
174                 curr_iface->add_property(name, val_vec);
175         };
176
177
178         std::vector<std::string> get_property(int i, std::string property){
179                 return ifaces[i]->get_property(property);
180         };
181         std::vector<std::string> get_properties(int i){
182                 return ifaces[i]->get_properties();
183         };
184
185         int finalize_iface(std::string &errs);
186         int finalize_host(std::string &errs);
187         bool failed(){return failure;};
188         std::string to_string();
189         std::vector<std::pair<std::string,std::string> > find_ifaces(predicate_t *pr);
190         std::vector<int> get_ifaces_by_Name(std::string host_name, std::string if_name);
191 private:
192         bool eval_pred(int i, predicate_t *pr);
193 };
194
195
196 class ifq_t{
197 public:
198         resparse_data *rpd;
199         std::map<std::string, predicate_t *> ifq_map;
200
201         ifq_t(){ rpd = NULL;};
202
203         int load_ifaces(std::string fname, bool use_live_hosts_file, bool distributed_mode, std::string &err);
204         int load_ifqs(std::string fname, std::string &err);
205
206         std::vector<std::pair<std::string,std::string> > eval(std::string qname, int &errnbr);
207         std::vector<std::string> get_iface_vals(std::string host_name, std::string if_name, std::string property, int &errnbr, std::string &err_str);
208         std::vector<std::string> get_iface_properties(std::string host_name, std::string basic_if_name, int &err_no, std::string &err_str);
209         iface_t *get_interface(std::string host_name, std::string basic_if_name, int &err_no, std::string &err_str);
210 };
211
212
213 /////////////////////////
214 //      for resource parser
215
216 void startElement(void *userData, const char *name, std::vector<std::string> &attr_vec, std::vector<std::string> &val_vec);
217 void endElement(void *userData, const char *name) ;
218
219
220
221
222 #endif