Kea  2.1.7-git
isc::data::SimpleParser Class Reference

A simple parser. More...

#include <simple_parser.h>

+ Inheritance diagram for isc::data::SimpleParser:

Public Member Functions

uint16_t getUint16 (isc::data::ConstElementPtr scope, const std::string &name)
 Returns a value converted to uint16_t. More...
 
uint32_t getUint32 (isc::data::ConstElementPtr scope, const std::string &name)
 Returns a value converted to uint32_t. More...
 
uint8_t getUint8 (ConstElementPtr scope, const std::string &name)
 Get an uint8_t value. More...
 
const isc::util::Triplet< uint32_t > parseIntTriplet (const data::ConstElementPtr &scope, const std::string &name)
 Parses an integer triplet. More...
 

Static Public Member Functions

static void checkKeywords (const SimpleKeywords &keywords, isc::data::ConstElementPtr scope)
 Checks acceptable keywords with their expected type. More...
 
static void checkRequired (const SimpleRequiredKeywords &required, isc::data::ConstElementPtr scope)
 Checks that all required keywords are present. More...
 
static size_t deriveParams (isc::data::ConstElementPtr parent, isc::data::ElementPtr child, const ParamsList &params)
 Derives (inherits) parameters from parent scope to a child. More...
 
static isc::asiolink::IOAddress getAddress (const ConstElementPtr &scope, const std::string &name)
 Returns a IOAddress parameter from a scope. More...
 
static bool getBoolean (isc::data::ConstElementPtr scope, const std::string &name)
 Returns a boolean parameter from a scope. More...
 
static double getDouble (const ConstElementPtr &scope, const std::string &name)
 Returns a floating point parameter from a scope. More...
 
static int64_t getInteger (isc::data::ConstElementPtr scope, const std::string &name)
 Returns an integer parameter from a scope. More...
 
static int64_t getInteger (isc::data::ConstElementPtr scope, const std::string &name, int64_t min, int64_t max)
 Returns an integer parameter from a scope and checks its range. More...
 
static const data::Element::PositiongetPosition (const std::string &name, const data::ConstElementPtr parent)
 Utility method that returns position of an element. More...
 
static std::string getString (isc::data::ConstElementPtr scope, const std::string &name)
 Returns a string parameter from a scope. More...
 
static size_t setDefaults (isc::data::ElementPtr scope, const SimpleDefaults &default_values)
 Sets the default values. More...
 
static size_t setListDefaults (isc::data::ConstElementPtr list, const SimpleDefaults &default_values)
 Sets the default values for all entries in a list. More...
 

Protected Member Functions

template<typename target_type , target_type convert>
target_type getAndConvert (isc::data::ConstElementPtr scope, const std::string &name, const std::string &type_name)
 Returns a converted value from a scope. More...
 
template<typename int_type >
int_type getIntType (isc::data::ConstElementPtr scope, const std::string &name)
 Returns an integer value with range checking from a scope. More...
 

Detailed Description

A simple parser.

This class is intended to be a simpler replacement for DhcpConfigParser. This class has been initially created to facilitate DHCPv4 and DHCPv6 servers' configuration parsing. Thus examples provided herein are related to DHCP configuration. Nevertheless, this is a generic class to be used in other modules too.

The simplification comes from several factors:

  • no build/commit nonsense. There's a single step: CfgStorage parse(ConstElementPtr json) that converts JSON configuration into an object and returns it.
  • no state kept. This greatly simplifies the parsers (no contexts, no child parsers list, no separate storage for uint32, strings etc. In fact, this base class is purely static. However, some derived classes may store some state. Implementors are advised to store as little state as possible.
  • no optional parameters (all are mandatory). This simplifies the parser, but introduces a new step before parsing where we insert the default values into client configuration before parsing. This is actually a good thing, because we now have a clear picture of the default parameters as they're defined in a single place (the DhcpConfigParser had the defaults spread out in multiple files in multiple directories).

Definition at line 68 of file lib/cc/simple_parser.h.

Member Function Documentation

◆ checkKeywords()

void isc::data::SimpleParser::checkKeywords ( const SimpleKeywords keywords,
isc::data::ConstElementPtr  scope 
)
static

Checks acceptable keywords with their expected type.

This methods throws an exception when a not acceptable keyword is found or when an acceptable entry does not have the expected type.

Parameters
keywordsThe SimpleKeywords keywords and types map.
scopeSpecified parameters which are checked.
Exceptions
DhcpConfigErrorif a not acceptable keyword is found.
DhcpConfigErrorif an acceptable entry does not have the expected type.

Definition at line 36 of file lib/cc/simple_parser.cc.

References isc_throw.

Referenced by isc::dhcp::SharedNetwork4Parser::parse(), isc::dhcp::OptionDataParser::parse(), isc::dhcp::SharedNetwork6Parser::parse(), isc::dhcp::OptionDefParser::parse(), isc::dhcp::PoolParser::parse(), isc::dhcp::Subnet4ConfigParser::parse(), isc::dhcp::PdPoolParser::parse(), and isc::dhcp::Subnet6ConfigParser::parse().

◆ checkRequired()

void isc::data::SimpleParser::checkRequired ( const SimpleRequiredKeywords required,
isc::data::ConstElementPtr  scope 
)
static

Checks that all required keywords are present.

This method throws an exception when a required entry is not present in the given scope.

Parameters
requiredRequired keywords.
scopeSpecified parameters which are checked.
Exceptions
DhcpConfigErrorif a required parameter is not present.

Definition at line 25 of file lib/cc/simple_parser.cc.

References isc_throw.

◆ deriveParams()

size_t isc::data::SimpleParser::deriveParams ( isc::data::ConstElementPtr  parent,
isc::data::ElementPtr  child,
const ParamsList params 
)
static

Derives (inherits) parameters from parent scope to a child.

This method derives parameters from the parent scope to the child, if there are no values specified in the child scope. For example, this method can be used to derive timers from global scope (e.g. for the whole DHCPv6 server) to a subnet scope. This method checks if the child scope doesn't have more specific values defined. If it doesn't, then the value from parent scope is copied over.

Parameters
parentscope to copy from (e.g. global)
childscope to copy from (e.g. subnet)
paramsnames of the parameters to copy
Returns
number of parameters copied

Definition at line 255 of file lib/cc/simple_parser.cc.

Referenced by isc::dhcp::SimpleParser4::deriveParameters(), isc::dhcp::SimpleParser6::deriveParameters(), and isc::netconf::NetconfSimpleParser::deriveParameters().

◆ getAddress()

IOAddress isc::data::SimpleParser::getAddress ( const ConstElementPtr scope,
const std::string &  name 
)
static

Returns a IOAddress parameter from a scope.

Unconditionally returns a parameter.

Parameters
scopespecified parameter will be extracted from this scope
namename of the parameter
Returns
an IOAddress representing the value of the parameter
Exceptions
DhcpConfigErrorif the parameter is not there or is not of appropriate type (or its conversion to IOAddress fails due to not being a proper address).

Definition at line 126 of file lib/cc/simple_parser.cc.

References isc_throw, and isc::Exception::what().

Referenced by isc::d2::D2SimpleParser::parse(), and isc::dhcp::D2ClientConfigParser::parse().

+ Here is the call graph for this function:

◆ getAndConvert()

template<typename target_type , target_type convert>
target_type isc::data::SimpleParser::getAndConvert ( isc::data::ConstElementPtr  scope,
const std::string &  name,
const std::string &  type_name 
)
inlineprotected

Returns a converted value from a scope.

This template should be instantiated in parsers when useful

Template Parameters
target_typethe type of the result
convertthe conversion function std::string -> target_type
Parameters
scopespecified parameter will be extracted from this scope
namename of the parameter for error report
type_namename of target_type for error report
Returns
a converted value of target_type
Exceptions
DhcpConfigErrorif the parameter is not there, is not of appropriate type or can not be converted

Definition at line 269 of file lib/cc/simple_parser.h.

References isc_throw.

Referenced by isc::dhcp::Subnets6ListConfigParser::createSubnetConfigParser().

◆ getBoolean()

bool isc::data::SimpleParser::getBoolean ( isc::data::ConstElementPtr  scope,
const std::string &  name 
)
static

Returns a boolean parameter from a scope.

Unconditionally returns a parameter.

Parameters
scopespecified parameter will be extracted from this scope
namename of the parameter
Returns
a boolean value of the parameter
Exceptions
DhcpConfigErrorif the parameter is not there or is not of appropriate type

Definition at line 109 of file lib/cc/simple_parser.cc.

References isc_throw.

Referenced by isc::dhcp::IfaceMgr::configureDHCPPacketQueue(), isc::dhcp::CfgMultiThreading::extract(), isc::dhcp::OptionDataParser::extractCSVFormat(), isc::dhcp::OptionDataParser::extractPersistent(), isc::dhcp::Subnet4ConfigParser::initSubnet(), isc::dhcp::Subnet6ConfigParser::initSubnet(), isc::dhcp::SharedNetwork4Parser::parse(), isc::dhcp::IfacesConfigParser::parse(), isc::agent::AgentSimpleParser::parse(), isc::dhcp::SharedNetwork6Parser::parse(), isc::dhcp::OptionDefParser::parse(), and isc::dhcp::D2ClientConfigParser::parse().

◆ getDouble()

double isc::data::SimpleParser::getDouble ( const ConstElementPtr scope,
const std::string &  name 
)
static

Returns a floating point parameter from a scope.

Unconditionally returns a parameter.

Parameters
scopespecified parameter will be extracted from this scope
namename of the parameter
Returns
a double value of the parameter
Exceptions
DhcpConfigErrorif the parameter is not there or is not an Element::real

Definition at line 139 of file lib/cc/simple_parser.cc.

References isc_throw.

◆ getInteger() [1/2]

int64_t isc::data::SimpleParser::getInteger ( isc::data::ConstElementPtr  scope,
const std::string &  name 
)
static

Returns an integer parameter from a scope.

Unconditionally returns a parameter.

Parameters
scopespecified parameter will be extracted from this scope
namename of the parameter
Returns
an integer value of the parameter
Exceptions
DhcpConfigErrorif the parameter is not there or is not of appropriate type

Definition at line 78 of file lib/cc/simple_parser.cc.

References isc_throw.

Referenced by isc::dhcp::CfgMultiThreading::extract(), isc::dhcp::OptionDataParser::extractCode(), isc::dhcp::Subnet4ConfigParser::initSubnet(), isc::dhcp::Subnet6ConfigParser::initSubnet(), isc::dhcp::PacketQueueMgr4::PacketQueueMgr4(), isc::dhcp::PacketQueueMgr6::PacketQueueMgr6(), and isc::dhcp::OptionDefParser::parse().

◆ getInteger() [2/2]

int64_t isc::data::SimpleParser::getInteger ( isc::data::ConstElementPtr  scope,
const std::string &  name,
int64_t  min,
int64_t  max 
)
static

Returns an integer parameter from a scope and checks its range.

Unconditionally returns a parameter. Checks that the value specified is in min =< X =< max range.

Parameters
scopespecified parameter will be extracted from this scope
namename of the parameter
minminimum allowed value
maxmaximum allowed value
Returns
an integer value of the parameter
Exceptions
DhcpConfigErrorif the parameter is not there or is not of appropriate type.
OutOfRangeif the parameter is out of range

Definition at line 95 of file lib/cc/simple_parser.cc.

References isc_throw.

◆ getIntType()

template<typename int_type >
int_type isc::data::SimpleParser::getIntType ( isc::data::ConstElementPtr  scope,
const std::string &  name 
)
inlineprotected

Returns an integer value with range checking from a scope.

This template should be instantiated in parsers when useful

Template Parameters
int_typethe integer type e.g. uint32_t
Parameters
scopespecified parameter will be extracted from this scope
namename of the parameter for error report
Returns
a value of int_type
Exceptions
DhcpConfigErrorif the parameter is not there, is not of appropriate type or is out of type value range

Definition at line 242 of file lib/cc/simple_parser.h.

References isc_throw.

◆ getPosition()

const data::Element::Position & isc::data::SimpleParser::getPosition ( const std::string &  name,
const data::ConstElementPtr  parent 
)
static

Utility method that returns position of an element.

It's mostly useful for logging. If the element is missing the parent position is returned or ZERO_POSITION if parent is null.

Parameters
nameposition of that element will be returned
parentparent element (optional)
Returns
position of the element specified.

Definition at line 159 of file lib/cc/simple_parser.cc.

Referenced by isc::dhcp::RelayInfoParser::addAddress(), isc::dhcp::OptionDataParser::createOption(), isc::dhcp::OptionDataParser::extractCode(), isc::dhcp::OptionDataParser::extractName(), isc::dhcp::OptionDataParser::extractSpace(), isc::dhcp::OptionDefParser::parse(), isc::dhcp::RelayInfoParser::parse(), and isc::dhcp::D2ClientConfigParser::parse().

◆ getString()

std::string isc::data::SimpleParser::getString ( isc::data::ConstElementPtr  scope,
const std::string &  name 
)
static

◆ getUint16()

uint16_t isc::data::SimpleParser::getUint16 ( isc::data::ConstElementPtr  scope,
const std::string &  name 
)
inline

Returns a value converted to uint16_t.

Instantiation of getIntType() to uint16_t

Parameters
scopespecified parameter will be extracted from this scope
namename of the parameter
Returns
an uint16_t value
Exceptions
isc::dhcp::DhcpConfigErrorwhen it is not an uint16_t

Definition at line 305 of file lib/cc/simple_parser.h.

◆ getUint32()

uint32_t isc::data::SimpleParser::getUint32 ( isc::data::ConstElementPtr  scope,
const std::string &  name 
)
inline

Returns a value converted to uint32_t.

Instantiation of getIntType() to uint32_t

Parameters
scopespecified parameter will be extracted from this scope
namename of the parameter
Returns
an uint32_t value
Exceptions
isc::dhcp::DhcpConfigErrorwhen it is not an uint32_t

Definition at line 292 of file lib/cc/simple_parser.h.

Referenced by isc::lease_cmds::LeaseCmdsImpl::lease4WipeHandler(), isc::lease_cmds::LeaseCmdsImpl::lease6WipeHandler(), isc::d2::D2SimpleParser::parse(), and isc::dhcp::D2ClientConfigParser::parse().

◆ getUint8()

uint8_t isc::data::SimpleParser::getUint8 ( ConstElementPtr  scope,
const std::string &  name 
)
inline

Get an uint8_t value.

Instantiation of getIntType() to uint8_t

Parameters
scopespecified parameter will be extracted from this scope
namename of the parameter
Returns
uint8_t value
Exceptions
isc::dhcp::DhcpConfigErrorwhen it is not an uint8_t

Definition at line 318 of file lib/cc/simple_parser.h.

Referenced by isc::dhcp::PdPoolParser::parse().

◆ parseIntTriplet()

const util::Triplet< uint32_t > isc::data::SimpleParser::parseIntTriplet ( const data::ConstElementPtr scope,
const std::string &  name 
)

Parses an integer triplet.

Parses an integer triplet parameter of the form:

min-<name>, <name>, max-<name>

Parameters
scopeData element holding e.g. shared network configuration to be parsed.
nameBase name of the parameter.
Returns
A triplet with the parsed value.

Definition at line 287 of file lib/cc/simple_parser.cc.

References isc_throw.

Referenced by isc::dhcp::Subnet6ConfigParser::initSubnet(), and isc::dhcp::SharedNetwork6Parser::parse().

◆ setDefaults()

size_t isc::data::SimpleParser::setDefaults ( isc::data::ElementPtr  scope,
const SimpleDefaults default_values 
)
static

Sets the default values.

This method sets the default values for parameters that are not defined. The list of default values is specified by default_values. If not present, those will be inserted into the scope. If a parameter is already present, the default value will not be inserted.

Parameters
scopedefault values will be inserted here
default_valueslist of default values
Returns
number of parameters inserted

Definition at line 171 of file lib/cc/simple_parser.cc.

References isc_throw, isc::data::SimpleDefault::name_, isc::data::SimpleDefault::type_, and isc::data::SimpleDefault::value_.

Referenced by isc::dhcp::SimpleParser4::setAllDefaults(), isc::dhcp::SimpleParser6::setAllDefaults(), and isc::agent::AgentSimpleParser::setAllDefaults().

◆ setListDefaults()

size_t isc::data::SimpleParser::setListDefaults ( isc::data::ConstElementPtr  list,
const SimpleDefaults default_values 
)
static

Sets the default values for all entries in a list.

This is a simple utility method that iterates over all parameters in a list and calls setDefaults for each entry.

Parameters
listlist to be iterated over
default_valueslist of default values
Returns
number of parameters inserted

Definition at line 245 of file lib/cc/simple_parser.cc.

Referenced by isc::dhcp::ClientClassDefParser::parse().


The documentation for this class was generated from the following files: