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