INET Framework for OMNeT++/OMNEST
xmlParser.cc
Go to the documentation of this file.
00001 //
00002 // Marek Cerny, 2MSK
00003 // FIT VUT 2011
00004 //
00005 // This program is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with this program.  If not, see http://www.gnu.org/licenses/.
00017 // 
00018 
00019 #include "xmlParser.h"
00020 #include <errno.h>
00021 
00022 cXMLElement * xmlParser::GetDevice(const char *deviceType, const char *deviceId, const char *configFile){
00023 
00024    // get access to the XML file (if exists)
00025    cXMLElement *config = ev.getXMLDocument(configFile);
00026    if (config == NULL)
00027       return NULL;
00028 
00029 
00030    string type = deviceType;
00031    string id = deviceId;
00032    if (type.empty() || id.empty())
00033       return NULL;
00034 
00035    // create string that describes device node, such as <Router id="10.0.0.1">
00036    string deviceNodePath = type;
00037    deviceNodePath += "[@id='";
00038    deviceNodePath += id;
00039    deviceNodePath += "']";
00040 
00041    // get access to the device node
00042    cXMLElement *device = config->getElementByPath(deviceNodePath.c_str());
00043 
00044    return device;
00045 }
00046 
00047 
00048 cXMLElement * xmlParser::GetInterface(cXMLElement *iface, cXMLElement *device){
00049 
00050    // initial call of the method - find <Interfaces> and get first "Interface" node
00051    if (device != NULL){
00052 
00053       cXMLElement *ifaces = device->getFirstChildWithTag("Interfaces");
00054       if (ifaces == NULL)
00055          return NULL;
00056 
00057       iface = ifaces->getFirstChildWithTag("Interface");
00058 
00059    // repeated call - get another "Interface" sibling node
00060    }else if (iface != NULL){
00061       iface = iface->getNextSiblingWithTag("Interface");
00062    }else{
00063       iface = NULL;
00064    }
00065 
00066    return iface;
00067 }
00068 
00069 cXMLElement * xmlParser::GetIPv6Address(cXMLElement *addr, cXMLElement *iface){
00070 
00071    // initial call of the method - get first "IPv6Address" child node
00072    if (iface != NULL){
00073       addr = iface->getFirstChildWithTag("IPv6Address");
00074 
00075    // repeated call - get another "IPv6Address" sibling node
00076    }else if (addr != NULL){
00077       addr = addr->getNextSiblingWithTag("IPv6Address");
00078    }else{
00079       addr = NULL;
00080    }
00081 
00082    return addr;
00083 }
00084 
00085 cXMLElement * xmlParser::GetAdvPrefix(cXMLElement *prefix, cXMLElement *iface){
00086 
00087    // initial call of the method - get first "NdpAdvPrefix" child node
00088    if (iface != NULL){
00089       prefix = iface->getFirstChildWithTag("NdpAdvPrefix");
00090 
00091    // repeated call - get another "NdpAdvPrefix" sibling node
00092    }else if (prefix != NULL){
00093       prefix = prefix->getNextSiblingWithTag("NdpAdvPrefix");
00094    }else{
00095       prefix = NULL;
00096    }
00097 
00098    return prefix;
00099 }
00100 
00101 cXMLElement * xmlParser::GetStaticRoute6(cXMLElement *route, cXMLElement *device){
00102 
00103    // initial call of the method - find <Routing6> -> <Static>
00104    // and then get first "Route" child node
00105    if (device != NULL){
00106 
00107       cXMLElement *routing = device->getFirstChildWithTag("Routing6");
00108       if (routing == NULL)
00109          return NULL;
00110 
00111       cXMLElement *staticRouting = routing->getFirstChildWithTag("Static");
00112       if (staticRouting == NULL)
00113          return NULL;
00114 
00115       route = staticRouting->getFirstChildWithTag("Route");
00116 
00117    // repeated call - get another "Route" sibling node
00118    }else if (route != NULL){
00119       route = route->getNextSiblingWithTag("Route");
00120    }else{
00121       route = NULL;
00122    }
00123 
00124    return route;
00125 }
00126 
00127 cXMLElement * xmlParser::GetOspfProcess6(cXMLElement *process, cXMLElement *device){
00128 
00129    // initial call of the method - get <Routing6> -> <Ospf>
00130    // and then first "Process" child node
00131    if (device != NULL){
00132 
00133       cXMLElement *routing = device->getFirstChildWithTag("Routing6");
00134       if (routing == NULL)
00135          return NULL;
00136 
00137       cXMLElement *ospf = routing->getFirstChildWithTag("Ospf");
00138       if (ospf == NULL)
00139          return NULL;
00140 
00141       process = ospf->getFirstChildWithTag("Process");
00142 
00143    // repeated call - get another "Process" sibling node
00144    }else if (process != NULL){
00145       process = process->getNextSiblingWithTag("Process");
00146    }else{
00147       process = NULL;
00148    }
00149 
00150    return process;
00151 }
00152 
00153 /*
00154  * A utility method for proper str -> int conversion with error checking.
00155  */
00156 bool xmlParser::Str2Int(int *retValue, const char *str){
00157 
00158    if (retValue == NULL || str == NULL){
00159       return false;
00160    }
00161 
00162    char *tail = NULL;
00163    long value = 0;
00164    errno = 0;
00165 
00166    value = strtol(str, &tail, 0);
00167 
00168    if (*tail != '\0' || errno == ERANGE || errno == EINVAL || value < INT_MIN || value > INT_MAX){
00169       return false;
00170    }
00171 
00172    *retValue = (int) value;
00173    return true;
00174 }
00175 
00176 bool xmlParser::Str2Bool(bool *ret, const char *str){
00177 
00178    if (  (strcmp(str, "yes") == 0)
00179       || (strcmp(str, "enabled") == 0)
00180       || (strcmp(str, "on") == 0)
00181       || (strcmp(str, "true") == 0)){
00182 
00183       *ret = true;
00184       return true;
00185    }
00186 
00187    if (  (strcmp(str, "no") == 0)
00188       || (strcmp(str, "disabled") == 0)
00189       || (strcmp(str, "off") == 0)
00190       || (strcmp(str, "false") == 0)){
00191 
00192       *ret = false;
00193       return true;
00194    }
00195 
00196    int value;
00197    if (Str2Int(&value, str)){
00198       if (value == 1){
00199          *ret = true;
00200          return true;
00201       }
00202 
00203       if (value == 0){
00204          *ret = false;
00205          return true;
00206       }
00207    }
00208 
00209    return false;
00210 }
00211 
00212 
00213 /* Check if IS-IS is enabled in XML config.
00214  * Return NULL if not presented, otherwise return main IS-IS element
00215  */
00216 cXMLElement * xmlParser::GetIsisRouting(cXMLElement * device)
00217 {
00218     if(device == NULL)
00219         return NULL;
00220 
00221     cXMLElement *routing = device->getFirstChildWithTag("Routing");
00222     if(routing == NULL)
00223         return  NULL;
00224 
00225     cXMLElement * isis = routing->getFirstChildWithTag("ISIS");
00226     return isis;
00227 }