2017-04-11 02:32:39 +03:00
/* -------------------------------------------------------------------------- */
2024-07-29 15:25:20 +03:00
/* Copyright 2002-2024, OpenNebula Project, OpenNebula Systems */
2017-04-11 02:32:39 +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 ZONE_SERVER_H_
# define ZONE_SERVER_H_
# include "ExtendedAttribute.h"
/**
* The VirtualMachine DISK attribute
*/
class ZoneServer : public ExtendedAttribute
{
public :
2017-04-19 21:44:31 +03:00
2024-06-03 12:40:24 +03:00
ZoneServer ( VectorAttribute * va , int id ) : ExtendedAttribute ( va , id ) { } ;
2017-04-11 02:32:39 +03:00
2024-06-03 12:40:24 +03:00
virtual ~ ZoneServer ( ) { } ;
2017-04-11 02:32:39 +03:00
2017-04-18 17:32:23 +03:00
/**
* Initialized server metadata :
* - NAME
* - ENDPOINT
* @ param error string if any
* @ return - 1 if server data could not be initialized , 0 on success
*/
2020-07-02 23:42:10 +03:00
int init ( std : : string & error )
2017-04-11 02:32:39 +03:00
{
2017-04-11 22:34:38 +03:00
if ( vector_value ( " NAME " ) . empty ( ) )
{
error = " Missing NAME in SERVER " ;
return - 1 ;
}
if ( vector_value ( " ENDPOINT " ) . empty ( ) )
{
error = " Missing ENDPOINT in SERVER " ;
return - 1 ;
}
2017-04-11 02:32:39 +03:00
return 0 ;
}
2017-04-23 14:09:12 +03:00
//--------------------------------------------------------------------------
// Server attributes
//--------------------------------------------------------------------------
2017-04-19 21:44:31 +03:00
/**
* @ return the ID of the server
*/
2017-04-11 02:32:39 +03:00
int get_id ( ) const
{
return ExtendedAttribute : : get_id ( ) ;
}
} ;
/**
* Set of Zone servers
*/
class ZoneServers : public ExtendedAttributeSet
{
public :
/* ---------------------------------------------------------------------- */
/* Constructor and Initialization functions */
/* ---------------------------------------------------------------------- */
/**
* Creates the ZoneServers set from a zone template with SERVER = [ . . . ]
* attributes
* @ param tmpl template with SERVER
*/
2023-02-02 14:48:43 +03:00
ZoneServers ( Template * tmpl )
: ExtendedAttributeSet ( false )
, next_id ( - 1 )
2017-04-11 02:32:39 +03:00
{
std : : vector < VectorAttribute * > vas ;
tmpl - > get ( SERVER_NAME , vas ) ;
init_attribute_map ( SERVER_ID_NAME , vas ) ;
for ( zone_iterator it = begin ( ) ; it ! = end ( ) ; + + it )
{
2020-07-02 23:42:10 +03:00
std : : string error ;
2017-04-11 02:32:39 +03:00
int i = ( * it ) - > get_id ( ) ;
if ( i > next_id )
{
next_id = i ;
}
( * it ) - > init ( error ) ;
}
next_id + = 1 ;
} ;
/**
* Creates an empty zone server set
*/
2023-02-02 14:48:43 +03:00
ZoneServers ( )
: ExtendedAttributeSet ( false )
, next_id ( - 1 )
{ }
2017-04-11 02:32:39 +03:00
2023-02-02 14:48:43 +03:00
virtual ~ ZoneServers ( ) = default ;
2017-04-11 02:32:39 +03:00
/* ---------------------------------------------------------------------- */
/* Iterators */
/* ---------------------------------------------------------------------- */
class ZoneIterator : public AttributeIterator
{
public :
2024-06-03 12:40:24 +03:00
ZoneIterator ( ) : AttributeIterator ( ) { } ;
ZoneIterator ( const AttributeIterator & dit ) : AttributeIterator ( dit ) { } ;
virtual ~ ZoneIterator ( ) { } ;
2017-04-11 02:32:39 +03:00
ZoneServer * operator * ( ) const
{
return static_cast < ZoneServer * > ( map_it - > second ) ;
}
} ;
ZoneIterator begin ( )
{
ZoneIterator it ( ExtendedAttributeSet : : begin ( ) ) ;
return it ;
}
ZoneIterator end ( )
{
ZoneIterator it ( ExtendedAttributeSet : : end ( ) ) ;
return it ;
}
typedef class ZoneIterator zone_iterator ;
/* ---------------------------------------------------------------------- */
/* ZoneServer interface */
/* ---------------------------------------------------------------------- */
/**
* Returns the SERVER attribute for a zone server
* @ param disk_id of the DISK
* @ return pointer to the attribute ir null if not found
*/
ZoneServer * get_server ( int id ) const
{
return static_cast < ZoneServer * > ( get_attribute ( id ) ) ;
}
2020-07-02 23:42:10 +03:00
int add_server ( VectorAttribute * va , int & sid , std : : string & error )
2017-04-11 02:32:39 +03:00
{
ZoneServer * server = new ZoneServer ( va , next_id ) ;
if ( server - > init ( error ) ! = 0 )
{
delete server ;
return - 1 ;
}
2017-04-11 22:34:38 +03:00
va - > replace ( SERVER_ID_NAME , next_id ) ;
2017-04-11 02:32:39 +03:00
add_attribute ( server , next_id ) ;
2017-05-02 02:38:30 +03:00
sid = next_id ;
2017-04-11 02:32:39 +03:00
next_id + = 1 ;
return 0 ;
} ;
2017-04-13 19:31:14 +03:00
ZoneServer * delete_server ( int id )
{
return static_cast < ZoneServer * > ( delete_attribute ( id ) ) ;
} ;
2017-04-21 17:52:54 +03:00
/**
* @ return servers in zone
*/
unsigned int size ( )
{
return ExtendedAttributeSet : : size ( ) ;
}
2017-04-13 19:31:14 +03:00
2017-04-11 02:32:39 +03:00
protected :
2023-02-07 10:50:30 +03:00
ExtendedAttribute * attribute_factory ( VectorAttribute * va , int id ) const override
2017-04-11 02:32:39 +03:00
{
return new ZoneServer ( va , id ) ;
} ;
private :
2017-04-11 22:34:38 +03:00
friend class Zone ;
2017-04-11 02:32:39 +03:00
static const char * SERVER_NAME ; //"SERVER"
static const char * SERVER_ID_NAME ; //"ID"
int next_id ;
} ;
# endif /*ZONE_SERVER_H_*/