INET Framework for OMNeT++/OMNEST
vlanTableXMLparser.cc
Go to the documentation of this file.
00001 /*
00002  * vlanTableXMLparser.cpp
00003  *
00004  *  Created on: 16.12.2010
00005  *      Author: aranel
00006  */
00007 
00008 #include "vlanTableXMLparser.h"
00009 
00010 VLANTableXMLparser::VLANTableXMLparser(VLANTable * _table) {
00011         table = _table;
00012         return;
00013 }
00014 
00015 VLANTableXMLparser::~VLANTableXMLparser() {
00016 
00017 }
00018 
00019 
00020 bool VLANTableXMLparser::parse(const char * filename, const char * switchID) {
00021 
00022          cXMLElement* switchConfig = ev.getXMLDocument(filename);
00023             if (switchConfig == NULL) {
00024                 return false;
00025             }
00026 
00027             std::string switchXPath("Switch[@id='");
00028             switchXPath += switchID;
00029             switchXPath += "']";
00030 
00031             /* FIND root of current switch's configuration subtree */
00032             cXMLElement* switchNode = switchConfig->getElementByPath(switchXPath.c_str());
00033             if (switchNode == NULL) {
00034                 EV << "Warning: Switch " << switchID << ": Has no config, running in default." << std::endl;
00035                 return false;
00036                 //opp_error("No configuration for Switch ID: %s", switchID);
00037                 }
00038 
00039             /* CHECK FOR CLEAR CONFIG TAG */
00040             cXMLElement* clear = switchNode->getFirstChildWithTag("ClearConfig");
00041                 if (clear == NULL) {
00042                         table->initDefault();
00043                 }
00044 
00045             /* FIND section Interfaces */
00046             cXMLElement* IntNode = switchNode->getFirstChildWithTag("Interfaces");
00047             if (IntNode != NULL) {
00048                 parseInterfaces(IntNode);
00049             }
00050 
00051             /* FIND section VLANs */
00052             cXMLElement* staticNode = switchNode->getFirstChildWithTag("VLANs");
00053             if (staticNode != NULL) {
00054                 parseVLANs(staticNode);
00055             }
00056 
00057             return true;
00058 
00059 }
00060 
00061 
00062 void VLANTableXMLparser::parseInterfaces(cXMLElement * node) {
00063         std::string tmp, port, name, vlan, subNodeName;
00064 
00065         cXMLElementList intConfig = node->getChildren();
00066         cXMLElementList::iterator it;
00067         for(it = intConfig.begin(); it != intConfig.end(); it++) {
00068                 subNodeName  = (*it)->getTagName();
00069                 if (subNodeName == "Interface" && (*it)->getAttribute("id")) {
00070                         port = (*it)->getAttribute("id");
00071 
00072                         cXMLElementList ifDetails = (*it)->getChildren();
00073                         cXMLElementList::iterator ifElemIt;
00074                         for (ifElemIt = ifDetails.begin(); ifElemIt != ifDetails.end(); ifElemIt++)
00075                         {
00076                           std::string nodeName = (*ifElemIt)->getTagName();
00077 
00078                           if (nodeName=="VLAN")
00079                           {
00080                                 vlan = (*ifElemIt)->getNodeValue();
00081                           }
00082 
00083                           if (nodeName=="Name")
00084                           {
00085                                 name = (*ifElemIt)->getNodeValue();
00086                           }
00087 
00088                         }
00089                 }
00090 
00091                 if (port.empty() == false && vlan.empty() == false) {
00092                         table->addPortVID(atoi(port.c_str()), atoi(vlan.c_str()));
00093                 }
00094 
00095 
00096         }
00097 
00098 
00099 }
00100 
00101 
00102 void VLANTableXMLparser::parseVLANs(cXMLElement * node) {
00103         std::string tmp, vid, name, subNodeName;
00104         std::vector<int> tagged;
00105         std::vector<int> untagged;
00106         std::vector<int> nountagged;
00107 
00108                 cXMLElementList intConfig = node->getChildren();
00109                 cXMLElementList::iterator it;
00110                 for(it = intConfig.begin(); it != intConfig.end(); it++) {
00111                         subNodeName  = (*it)->getTagName();
00112 
00113                         tagged.clear();
00114                         untagged.clear();
00115                         nountagged.clear();
00116 
00117 
00118                         if (subNodeName == "VLAN" && (*it)->getAttribute("id")) {
00119                                 vid = (*it)->getAttribute("id");
00120 
00121                                 cXMLElementList ifDetails = (*it)->getChildren();
00122                                 cXMLElementList::iterator ifElemIt;
00123                                 for (ifElemIt = ifDetails.begin(); ifElemIt != ifDetails.end(); ifElemIt++)
00124                                 {
00125                                   std::string nodeName = (*ifElemIt)->getTagName();
00126 
00127                                   if (nodeName == "Tagged")
00128                                   {
00129                                         //tagged.push_back(atoi((*ifElemIt)->getNodeValue()));
00130                                           parseList(tagged, (*ifElemIt)->getNodeValue());
00131                                   }
00132 
00133                                   if (nodeName == "Untagged")
00134                                   {
00135                                           parseList(untagged, (*ifElemIt)->getNodeValue());
00136                                   }
00137 
00138                                   if (nodeName == "Nountagged")
00139                                   {
00140                                           parseList(nountagged, (*ifElemIt)->getNodeValue());
00141                                   }
00142 
00143                                   if(nodeName == "Name") {
00144                                           name = (*ifElemIt)->getNodeValue();
00145                                   }
00146                                 }
00147                         }
00148 
00149                         if (vid.empty() == false) {
00150                                 int VID = atoi(vid.c_str());
00151 
00152                                 table->extendTable(VID);
00153 
00154                                 /* removing by "no untagged" command */
00155                                 for (unsigned int i = 0; i < nountagged.size(); i++) {
00156                                         table->delPort(nountagged.at(i), VID);
00157                                 }
00158 
00159                                 /* adding by "tagged" command */
00160                                 table->addTagged(VID, tagged);
00161 
00162                                 /* adding by "untagged" command */
00163                                 table->addUntagged(VID, untagged);
00164 
00165                                 table->setVLANName(VID, name);
00166 
00167                                 table->regVLAN(VID);
00168                         }
00169 
00170 
00171                 }
00172 
00173 }
00174 
00175 void VLANTableXMLparser::parseList(std::vector<int>& out, const char * in) {
00176         std::string tmp, xtmp;
00177         tmp.assign(in);
00178         size_t begin, end;
00179 
00180         begin = 0;
00181         end = tmp.find_first_of(",");
00182 
00183 
00184         if (end == std::string::npos) {
00185                 out.push_back(atoi(tmp.c_str()));
00186                 return;
00187         }
00188 
00189         EV << "Parsing: '" << tmp << "'\n";
00190 
00191         while (true) {
00192 
00193 
00194                 if (end == std::string::npos) {
00195                         xtmp = tmp.substr(begin, end);
00196                 } else {
00197                         xtmp = tmp.substr(begin, end-begin);
00198                 }
00199 
00200                 EV << begin << ":" << end << " " << xtmp  << "\n";
00201 
00202                 out.push_back(atoi(xtmp.c_str()));
00203 
00204                 begin = end;
00205                 if (begin == std::string::npos) {
00206                         break;
00207                 } else {
00208                         begin++;
00209                 }
00210                 end = tmp.find_first_of(',', begin);
00211         }
00212         return;
00213 }
00214