2016-08-18 14:30:31 +03:00
/* -------------------------------------------------------------------------- */
2020-04-30 16:00:02 +03:00
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
2016-08-18 14:30:31 +03:00
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
# ifndef IPAM_REQUEST_H_
# define IPAM_REQUEST_H_
# include "SyncRequest.h"
2016-08-18 22:42:24 +03:00
# include "NebulaUtil.h"
2019-09-16 16:51:38 +03:00
# include "Attribute.h"
# include "Template.h"
2016-08-18 14:30:31 +03:00
2019-09-17 18:19:04 +03:00
class AddressRange ;
2016-08-18 14:30:31 +03:00
/**
2016-08-18 22:42:24 +03:00
* The IPAMRequest class represents a request for the IPAM driver . The request
* is in the form
2016-08-18 14:30:31 +03:00
* request to the AuthManager . The result of the request will be stored
* in the result and message attributes of this class .
*/
class IPAMRequest : public SyncRequest
{
public :
2016-08-18 22:42:24 +03:00
/* ---------------------------------------------------------------------- */
/* IPAM Request constructors */
/* ---------------------------------------------------------------------- */
2019-09-16 16:51:38 +03:00
IPAMRequest ( VectorAttribute * _ar_vattr ) : IPAMRequest ( _ar_vattr ,
" <ADDRESS><MAC/><IP/><IP6_GLOBAL/><IP6_ULA/><IP6/><SIZE/></ADDRESS> " ) { } ;
2016-08-18 14:30:31 +03:00
2019-09-16 16:51:38 +03:00
IPAMRequest ( VectorAttribute * _ar_vattr , const std : : string & _address_xml ) ;
2016-08-18 14:30:31 +03:00
2019-09-17 18:19:04 +03:00
IPAMRequest ( AddressRange * _ar ) : IPAMRequest ( _ar ,
" <ADDRESS><MAC/><IP/><IP6_GLOBAL/><IP6_ULA/><IP6/><SIZE/></ADDRESS> " ) { } ;
IPAMRequest ( AddressRange * _ar , const std : : string & _address_xml ) ;
2016-08-18 22:42:24 +03:00
virtual ~ IPAMRequest ( ) { } ;
/* ---------------------------------------------------------------------- */
/* Driver message formatting and processing */
/* ---------------------------------------------------------------------- */
2016-08-19 14:26:08 +03:00
/**
* Builds an base64 encoded XML string with the request for the driver :
* < IPAM_DRIVER_ACTION_DATA >
* < AR >
* < TYPE >
* < IP >
* < MAC >
* < SIZE >
* . . .
* < / AR >
* < ADDRESS >
* < MAC >
* < IP >
2017-03-21 14:22:19 +03:00
* < IP6 >
2016-08-19 14:26:08 +03:00
* < IP6_ULA >
* < IP6_GLOBAL >
* < SIZE >
* < / ADDRESS >
* < / IPAM_DRIVER_ACTION_DATA >
*
* < AR > Element with the network description for this request :
* - Maybe incomplete for REQUEST_ADDRESS_RANGE
* < ADDRESS > Lease request
* - Will not be present for REQUEST_ADDRESS_RANGE
*/
2016-08-18 22:42:24 +03:00
std : : string & to_xml64 ( std : : string & action_data ) const
{
std : : ostringstream oss ;
std : : string * aux_str ;
2016-08-18 14:30:31 +03:00
2016-08-18 22:42:24 +03:00
oss < < " <IPAM_DRIVER_ACTION_DATA> "
< < ar_xml
< < address_xml
< < " </IPAM_DRIVER_ACTION_DATA> " ;
2016-08-18 14:30:31 +03:00
2016-08-18 22:42:24 +03:00
aux_str = one_util : : base64_encode ( oss . str ( ) ) ;
action_data = * aux_str ;
2019-09-09 15:43:51 +03:00
delete aux_str ;
2016-08-18 22:42:24 +03:00
return action_data ;
}
2016-08-19 14:26:08 +03:00
/**
* Response from drivers is a base64 template with the AR definition
* ( only for REQUEST_ADDRESS_RANGE ) and ADDRESS for GET / ALLOCATE / FREE
* requests .
*
* AR = [
* TYPE = . . . ,
* IP = . . . ,
* . . .
* ]
*
* ADDRESS = [
* MAC =
* IP =
* . . .
* ]
*
* NOTE : XML syntax should be also valid
*
* The following functions are helpers to get the response IP and AR
* @ param error description if any
* @ return 0 on success
*/
int get_ar ( VectorAttribute * vattr , std : : string & error ) const
{
Template response ;
if ( parse_response ( response , error ) ! = 0 )
{
return - 1 ;
}
VectorAttribute * new_ar = response . get ( " AR " ) ;
if ( new_ar = = 0 )
{
error = " AR not found in IPAM driver response " ;
}
vattr - > replace ( new_ar - > value ( ) ) ;
return 0 ;
}
int get_ip ( std : : string & ip , std : : string & error ) const
{
Template response ;
if ( parse_response ( response , error ) ! = 0 )
{
return - 1 ;
}
VectorAttribute * addr = response . get ( " ADDRESS " ) ;
if ( addr = = 0 )
{
error = " ADDRESS not found in IPAM driver response " ;
}
ip = addr - > vector_value ( " IP " ) ;
return 0 ;
}
2016-08-18 22:42:24 +03:00
private :
2016-08-18 14:30:31 +03:00
/**
2016-08-18 22:42:24 +03:00
* XML representation for this request < AR > . . . < / AR >
2016-08-18 14:30:31 +03:00
*/
2016-08-18 22:42:24 +03:00
string ar_xml ;
2016-08-18 14:30:31 +03:00
2016-08-18 22:42:24 +03:00
/**
* Address request representation
*/
string address_xml ;
2016-08-19 14:26:08 +03:00
/**
* Parse a response from an IPAM driver OpenNebula template or XML
* base64 encoded message .
* @ param response Template with the parsed response
* @ param error description if any
* @ return 0 on success
*
*/
int parse_response ( Template & response , std : : string & error ) const
{
std : : string * msg = one_util : : base64_decode ( message ) ;
if ( msg = = 0 )
{
error = " Error decoding base64 IPAM driver response " ;
return - 1 ;
}
int rc = response . parse_str_or_xml ( * msg , error ) ;
free ( msg ) ;
return rc ;
}
2016-08-18 14:30:31 +03:00
} ;
# endif