Modifying Object State

Modifing the the object(s) state which are in a list Based on another objects state which are also in the another  list
-----------------------------------------------------------------------------------------------------------------------
public void doInventory(String accountName, String cmdKey, Class invPojo,
InventoryContext context, String deleteQuery) throws Exception {
if (deleteQuery == null)
{
deleteQuery = buildDeleteQuery(accountName);
logger.info("Delete Query : " + deleteQuery);
}
String output = execCommand(accountName, cmdKey);
logger.debug("Collected data:  " + output);
InfraAccount infraaccount = InfrastructureDataUtil
.getAccount(accountName);
String dcName = infraaccount.getDcName();
List<Object> volumeInfoList = cli2Pojo(dcName, accountName, cmdKey,
output, invPojo);
if (invPojo == VolumeInfo.class) {
String cmdkey1 = " lshostvdiskmap -delim :";
String output1 = execCommand(accountName, cmdkey1);
logger.debug("Collected data:  " + output);
Class hostVdiskpojo = IBMVolumeHostMappingsInfo.class;
List<Object> mappedVolumesList = cli2Pojo(dcName, accountName,
cmdkey1, output1, hostVdiskpojo);
IBMVolumeHostMappingsInfo ibmVolumeHostMappingsInfo = null;
VolumeInfo volumeInfo = null;
for (Object o : volumeInfoList)
{
VolumeInfo volumeInfo1 = (VolumeInfo) o;
for (Object mappedVolumes1 : mappedVolumesList) {
IBMVolumeHostMappingsInfo v1 = (IBMVolumeHostMappingsInfo) mappedVolumes1;
if (v1.getVdisk_id().equals(volumeInfo1.getId()))
{
volumeInfo1.setHost_mappings("Yes");
volumeInfo1.setHost_name(v1.getName());
continue;
}
System.out.println(volumeInfoList);
}
}
for (Object o : volumeInfoList) {
VolumeInfo volumeInfo2 = (VolumeInfo) o;
if (!("Yes".equals(volumeInfo2.getHost_mappings())))
{
volumeInfo2.setHost_mappings("No");
volumeInfo2.setHost_name("Empty");
}
}
persist(deleteQuery, invPojo, volumeInfoList);

} else
{
System.out.println(volumeInfoList);
persist(deleteQuery, invPojo, volumeInfoList);
}

if (logger != null)
logger.info("Persisted data successfully");
}
=====================================================================
public void createVolume(IBMCreateVolumeConfig ibmVolumeReportActionConfig)throws Exception {

Map<String,String> cmdMap=getCmdMap(ibmVolumeReportActionConfig.getVolumeType(),ibmVolumeReportActionConfig);
try{
String cmd=new String();
cmd=" mkvdisk ";
for (Map.Entry<String, String> entry : cmdMap.entrySet()) {
if(entry.getKey()!=null)
cmd=cmd+entry.getKey()+entry.getValue();
}
System.out.println(cmd);
String resp = execCommand(ibmVolumeReportActionConfig.getAccountName(),cmd );
            InventoryContext log = new InventoryContext();
            doInventory(ibmVolumeReportActionConfig.getAccountName(), log);
         
        } catch (Exception e)
        {
            logger.error("Error while createVolume  - " + e.getMessage());
            throw new Exception(e.getMessage());
        }
}

public static Map getCmdMap(String volumeType,IBMCreateVolumeConfig ibmVolumeReportActionConfig)
 {
Map<String,String> cmdKeyValuePair=new HashMap<String, String>();
// if(("ThinProvision".equals(volumeType))||("Compressed".equals(volumeType))||("CompressedMirror".equals(volumeType)))
    if((IBMAdvancedPropertyConstants.VOLUME_TYPE_THINPROVISION.equals(volumeType))||(IBMAdvancedPropertyConstants.VOLUME_TYPE_COMPRESSED.equals(volumeType))||(IBMAdvancedPropertyConstants.VOLUME_TYPE_COMPRESSEDMIRROR.equals(volumeType)))
{
if(ibmVolumeReportActionConfig.getAutomaticallyExapand())
{
cmdKeyValuePair.put(" -autoexpand", "");
}
}
cmdKeyValuePair.put(" -cache ", "readwrite");
// if(("Compressed".equals(volumeType))||("CompressedMirror".equals(volumeType)))
if((IBMAdvancedPropertyConstants.VOLUME_TYPE_COMPRESSED.equals(volumeType))||(IBMAdvancedPropertyConstants.VOLUME_TYPE_COMPRESSEDMIRROR.equals(volumeType)))
{
cmdKeyValuePair.put(" -compressed", "");
}

// if(!(("Mirror".equals(volumeType))||("ThinMirror".equals(volumeType))||("CompressedMirror".equals(volumeType))))
if(!((IBMAdvancedPropertyConstants.VOLUME_TYPE_MIRROR.equals(volumeType))||(IBMAdvancedPropertyConstants.VOLUME_TYPE_THINMIRROR.equals(volumeType))||(IBMAdvancedPropertyConstants.VOLUME_TYPE_COMPRESSEDMIRROR.equals(volumeType))))
{
cmdKeyValuePair.put(" -copies ", "1");
}
else
{
cmdKeyValuePair.put(" -copies ", "2");
}
// if(("CompressedMirror".equals(volumeType)))
if((IBMAdvancedPropertyConstants.VOLUME_TYPE_COMPRESSEDMIRROR.equals(volumeType)))
{
cmdKeyValuePair.put(" -createsync", "");
}
if (ibmVolumeReportActionConfig.getFormatBeforeUse())
{
cmdKeyValuePair.put(" -fmtdisk", "");
}

if(ibmVolumeReportActionConfig.getThinProvisionedGrainSize()!=null)//thinProvisionedGrainSize//not required
{
// if(("ThinProvision".equals(ibmVolumeReportActionConfig.getVolumeType())))
if((IBMAdvancedPropertyConstants.VOLUME_TYPE_THINPROVISION.equals(ibmVolumeReportActionConfig.getVolumeType())))
{
cmdKeyValuePair.put(" -grainsize ", ibmVolumeReportActionConfig.getThinProvisionedGrainSize());
}
}
cmdKeyValuePair.put(" -iogrp ", "io_grp0");
// if((("Mirror".equals(volumeType))||("ThinMirror".equals(volumeType))||("CompressedMirror".equals(volumeType))))
if(((IBMAdvancedPropertyConstants.VOLUME_TYPE_MIRROR.equals(volumeType))||(IBMAdvancedPropertyConstants.VOLUME_TYPE_THINMIRROR.equals(volumeType))||(IBMAdvancedPropertyConstants.VOLUME_TYPE_COMPRESSEDMIRROR.equals(volumeType))))
{
cmdKeyValuePair.put(" -mdiskgrp ", ibmVolumeReportActionConfig.getPool()+":"+ibmVolumeReportActionConfig.getSecondaryPool());//mirror and thin mirror secondary pool
}
else
{
cmdKeyValuePair.put(" -mdiskgrp ", ibmVolumeReportActionConfig.getPool());
}

cmdKeyValuePair.put(" -name ", ibmVolumeReportActionConfig.getName());
if(!"Automatic".equals(ibmVolumeReportActionConfig.getPreferredNode()))//automatic
{
cmdKeyValuePair.put(" -node ", ibmVolumeReportActionConfig.getPreferredNode());//optional in genric
}

cmdKeyValuePair.put(" -size ",ibmVolumeReportActionConfig.getCapacity().toString());
cmdKeyValuePair.put(" -syncrate ",ibmVolumeReportActionConfig.getSyncRate());

if(ibmVolumeReportActionConfig.getUnits().toLowerCase().equals("Bytes"))
{cmdKeyValuePair.put(" -unit ", "-b");}//kb,mb,gb,tb required ,for command no -b
else
{cmdKeyValuePair.put(" -unit ", ibmVolumeReportActionConfig.getUnits().toLowerCase());}//kb,mb,gb,tb required ,for command no -b

cmdKeyValuePair.put(" -vtype ", "striped");

if(ibmVolumeReportActionConfig.getWarningThresold())
{
cmdKeyValuePair.put(" -warning ",ibmVolumeReportActionConfig.getWarningThresoldUnits()+"%");//-warning 80%
}

// if(("ThinProvision".equals(volumeType))||("Compressed".equals(volumeType))||("CompressedMirror".equals(volumeType)))
if((IBMAdvancedPropertyConstants.VOLUME_TYPE_THINPROVISION.equals(volumeType))||(IBMAdvancedPropertyConstants.VOLUME_TYPE_COMPRESSED.equals(volumeType))||(IBMAdvancedPropertyConstants.VOLUME_TYPE_COMPRESSEDMIRROR.equals(volumeType)))
{
if(("TB".equals(ibmVolumeReportActionConfig.getRealCapacityUnits()))||("GB".equals(ibmVolumeReportActionConfig.getRealCapacityUnits())||("MB".equals(ibmVolumeReportActionConfig.getRealCapacityUnits())||("KB".equals(ibmVolumeReportActionConfig.getRealCapacityUnits())||("Bytes".equals(ibmVolumeReportActionConfig.getRealCapacityUnits()))))))
{
cmdKeyValuePair.put(" -rsize ", ibmVolumeReportActionConfig.getRealCapacity());
}
else
{
cmdKeyValuePair.put(" -rsize ", ibmVolumeReportActionConfig.getRealCapacity()+"%");
}
}

return cmdKeyValuePair;
 }
===================================================================
public static String execCommand(String accountName, String cmdKey) throws Exception{
String output=null;
IBMInfraAccount acct = null;
IBMSSHConnector bdc = new IBMSSHConnector();
DefaultNodeId node = new DefaultNodeId("ibmst");
CredentialStore<IBMInfraAccount> store = CredentialStore.getStore(IBMInfraAccount.class);
try {
acct = store.getCredential(accountName);


DeviceCredential dc = new DeviceCredential();
dc.setDeviceIp(acct.getServer());
dc.setLogin(acct.getUsername());
dc.setProtocol(acct.getProtocol());
dc.setPort(""+acct.getPort());
dc.setPassword(acct.getPassword());
bdc.connect(dc, null);
DefaultItem item1 = new DefaultItem(cmdKey, cmdKey, null);
ItemResponse resp = bdc.getItem(node, item1);
output = resp.getCollectedData();
if (output.indexOf("CMMVC") != -1){
throw new Exception(output);
}
} catch (Exception e) {
// TODO Auto-generated catch block
throw e;

//TBI - Error code to be reposted in exceptions
}finally{
bdc.disconnect();
}
return output;
}====================================================
public static List<Object> cli2Pojo(String dcName,String accountName,String cmd, String text,Class pojo) throws Exception //text is the output of raw CLI output,target data object
{

int count = 1;
ArrayList cliList ;
// getCLIListWithSpaceDelim
if(" lsnodecanister -delim  '!'".equals(cmd))
{
  cliList = IBMCLIUtil.getCLIListWithSpaceDelim(cmd, text);
}
else
{
  cliList = IBMCLIUtil.getCLIList(cmd, text);
}
ArrayList headers = null;
ArrayList valuesList = new ArrayList();
ArrayList returnValueObjs = new ArrayList();

for (Object object : cliList)
{
if (count == 1)
{
headers = new ArrayList();
headers.addAll((ArrayList) object);
count++;
}
else if (count > 1)
{
valuesList.add(object);
count++;
}
}
Method[] methods = pojo.getMethods();
//ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
Iterator listofvalues = valuesList.iterator();
while (listofvalues.hasNext())
{
ArrayList rowList = (ArrayList) listofvalues.next();
//Class myClass = myClassLoader.loadClass(pojo.toString());
Object whatInstance = pojo.newInstance();// object of that class
whatInstance.getClass().getMethod("setDcName", new Class[]{String.class}).invoke(whatInstance, new Object[] {dcName });
whatInstance.getClass().getMethod("setAccountName", new Class[]{String.class}).invoke(whatInstance, new Object[] {accountName });
for (int counter = 0; counter < headers.size(); counter++)
{
Object column = headers.get(counter);
for (Method method : methods)
{
if (("set" + column).equalsIgnoreCase(method.getName())) //
{
method.invoke(whatInstance, new Object[] { rowList.get(counter) });//
}
}
}
returnValueObjs.add(whatInstance);
}
System.out.println();
return returnValueObjs;
}


No comments:

Post a Comment