用來建立和啟動 FlashCopy 對映的範例程式碼
本資訊示範 CIMOM 方法如何控制系統。範例程式碼包含來自 Java™ 類別且設計來建立和啟動 FlashCopy® 對映的主要方法,以及從主要方法所呼叫的其他方法。
在本主題中,術語「方法」(method) 指 Java 方法。術語「方法」(Method,首字母大寫)指 CIM 方法。
Java 主要方法
本範例顯示用來建立和啟動 FlashCopy 對映的 Java 主要方法。本範例會假設 Java 程式是設計成每次都控制同一個系統。這是相對簡單的程序,以便有更多的彈性,但是具體決定由使用者負責。
/*
* FC Mapping states
*/
private static UnsignedInt16 INITIALIZED = new UnsignedInt16(2);
private static UnsignedInt16 PREPARING = new UnsignedInt16(3);
private static UnsignedInt16 PREPARED = new UnsignedInt16(4);
public static void main(String[] args) throws CIMException
{
/*
* First step is to connect to the CIMOM
*/
UserPrincipal user = new UserPrincipal("superuser");
PasswordCredential pwd = new PasswordCredential("itso13sj");
CIMNameSpace ns = new CIMNameSpace("https://9.43.86.115:5989/root/ibm");
CIMClient client = null;
client = new CIMClient(ns,user,pwd);
/*
* Next, select the clustered system that we are interested in
*/
CIMInstance chosenCluster = getCluster("ITSOCL1",client);
/*
* At this point, the relevant clustered system has been selected
* and 'chosenCluster' is a CIMInstance of this clustered system
*
* Get the Config Service of this clustered system
*/
CIMObjectPath cService = getConfigService(chosenCluster, client);
/*
* Now, get all of the Volumes in this clustered system
*/
Map<Integer,CIMObjectPath> volumesById = getVolumes(chosenCluster,client);
/*
* Select the FlashCopy Source
*
* In this case, Volume 10 is our source
* Volume 11 is our target
*/
CIMObjectPath fcSrc = volumesById.get(new Integer(10));
CIMObjectPath fcTgt = volumesById.get(new Integer(11));/*
/*
* Now create FC Mapping
*/
CIMValue rc = makeFlashCopyMapping("CIMOMTestMap", fcSrc, fcTgt, cService,
client,false);
/*
* Now that this has been created, we need to get an
* Object Path to the newly created Association
*/
List<CIMObjectPath> fcMaps = getFCMappings(fcSrc, client);
CIMObjectPath fcMapping = fcMaps.get(0);
/*
* Now we prepare the FC Mapping
*/
CIMArgument[] outArgs = new CIMArgument[2];
rc = prepareFCMapping(cService, fcMapping, client, outArgs);
System.out.println("Got value:"+
Integer.toHexString(Integer.parseInt(rc.toString())));
/*
* Loop until it is prepared
*/
CIMValue fcMapState = new CIMValue(PREPARING);
while(fcMapState.equals(new CIMValue(PREPARING)))
{
CIMInstance fcMapInfo = client.getInstance(fcMapping);
fcMapState = fcMapInfo.getProperty("SyncState").getValue();
}
/*
* Now start the FC Mapping
*/
rc = startFCMapping(cService, fcMapping, client, outArgs);
System.out.println("Got value:"+
Integer.toHexString(Integer.parseInt(rc.toString())));
}getCluster 方法
getCluster 方法會傳回對應至使用所提供名稱之系統的 CIM 實例。其作法是列舉 IBMTSSVC_Cluster 類別的所有實例,然後檢查每個實例的名稱。一旦找到符合所提供名稱的實例時,會傳回該實例的物件路徑。
static private CIMInstance getCluster(String clusterName, CIMClient client) throws
CIMException
{
CIMInstance chosenCluster = null;
Enumeration<CIMInstance> clusters =
client.enumerateInstances(new CIMObjectPath("/root/ibm:IBMTSSVC_Cluster"));
while(clusters.hasMoreElements())
{
CIMInstance possibleCluster = clusters.nextElement();
String possibleName =
possibleCluster.getProperty("ElementName").getValue().toString();
if(possibleName.equals("\""+clusterName+"\""))
{
chosenCluster = possibleCluster;
}
}
return chosenCluster;
}
getConfigService 方法
CIM_StorageConfigurationService 類別在系統中沒有直接對等項目,但是當對系統呼叫方法時,卻需要這個類別的實例。
在這個方法中,需要與所提供的系統相關聯的所有實例。使系統得以連接至其配置服務的關聯是 CIM_HostedService。由於系統只有配置服務與其相關聯,因而選取了列舉中的第一個物件路徑。
static private CIMObjectPath getConfigService(CIMInstance cluster, CIMClient
client) throws CIMException
{
Enumeration<CIMObjectPath> configServices = null;
configServices = client.associatorNames(
cluster.getObjectPath(),
"CIM_HostedService",
"CIM_StorageConfigurationService",
null,
null);
return configServices.nextElement();
}getVolumes 方法
這個方法會傳回一項對映,這項對映建立了磁區 ID(整數)與 IBMTSSVC_StorageVolume 物件路徑的關係。此方法會要求與所提供之叢集系統實例相關聯的所有 IBMTSSVC_StorageVolume 實例。
static private Map<Integer,CIMObjectPath> getVolumes(CIMInstance cluster, CIMClient
client) throws CIMException
{
Enumeration<CIMObjectPath> volumes = client.associatorNames(
cluster.getObjectPath(),
null,
"IBMTSSVC_StorageVolume",
null,
null);
Map<Integer,CIMObjectPath> volumesById = new HashMap<Integer, CIMObjectPath>();
while(volumes.hasMoreElements())
{
CIMObjectPath volumeOP = volumes.nextElement();
CIMValue volumesId = volumeOP.getKey("DeviceID").getValue();
String idAsString = volumeId.toString();
String idNoQuotes = idAsString.substring(1, idAsString.length()-1);
volumesById.put(Integer.parseInt(idNoQuotes), volumeOP);
}
return volumesById;
}makeFlashCopyMapping 方法
這個範例會針對系統配置服務呼叫 AttachReplica。CIM 方法會採用類型化參數;此方法會使用 argRef、argString 和 argUint16 方法。這些方法可當作為「CIM 方法」產生必要引數時的捷徑。AttachReplica 方法用於 FlashCopy、Metro Mirror 和 Global Mirror。CopyType 引數指出所需的類型。
static private CIMValue makeFlashCopyMapping(
String name,
CIMObjectPath source,
CIMObjectPath target,
CIMObjectPath configService,
CIMClient client,
boolean autodelete) throws CIMException
{
CIMArgument src = argRef("SourceElement", source, "IBMTSSVC_StorageVolume");
CIMArgument tgt = argRef("TargetElement", target, "IBMTSSVC_StorageVolume");
CIMArgument fcName = argString("ElementName",name);
CIMArgument type = argUint16("CopyType",autodelete?5:4);
CIMArgument[] inArgs = {src,tgt,fcName,type};
CIMArgument[] outArgs = new CIMArgument[1];
CIMValue rc = client.invokeMethod(configService,
"AttachReplica",
inArgs,
outArgs);
return rc;
}getFCMappings 方法
getFCMappings 方法會傳回與所提供磁區相關聯的所有 FCMapping 清單。這個方法會要求一份參照所提供 IBMTSSVC_StorageVolume 的所有關聯的清單。目前,此類型的所有 Java WBEM 服務方法都會傳回列舉;此方法會產生清單以便於使用。
static private List<CIMObjectPath> getFCMappings(CIMObjectPath volume, CIMClient
client) throws CIMException
{
Enumeration<CIMObjectPath> assocs = client.referenceNames(
volume,
"IBMTSSVC_LocalStorageSynchronized",
null);
return Collections.list(assocs);
}prepareFCMapping 方法
prepareFCMapping 方法會準備 FlashCopy 對映。相當類似於 AttachReplica 方法,ModifySynchronization 方法控制 FlashCopy、Metro Mirror 和 Global Mirror。operation 參數指出要執行的作業。
private static CIMValue prepareFCMapping(
CIMObjectPath configService,
CIMObjectPath fcMapping,
CIMClient client,
CIMArgument[] outArgs) throws CIMException
{
CIMArgument operation = argUint16("Operation", 6);
CIMArgument synch = argRef("Synchronization",
fcMapping,"IBMTSSVC_FlashCopyStorageSynchronized");
CIMArgument[] inArgs = new CIMArgument[]{operation,synch};
outArgs = new CIMArgument[2];
return client.invokeMethod(configService,
"ModifySynchronization",
inArgs,
outArgs);startFCMapping 方法
startFCMapping 方法會啟動 FlashCopy 對映。這個方法如同 prepareFCMapping 方法 中一樣,會呼叫 ModifySynchronization 方法,但是使用的是不同的 operation 參數。
private static CIMValue startFCMapping(
CIMObjectPath configService,
CIMObjectPath fcMapping,
CIMClient client,
CIMArgument[] outArgs) throws CIMException
{
CIMArgument operation = argUint16("Operation", 4);
CIMArgument synch = argRef("Synchronization",
fcMapping,"IBMTSSVC_FlashCopyStorageSynchronized");
CIMArgument[] inArgs = new CIMArgument[]{operation,synch};
outArgs = new CIMArgument[2];
return client.invokeMethod(configService,
"ModifySynchronization",
inArgs,
outArgs);
}引數產生器類別
這個類別會使用下列的引數產生器: