Shell Script

#! /bin/bash
#########################################################################
# Here we do the print mibs and other stuff prior to the here document



CREATE_SEL_UID_PARAMS=`
    for sel_uid in ${SELECTION_UIDS}
    do
    echo "     UID ${sel_uid}_uid, "

    done
`

# print_mibs does most of the work.
    
DETAILS_TO_MEMBER_ASSIGNMENTS=` print_mibs  -t $tablename \
 -T "" \
 -f "  m_%14\\$s = details.%14\\$s;\$newline" \
 -s "  m_%14\\$s = details.%14\\$s.in();\$newline" \
 -o "  m_Info_len = details.Info.length();\$newline" \
 -o "  for (int tmp_%14\\$s_counter = 0; tmp_%14\\$s_counter < m_%14\\$s_len; tmp_%14\\$s_counter ++)\$newline" \
 -o "  {\$newline" \
 -o "      m_%14\\$s[tmp_%14\\$s_counter] = details.%14\\$s[tmp_%14\\$s_counter];\$newline" \
 -o "  }\$newline" \
 `
RW_DETAILS_TO_MEMBER_ASSIGNMENTS=` print_mibs  -R -t $tablename \
 -T "" \
 -f "    m_%14\\$s = details.%14\\$s;\$newline" \
 -s "    m_%14\\$s = details.%14\\$s.in();\$newline" \
 -o "  m_Info_len = details.Info.length();\$newline" \
 -o "  for (int tmp_%14\\$s_counter = 0; tmp_%14\\$s_counter < m_%14\\$s_len; tmp_%14\\$s_counter ++)\$newline" \
 -o "  {\$newline" \
 -o "      m_%14\\$s[tmp_%14\\$s_counter] = details.%14\\$s[tmp_%14\\$s_counter];\$newline" \
 -o "  }\$newline" \
    `
    
# this is the parent assignment
# only the first one is used
SELECTION_UID_TO_PARENT_ASSIGNMENTS=`
    sep=""
    for sel_uid in ${SELECTION_UIDS}
 do
     echo "     $sep m_parent = ${sel_uid}_uid; ";
 sep="//";
 done
    `



# ems create name
EMS_CREATE_PARAMS=`
 sep=""
    for sel_uid in ${SELECTION_UIDS}
 do
     echo "     $sep ${sel_uid}_uid ";
 sep=",";
 done
 `

# print_mibs does most of the work.
MEMBER_TO_DETAILS_ASSIGNMENTS=`
 print_mibs -t $tablename \
	-T "" \
	-f "  details->%14\\$s = m_%14\\$s;\$newline" \
	-s "  details->%14\\$s = m_%14\\$s.c_str();;\$newline" \
-o " details->%14\\$s.length(m_%14\\$s_len);\$newline" \
-o "  for (int tmp_%14\\$s_counter = 0; tmp_%14\\$s_counter < m_%14\\$s_len; tmp_%14\\$s_counter ++)\$newline" \
-o "  {\$newline" \
-o "      details->%14\\$s[tmp_%14\\$s_counter] = m_%14\\$s[tmp_%14\\$s_counter];\$newline" \
-o "  }\$newline" \
 `

INITIALISE_DATA_MEMBERS=`print_mibs  -t $tablename \
	-T "" \
	-f "  m_%14\\$s = 0;\$newline" \
	-s "  m_%14\\$s = \"\";\$newline" \
	-p "  memset(&m_%14\\$s,0,sizeof(m_%14\\$s));\$newline" \
	-o "  memset(m_%14\\$s,0,sizeof(m_%14\\$s));\$newline" \
        -o "  m_%14\\$s_len = 0;\$newline" \
	-e "  m_%14\\$s = " \
	-e "static_cast<ProtocolModules::\$protocol::%14\\$s\$enumsuffix>( 0 )" \
	-e ";\$newline" `

DB_TO_MEMBER_ATTS=`
 print_mibs  -t $tablename \
	-T "" \
	-f "        m_%14\\$s = outRec.\${database_struct_prefix}%14\\$s;\$newline" \
	-e "        m_%14\\$s =\$newline" \
	-e "          static_cast<ProtocolModules::\$protocol::%14\\$s\$enumsuffix>"\
	-e "(\$newline" \
	-e "            outRec.\${database_struct_prefix}%14\\$s);\$newline" \
	-o "        memcpy(m_%14\\$s, outRec.\${database_struct_prefix}%14\\$s, outRec.\${database_struct_prefix}%14\\$s_len * sizeof(m_%14\\$s[0]));\$newline" \
	-o "          m_%14\\$s_len = outRec.\${database_struct_prefix}%14\\$s_len;\$newline" \
	-s "        m_%14\\$s = (char *)  outRec.\${database_struct_prefix}%14\\$s;\$newline"\
	-p "        memcpy( &m_%14\\$s, outRec.\${database_struct_prefix}%14\\$s, 4 );\$newline" `


MEMBER_TO_DB=`
 print_mibs  -t $tablename \
	-T "" \
	-f "  inRec.\${database_struct_prefix}%14\\$s = m_%14\\$s;\$newline" \
	-s "  strcpy( (char *) inRec.\${database_struct_prefix}%14\\$s, m_%14\\$s.c_str() );\$newline" \
	-s "  inRec.\${database_struct_prefix}%14\\$s_len = m_%14\\$s.length();\$newline" \
	-p "  memcpy( inRec.\${database_struct_prefix}%14\\$s, &m_%14\\$s,4);\$newline" \
	-o "        memcpy(inRec.\${database_struct_prefix}%14\\$s, m_%14\\$s,  m_%14\\$s_len * sizeof(m_%14\\$s[0]));\$newline" \
        -o "       inRec.\${database_struct_prefix}%14\\$s_len = m_%14\\$s_len;\$newline" \
`

 NUMBER_OF_ATTRIBUTES=`
 print_mibs  -t $tablename \
	-T "" \
 | wc -l `
 
 ATTRIBUTE_POSITION='%15$d'
 ATTRIBUTE_SHORT_NAME='%14$s'
 export  ATTRIBUTE_POSITION ATTRIBUTE_SHORT_NAME

 NUMBER_OF_ATTRIBUTES=`
 print_mibs  -t $tablename \
	-T "" \
 | wc -l `

 
 
 GET_VALUES_TO_NAME_VALUE_LIST=`
 print_mibs  -t $tablename \
	-T "" \
	-i "     sprintf(tmp, \"%%ld\",  m_${ATTRIBUTE_SHORT_NAME});\$newline" \
    -i "     m_summary.settings[${ATTRIBUTE_POSITION}].name = CORBA::string_dup(\"${ATTRIBUTE_SHORT_NAME}\");\$newline" \
    -i "     m_summary.settings[${ATTRIBUTE_POSITION}].value =\$newline" \
    -i "       CORBA::string_dup(tmp);\$newline\$newline" \
 	-p "     sprintf(tmp, \"%%ld\",  m_${ATTRIBUTE_SHORT_NAME});\$newline" \
    -p "     m_summary.settings[${ATTRIBUTE_POSITION}].name = CORBA::string_dup(\"${ATTRIBUTE_SHORT_NAME}\");\$newline" \
    -p "     m_summary.settings[${ATTRIBUTE_POSITION}].value =\$newline" \
    -p "       CORBA::string_dup(tmp);\$newline\$newline" \
 	-s "     sprintf(tmp, \"%%s\",  m_${ATTRIBUTE_SHORT_NAME}.c_str());\$newline" \
    -s "     m_summary.settings[${ATTRIBUTE_POSITION}].name = CORBA::string_dup(\"${ATTRIBUTE_SHORT_NAME}\");\$newline" \
    -s "     m_summary.settings[${ATTRIBUTE_POSITION}].value =\$newline" \
    -s "       CORBA::string_dup(tmp);\$newline\$newline" \
    -o "     memset(tmp,0,sizeof(tmp));\$newline" \
    -o "     OIDAddrToDB(m_${ATTRIBUTE_SHORT_NAME}, m_${ATTRIBUTE_SHORT_NAME}_len, tmp); \$newline" \
    -o "     m_summary.settings[${ATTRIBUTE_POSITION}].name = CORBA::string_dup(\"${ATTRIBUTE_SHORT_NAME}\");\$newline" \
    -o "     m_summary.settings[${ATTRIBUTE_POSITION}].value =\$newline" \
    -o "       CORBA::string_dup(tmp);\$newline\$newline" \
 `

 
##########################################################################
cat <<EOF
#ifndef _${newname}_CC_
#define _${newname}_CC_

// File: nms_${protocol}ServiceClasses.cc
//
// generated from: \$Source: /projects7/nc867.15/007.Source/src/gen_nms/db_gen/RCS/oop_serviceclass_db_cc.sh,v $
//
// using base class ProtocolModule::${protocol}
//
// nms database access classes for - ${tablename}
//
#include <$newname.h>
#include <citr_EMS_SetGetDB.h>
#include <citr_EMS_Query.h>
#include <${PARENT_OBJ_TYPE}.h>
#include <dx_nms_mo_utils.h>

// *************************************************************************
// $tablename -> $newname

////////////////////////////////////////////////////////////////////////////
// constructor
$newname::$newname( UID this_entryID  )
{
    TRACEFUNC("%s:%d: $newname::$newname (%lld )\n",
              __FILE__,__LINE__,
              this_entryID);

    init(); // do member initialisation

    if( 0 != this_entryID )
    {
        // if we are given a uid, look for an existing db entry to initialise from
        find( this_entryID );
    }
} // $newname::$newname


////////////////////////////////////////////////////////////////////////////
// destructor
$newname::~$newname()
{
    // member destructors are sufficient
} // $newname::~$newname

////////////////////////////////////////////////////////////////////////////
// populate the object with the current db data
ManagedObjectStatus
$newname::find( UID this_entryID )
{
    TRACEFUNC("%s:%d: $newname::find (%lld )\n",
              __FILE__,__LINE__,
              this_entryID);
    ManagedObjectStatus moStatus = mo_ok;
    
    if( 0 != this_entryID )
    {

        if ((moStatus = ManagedObject::retrieve(this_entryID)) == mo_ok)
        {
            
            moStatus = retrieve(this_entryID);
        }
        
        if (moStatus != mo_ok)
        {
            TRACEFLOW("%s:%d: $newname::find did not retrieve rc %d \n",
                      __FILE__,__LINE__,
                      moStatus);

            init();
        }
        
    }
    return( moStatus);
} // $newname::find


////////////////////////////////////////////////////////////////////////////
// create a new $newname entry from a struct
ManagedObjectStatus
$newname::create(// each of the selection items
                 ${CREATE_SEL_UID_PARAMS}
                 const ${newname}Details & details )
{
    init(); // re-initialise members
    
    // lookup db to check if row exists
    ManagedObjectStatus moStatus = find( details.entryId );
    
    if( m_uid != 0 )
    {
        // if we found an entry dont continue
        TRACEFLOW("%s:%d: $newname::create found a duplicate %lld\n",
                  __FILE__,__LINE__,
                  details.entryId);
        
        sprintf( m_errorMessage, 
                 "%s:%d: $newname::create found a duplicate %lld\n",
                 __FILE__,__LINE__,
                 details.entryId);
        return( mo_duplicate );
    }
    // intialise members from $newname struct and
    // Set the uid of the parent item to the first selection item
    

    ${DETAILS_TO_MEMBER_ASSIGNMENTS}
    // only the first parent is used
    ${SELECTION_UID_TO_PARENT_ASSIGNMENTS}

    
    // need to get the parent object
    ${PARENT_OBJ_TYPE} parent_object;
    moStatus = parent_object.find(m_parent);
    if (mo_ok != moStatus)
    {
        TRACEFLOW("%s:%d: $newname::create - could not find parent ${PARENT_OBJ_TYPE} object %lld \n",
                  __FILE__,__LINE__,
                  m_parent);

        sprintf(m_errorMessage,
                "%s:%d: $newname::create - could not find parent ${PARENT_OBJ_TYPE} object %lld \n",
                __FILE__,__LINE__,
                m_parent);
        return(moStatus);
    }
     
    // now need to find the owner

    m_owner = parent_object.getOwner();

    // need to create the object in the EMS
    // We may also change some member attributes
    // in the call to AssignSnmpIndexAtts
    // which is called as part of this
    // ${ems_create_name}
    // Whilst it may be nice to have a separate method
    // call to explicitly change the member attribute,
    // It is expected that most of the time the attributes
    // will only be changed on creation
    // and putting the change in AssignSnmpIndexAtts
    // localises the code changes.

    moStatus = ${ems_create_name}(${EMS_CREATE_PARAMS});
     
    if (moStatus != mo_ok)
    {
        TRACEFLOW("%s:%d: ${newname}::create ${ems_create_name} failed \n",
                  __FILE__,__LINE__);
        sprintf(m_errorMessage,
                "%s:%d: ${newname}::create ${ems_create_name} failed \n",
                __FILE__,__LINE__);
        

        return moStatus;
    }

    // Update the verified fields
    
    m_LastUpdated    = MoTimeNow();
    m_LastVerified   = MoTimeNow();
    m_LastVerAttempt = MoTimeNow();
    
    // Create a row in the ManagedObject table.
    moStatus = ManagedObject::create();
     
    if (moStatus != mo_ok)
    {
        TRACEFLOW("%s:%d: ${newname}::create mo create failed \n",
                  __FILE__,__LINE__);
        sprintf(m_errorMessage,
                "%s:%d: ${newname}::create mo create failed \n",
                __FILE__,__LINE__);
        return moStatus;
    }
     


    // Insert a row into the database.
    moStatus = db_update();
     
    if (moStatus == mo_ok)
    {
        TRACEFLOW("%s:%d: ${newname}::create success \n",
                  __FILE__,__LINE__);
        return mo_ok;
    }
     
     
    // If we get to here, the MO insert succeeded, but the  insert 
    // failed, so we need to remove the MO row, and return an error.
    ManagedObject::remove(m_uid);
    TRACEFLOW("%s:%d: ${newname}::create insert failed \n",
              __FILE__,__LINE__);
    sprintf(m_errorMessage,
            "%s:%d: ${newname}::create insert failed \n",
            __FILE__,__LINE__);
    return mo_error;
} // $newname::create

////////////////////////////////////////////////////////////////////////////
// get the details of this object
ManagedObjectStatus
$newname::read( ${newname}Details *details )
{
    ManagedObjectStatus moStatus = mo_ok;
    
    if ( (NULL == details ))
    {
        TRACEFUNC("%s:%d: $newname::read (Null Param)\n",
                  __FILE__,__LINE__);
        return(mo_error);
    }
    
    TRACEFUNC("%s:%d: $newname::read (%p )\n",
              __FILE__,__LINE__,
              details);
    
    if( 0 == m_uid )
    {
        TRACEFLOW("%s:%d: $newname::read row not found \n",
                  __FILE__,__LINE__);

        sprintf( m_errorMessage,
                 "%s:%d: $newname::read row not found \n",
                 __FILE__,__LINE__); 
        return( mo_notfound );
    }
    
    details->entryId = m_uid;
    details->pad = const_cast<const char *>("");
    
    
   
    
    // should go to the ems to get updated values
    moStatus=${ems_read_name}();
    if (mo_ok != moStatus)
    {
        TRACEFLOW("%s:%d:  $newname::read could not read values from ems agent \n",
                  __FILE__,__LINE__);
        sprintf(m_errorMessage,
                "%s:%d:  $newname::read could not read values from ems agent \n",
                __FILE__,__LINE__);
        return(moStatus);
    }
    // should update the values in the DB
    db_update();
    
    // now assign the latest values
    
    ${MEMBER_TO_DETAILS_ASSIGNMENTS}
    
    details->LastUpdated    = m_LastUpdated;
    details->LastVerified   = m_LastVerified;
    details->LastVerAttempt = m_LastVerAttempt;
    
    return(mo_ok);
 
} // $newname::read

///////////////////////////////////////////////////////////////////
// member initialisation
void
$newname::init()
{
    ManagedObject::init();

    // Set the type and table name for use with the ManagedObject base class.
    strcpy (m_tableName, "dx_nms_${tablename}");
    m_type = NetworkManagement::${NETWORK_MANAGEMENT_TYPE};
    strcpy (m_name, "${tablename}");

    // initialise data members
    ${INITIALISE_DATA_MEMBERS}
    
    m_LastUpdated    = 0;
    m_LastVerified   = 0;
    m_LastVerAttempt = 0;

} // $newname::init

///////////////////////////////////////////////////////////////////
// get the members from the database
ManagedObjectStatus
$newname::retrieve( UID this_entryID )
{
    ${database_struct_name}_t inRec;
    ${database_struct_name}_t outRec;
    
    // initialise db structures
    memset( &inRec, 0, sizeof(inRec) );
    memset( &outRec, 0, sizeof(outRec) );
    inRec.${database_struct_prefix}UID = this_entryID;
    
    // do actual db lookup
    nmsLock dblock( DB_LOCK, TRUE );
    int dbResult = ${database_struct_name}GetFromCDB(NMS_DB_CONTEXT, &inRec, &outRec );
    dblock.unlock();
    
    // handle result
    if( MOCDB_SUCCESS == dbResult )
    {
        // initialise class members (m_uid is assigned by the calling method)
        ${DB_TO_MEMBER_ATTS}
        
        m_LastUpdated    = outRec.${database_struct_prefix}LastUpdated;
        m_LastVerified   = outRec.${database_struct_prefix}LastVerified;
        m_LastVerAttempt = outRec.${database_struct_prefix}LastVerAttempt;
        
        return( mo_ok );    
    }

    if( MOCDB_NO_SUCH_INSTANCE == dbResult )
    {
        TRACEFLOW("%s:%d: $newname::retrieve did not find entry  \n",
                  __FILE__,__LINE__);
        sprintf(m_errorMessage,
                "%s:%d: $newname::retrieve did not find entry  \n",
                __FILE__,__LINE__);
      
      
        return( mo_notfound );
    }
    return( mo_error );
} // $newname::retrieve


///////////////////////////////////////////////////////////////////
// update the database from the members.
ManagedObjectStatus
$newname::db_update()
{
    TRACEFUNC("%s:%d: $newname::db_update ()\n",
              __FILE__,__LINE__);

    ${database_struct_name}_t inRec;
    // initialise db structure
    memset( &inRec, 0, sizeof(inRec) );
    
    // initialise db structure members
    inRec.${database_struct_prefix}UID = m_uid;
    ${MEMBER_TO_DB}
    
    inRec.${database_struct_prefix}LastUpdated    = m_LastUpdated;
    inRec.${database_struct_prefix}LastVerified   = m_LastVerified;
    inRec.${database_struct_prefix}LastVerAttempt = m_LastVerAttempt;
    
    
    // now do db update
    nmsLock dblock( DB_LOCK, TRUE );
    int dbResult = ${database_struct_name}UpdateToCDB(NMS_DB_CONTEXT, &inRec, TRUE );
    dblock.unlock();

    if( MOCDB_SUCCESS == dbResult )
    {
        return( mo_ok );
    }
    TRACEFLOW("%s:%d:  $newname::db_update could not update database  \n",
              __FILE__,__LINE__);
    sprintf(m_errorMessage,
            "%s:%d:  $newname::db_update could not update database  \n",
            __FILE__,__LINE__);

    return( mo_error );
} // $newname::db_update
//////////////////////////////////////////////////////////////
//
//
//
/////////////////////////////////////////////////////////////
ManagedObjectStatus
${newname}::remove (UID uid)
{
    TRACEFUNC("%s:%d: ${newname}::remove (uid %lld )\n",
              __FILE__, __LINE__,
              uid);

    if (uid == 0)
    {
        TRACEFLOW("%s:%d: ${newname}::remove uid is 0 \n",
                  __FILE__,__LINE__);

        return mo_error;
    }
    ManagedObjectStatus moStatus = mo_ok;

    // we need to find this one
    moStatus = find(uid);
    
    if (mo_ok != moStatus)
    {
        TRACEFLOW("%s:%d: ${newname}::remove could not find %lld \n",
                  __FILE__,__LINE__,
                  uid);
        sprintf(m_errorMessage,
                "%s:%d: ${newname}::remove could not find %lldS \n",
                __FILE__,__LINE__,
                uid);
        return(moStatus);
    }
    
    // First - remove from the EMS
    moStatus = ${ems_remove_name}();
    
    if (mo_ok != moStatus)
    {
        TRACEFLOW("%s:%d: ${newname}::remove could not remove from the EMS \n",
                  __FILE__,__LINE__);
        sprintf(m_errorMessage,
                "%s:%d: ${newname}::remove could not remove from the EMS \n",
                __FILE__,__LINE__);
        return(moStatus);
    }
    

    // now remove from the database
    ${database_struct_name}_t inRec;
    memset (&inRec, 0, sizeof(inRec));

    inRec.${database_struct_prefix}UID = uid;

    nmsLock dblock(DB_LOCK, TRUE);
    int dbResult = ${database_struct_name}DeleteFromCDB(NMS_DB_CONTEXT,
                                                        &inRec);
    dblock.unlock();
    
    

    if ( dbResult == MOCDB_SUCCESS )
    {
        
        moStatus = ManagedObject::remove(uid);
    }
    

    if ( (MOCDB_SUCCESS == dbResult )
         && (mo_ok == moStatus))
    {
        init();
        return mo_ok;
    }

    if ( dbResult == MOCDB_NO_SUCH_INSTANCE )
    {
        TRACEFLOW("%s:%d: ${newname}::remove did not find the entry \n",
                  __FILE__,__LINE__);
        sprintf(m_errorMessage,
                "%s:%d: ${newname}::remove did not find the entry \n",
                __FILE__,__LINE__);
        
        return mo_notfound;
    }
    
    

    return mo_error;
}

////////////////////////////////////////////////////////////////////////////
// set the details of this object
ManagedObjectStatus
$newname::write(const ${newname}Details &details )
{
    ManagedObjectStatus moStatus = mo_ok;
    TRACEFUNC("%s:%d: $newname::write ()\n",
              __FILE__,__LINE__);
    // the current attributes are loaded from the impl call
    // prior to this call
    
    if (m_uid == 0)
    {
        TRACEFLOW("%s:%d: ${newname}::write uid is 0 \n",
                  __FILE__,__LINE__);

        return mo_error;
    }
    
    // now copy over the RW attributes in the details
    
    ${RW_DETAILS_TO_MEMBER_ASSIGNMENTS}

    // do the snmp set
    
    moStatus = ${ems_set_name}();
     
    if (moStatus != mo_ok)
    {
        TRACEFLOW("%s:%d: ${newname}::write ${ems_set_name} failed \n",
                  __FILE__,__LINE__);
        sprintf(m_errorMessage,
                "%s:%d: ${newname}::write ${ems_set_name} failed \n",
                __FILE__,__LINE__);

        return moStatus;
    }
    
    m_LastUpdated    = MoTimeNow();
    m_LastVerified   = MoTimeNow();
    m_LastVerAttempt = MoTimeNow();
    
    // Insert a row into the database.
    moStatus = db_update();
     
    if (moStatus == mo_ok)
    {
        TRACEFLOW("%s:%d: ${newname}::write success \n",
                  __FILE__,__LINE__);
        return mo_ok;
    }
    
    // If we get to here, the MO insert succeeded, but the  insert 
    // failed, so we need to remove the MO row, and return an error.

    TRACEFLOW("%s:%d: ${newname}::write db insert failed \n",
              __FILE__,__LINE__);
    sprintf(m_errorMessage,
            "%s:%d: ${newname}::write db insert failed \n",
            __FILE__,__LINE__);
    return mo_error;
    
} // $newname::write




ManagedObjectSummary *
${newname}::get ()
{
	const char * METHOD = "${newname}::get"; 
    TRACEFUNC("%s:%d: %s()\n",
              __FILE__,__LINE__, METHOD);
    
   
   m_summary.name   = CORBA::string_dup(m_name);
   m_summary.type   = m_type;
   m_summary.id     = m_uid;
   m_summary.parent = m_parent;
   m_summary.vpn    = m_vpn;
   m_summary.device = m_owner;
   
   NameValueList list(${NUMBER_OF_ATTRIBUTES});
   list.length(${NUMBER_OF_ATTRIBUTES});
   m_summary.settings = list;
#ifndef MAX_BUF
#define MAX_BUF 128
#endif

   char tmp[MAX_BUF];

   ${GET_VALUES_TO_NAME_VALUE_LIST}


   return &m_summary;
}

// end $newname
// *************************************************************************

#endif // _${newname}_CC_
EOF
###########################################################################