Hitachi

JP1 Version 12 JP1/Integrated Management 2 - Manager Command, Definition File and API Reference


7.5.4 Sample plugin

Following is a sample plug-in when managing the management tool A by the Intelligent Integrated Management Base.

/*
 * Copyright (C) 2018, Hitachi, Ltd.
 * Copyright (C) 2018, Hitachi Solutions, Ltd.
 * Licensed Material of Hitachi, Ltd.
 * Licensed Material of Hitachi Solutions, Ltd.
 */
 
// Component name of the management tool A
const TOOLA_COMPONENT_NAME = "/HITACHI/TOOLA";
// JavaScript name of the management tool A
const JS_NAME = "toola";
// The type of SID for representing the SID related to host
const TYPE_SID_HOST = "HOST";
// The type of SID for representing the SID related to the server of the management tool A
const TYPE_SID_TOOLA_M = "ToolA-M";
// The type of SID for representing the SID related to the agent of the management tool A
const TYPE_SID_TOOLA_A = "ToolA-A";
// The type of SID for representing the SID related to the management tool A - Manager
const TYPE_SID_TOOLA_MGR = "TOOLAMGR";
// The type of SID for representing the SID related to the management tool A - Agent
const TYPE_SID_TOOLA_AGT = "TOOLAAGT";
// The type of SID for representing the SID related to the platform
const TYPE_SID_PLATFORM = "PLATFORM";
// The type of SID for representing the SID related to the user program
const TYPE_SID_UP = "UP";
 
module.exports = {
 
    /**
     * Convert ToolA configuration information to JSON format.
     *
     * @param {Object} args
     *     args = {
     *       hostname: string,        // Acquired host name
     *       component: string,       // Target component("/HITACHI/TOOLA")
     *       data: string,             // Result of execution of adapter command(UTF-8) (except header)
     *       jp1UserName: string,     // JP1 user name
     *       jp1Token: string,        // JP1 token
     *       protocolName:string,     // Protocol name with adapter command
     *       protocolVersion:string, // Protocol version with adapter command
     *       codeset:string,          // Codeset with adapter command
     *       productName:string      // Product name with adapter command
     *       setResult:function      // Function for normal case
     *       setError:function       // Function for error case
     *     }
     */
    __configurationGet: function(args) {
        logTrace("__configurationGet start");
 
        if (!isValidProductName(args.component)) {
            var msg = "Componentname (" + String(args.component) + ") is invalid.";
            logTrace(msg);
            return;
        }
 
        // Configuration information object in JSON format.
        var configObj = {
            "meta": {
                "timestamp": ""
            },
            "simtData": []
        };
 
        try {
            // adapter command data.
            var adapterCmdDataObj = parseAdapterCmdData(args.data);
            
            // ToolA - Manager host name
            var mgrHostName = encodeURI(adapterCmdDataObj.hostName);
            
            // push SimtData for ToolA - Manager
            pushSimtDataForManagerHost(configObj.simtData, mgrHostName);
            
            // ToolA - Agent host object list
            var agtHostObjList = getAgentHostObjList(adapterCmdDataObj.baseURL, adapterCmdDataObj.uid, adapterCmdDataObj.pwd);
            agtHostObjList.forEach(function(agtHostObj) {
                // push SimtData for ToolA - Agent
                pushSimtDataForAgentHost(configObj.simtData, mgrHostName, agtHostObj);
            });
        } catch(e) {
            var msg = "Exception occurs. message=" + e.message;
            logTrace(msg);
            args.setError(msg);
        }
 
        configObj.meta.timestamp = (new Date()).toISOString();
        args.setResult(JSON.stringify(configObj));
        logTrace("__configurationGet end");
    },
 
    /**
     * Generate sid from JP1 event.
     *
     * @param {Object} args
     *     args = {
     *       productName: string,    // Name of the program that issued the JP1 event.
     *       idBase: number,        // Event ID
     *       event: {value: Object}    // JP1 event attribute information
     *     }
     */
    __eventGet: function(args) {
        logTrace("__eventGet start");
        
        if (!isValidProductName(args.productName)) {
            return;
        }
        
        try {
            var sid = null;
            switch (args.idBase) {
                // generate sid of ToolA - Agent event.
                case 0x7FFF8000:
                    sid = createToolAAgtSidFromEvent(args.event.value);
                    break;
 
                // generate sid of os event.
                case 0x7FFF8001:
                    sid = createPlatformSidFromEvent(args.event.value);
                    break;
 
                // generate sid of user program event.
                case 0x7FFF8002:
                    sid = createUPSidFromEvent(args.event.value);
                    break;
 
                default:
                    logTrace("Unsupported args.idBase=" + args.idBase.toString(16));
                    break;
            }
            
            if (sid !== null) {
                logTrace("args.setTargetSid=" + sid);
                args.setTargetSid(sid);
            }
            
        } catch(e) {
            var msg = "Exception occurs. message=" + e.message;
            logTrace(msg);
            args.setError(msg);
        }
        logTrace("__eventGet end");
    },
 
    __createLink: function(args) {
        logTrace("__createLink start");
        
        logTrace("__createLink end");
    },
 
    _xxxt: function(args) {
        logTrace("_xxxt start");
        
        logTrace("_xxxt end");
    }
};
 
// if debug is true, output debug message.
var isDebug = true;
 
/**
 * Output log message.
 * @param {string} msg message
*/
function logTrace(msg) {
    jp1Logger.trace(JS_NAME, msg);
}
 
/**
 * Output log message.
 * @param {string} msg message
*/
function logDebug(msg) {
    if (!isDebug) {
        return;
    }
    jp1Logger.trace(JS_NAME, msg);
}
 
/**
 * Return whether product name is valid
 * @param {string} productName product name
 * @return {boolean} true if product name is valid
*/
function isValidProductName(productName) {
    return (String(productName) === TOOLA_COMPONENT_NAME);
}
 
/**
 * Parse data of adapter command
 * @param {string} adapterCmdData adapter command data
 * @return {Object} adapterCmdDataObj
 *     adapterCmdDataObj = {
 *       hostname: string,    // Server name
 *       baseURL: string,    // base URL for REST API
 *       uid: string,        // user id for REST API
 *       pwd: string,        // user password for REST API
 *     }
*/
function parseAdapterCmdData(adapterCmdData) {
    return JSON.parse(adapterCmdData);
}
 
/**
 * push simtData of ToolA - Manager
 * @param {Object[]} simtData simt data
 * @param {string} mgrHostName ToolA-Manager host name
 */
function pushSimtDataForManagerHost(simtData, mgrHostName) {
    // create Manager Host SID
    var mgrHostSid = jp1SimtService.join(
        jp1SimtService.packHost(TYPE_SID_TOOLA_M, mgrHostName),
        jp1SimtService.packHost(TYPE_SID_HOST, mgrHostName));
    var mgrHostSidValue = {
        component: TOOLA_COMPONENT_NAME,
        label: mgrHostName
    };
    simtData.push({
        sid: mgrHostSid,
        value: mgrHostSidValue
    });
    
    // create Manager SID
    var mgrSid = jp1SimtService.join(
        jp1SimtService.packHost(TYPE_SID_TOOLA_M, mgrHostName),
        jp1SimtService.packHost(TYPE_SID_HOST, mgrHostName),
        jp1SimtService.pack(TYPE_SID_TOOLA_MGR, ""));
    var mgrSidValue = {
        component: TOOLA_COMPONENT_NAME,
        category: "managementApplications",
        label: "ToolA - Manager"
    };
    simtData.push({
        sid: mgrSid,
        value: mgrSidValue
    });
}
 
/**
 * push simtData of ToolA - Agent
 * @param {Object[]} simtData simt data
 * @param {string} mgrHostName ToolA-Manager host name
 * @param {Object} agtHostObj
 */
function pushSimtDataForAgentHost(simtData, mgrHostName, agtHostObj) {
    // ToolA - Agent host name
    var agtHostName = encodeURI(agtHostObj.hostName);
    
    // create Agent Host SID
    var agtHostSid = jp1SimtService.join(
        jp1SimtService.packHost(TYPE_SID_TOOLA_M, mgrHostName),
        jp1SimtService.packHost(TYPE_SID_TOOLA_A, agtHostName),
        jp1SimtService.packHost(TYPE_SID_HOST, agtHostName));
    var agtHostSidValue = {
        component: TOOLA_COMPONENT_NAME,
        label: agtHostName
    };
    simtData.push({
        sid: agtHostSid,
        value: agtHostSidValue
    });
 
    // create Agent SID
    var agtSid = jp1SimtService.join(
        jp1SimtService.packHost(TYPE_SID_TOOLA_M, mgrHostName),
        jp1SimtService.packHost(TYPE_SID_TOOLA_A, agtHostName),
        jp1SimtService.packHost(TYPE_SID_HOST, agtHostName),
        jp1SimtService.pack(TYPE_SID_TOOLA_AGT, ""));
    var agtSidValue = {
        component: TOOLA_COMPONENT_NAME,
        category: "managementApplications",
        label: "ToolA - Agent"
    };
    simtData.push({
        sid: agtSid,
        value: agtSidValue
    });
 
    // create Platform SID
    var agtSid = jp1SimtService.join(
        jp1SimtService.packHost(TYPE_SID_TOOLA_M, mgrHostName),
        jp1SimtService.packHost(TYPE_SID_TOOLA_A, agtHostName),
        jp1SimtService.packHost(TYPE_SID_HOST, agtHostName),
        jp1SimtService.pack(TYPE_SID_PLATFORM, ""));
    var agtSidValue = {
        component: TOOLA_COMPONENT_NAME,
        category: "platform",
        label: agtHostObj.osName
    };
    simtData.push({
        sid: agtSid,
        value: agtSidValue
    });
 
    // create UserProgram SID
    agtHostObj.upList.forEach(function(upName){
        var agtSid = jp1SimtService.join(
            jp1SimtService.packHost(TYPE_SID_TOOLA_M, mgrHostName),
            jp1SimtService.packHost(TYPE_SID_TOOLA_A, agtHostName),
            jp1SimtService.packHost(TYPE_SID_HOST, agtHostName),
            jp1SimtService.pack(TYPE_SID_UP, upName));
        var agtSidValue = {
            component: TOOLA_COMPONENT_NAME,
            category: "up",        // custom categoryId
            label: upName
        };
        simtData.push({
            sid: agtSid,
            value: agtSidValue
        });
    });
}
 
/**
 * Get ToolA Agent data from ToolA - Manager
 * @param {string} baseURL ToolA-Manager host name
 * @param {string} uid userid for RESTAPI
 * @param {string} pwd password for RESTAPI
 * @return {Object[]} agtHostObjList list of agtHostObj
 *     agtHostObj = {
 *       hostName: string,    // Server name
 *       osName: string,      // OS name
 *       upList: [string]     // user program list
 *     }
 */
function getAgentHostObjList(baseURL, uid, pwd) {
    var agtHostObjList = [];
 
    // make url for authentication
    var fullUrl = baseURL + "/v1/authentication";
    var requestHeaderObj = {"ContentType" : "application/json"};
    var requestBody = JSON.stringify({"Username" : uid, "Password" : pwd});
    
    // call authentication REST API
    logTrace(fullUrl);
    logDebug(JSON.stringify(requestHeaderObj));
    logDebug(requestBody);
 
//    var resultObj = jp1Imdd.callRest(
//        "POST"
//        , fullUrl
//        , requestHeaderObj
//        , requestBody
//    );
//    if (resultObj.response === undefined) {
//        logTrace(JSON.stringify(resultObj));
//        return agtHostObjList;
//    }
    var resultObj = {
        "response": {
            "body": JSON.stringify({"token": "auth_token"})
        }
    };
    
    // authentication result
    var bodyObj = JSON.parse(resultObj.response.body);
    logDebug(JSON.stringify(bodyObj));
    
    // make url for agent configuration
    fullUrl = baseURL + "/v1/devices/list";
    requestHeaderObj = {"ContentType" : "application/json", "X-Authorization" : bodyObj.token};
    requestBody = JSON.stringify({"filter": ""});
    
    // call agent configuration REST API
    logTrace(fullUrl);
    logDebug(JSON.stringify(requestHeaderObj));
    logDebug(requestBody);
 
//    resultObj = jp1Imdd.callRest(
//        "POST"
//        , fullUrl
//        , requestHeaderObj
//        , requestBody
//    );
//    if (resultObj.response === undefined) {
//        logTrace(JSON.stringify(resultObj));
//        return agtHostObjList;
//    }
    resultObj = {
        "response": {
            "body": JSON.stringify({
                "deviceList": [
                {"hostName": "hostB", "osName": "Linux", "upList": ["UP-1"]},
                {"hostName": "hostC", "osName": "Linux", "upList": ["UP-2"]},
                {"hostName": "hostD", "osName": "Windows", "upList": ["UP-3"]},
                {"hostName": "hostE", "osName": "Windows", "upList": ["UP-1", "UP-4"]}]
            })
        }
    };
    
    bodyObj = JSON.parse(resultObj.response.body);
    logDebug(JSON.stringify(bodyObj));
    
    bodyObj.deviceList.forEach(function(deviceObj) {
        agtHostObjList.push({
            "hostName": deviceObj.hostName,
            "osName": deviceObj.osName,
            "upList": deviceObj.upList
        });
    });
    
    return agtHostObjList;
}
 
/**
 * create SID related to the ToolA - Agent from JP1 event
 * @param {Object} eventValue event value
 * @return {string} sid
*/
function createToolAAgtSidFromEvent(eventValue) {
    var mgrHostName = encodeURI(eventValue["B.SOURCESERVER"]);
    var agtHostName = encodeURI(eventValue["E.TOOLA_AGTHOST"]);
 
    return jp1SimtService.join(
        jp1SimtService.packHost(TYPE_SID_TOOLA_M, mgrHostName),
        jp1SimtService.packHost(TYPE_SID_TOOLA_A, agtHostName),
        jp1SimtService.packHost(TYPE_SID_HOST, agtHostName),
        jp1SimtService.pack(TYPE_SID_TOOLA_AGT, ""));
}
 
/**
 * create SID related to the platform from JP1 event
 * @param {Object} eventValue event value
 * @return {string} sid
*/
function createPlatformSidFromEvent(eventValue) {
    var mgrHostName = encodeURI(eventValue["B.SOURCESERVER"]);
    var agtHostName = encodeURI(eventValue["E.TOOLA_AGTHOST"]);
 
    return jp1SimtService.join(
        jp1SimtService.packHost(TYPE_SID_TOOLA_M, mgrHostName),
        jp1SimtService.packHost(TYPE_SID_TOOLA_A, agtHostName),
        jp1SimtService.packHost(TYPE_SID_HOST, agtHostName),
        jp1SimtService.pack(TYPE_SID_PLATFORM, ""));
}
 
/**
 * create SID related to the user program from JP1 event
 * @param {Object} eventValue event value
 * @return {string} sid
*/
function createUPSidFromEvent(eventValue) {
    var mgrHostName = encodeURI(eventValue["B.SOURCESERVER"]);
    var agtHostName = encodeURI(eventValue["E.TOOLA_AGTHOST"]);
    var upName = encodeURI(eventValue["E.UP_NAME"]);
 
    return jp1SimtService.join(
        jp1SimtService.packHost(TYPE_SID_TOOLA_M, mgrHostName),
        jp1SimtService.packHost(TYPE_SID_TOOLA_A, agtHostName),
        jp1SimtService.packHost(TYPE_SID_HOST, agtHostName),
        jp1SimtService.pack(TYPE_SID_UP, upName));
}