Hello all,

    I have an AMF application which runs as (High Availability Framework)HAFW in 2N redundant systems for confD active/standby configuration. So, when I call the function *saAmfComponentNameGet(confd_ha_cb.info.amfHandle, &confd_ha_cb.info.compName)* I am getting an error stating *(SA_AIS_ERR_NOT_EXIST, 12). *what am i doing wrong here? why am i getting this error? I have attached the AppConfig file and my application files for your reference. Please find the attachments.

Thanks in advance,

Regards.

<?xml version="1.0" encoding="UTF-8"?>
<!--
 *      -*- CONFD_HA  -*-
 *
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
 * under the GNU Lesser General Public License Version 2.1, February 1999.
 * The complete license can be accessed from the following location:
 * http://opensource.org/licenses/lgpl-license.php
 * See the Copying file included with the OpenSAF distribution for full
 * licensing terms.
 *
 * Author(s): High Availability Solutions
 *

-->

<imm:IMM-contents xmlns:imm="http://www.saforum.org/IMMSchema"; xsi:noNamespaceSchemaLocation="SAI-AIS-IMM-XSD-A.01.01.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
	<object class="SaAmfAppBaseType">
		<dn>safAppType=CONFD_HA</dn>
	</object>
	<object class="SaAmfSGBaseType">
		<dn>safSgType=CONFD_HA</dn>
	</object>
	<object class="SaAmfSUBaseType">
		<dn>safSuType=CONFD_HA</dn>
	</object>
	<object class="SaAmfCompBaseType">
		<dn>safCompType=CONFD_HA</dn>
	</object>
	<object class="SaAmfSvcBaseType">
	    <dn>safSvcType=CONFD_HA</dn>
	</object>
	<object class="SaAmfCSBaseType">
	    <dn>safCSType=CONFD_HA</dn>
	</object>
	<object class="SaAmfSvcType">
	    <dn>safVersion=1,safSvcType=CONFD_HA</dn>
	</object>
	<object class="SaAmfAppType">
		<dn>safVersion=1,safAppType=CONFD_HA</dn>
		<attr>
			<name>saAmfApptSGTypes</name>
			<value>safVersion=1,safSgType=CONFD_HA</value>
		</attr>
	</object>
	<object class="SaAmfSGType">
		<dn>safVersion=1,safSgType=CONFD_HA</dn>
		<attr>
			<name>saAmfSgtRedundancyModel</name>
			<value>1</value>
		</attr>
		<attr>
			<name>saAmfSgtValidSuTypes</name>
			<value>safVersion=1,safSuType=CONFD_HA</value>
		</attr>
		<attr>
			<name>saAmfSgtDefAutoAdjustProb</name>
			<value>10000000000</value>
		</attr>
		<attr>
			<name>saAmfSgtDefCompRestartProb</name>
			<value>4000000000</value>
		</attr>
		<attr>
			<name>saAmfSgtDefCompRestartMax</name>
			<value>10</value>
		</attr>
		<attr>
			<name>saAmfSgtDefSuRestartProb</name>
			<value>4000000000</value>
		</attr>
		<attr>
			<name>saAmfSgtDefSuRestartMax</name>
			<value>10</value>
		</attr>
	</object>
	<object class="SaAmfSUType">
		<dn>safVersion=1,safSuType=CONFD_HA</dn>
		<attr>
			<name>saAmfSutIsExternal</name>
			<value>0</value>
		</attr>
		<attr>
			<name>saAmfSutDefSUFailover</name>
			<value>1</value>
		</attr>
		<attr>
			<name>saAmfSutProvidesSvcTypes</name>
			<value>safVersion=1,safSvcType=CONFD_HA</value>
		</attr>
	</object>
	<object class="SaAmfCompType">
		<dn>safVersion=1,safCompType=CONFD_HA</dn>
		<attr>
			<name>saAmfCtCompCategory</name>
			<value>1</value>
		</attr>
		<attr>
			<name>saAmfCtSwBundle</name>
			<value>safSmfBundle=CONFD_HA</value>
		</attr>
		<attr>
			<name>saAmfCtDefClcCliTimeout</name>
			<value>60000000000</value>
		</attr>
		<attr>
			<name>saAmfCtDefCallbackTimeout</name>
			<value>10000000000</value>
		</attr>
		<attr>
			<name>saAmfCtRelPathInstantiateCmd</name>
			<value>confd_ha_inst.sh</value>
		</attr>
		<attr>
			<name>saAmfCtDefInstantiateCmdArgv</name>
			<value></value>
		</attr>
		<attr>
			<name>saAmfCtRelPathCleanupCmd</name>
			<value>confd_ha_cleanup.sh</value>
		</attr>
		<attr>
			<name>saAmfCtDefCleanupCmdArgv</name>
			<value></value>
		</attr>
		<attr>
			<name>saAmfCtDefQuiescingCompleteTimeout</name>
			<value>50000000000</value>
		</attr>
		<attr>
			<name>saAmfCtDefRecoveryOnError</name>
			<value>2</value>
		</attr>
		<attr>
			<name>saAmfCtDefDisableRestart</name>
			<value>0</value>
		</attr>
		<attr>
			<name>saAmfCtDefCmdEnv</name>
			<value>AMF_DEMO_VAR1=CT_VALUE1</value>
			<value>AMF_DEMO_VAR2=CT_VALUE2</value>
		</attr>
	</object>
	<object class="SaAmfCSType">
		<dn>safVersion=1,safCSType=CONFD_HA</dn>
	</object>
	<object class="SaAmfSutCompType">
		<dn>safMemberCompType=safVersion=1\,safCompType=CONFD_HA,safVersion=1,safSuType=CONFD_HA</dn>
	</object>
	<object class="SaAmfSvcTypeCSTypes">
	  <dn>safMemberCSType=safVersion=1\,safCSType=CONFD_HA,safVersion=1,safSvcType=CONFD_HA</dn>
	</object>
	<object class="SaAmfCtCsType">
		<dn>safSupportedCsType=safVersion=1\,safCSType=CONFD_HA,safVersion=1,safCompType=CONFD_HA</dn>
		<attr>
			<name>saAmfCtCompCapability</name>
			<value>1</value>
		</attr>
	</object>
	<object class="SaAmfHealthcheckType">
		<dn>safHealthcheckKey=HC_CONFD_HA,safVersion=1,safCompType=CONFD_HA</dn>
		<attr>
			<name>saAmfHctDefPeriod</name>
			<value>15000000000</value>
		</attr>
		<attr>
			<name>saAmfHctDefMaxDuration</name>
			<value>60000000000</value>
		</attr>
	</object>

	<object class="SaAmfApplication">
		<dn>safApp=CONFD_HA</dn>
		<attr>
			<name>saAmfAppType</name>
			<value>safVersion=1,safAppType=CONFD_HA</value>
		</attr>
	</object>
	<object class="SaAmfSG">
		<dn>safSg=SG_CONFD_HA,safApp=CONFD_HA</dn>
		<attr>
			<name>saAmfSGType</name>
			<value>safVersion=1,safSgType=CONFD_HA</value>
		</attr>
		<attr>
			<name>saAmfSGSuHostNodeGroup</name>
			<value>safAmfNodeGroup=SCs,safAmfCluster=myAmfCluster</value>
		</attr>
		<attr>
			<name>saAmfSGAutoRepair</name>
			<value>0</value>
		</attr>
		<attr>
			<name>saAmfSGAutoAdjust</name>
			<value>0</value>
		</attr>
		<attr>
			<name>saAmfSGNumPrefInserviceSUs</name>
			<value>10</value>
		</attr>
		<attr>
			<name>saAmfSGNumPrefAssignedSUs</name>
			<value>10</value>
		</attr>
	</object>
	<object class="SaAmfSI">
		<dn>safSi=Si_CONFD_HA,safApp=CONFD_HA</dn>
		<attr>
			<name>saAmfSvcType</name>
			<value>safVersion=1,safSvcType=CONFD_HA</value>
		</attr>
		<attr>
			<name>saAmfSIProtectedbySG</name>
			<value>safSg=SG_CONFD_HA,safApp=CONFD_HA</value>
		</attr> 
	</object>
	<object class="SaAmfCSI">
	    <dn>safCsi=Csi_CONFD_HA,safSi=Si_CONFD_HA,safApp=CONFD_HA</dn>
	    <attr>
		<name>saAmfCSType</name>
		<value>safVersion=1,safCSType=CONFD_HA</value>
	    </attr>
	</object>
	<object class="SaSmfSwBundle">
			<dn>safSmfBundle=CONFD_HA</dn>
	</object>
	<object class="SaAmfNodeSwBundle">
		<dn>safInstalledSwBundle=safSmfBundle=CONFD_HA,safAmfNode=SC-1,safAmfCluster=myAmfCluster</dn>
		<attr>
			<name>saAmfNodeSwBundlePathPrefix</name>
			<value>/etc/opt/opensaf</value>
		</attr>
	</object>
	<object class="SaAmfSU">
		<dn>safSu=SuT_CONFD_HA_1,safSg=SG_CONFD_HA,safApp=CONFD_HA</dn>
		<attr>
			<name>saAmfSUType</name>
			<value>safVersion=1,safSuType=CONFD_HA</value>
		</attr>
		<attr>
			<name>saAmfSURank</name>
			<value>1</value>
		</attr>
		<attr>
			<name>saAmfSUAdminState</name>
			<value>3</value>
		</attr>
		<attr>
			<name>saAmfSUHostNodeOrNodeGroup</name>
			<value>safAmfNode=SC-1,safAmfCluster=myAmfCluster</value>
                </attr>
	</object>
	<object class="SaAmfComp">
                <dn>safComp=CompT_CONFD_HA,safSu=SuT_CONFD_HA_1,safSg=SG_CONFD_HA,safApp=CONFD_HA</dn>
	        <attr>
		        <name>saAmfCompType</name>
		        <value>safVersion=1,safCompType=CONFD_HA</value>
	        </attr>
	</object>
	<object class="SaAmfCompCsType">
	    <dn>safSupportedCsType=safVersion=1\,safCSType=CONFD_HA,safComp=CompT_CONFD_HA,safSu=SuT_CONFD_HA_1,safSg=SG_CONFD_HA,safApp=CONFD_HA</dn>
	</object>
	<object class="SaAmfNodeSwBundle">
		<dn>safInstalledSwBundle=safSmfBundle=CONFD_HA,safAmfNode=SC-2,safAmfCluster=myAmfCluster</dn>
		<attr>
			<name>saAmfNodeSwBundlePathPrefix</name>
			<value>/etc/opt/opensaf</value>
		</attr>
	</object>
	<object class="SaAmfSU">
		<dn>safSu=SuT_CONFD_HA_2,safSg=SG_CONFD_HA,safApp=CONFD_HA</dn>
		<attr>

			<name>saAmfSUType</name>
			<value>safVersion=1,safSuType=CONFD_HA</value>
		</attr>
		<attr>
			<name>saAmfSURank</name>
			<value>2</value>
		</attr>
		<attr>
			<name>saAmfSUAdminState</name>
			<value>3</value>
		</attr>
                <attr>
                        <name>saAmfSUHostNodeOrNodeGroup</name>
                        <value>safAmfNode=SC-2,safAmfCluster=myAmfCluster</value>
                </attr>
	</object>
	<object class="SaAmfComp">
	        <dn>safComp=CompT_CONFD_HA,safSu=SuT_CONFD_HA_2,safSg=SG_CONFD_HA,safApp=CONFD_HA</dn>
	        <attr>
		        <name>saAmfCompType</name>
		        <value>safVersion=1,safCompType=CONFD_HA</value>
	        </attr>
	</object>
	<object class="SaAmfCompCsType">
	    <dn>safSupportedCsType=safVersion=1\,safCSType=CONFD_HA,safComp=CompT_CONFD_HA,safSu=SuT_CONFD_HA_2,safSg=SG_CONFD_HA,safApp=CONFD_HA</dn>
	</object>
</imm:IMM-contents>
/*
Integration glue between ConfD and OpenSAF AMF

Copyright (C) 2009 Tail-f Systems AB

This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version. 

This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details. 

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA

*/

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/poll.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>
#include <pthread.h>


#include <confd.h>
#include <confd_ha.h>
#include <confd_ha_local.h>

/* SAF header files */
#include "saAis.h"
#include "saAmf.h"

static SaNameT compName;

/* HealthCheck Key on which healthcheck is started */
SaAmfHealthcheckKeyT gl_healthcheck_key = {"A9FD64E12D", 10};


/* Entry level routine AMF Interface task */
static void *confd_ha_amf_process(void *arg);
static SaUint32T confd_ha_proxy_amf_init(void);


/* 'CSI Set' callback that is registered with AMF */
static void csi_set_callback(SaInvocationT, 
                                   const SaNameT *,
                                   SaAmfHAStateT,
                                   SaAmfCSIDescriptorT);

/* 'CSI Remove' callback that is registered with AMF */
static void csi_remove_callback(SaInvocationT, 
                                      const SaNameT *,
                                      const SaNameT *,
                                      SaAmfCSIFlagsT);


/* 'Component Terminate' callback that is registered with AMF */
static void comp_terminate_callback(SaInvocationT, const SaNameT *);

void confd_phase2(void);

/****************************************************************************
  Name          : confd_ha_amf_init
 
  Description   : This routine creates & starts the AMF interface task.
 
  Arguments     : None.
 
  Return Values : NCSCC_RC_SUCCESS/NCSCC_RC_FAILURE
 
  Notes         : None.
******************************************************************************/
SaUint32T confd_ha_amf_init(void)
{
    SaUint32T rc = 0;
    char hst[BUFSIZ];
    pthread_t thread;
    pthread_attr_t attr;

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

    if (pthread_create(&thread, &attr, confd_ha_amf_process, NULL) != 0) {
	    goto err;
    }

    gethostname(hst, BUFSIZ);
    printf("Running at %s\n", hst);
    return SA_AIS_OK;

  err:
    /* destroy the task */
    pthread_attr_destroy(&attr);
    printf("\n\n AMF-INTF TASK CREATION FAILED !!! \n\n");

    return rc;
}

#define HEARTBEAT 5000

static int notif_sock() {

       struct sockaddr_in addr;
       int notsock;

       if ((notsock = socket(PF_INET, SOCK_STREAM, 0)) < 0 )  {
           perror("Failed to open notsocket\n");
           return -1;
       }

       addr.sin_addr.s_addr = inet_addr("0.0.0.0");
       addr.sin_family = AF_INET;
       addr.sin_port = htons(CONFD_PORT);
       
       struct confd_notifications_data dt;
       int dflag = CONFD_NOTIF_HEARTBEAT | CONFD_NOTIF_HA_INFO;

       dt.heartbeat_interval = HEARTBEAT;
       if (confd_notifications_connect2(
               notsock,
               (struct sockaddr*)&addr, 
               sizeof (struct sockaddr_in), dflag, &dt) < 0 ) {
           close(notsock);
           printf("Failed to confd_notifications_connect() "
                   "to confd : %d\n", confd_errno);
           return -1;
       }
       return notsock;
}

/* Once we report bad health, amf will fail over and */
/* we might as well exit                             */

static void bad_health() {
    saAmfHealthcheckConfirm( confd_ha_cb.info.amfHandle, 
                             &confd_ha_cb.info.compName,
                             &gl_healthcheck_key,
                             SA_AIS_ERR_FAILED_OPERATION);
    printf("Reported bad health - exiting \n");
}

    
static void good_health() {
    saAmfHealthcheckConfirm( confd_ha_cb.info.amfHandle, 
                             &confd_ha_cb.info.compName,
                             &gl_healthcheck_key,
                             SA_AIS_OK);
}


static int be_something(int state) {
    
    int s = socket(PF_INET, SOCK_STREAM, 0);
    if (s <  0) return SA_AIS_ERR_FAILED_OPERATION;
    
    struct sockaddr_in addr;

    addr.sin_addr.s_addr = inet_addr("0.0.0.0");
    addr.sin_family = AF_INET;
    addr.sin_port = htons(4565);

    if (confd_ha_connect(s, (struct sockaddr *)&addr, sizeof(addr), "token") !=
        CONFD_OK) { 
        close (s); 
        return SA_AIS_ERR_FAILED_OPERATION;
    }

    switch (state) {
    case CONFD_HA_STATE_NONE:
        printf("Ordering this node to be NONE \n");
        if (confd_ha_benone(s) != CONFD_OK) {
            close(s);
            return SA_AIS_ERR_FAILED_OPERATION;
        }
        break;
    case CONFD_HA_STATE_MASTER:
        printf("Ordering this node to be MASTER \n");
        if (confd_ha_bemaster(s, &mynodeid) != CONFD_OK) {
            close(s);
            return SA_AIS_ERR_FAILED_OPERATION;
        }
        break;
    case CONFD_HA_STATE_SLAVE:
        printf("Ordering this node to be SLAVE \n");
        if (confd_ha_beslave(s, &mynodeid, &othernode, 0) != CONFD_OK) {
            close(s);
            return SA_AIS_ERR_FAILED_OPERATION;
        }
        break;
    }
    close (s);
    return SA_AIS_OK;
}



void *confd_ha_amf_process(void *arg)
{
    SaAisErrorT        rc;
    SaSelectionObjectT amf_sel_obj;
    int notsock = -1;

    /* this is to allow to establish MDS session with AvSv */
    sleep(3);
    rc = confd_ha_proxy_amf_init();
    if (SA_AIS_OK != rc)
        return NULL;

    /* Get the selection object corresponding to this AMF handle */
    rc = saAmfSelectionObjectGet(confd_ha_cb.info.amfHandle, &amf_sel_obj);
    if (SA_AIS_OK != rc) {
        saAmfFinalize(confd_ha_cb.info.amfHandle);
        return NULL;
    }
   
    printf("\n AMF Selection Object Get Successful !!! \n");
printf("comp name1 %llu \n", confd_ha_cb.info.amfHandle);
printf("comp name1 %c \n", confd_ha_cb.info.compName.value);

    rc = saAmfComponentNameGet(confd_ha_cb.info.amfHandle, 
                               &confd_ha_cb.info.compName);
         printf("SA_AIS_OK %d \n", SA_AIS_OK);                          
    printf("what is rc %d \n", rc);
    if (SA_AIS_OK != rc) {
     printf("inside if \n");
        saAmfFinalize(confd_ha_cb.info.amfHandle);
        printf("after function \n");
        return NULL;
    }
printf("out of if \n");
    printf("\n Component Name Get Successful !!! \n CompName: %s \n",
                      confd_ha_cb.info.compName.value);
    

    rc = saAmfComponentRegister(confd_ha_cb.info.amfHandle, 
                                &confd_ha_cb.info.compName, 0);
    if (SA_AIS_OK != rc) {
            saAmfFinalize(confd_ha_cb.info.amfHandle);
            return NULL;
    }
    printf("\n Component Registered !!! \n");

    rc = saAmfHealthcheckStart(confd_ha_cb.info.amfHandle, 
                               &confd_ha_cb.info.compName,
                               &gl_healthcheck_key,
                               SA_AMF_HEALTHCHECK_COMPONENT_INVOKED,
                               SA_AMF_COMPONENT_FAILOVER);  
    
    if (SA_AIS_OK != rc) {
        printf("Failed to start health check \n");
        saAmfFinalize(confd_ha_cb.info.amfHandle);
        return NULL;
    }


    notsock = notif_sock();
    printf("notsock = %d amf_sel_obj = %d \n", notsock, (int)amf_sel_obj);
    if (notsock < 0) {
        bad_health();
    }    

    while (1) {
        int pval;
        struct pollfd set[2];
        struct confd_notification n;

        set[0].fd = amf_sel_obj;
        set[0].events = POLLIN;
        set[0].revents = 0;

        set[1].fd = notsock;
        set[1].events = POLLIN;
        set[1].revents = 0;
        
        
        if ((pval = poll(&set[0], 2, HEARTBEAT * 4)) < 0) {
            perror("Poll failed:");
            continue;
        }
        
        if (pval == 0) { /* We have a timeout, ConfD isn't sending any ticks */

            printf("poll timeout\n");
            close(notsock);

            /* Try to reconnect */
            if ((notsock =  notif_sock()) < 0 ) {
                printf("ConfD not running \n");
                bad_health();
            }
        }

        if (set[0].revents & POLLIN) {
            rc = saAmfDispatch(confd_ha_cb.info.amfHandle, 
                               SA_DISPATCH_ALL);
            if (SA_AIS_OK != rc) {
                printf("\n saAmfDispatch failed !!! \n");
                break;
            }
        }


        if (set[1].revents & POLLIN) {
            int rval;
            if ((rval = confd_read_notification(notsock, &n)) != CONFD_OK) {
                printf("Failed to read notif %d\n", rval);
                close(notsock); 
                bad_health();
            }
            switch(n.type) {
            case CONFD_NOTIF_HEARTBEAT:
                good_health();
                break;
            case CONFD_NOTIF_HA_INFO: {
                struct confd_ha_notification *hnot = &n.n.hnot;
                switch (hnot->type) {
                case CONFD_HA_INFO_NOMASTER:
                    /* We're alone without a master, either confd */
                    /* died at the master, and should be restarted */
                    /* or the node died entirely - in which case ... */
                    
                    printf("CONFD_HA_INFO  -  NOMASTER\n");
                    break;
                    
                case CONFD_HA_INFO_SLAVE_DIED:
                    /* A slave disappeared, no worries */
                    printf("CONFD_HA_INFO  -  SLAVE_DIED\n");
                    break;
                    
                case CONFD_HA_INFO_SLAVE_ARRIVED:
                    /* A slave connected - fine */
                    printf("CONFD_HA_INFO  -  SLAVE_ARRIVED\n");
                    break;
                    
                case CONFD_HA_INFO_SLAVE_INITIALIZED:
                    /* Slave is up and initialized - even better */
                    printf("CONFD_HA_INFO  -  SLAVE_INITIALIZED\n");
                    break;

		default:
		    printf("Got Unexpected hnot->type(%d) from ConfD ... \n", hnot->type);
                    break;
                }
            }
                break;
            default:
                printf("Got bad data from ConfD ... \n");
                close(notsock); 
                notsock = -1;
            }
        }
    }
    return NULL;
}

/****************************************************************************
  Name          : csi_set_callback
 
  Description   : This routine is a callback to set (add/modify) the HA state
                  of a CSI (or all the CSIs) that is newly/already assigned 
                  to the component. It is specified as a part of AMF 
                  initialization.
 
  Arguments     : inv       - particular invocation of this callback function
                  comp_name - ptr to the component name
                  ha_state  - ha state to be assumed by the CSI (or all the 
                              CSIs)
                  csi_desc  - CSI descriptor

  Return Values : None.
 
  Notes         : None. 
******************************************************************************/
void csi_set_callback(SaInvocationT       inv, 
                      const SaNameT       *comp_name,
                      SaAmfHAStateT       ha_state,
                      SaAmfCSIDescriptorT csi_desc) {


    SaAisErrorT      rc = SA_AIS_OK;


    printf("\n'CSI Set' Callback \n Component: %s \n"
           " CSIName: %s \n HAState: %s \n CSIFlags: %s \n", 
           comp_name->value, csi_desc.csiName.value, 
           ha_state_str[ha_state], csi_flag_str[csi_desc.csiFlags]);

    switch(ha_state) {
    case SA_AMF_HA_ACTIVE:
                
        confd_ha_cb.info.haState = ha_state;
        rc = be_something(CONFD_HA_STATE_MASTER);
        break;

    case SA_AMF_HA_QUIESCED: 
        
        if(SA_AMF_HA_ACTIVE == confd_ha_cb.info.haState) {
            ;
        }
        confd_ha_cb.info.haState = ha_state;
        rc = be_something(CONFD_HA_STATE_NONE);
        break;
    case SA_AMF_HA_STANDBY:
        confd_ha_cb.info.haState = ha_state;
        rc = be_something(CONFD_HA_STATE_SLAVE);
        break;
    default:
        rc=SA_AIS_ERR_FAILED_OPERATION;
        break;
    };

    rc = saAmfResponse(confd_ha_cb.info.amfHandle, inv, rc);
    if ( SA_AIS_OK != rc ) {

        printf("\nsaAmfResponse returned failure for CSI Set. Result %d\n",rc);
        saAmfComponentUnregister(confd_ha_cb.info.amfHandle, 
                                 &confd_ha_cb.info.compName, 0);
        saAmfFinalize(confd_ha_cb.info.amfHandle);
        return;
    }
    confd_phase2();

    return;
}

/****************************************************************************
  Name          : csi_remove_callback
 
  Description   : This routine is a callback to remove the CSI (or all the 
                  CSIs) that is/are assigned to the component. It is specified
                  as a part of AMF initialization.
 
  Arguments     : inv       - particular invocation of this callback function
                  comp_name - ptr to the component name
                  csi_name  - ptr to the CSI name that is being removed
                  csi_flags - specifies if one or more CSIs are affected

  Return Values : None.
 
  Notes         : None
******************************************************************************/
void csi_remove_callback(SaInvocationT  inv, 
                         const SaNameT  *comp_name,
                         const SaNameT  *csi_name,
                         SaAmfCSIFlagsT csi_flags) {
    SaAisErrorT rc;

    printf("\n 'CSI Remove' Callback \n Component: %s "
           "\n CSI: %s \n CSIFlags: %s \n", 
           comp_name->value, csi_name->value, 
           csi_flag_str[csi_flags]);

    be_something(CONFD_HA_STATE_NONE);

    /* Reset the ha state */
    confd_ha_cb.info.haState = 0;

    /* Respond immediately */
    rc = saAmfResponse(confd_ha_cb.info.amfHandle, inv, SA_AIS_OK);
    if ( SA_AIS_OK != rc ) {
        saAmfComponentUnregister(confd_ha_cb.info.amfHandle, 
                                 &confd_ha_cb.info.compName, 0);
        saAmfFinalize(confd_ha_cb.info.amfHandle);
        printf("\nsaAmfResponse returned failure for CSI Remove. "
               "Result %d\n",rc);
    }
    
    return;
}



/****************************************************************************
  Name          : comp_terminate_callback
 
  Description   : This routine is a callback to terminate the component. It 
                  is specified as a part of AMF initialization.
 
  Arguments     : inv             - particular invocation of this callback 
                                    function
                  comp_name       - ptr to the component name
 
  Return Values : None.
 
  Notes         : None
******************************************************************************/
void comp_terminate_callback(SaInvocationT inv, 
                             const SaNameT *comp_name) {

    SaAisErrorT rc;
    
    printf("\n 'Component Terminate' Callback \n Component: %s \n", 
           comp_name->value);
    
    rc = saAmfResponse(confd_ha_cb.info.amfHandle, inv, SA_AIS_OK);
    if ( SA_AIS_OK != rc ) {
        saAmfComponentUnregister(confd_ha_cb.info.amfHandle, 
                                 &confd_ha_cb.info.compName, 0);
        saAmfFinalize(confd_ha_cb.info.amfHandle);
        printf("\nsaAmfResponse returned failure for Comp Terminate. "
               "Result %d\n",rc);
    }
    // should we exit here ??? I think so ???
    exit(0);
    return;
}

/****************************************************************************
  Name          : comp_pg_callback

  Description   : This routine is a callback to notify the application of any
                  changes in the protection group.

  Arguments     : csi_name - ptr to the csi-name
                  not_buf  - ptr to the notification buffer
                  mem_num  - number of components that belong to this
                             protection group
                  err      - error code

  Return Values : None.

  Notes         : None.
******************************************************************************/
void comp_pg_callback(const SaNameT *csi_name,
                      SaAmfProtectionGroupNotificationBufferT *not_buf,
                      SaUint32T mem_num,
                      SaAisErrorT err)
{
    SaUint32T  item_count;
    
    
    printf("\n Dispatched 'Protection Group' Callback "
           "\n CSI: %s \n No. of Members: %d \n",
           csi_name->value, (SaUint32T)mem_num);
    
    if ( SA_AIS_OK != err ) {
        printf("\n Error Returned is %d\n",err);   
        return;
    }
    
    /* Print the Protection Group members */
    for (item_count= 0; item_count < not_buf->numberOfItems; item_count++){
        printf(" CompName[%d]: %s \n",
               item_count, 
               not_buf->notification[item_count].member.compName.value);
        printf(" Rank[%d]    : %d \n",
               item_count, 
               (SaUint32T)not_buf->notification[item_count].member.rank);
        printf(" HAState[%d] : %s \n",
               item_count, 
               ha_state_str[not_buf->notification[item_count].member.
                            haState]);
        printf(" Change[%d]  : %s \n",
               item_count, 
               pg_change_str[not_buf->notification[item_count].change]);
        
        printf("\n");
    }
    return;
}



/****************************************************************************
  Name          : confd_ha_proxy_amf_init

  Description   : This routine initializes amf interface.

  Arguments     : None.

  Return Values : SUCC/FAILURE

  Notes         : None.
******************************************************************************/
SaUint32T confd_ha_proxy_amf_init(void)
{
    SaAisErrorT        rc;
    SaAmfCallbacksT    reg_callback_set;
    SaVersionT         ver;
    
    /* Fill the callbacks that are to be registered with AMF */
    memset(&reg_callback_set, 0, sizeof(SaAmfCallbacksT));
    reg_callback_set.saAmfCSISetCallback = csi_set_callback;
    reg_callback_set.saAmfCSIRemoveCallback = csi_remove_callback;
    reg_callback_set.saAmfComponentTerminateCallback = 
        comp_terminate_callback;
    reg_callback_set.saAmfProtectionGroupTrackCallback = comp_pg_callback;
    
    /* Fill the AMF version */
    m_CONFD_HA_VER_GET(ver);
    
    /* Initialize AMF */
    rc = saAmfInitialize(&confd_ha_cb.info.amfHandle, &reg_callback_set, 
                         &ver);
    if (SA_AIS_OK != rc) {
        printf("\n AMF Initialization Failed !!! \n AmfHandle: %lld \n", 
               confd_ha_cb.info.amfHandle);
        return rc;
    }
    
    printf("\n AMF Initialization Done !!! \n AmfHandle: %lld \n", 
           confd_ha_cb.info.amfHandle);
    return rc;
    
}
/*
Integration glue between ConfD and OpenSAF AMF

Copyright (C) 2009 Tail-f Systems AB

This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version. 

This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details. 

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA

*/

#ifndef CONFD_HA_LOCAL_H
#define CONFD_HA_LOCAL_H

#include <saAis.h>
#include <saAmf.h>

#include <confd.h>
#include <unistd.h>
/*############################################################################
                            Global Variables
############################################################################*/

typedef struct confd_ha_comp_info
{
   SaNameT         compName;        /* Component name */
   SaNameT         csi_compName;    /* CSI name */
   SaAmfHandleT    amfHandle;       /* AMF handle obtained during AMF initialisation */
   SaAmfHAStateT   haState;         /* Current state of Proxy */
   SaAmfHandleT    amf_hdl;
   SaAmfHealthcheckKeyT healthcheck_key;
   SaBoolT         health_start;
   SaBoolT         health_start_comp_inv;
   SaBoolT         csi_assigned;
} CONFD_HA_COMP_INFO;

typedef struct confd_ha_cb
{
   CONFD_HA_COMP_INFO  info; 
}CONFD_HA_CB;

CONFD_HA_CB  confd_ha_cb;

/* Canned strings for HA State */
const char *ha_state_str[] =
{
   "None",
   "Active",    /* SA_AMF_HA_ACTIVE       */
   "Standby",   /* SA_AMF_HA_STANDBY      */
   "Quiesced",  /* SA_AMF_HA_QUIESCED     */
   "Quiescing"  /* SA_AMF_HA_QUIESCING    */
};

/* Canned strings for CSI Flags */
const char *csi_flag_str[] =
{
   "None",
   "Add One",    /* SA_AMF_CSI_ADD_ONE    */
   "Target One", /* SA_AMF_CSI_TARGET_ONE */
   "None",
   "Target All", /* SA_AMF_CSI_TARGET_ALL */
};

/* Canned strings for Transition Descriptor */
const char *trans_desc_str[] =
{
   "None",
   "New Assign",   /* SA_AMF_CSI_NEW_ASSIGN   */
   "Quiesced",     /* SA_AMF_CSI_QUIESCED     */
   "Not Quiesced", /* SA_AMF_CSI_NOT_QUIESCED */
   "Still Active"  /* SA_AMF_CSI_STILL_ACTIVE */
};

/* Canned strings for Protection Group Change */
const char *pg_change_str[] =
{
   "None",
   "No Change",    /* SA_AMF_PROTECTION_GROUP_NO_CHANGE    */
   "Added",        /* SA_AMF_PROTECTION_GROUP_ADDED        */
   "Removed",      /* SA_AMF_PROTECTION_GROUP_REMOVED      */
   "State Change"  /* SA_AMF_PROTECTION_GROUP_STATE_CHANGE */
};



#define CONFD_HA_TASK_PRIORITY   (5)
#define CONFD_HA_STACKSIZE       NCS_STACKSIZE_HUGE

/* Macro to retrieve the AMF version */
#define m_CONFD_HA_VER_GET(ver) \
{ \
   ver.releaseCode = 'B'; \
   ver.majorVersion = 0x01; \
   ver.minorVersion = 0x01; \
};

extern SaUint32T confd_ha_init(void);

extern struct confd_ha_node mynode, othernode;
extern confd_value_t mynodeid, othernodeid;



#endif
/*
Integration glue between ConfD and OpenSAF AMF

Copyright (C) 2009 Tail-f Systems AB

This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version. 

This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details. 

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA

*/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <confd.h>
#include <string.h>

static int debuglevel = CONFD_TRACE;

struct confd_ha_node mynode, othernode;
confd_value_t mynodeid, othernodeid;
static char *confd_dir = NULL;

int confd_ha_amf_init();


/* This program is invoked by the start_confd script which is resonsible */
/* for bringing confd up to phase1. Once we know if we're active or standby */
/* we bring confd up to phase2 */
/* There are several failure scenarios there - for example failing to bind */
/* if confd fails to go to phase 2 - our heartbeat code in this program */
/* will notice that - and we will report health failure */


void confd_phase2() {
    static int done = 0;
    if (done) return;
    char buf[BUFSIZ];
    sprintf(buf, "%s/bin/confd --start-phase2", confd_dir);
    system (buf);
    printf("Brought confd up to phase 2\n");
}


int main(int argc, char **argv)
{
	printf("starting point \n");
    char *rde_cfg = "/etc/opensaf/rde.conf";
    int c;

    while ((c = getopt(argc, argv, "f:tdpsc:")) != -1) {
    	printf("the value of c is %d",c);
        switch(c) {
        case 'f':
            rde_cfg = optarg;
            break;
        case 't':
            debuglevel = CONFD_TRACE;
            break;
        case 'd':
            debuglevel = CONFD_DEBUG;
            break;
        case 'p':
            debuglevel = CONFD_PROTO_TRACE;
            break;
        case 's':
            debuglevel = CONFD_SILENT;
            break;
        case 'c':
            confd_dir = optarg;
            break;
        }
    }

    printf("after while condition \n");
    /* Now parse the rde file - best way I can can think of        */
    /* I'm sure there is a "right" way of doing this in opensaf !! */
    FILE *fp;
    if ((fp = fopen(rde_cfg, "r")) == NULL) {
        printf("No rde cfg : %s \n", rde_cfg);
        exit(1);
    }
    if (confd_dir == NULL) {
        printf("No confd_dir \n");
        exit (1);
    }

    char buf[BUFSIZ];


    /* This code assumes we have a 2N scenario with 2 controllers */
    /* and that the contents of rde.conf looks something like:    */
    /* export CONTROLLER1=192.168.1.73                            */
    /* export CONTROLLER2=192.168.1.76                            */
    /* export RDE_PORT_NUMBER=5003                                */

    int myh = 0;
    int otherh = 0;

    printf("before fgets \n");

    while (fgets(buf, BUFSIZ, fp) != NULL) {

    	printf ("buf = %s, BUFSIZ:%d",buf, BUFSIZ);

//    	char buf1[100];
 //  	strcpy(buf1, buf);


        char *t = strtok(buf, "=");
        printf("inside the while t: \n");
        in_addr_t ipaddr;
        if (strstr(t, "CONTROLLER") != NULL) {

            char *ipstr = strtok(NULL, "=");
            ipstr[strlen(ipstr)-1] = 0;
            printf("Found IP %s \n", ipstr);
            /* Now figure out if we have that IP */
            
            char cmd[BUFSIZ];
            sprintf(cmd, "/sbin/ifconfig | grep addr:%s  > /dev/null", 
                    ipstr);
            int sret = system(cmd);
            if (sret == 0) {
                /* Ip address is on this host - assign global vars */
                CONFD_SET_BUF(&mynodeid, (unsigned char*)strdup(ipstr), 
                              strlen(ipstr));
                mynode.nodeid = mynodeid;
                mynode.af = AF_INET;
                ipaddr = inet_addr(ipstr);;
                mynode.addr.ip4.s_addr = ipaddr;  
                myh++;
            }

            else {
                CONFD_SET_BUF(&othernodeid, (unsigned char*)strdup(ipstr), 
                              strlen(ipstr));
                othernode.nodeid = othernodeid;
                othernode.af = AF_INET;
                ipaddr = inet_addr(ipstr);;
                othernode.addr.ip4.s_addr = ipaddr; 
                otherh++;

            }
        }
    }

    printf(" after 2nd while loop");
    if (myh != 1 || otherh != 1) {
        printf("%s doesn't contain 2 proper IPs ", rde_cfg);
        exit(1);
    }

    confd_pp_value(buf, BUFSIZ, &mynodeid);
    printf("My nodeid = %s \n", buf);
    confd_pp_value(buf, BUFSIZ, &othernodeid);
    printf("Other nodeid = %s \n", buf);

    confd_init("confd_ha", stderr, debuglevel);

    /* Start the AMF thread */ 
    confd_ha_amf_init();

    /* Keep waiting forever */
    while (1)
        sleep(30);
   
    return 0;    
}

_______________________________________________
Opensaf-devel mailing list
Opensaf-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/opensaf-devel

Reply via email to