#!/bin/bash
echo $* >>/var/tmp/capmc.log

first_nid=1
last_nid=22000


if [ $# -lt 1 ] ; then
echo 'no argument'
exit 1
fi

if [ "$1" = "get_power_cap_capabilities" ] ; then
echo '{
    "groups":[
        {
            "name":"01:000d:306e:0082:000a:0020:3a34:8300",
            "desc":"ComputeANC_IVB_130W_10c_32GB_14900_IntelKNCAccel",
            "supply":425,
            "host_limit_min":100,
            "host_limit_max":200,
            "static":0,
            "powerup":120,
            "controls":[
                {
                    "name":"accel",
                    "desc":"Accelerator control",
                    "min":220,
                    "max":260
                },
                {
                    "name":"node",
                    "desc":"Node manager control",
                    "min":320,
                    "max":460
                }
            ],
            "nids":[
                44,
                45,
                46,
                47,
                60,
                61,
                62,
                63
            ]
        },
        {
            "name":"01:000d:306e:00e6:0014:0040:3a34:0000",
            "desc":"ComputeANC_IVB_230W_20c_64GB_14900_NoAccel",
            "supply":425,
            "host_limit_min":200,
            "host_limit_max":350,
            "static":0,
            "powerup":150,
            "controls":[{
                    "name":"node",
                    "desc":"Node manager control",
                    "min":200,
                    "max":350
                }],
            "nids":[
                52,
                53,
                54,
                55
            ]
        },
        {
            "name":"01:000d:206d:0073:0008:0020:3a34:8100",
            "desc":"ComputeANC_SNB_115W_8c_32GB_14900_KeplerK20XAccel",
            "supply":425,
            "host_limit_min":95,
            "host_limit_max":185,
            "static":0,
            "powerup":120,
            "controls":[
                {
                    "name":"accel",
                    "desc":"Accelerator control",
                    "min":180,
                    "max":225
                },
                {
                    "name":"node",
                    "desc":"Node manager control",
                    "min":275,
                    "max":410
                }
            ],
            "nids":[
                4,
                5,
                6,
                7
            ]
        },
        {
            "name":"01:000d:206d:00e6:0010:0040:3200:0000",
            "desc":"ComputeANC_SNB_230W_16c_64GB_12800_NoAccel",
            "supply":425,
            "host_limit_min":200,
            "host_limit_max":350,
            "static":0,
            "powerup":150,
            "controls":[{
                    "name":"node",
                    "desc":"Node manager control",
                    "min":200,
                    "max":350
                }],
            "nids":[
                32,
                33,
                34,
                35,
                48,
                49,
                50,
                51
            ]
        },
        {
            "name":"01:000d:206d:00e6:0010:0020:3200:0000",
            "desc":"ComputeANC_SNB_230W_16c_32GB_12800_NoAccel",
            "supply":425,
            "host_limit_min":200,
            "host_limit_max":350,
            "static":0,
            "powerup":150,
            "controls":[{
                    "name":"node",
                    "desc":"Node manager control",
                    "min":200,
                    "max":350
                }],
            "nids":[
                36,
                37,
                38,
                39,
                40,
                41,
                42,
                43
            ]
        },
        {
            "name":"01:000d:306e:00e6:0018:0020:3200:0000",
            "desc":"ComputeANC_IVB_230W_24c_32GB_12800_NoAccel",
            "supply":425,
            "host_limit_min":200,
            "host_limit_max":350,
            "static":0,
            "powerup":150,
            "controls":[{
                    "name":"node",
                    "desc":"Node manager control",
                    "min":200,
                    "max":350
                }],
            "nids":[
                56,
                57,
                58,
                59
            ]
        },
        {
            "name":"01:000a:206d:0073:0008:0020:3200:0000",
            "desc":"Service_SNB_115W_8c_32GB_12800_NoAccel",
            "supply":425,
            "host_limit_min":95,
            "host_limit_max":185,
            "static":0,
            "powerup":120,
            "controls":[{
                    "name":"node",
                    "desc":"Node manager control",
                    "min":95,
                    "max":185
                }],
            "nids":[
                1,
                2,
                3,
                10,
                13,
                14,
                17,
                18,
                21,
                22,
                25,
                26,
                29,
                30
            ]
        }
    ],
    "e":0,
    "err_msg":""
}'
exit 0
fi

if [ "$1" = "get_node_energy_counter" ] ; then
echo '{
    "e": 0,
    "err_msg": "",
    "nid_count": 3,
    "nodes": [
        {
            "energy_ctr": 4761800,
            "nid": 1,
            "time": "2015-02-19 15:50:18.581552-06"
        },
        {
            "energy_ctr": 4821800,
            "nid": 2,
            "time": "2015-02-19 15:50:18.581652-06"
        },
        {
            "energy_ctr": 4821800,
            "nid": 3,
            "time": "2015-02-19 15:50:19.981752-06"
        }
    ]
}'
exit 0
fi

if [ "$1" = "node_off" ] ; then
echo '{
    "e":0,
    "err_msg":"uccess"
}'
exit 0
fi

if [ "$1" = "node_offx" ] ; then
echo '{
    "e":22,
    "err_msg":"Invalid nids"

}'
exit 0
fi

if [ "$1" = "node_on" ] ; then
echo '{
    "e":0,
    "err_msg":"Success"
}'
exit 0
fi

if [ "$1" = "node_onx" ] ; then
echo '{
    "e":22,
    "err_msg":"Invalid nids"

}'
exit 0
fi

if [ "$1" = "node_reinit" ] ; then
sleep 5
echo '{
    "e":0,
    "err_msg":"Success"
}'
exit 0
fi

if [ "$1" = "node_reinitx" ] ; then
echo '{
    "e":22,
    "err_msg":"Invalid nids"

}'
exit 0
fi

if [ "$1" = "node_status" ] ; then
echo '{
    "e": 0,
    "err_msg": "",
    "off": [
        1,
        2,
        3,
        14,
        15
    ],
    "on": [
        1,
        2,
        3,
        4,
        5,
	6
    ],
    "ready": [
        16,
        27,
        18
    ]
}'
exit 0
fi

if [ "$1" = "get_power_cap" ]; then
echo '{
    "e":0,
    "err_msg":"",
    "nids":[
	{
            "nid":1,
            "controls":[
                {
                    "name":"accel",
                    "val":101
                },
                {
                    "name":"node",
                    "val":101
                }
            ]
        },
	{
            "nid":2,
            "controls":[
                {
                    "name":"node",
                    "val":102
                },
                {
                    "name":"accel",
                    "val":102
                }
            ]
        },
	{
            "nid":3,
            "controls":[
                {
                    "name":"node",
                    "val":103
                }
            ]
        },
	{
            "nid":4,
            "controls":[
                {
                    "name":"node",
                    "val":304
                },
                {
                    "name":"node-biased",
                    "val":411
                },
                {
                    "name":"bias-factor",
                    "val":1.0
                },
                {
                    "name":"accel",
                    "val":230
                }
            ]
        }
     ]
}'
exit 0
fi

if [ "$1" = "set_power_cap" ]; then
echo '{
    "e":0,
    "err_msg":""
}'
exit 0
fi

if [ "$1" = "get_mcdram_capabilities" ] ; then
echo '{
    "nids": ['
inx=$first_nid
while [ $inx -lt $last_nid ]
do
	echo "        {
            \"mcdram_cfg\": \"flat,0,split,25,equal,50,cache,100\",
            \"nid\": $inx
        },"
	inx=$((inx+1))
done
	echo "        {
            \"mcdram_cfg\": \"flat,0,split,25,equal,50,cache,100\",
            \"nid\": $inx
        } "
echo '    ],
    "err_msg": "Success; nids not eligible for numa operations were ignored",
    "e": 0
}'
exit 0
fi

if [ "$1" = "get_mcdram_capabilitiesx" ] ; then
echo '{
    "nids": [
        {
            "mcdram_cfg": "flat,0,split,25,equal,50,cache,100",
            "nid": 1
        },
        {
            "mcdram_cfg": "flat,0,split,25,equal,50,cache,100",
            "nid": 2
        },
        {
            "mcdram_cfg": "flat,0,split,25,equal,50,cache,100",
            "nid": 3
        }
    ],
    "err_msg": "Success; nids not eligible for mcdram operations were ignored",
    "e": 0
}'
exit 0
fi

if [ "$1" = "get_mcdram_cfg" ]; then
echo '{
    "nids": ['
inx=$first_nid
while [ $inx -lt $last_nid ]
do
	echo "        {
            \"mcdram_cfg\": \"flat\",
            \"mcdram_pct\": \"0\",
            \"mcdram_size\": \"8192MB\",
            \"nid\": $inx,
            \"dram_size\": \"96GB\"
        },"
	inx=$((inx+1))
done
	echo "        {
            \"mcdram_cfg\": \"flat\",
            \"mcdram_pct\": \"0\",
            \"mcdram_size\": \"8192MB\",
            \"nid\": $inx,
            \"dram_size\": \"96GB\"
        } "
echo '    ],
    "err_msg": "Success; nids not eligible for numa operations were ignored",
    "e": 0
}'
exit 0
fi

if [ "$1" = "get_mcdram_cfgx" ]; then
echo '{
    "nids": [
        {
            "mcdram_cfg": "flat",
            "mcdram_pct": "0",
            "mcdram_size": "8192MB",
            "nid": 1,
            "dram_size": "96GB"
        },
        {
            "mcdram_cfg": "flat",
            "mcdram_pct": "0",
            "mcdram_size": "8192MB",
            "nid": 2,
            "dram_size": "96GB"
        },
        {
            "mcdram_cfg": "flat",
            "mcdram_pct": "0",
            "mcdram_size": "8192MB",
            "nid": 3,
            "dram_size": "96GB"
        }
    ],
    "err_msg": "Success; nids not eligible for mcdram operations were ignored",
    "e": 0
}'
exit 0
fi

if [ "$1" = "get_numa_capabilities" ]; then
echo '{
    "nids": ['
inx=$first_nid
while [ $inx -lt $last_nid ]
do
	echo "        {
            \"numa_cfg\": \"a2a,snc2,snc4,hemi,quad\",
            \"nid\": $inx
        },"
	inx=$((inx+1))
done
	echo "        {
            \"numa_cfg\": \"a2a,snc2,snc4,hemi,quad\",
            \"nid\": $inx
        } "
echo '    ],
    "err_msg": "Success; nids not eligible for numa operations were ignored",
    "e": 0
}'
exit 0
fi

if [ "$1" = "get_numa_capabilitiesx" ]; then
echo '{
    "nids": [
        {
            "numa_cfg": "a2a,snc2,snc4,hemi,quad",
            "nid": 1
        },
        {
            "numa_cfg": "a2a,snc2,snc4,hemi,quad",
            "nid": 2
        },
        {
            "numa_cfg": "a2a,snc2,snc4,hemi,quad",
            "nid": 3
        }
    ],
    "err_msg": "Success; nids not eligible for numa operations were ignored",
    "e": 0
}'
exit 0
fi

if [ "$1" = "get_numa_cfg" ]; then
echo '{
    "nids": ['
inx=$first_nid
while [ $inx -lt $last_nid ]
do
	echo "        {
            \"numa_cfg\": \"a2a\",
            \"nid\": $inx
        },"
	inx=$((inx+1))
done
	echo "        {
            \"numa_cfg\": \"a2a\",
            \"nid\": $inx
        } "
echo '    ],
    "err_msg": "Success; nids not eligible for numa operations were ignored",
    "e": 0
}'
exit 0
fi

if [ "$1" = "get_numa_cfgx" ]; then
echo '{
    "nids": [
        {
            "numa_cfg": "a2a",
            "nid": 1
        },
        {
            "numa_cfg": "a2a",
            "nid": 2
        },
        {
            "numa_cfg": "a2a",
            "nid": 3
        },
    ],
    "err_msg": "Success; nids not eligible for numa operations were ignored",
    "e": 0
}'
exit 0
fi

if [ "$1" = "json" ]; then
echo '{
    "e":0,
    "err_msg":""
}'
exit 0
fi

# DEFAULT
echo '{
    "e":0,
    "err_msg":"Success"
}'
exit 0
