EMANE  1.2.1
manifest.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013,2015 - Adjacent Link LLC, Bridgewater, New Jersey
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * * Neither the name of Adjacent Link LLC nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <libxml/parser.h>
34 
37 
38 namespace EMANE
39 {
40  namespace Application
41  {
42 std::string manifest(BuildId buildId, const std::string & sName)
43 {
44  xmlDocPtr pDoc{xmlNewDoc(BAD_CAST "1.0")};
45 
46  xmlNodePtr pManifest{xmlNewNode(NULL,BAD_CAST "manifest")};
47 
48  xmlNodePtr pPlugin{xmlNewChild(pManifest,NULL,BAD_CAST "plugin",NULL)};
49 
50  xmlNewProp(pPlugin, BAD_CAST "name", BAD_CAST sName.c_str());
51 
52  auto manifest =
54 
55  xmlNodePtr pConfiguration{xmlNewChild(pPlugin,NULL,BAD_CAST "configuration",NULL)};
56 
57  for(const auto & entry : manifest)
58  {
59  xmlNodePtr pParam{xmlNewChild(pConfiguration,NULL,BAD_CAST "parameter",NULL)};
60 
61  xmlNewProp(pParam, BAD_CAST "name", BAD_CAST entry.getName().c_str());
62  xmlNewProp(pParam, BAD_CAST "default", BAD_CAST (entry.hasDefault() ? "yes" : "no"));
63  xmlNewProp(pParam, BAD_CAST "required", BAD_CAST (entry.isRequired() ? "yes" : "no"));
64  xmlNewProp(pParam, BAD_CAST "modifiable", BAD_CAST (entry.isModifiable() ? "yes" : "no"));
65 
66 
67  std::string sType;
68  std::string sMinValue;
69  std::string sMaxValue;
70  bool bNumeric{true};
71 
72  std::vector<std::string> values;
73 
74  const auto & anyValues = entry.getValues();
75 
76  switch(entry.getType())
77  {
79  sType="int64";
80  sMinValue = std::to_string(entry.getMinValue().asINT64());
81  sMaxValue = std::to_string(entry.getMaxValue().asINT64());
82 
83  std::transform(anyValues.begin(),
84  anyValues.end(),
85  std::back_inserter(values),[](const Any & any)->std::string
86  {
87  return std::to_string(any.asINT64());
88  });
89 
90  break;
91 
93  sType="uint64";
94  sMinValue = std::to_string(entry.getMinValue().asUINT64());
95  sMaxValue = std::to_string(entry.getMaxValue().asUINT64());
96 
97  std::transform(anyValues.begin(),
98  anyValues.end(),
99  std::back_inserter(values),[](const Any & any)->std::string
100  {
101  return std::to_string(any.asUINT64());
102  });
103  break;
104 
106  sType="int32";
107  sMinValue = std::to_string(entry.getMinValue().asINT32());
108  sMaxValue = std::to_string(entry.getMaxValue().asINT32());
109 
110  std::transform(anyValues.begin(),
111  anyValues.end(),
112  std::back_inserter(values),[](const Any & any)->std::string
113  {
114  return std::to_string(any.asINT32());
115  });
116  break;
117 
119  sType="uint32";
120  sMinValue = std::to_string(entry.getMinValue().asUINT32());
121  sMaxValue = std::to_string(entry.getMaxValue().asUINT32());
122 
123  std::transform(anyValues.begin(),
124  anyValues.end(),
125  std::back_inserter(values),[](const Any & any)->std::string
126  {
127  return std::to_string(any.asUINT32());
128  });
129 
130  break;
131 
133  sType="int16";
134  sMinValue = std::to_string(entry.getMinValue().asINT16());
135  sMaxValue = std::to_string(entry.getMaxValue().asINT16());
136 
137  std::transform(anyValues.begin(),
138  anyValues.end(),
139  std::back_inserter(values),[](const Any & any)->std::string
140  {
141  return std::to_string(any.asINT16());
142  });
143 
144  break;
145 
147  sType="uint16";
148  sMinValue = std::to_string(entry.getMinValue().asUINT16());
149  sMaxValue = std::to_string(entry.getMaxValue().asUINT16());
150 
151  std::transform(anyValues.begin(),
152  anyValues.end(),
153  std::back_inserter(values),[](const Any & any)->std::string
154  {
155  return std::to_string(any.asUINT16());
156  });
157 
158  break;
159 
161  sType="int8";
162  sMinValue = std::to_string(entry.getMinValue().asINT8());
163  sMaxValue = std::to_string(entry.getMaxValue().asINT8());
164 
165  std::transform(anyValues.begin(),
166  anyValues.end(),
167  std::back_inserter(values),[](const Any & any)->std::string
168  {
169  return std::to_string(any.asINT8());
170  });
171  break;
172 
174  sType="uint8";
175  sMinValue = std::to_string(entry.getMinValue().asUINT8());
176  sMaxValue = std::to_string(entry.getMaxValue().asUINT8());
177 
178  std::transform(anyValues.begin(),
179  anyValues.end(),
180  std::back_inserter(values),[](const Any & any)->std::string
181  {
182  return std::to_string(any.asUINT8());
183  });
184  break;
185 
187  sType="float";
188  sMinValue = std::to_string(entry.getMinValue().asFloat());
189  sMaxValue = std::to_string(entry.getMaxValue().asFloat());
190 
191  std::transform(anyValues.begin(),
192  anyValues.end(),
193  std::back_inserter(values),[](const Any & any)->std::string
194  {
195  return std::to_string(any.asFloat());
196  });
197  break;
198 
200  sType="double";
201  sMinValue = std::to_string(entry.getMinValue().asDouble());
202  sMaxValue = std::to_string(entry.getMaxValue().asDouble());
203 
204  std::transform(anyValues.begin(),
205  anyValues.end(),
206  std::back_inserter(values),[](const Any & any)->std::string
207  {
208  return std::to_string(any.asDouble());
209  });
210  break;
211 
213  bNumeric = false;
214  sType="inetaddr";
215 
216  std::transform(anyValues.begin(),
217  anyValues.end(),
218  std::back_inserter(values),[](const Any & any)->std::string
219  {
220  return any.asINETAddr().str();
221  });
222 
223  break;
224 
226  sType="bool";
227  sMinValue = "false";
228  sMaxValue = "true";
229 
230  std::transform(anyValues.begin(),
231  anyValues.end(),
232  std::back_inserter(values),[](const Any & any)->std::string
233  {
234  return any.asBool() ? "true" : "false";
235  });
236 
237  break;
238 
240  bNumeric = false;
241  sType="string";
242 
243  std::transform(anyValues.begin(),
244  anyValues.end(),
245  std::back_inserter(values),[](const Any & any)->std::string
246  {
247  return any.asString();
248  });
249  break;
250  }
251 
252  xmlNodePtr pNumericOrNonNumeric{};
253 
254  if(bNumeric)
255  {
256  pNumericOrNonNumeric = xmlNewChild(pParam,NULL,BAD_CAST "numeric",NULL);
257  xmlNewProp(pNumericOrNonNumeric, BAD_CAST "minValue", BAD_CAST sMinValue.c_str());
258  xmlNewProp(pNumericOrNonNumeric,BAD_CAST "maxValue", BAD_CAST sMaxValue.c_str());
259  }
260  else
261  {
262  pNumericOrNonNumeric = xmlNewChild(pParam,NULL,BAD_CAST "nonnumeric",NULL);
263  }
264 
265  const auto & sPattern = entry.getRegexPattern();
266 
267  xmlNewProp(pNumericOrNonNumeric, BAD_CAST "type", BAD_CAST sType.c_str());
268 
269  xmlNodePtr pValues = xmlNewChild(pNumericOrNonNumeric, NULL, BAD_CAST "values", NULL);
270  xmlNewProp(pValues,BAD_CAST "minOccurs", BAD_CAST std::to_string(entry.getMinOccurs()).c_str());
271  xmlNewProp(pValues,BAD_CAST "maxOccurs", BAD_CAST std::to_string(entry.getMaxOccurs()).c_str());
272 
273 
274  std::for_each(values.begin(),
275  values.end(),
276  [pValues](const std::string & s)
277  {
278  xmlNewChild(pValues, NULL, BAD_CAST "value", BAD_CAST s.c_str());
279  });
280 
281 
282  if(!sPattern.empty())
283  {
284  xmlNodePtr pRegex = xmlNewChild(pNumericOrNonNumeric, NULL, BAD_CAST "regex", NULL);
285  xmlNodePtr pCDATA = xmlNewCDataBlock(pDoc, BAD_CAST sPattern.c_str(), sPattern.size());
286  xmlAddChild(pRegex,pCDATA);
287  }
288 
289  if(!entry.getUsage().empty())
290  {
291  xmlNewChild(pNumericOrNonNumeric, NULL, BAD_CAST "description", BAD_CAST entry.getUsage().c_str());
292  }
293  }
294 
295 
296  xmlNodePtr pStatistic{xmlNewChild(pPlugin,NULL,BAD_CAST "statistics",NULL)};
297 
298  for(const auto & entry : StatisticController::getStatisticManifest(buildId))
299  {
300  xmlNodePtr pStat{xmlNewChild(pStatistic,NULL,BAD_CAST "element",NULL)};
301 
302  xmlNewProp(pStat, BAD_CAST "name", BAD_CAST entry.getName().c_str());
303 
304  xmlNewProp(pStat, BAD_CAST "type", BAD_CAST anyTypeAsString(entry.getType()).c_str());
305 
306  xmlNewProp(pStat, BAD_CAST "clearable", BAD_CAST (entry.isClearable() ? "yes" : "no"));
307 
308  if(!entry.getDescription().empty())
309  {
310  xmlNewChild(pStat, NULL, BAD_CAST "description", BAD_CAST entry.getDescription().c_str());
311  }
312  }
313 
314  xmlNodePtr pStatisticTables{xmlNewChild(pPlugin,NULL,BAD_CAST "statistictables",NULL)};
315 
316  for(const auto & entry : StatisticController::getTableManifest(buildId))
317  {
318  xmlNodePtr pTable{xmlNewChild(pStatisticTables,NULL,BAD_CAST "table",NULL)};
319 
320  xmlNewProp(pTable, BAD_CAST "name", BAD_CAST entry.getName().c_str());
321 
322  xmlNewProp(pTable, BAD_CAST "clearable", BAD_CAST (entry.isClearable() ? "yes" : "no"));
323 
324  if(!entry.getDescription().empty())
325  {
326  xmlNewChild(pTable, NULL, BAD_CAST "description", BAD_CAST entry.getDescription().c_str());
327  }
328  }
329 
330  xmlDocSetRootElement(pDoc,pManifest);
331 
332  xmlChar * xmlbuff;
333 
334  int buffersize;
335 
336  xmlDocDumpFormatMemory(pDoc, &xmlbuff, &buffersize, 1);
337 
338  std::string sManifest(reinterpret_cast<char *>(xmlbuff),buffersize);
339 
340  xmlFree(xmlbuff);
341 
342  xmlFreeDoc(pDoc);
343 
344  return sManifest;
345 }
346  }
347 }
static StatisticTableManifest getTableManifest(BuildId buildId)
std::string manifest(BuildId buildId, const std::string &sName)
Definition: manifest.cc:42
std::string anyTypeAsString(const Any::Type &type)
Definition: any.cc:551
static ConfigurationManifest getConfigurationManifest(BuildId buildId)
static StatisticManifest getStatisticManifest(BuildId buildId)
The Any class can contain an instance of one of any type in its support type set. ...
Definition: any.h:49
std::uint32_t BuildId
Definition: types.h:60
Definition: agent.h:43