EMANE  1.2.1
nemconfiguration.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2016 - Adjacent Link LLC, Bridgewater, New Jersey
3  * Copyright (c) 2009-2010 - DRS CenGen, LLC, Columbia, Maryland
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of DRS CenGen, LLC nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "nemconfiguration.h"
35 #include "pluginconfiguration.h"
36 
37 #include "emane/exception.h"
38 
39 #include <libxml/tree.h>
40 
41 
43  std::string sURI) :
44  LayerConfiguration{"nem"},
45  u16Id_{0},
46  type_{STRUCTURED}
47 {
48  processDefinition("nem", sURI);
49 
50  u16Id_ = getAttrValNumeric(pNEMNode, "id");
51 
52  bExternalTransport_ = getAttrVal(pNEMNode, "transport") == "external" ? true : false;
53 
54  // overlay these param values on previously parsed
55  overlayParams(pNEMNode);
56 }
57 
58 
60 {
61  for(auto layer : layers_) { delete layer; }
62 
63  layers_.clear();
64 }
65 
66 
69 {
70  return u16Id_;
71 }
72 
73 
76 {
77  return layers_;
78 }
79 
80 
81 bool
83 {
84  return bExternalTransport_;
85 }
86 
87 
88 bool
90 {
91  return (type_ == UNSTRUCTURED) || (haveLayer("phy") &&
92  haveLayer("mac") &&
93  haveLayer("transport"));
94 }
95 
96 
97 void
99 {
100  if(getAttrVal(pRoot, "type") == "unstructured")
101  {
102  type_ = UNSTRUCTURED;
103  }
104 }
105 
106 
107 void
109 {
110  doProcessLayer(pNode);
111 }
112 
113 
114 void
115 EMANE::NEMConfiguration::doProcessLayer(xmlNodePtr pNode)
116 {
117  std::string sLayerName{reinterpret_cast<const char*>(pNode->name)};
118 
119  std::string sDefinitionFile{getAttrVal(pNode, "definition")};
120 
121  std::string sURI{};
122 
123  if(!sDefinitionFile.empty())
124  {
125  sURI = getDefinitionPath() + sDefinitionFile;
126  }
127 
128  if(sLayerName == "shim")
129  {
130  layers_.push_back(new PluginConfiguration{pNode, sURI, sLayerName});
131  }
132  else
133  {
134  LayerConfigurationsIter pIter = findFirstLayer(sLayerName);
135 
136  if (pIter == layers_.end())
137  {
138  pIter =
139  layers_.insert(pIter,
140  new PluginConfiguration{pNode, sURI, sLayerName});
141  }
142  else
143  {
144  // we previously parsed a similar layer, if the new definition
145  // matches the old, just update
146  if ((*pIter)->getDefinitionFile() == sDefinitionFile)
147  {
148  (*pIter)->overlayParams(pNode);
149  }
150  else
151  {
152  throw makeException<ValidateException>(
153  "Unexpected definition for '%s'.\n\n"
154  " * Found: '%s', expected: '%s'\n",
155  pNode->name,
156  sDefinitionFile.c_str(),
157  (*pIter)->getDefinitionFile().c_str());
158  }
159  }
160  }
161 }
162 
163 
165 EMANE::NEMConfiguration::findFirstLayer(const std::string & sType)
166 {
167  return find_if(layers_.begin(),
168  layers_.end(),
169  [sType](LayerConfiguration *l)
170  {
171  return l->getType() == sType;
172  });
173 }
174 
175 
176 bool
177 EMANE::NEMConfiguration::haveLayer(const std::string & type)
178 {
179  return findFirstLayer(type) != layers_.end();
180 }
std::uint16_t u16Id_
Provides default implementation to common layer functionalities.
NEMConfiguration(xmlNodePtr pNEMNode, const std::string sDefinitionURI)
void doProcessRootAttributes(xmlNodePtr pRoot)
std::string getDefinitionPath() const
std::list< LayerConfiguration * > LayerConfigurations
void doProcessChildNode(xmlNodePtr pNode)
std::uint16_t getAttrValNumeric(xmlNodePtr pNode, const char *attributename)
const LayerConfigurations & getLayers()
void processDefinition(const char *pxzName, const std::string &sURI)
std::uint16_t NEMId
Definition: types.h:52
void overlayParams(xmlNodePtr pNode)
LayerConfigurations::iterator LayerConfigurationsIter
Contains the configuration for a MAC layer.
std::string getAttrVal(xmlNodePtr pNode, const char *attributename)