/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.imperius.cimspl.client;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import org.sblim.wbem.cim.CIMDataType;
import org.sblim.wbem.cim.CIMException;
import org.sblim.wbem.cim.CIMInstance;
import org.sblim.wbem.cim.CIMNameSpace;
import org.sblim.wbem.cim.CIMObjectPath;
import org.sblim.wbem.cim.CIMProperty;
import org.sblim.wbem.cim.CIMValue;
import org.sblim.wbem.client.CIMClient;
import org.sblim.wbem.client.PasswordCredential;
import org.sblim.wbem.client.UserPrincipal;
public class TestClient
{
public static final int INVALID_OPERATION = -1;
public static final int LIST_POLICY = 0;
public static final int EXECUTE_POLICY = 1;
public static final int CREATE_POLICY = 2;
public static final int DELETE_POLICY = 3;
public static final int MODIFY_POLICY = 4;
public static final int LISTALLNAMES_POLICY = 5;
public static final int LISTALL_POLICY = 6;
private static String USER = "";
private static String PWD = "";
private static int PORT = 5988;
private static String SERVER = "localhost";
private static String NAMESPACE = "root/cimv2";
private static String POLICY_NAME;
private static String POLICY_PATH;
private CIMClient _cimClient;
public TestClient() throws CIMException
{
_establishConnection();
}
public TestClient(String user, String pwd,
int port, String server, String ns,
int operation) throws CIMException
{
_establishConnection();
}
/**
* @param args
*/
public static void main(String[] args)
{
int operation = _getInputs(args);
TestClient ptc = null;
try
{
try
{
ptc = new TestClient(USER,PWD,PORT,SERVER,NAMESPACE,operation);
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
System.exit(0);
}
switch (operation)
{
case CREATE_POLICY :
{
System.out.println("create policy :" + POLICY_NAME
+ " path " + POLICY_PATH);
ptc.createPolicy(POLICY_PATH, POLICY_NAME);
}
break;
case LIST_POLICY :
{
System.out.println("list policy" + POLICY_NAME);
ptc.listPolicy(POLICY_NAME);
}
break;
case MODIFY_POLICY :
{
System.out.println("modify policy" + POLICY_NAME
+ " path " + POLICY_PATH);
ptc.modifyPolicy(POLICY_PATH, POLICY_NAME);
}
break;
case LISTALL_POLICY :
{
ptc.listAllPolicies();
System.out.println("list all policies" );
}
break;
case LISTALLNAMES_POLICY :
{
ptc.listAllPolicyNames();
System.out.println("list all names");
}
break;
case DELETE_POLICY :
{
System.out.println("delete policy" + POLICY_NAME);
ptc.deletePolicy(POLICY_NAME);
}
break;
case EXECUTE_POLICY :
{
System.out.println("exec policy" + POLICY_NAME);
ptc.executePolicy(POLICY_NAME);
}
break;
case INVALID_OPERATION :
_throwUsage();
break;
}
ptc.shutdown();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
/*finally
{
ptc.shutdown();
}*/
}
public void shutdown()
{
try
{
if(_cimClient != null)
{
_cimClient.close();
}
}
catch(Exception e)
{
System.err.println("Exception closing cimclient connection");
}
}
public void executePolicy(String policyName) throws CIMException
{
CIMValue result = null;
CIMObjectPath cimObjectPath = new CIMObjectPath("CIM_SPLPolicyRule");
CIMNameSpace clientNameSpace = _cimClient.getNameSpace();
cimObjectPath.setNameSpace(clientNameSpace.getNameSpace());
cimObjectPath.setHost(clientNameSpace.getHost());
System.out.println("cimObjectPath : "+cimObjectPath.toString());
CIMDataType strType = new CIMDataType(CIMDataType.STRING);
CIMValue policyNameCimValue = new CIMValue (policyName,strType);
cimObjectPath.addKey("PolicyRuleName", policyNameCimValue);
if(_cimClient.getInstance(cimObjectPath, true, true, true, null) != null)
{
System.out.println("before invoke method, cimObjectPath : "+cimObjectPath.toString());
result = _cimClient.invokeMethod(cimObjectPath, "evaluatePolicy",
(Vector)null, (Vector)null);
// result = _cimClient.invokeMethod(cimObjectPath, "evaluatePolicy",
// new Vector(), new Vector());
System.out.println("Evaluated Policy with result " + result.toString());
}
else
{
System.err.println("Instance does not exist" + cimObjectPath);
}
}
public void deletePolicy(String policyName) throws CIMException
{
CIMObjectPath cimObjectPath = new CIMObjectPath("CIM_SPLPolicyRule");
CIMNameSpace clientNameSpace = _cimClient.getNameSpace();
cimObjectPath.setNameSpace(clientNameSpace.getNameSpace());
cimObjectPath.setHost(clientNameSpace.getHost());
CIMDataType strType = new CIMDataType(CIMDataType.STRING);
CIMValue policyNameCimValue = new CIMValue (policyName,strType);
cimObjectPath.addKey("PolicyRuleName", policyNameCimValue);
_cimClient.deleteInstance(cimObjectPath);
}
public void listAllPolicyNames() throws CIMException
{
CIMObjectPath cop = new CIMObjectPath("CIM_SPLPolicyRule");
cop.setNameSpace("root/cimv2");
Enumeration enm = _cimClient.enumerateInstanceNames(cop);
while (enm.hasMoreElements ())
{
CIMObjectPath cimObjectPath = (CIMObjectPath)enm.nextElement ();
System.out.println("----------------------------");
System.out.println("Policy : " + cimObjectPath);
System.out.println("----------------------------");
}
}
public void listAllPolicies() throws CIMException
{
CIMObjectPath cop = new CIMObjectPath("CIM_SPLPolicyRule");
cop.setNameSpace("root/cimv2");
Enumeration enm = _cimClient.enumerateInstances(cop, true, true, true, true, null);
while (enm.hasMoreElements ())
{
CIMInstance cimInstance = (CIMInstance)enm.nextElement ();
CIMProperty cimProperty = cimInstance.getProperty("PolicyString");
CIMValue policyStringCimValue = cimProperty.getValue();
String policyString = policyStringCimValue.toString();
CIMProperty cimProperty2 = cimInstance.getProperty("PolicyRuleName");
CIMValue policyRuleCimValue = cimProperty2.getValue();
String policyName = policyRuleCimValue.toString();
System.out.println("----------------------------");
System.out.println("Policy Name: " + policyName);
System.out.println("Policy String: " + policyString);
System.out.println("----------------------------");
}
}
public void modifyPolicy(String policyPath, String policyName) throws CIMException
{
String policyString = _readPolicyString(policyPath,policyName);
boolean localOnly = true;
Vector kvp = new Vector ();
CIMDataType strType = new CIMDataType(CIMDataType.STRING);
CIMValue policyStringCimValue = new CIMValue (policyString,strType);
kvp.addElement (new CIMProperty ("PolicyRuleName", new CIMValue (policyName,strType)));
CIMObjectPath cop = new CIMObjectPath ("CIM_SPLPolicyRule", kvp);
cop.setNameSpace ("root/cimv2");
CIMInstance cimInstance = _cimClient.getInstance(cop, localOnly);
cimInstance.setProperty("PolicyString", policyStringCimValue);
_cimClient.setInstance(cop, cimInstance, true, null);
}
public String listPolicy(String policyName) throws CIMException
{
boolean localOnly = true;
CIMObjectPath cop=new CIMObjectPath("CIM_SPLPolicyRule","root/cimv2");
CIMDataType strType = new CIMDataType(CIMDataType.STRING);
CIMValue policyNameCimValue = new CIMValue (policyName,strType);
cop.addKey("PolicyRuleName", policyNameCimValue);
CIMInstance cimInstance = _cimClient.getInstance(cop, localOnly);
CIMProperty cimProperty = cimInstance.getProperty("PolicyString");
CIMValue policyStringCimValue = cimProperty.getValue();
String policyString = policyStringCimValue.toString();
System.out.println("Policy String: " + policyString);
return policyString;
}
public void createPolicy(String policyPath, String policyName) throws CIMException
{
System.out.println("createPolicy** : "+policyPath+" "+policyName) ;
String policyString = _readPolicyString(policyPath,policyName);
Vector kvp = new Vector ();
CIMDataType strType = new CIMDataType(CIMDataType.STRING);
CIMValue policyNameCimValue = new CIMValue (policyName,strType);
CIMValue policyStringCimValue = new CIMValue(policyString,strType);
kvp.addElement (new CIMProperty ("PolicyRuleName", policyNameCimValue));
kvp.addElement (new CIMProperty ("PolicyString", policyStringCimValue));
CIMObjectPath cop = new CIMObjectPath ("CIM_SPLPolicyRule", kvp);
cop.setNameSpace ("root/cimv2");
CIMInstance ci = new CIMInstance (cop);
ci.setObjectPath (cop);
CIMObjectPath returnpath=_cimClient.createInstance(ci.getObjectPath(), ci);
System.out.println("returnpath :"+returnpath);
}
static int _getInputs(String[] args)
{
int operation = INVALID_OPERATION;
if (args.length < 1)
{
_throwUsage();
}
for (int i=0; i < args.length; i++)
{
if ( args[i].equalsIgnoreCase("-u"))
{
if ( i + 1 < args.length )
{
USER=args[i+1];
i++;
}
else
{
System.err.println("A user name is required");
_throwUsage();
}
}
else if (args[i].equalsIgnoreCase("-pwd"))
{
if (i+1 < args.length)
{
PWD = args[i+1];
i++;
}
else
{
System.err.println("A pwd is required");
_throwUsage();
}
}
else if (args[i].equalsIgnoreCase("-n"))
{
if (i +1 < args.length)
{
NAMESPACE = args[i+1];
i++;
}
else
{
System.err.println("A namespace is required");
_throwUsage();
}
}
else if (args[i].equalsIgnoreCase("-s"))
{
if (i +1 < args.length)
{
SERVER = args[i+1];
i++;
}
else
{
System.err.println("A servername is required");
_throwUsage();
}
}
else if (args[i].equalsIgnoreCase("-p"))
{
if (i+1 < args.length)
{
PORT = Integer.parseInt(args[i+1]);
i++;
}
else
{
System.err.println("A servername is required");
_throwUsage();
}
}
else if (args[i].equalsIgnoreCase("-l"))
{
if (i+1 < args.length)
{
POLICY_NAME = args[i+1];
operation = LIST_POLICY;
break;
}
else
{
_throwUsage();
}
}
else if (args[i].equalsIgnoreCase("-x"))
{
if (i + 1 < args.length)
{
POLICY_NAME = args[i+1];
operation = EXECUTE_POLICY;
break;
}
else
{
_throwUsage();
}
}
else if (args[i].equalsIgnoreCase("-m"))
{
if ((i+2) < args.length)
{
POLICY_PATH = args[i+1];
POLICY_NAME = args[i+2];
operation = MODIFY_POLICY;
break;
}
else
{
_throwUsage();
}
}
else if (args[i].equalsIgnoreCase("-d"))
{
if (i+1 < args.length)
{
POLICY_NAME = args[i+1];
operation = DELETE_POLICY;
break;
}
else
{
_throwUsage();
}
}
else if (args[i].equalsIgnoreCase("-i"))
{
operation = LISTALL_POLICY;
break;
}
else if (args[i].equalsIgnoreCase("-a"))
{
operation = LISTALLNAMES_POLICY;
break;
}
else if (args[i].equalsIgnoreCase("-c"))
{
if ((i+2) <args.length)
{
POLICY_PATH = args[i+1];
POLICY_NAME = args[i+2];
operation = CREATE_POLICY;
break;
}
else
{
_throwUsage();
}
}
else
{
_throwUsage();
}
}
if(operation == INVALID_OPERATION)
{
_throwUsage();
}
return operation;
}
private static void _throwUsage()
{
System.out.println(" Usage: PolicyClient ");
System.out.println( " [-s <server name>] [-p <port>] [-u <user>] [-p <pwd>] ");
System.out.println( " -l <policy name> OR " );
System.out.println( " -c <policy path> <policy name> OR " );
System.out.println( " -m <policy path> <policy name> OR" );
System.out.println( " -d <policy name> OR" );
System.out.println( " -x <policy name> OR" );
System.out.println( " -i OR" );
System.out.println( " -a " );
System.exit(0);
}
private String _readPolicyString(String path, String policyName)
{
StringBuffer policy = new StringBuffer();
if(!path.endsWith("/"))
{
path = path + "/" + policyName;
}
else {
path = path + policyName;
}
//path.concat(policyName);
if(!path.endsWith(".spl"))
{
path = path + ".spl";
}
try
{
System.out.println("path="+path);
FileReader input = new FileReader(path);
BufferedReader bufRead = new BufferedReader(input);
String line; // String that holds current file line
int count = 0; // Line number of count
line = bufRead.readLine();
policy.append(line);
policy.append("\n");
count++;
while (line != null)
{
line = bufRead.readLine();
if(line != null){
policy.append(line);
policy.append("\n");
}
count++;
}
bufRead.close();
}
catch (FileNotFoundException e)
{
System.err.println("Error Reading file");
e.printStackTrace();
}
catch (IOException e)
{
System.err.println("Error Reading file");
e.printStackTrace();
}
System.out.println(policy.toString());
return policy.toString();
}
void _establishConnection() throws CIMException
{
String url="http://"+SERVER+":"+PORT;
// *****************************
// 1. Create user credentials
// *****************************
UserPrincipal userPr = new UserPrincipal(USER);
char [] pwdCharArray = PWD.toCharArray();
PasswordCredential pwCred = new PasswordCredential(pwdCharArray);
// *****************************
// 2. Set NameSpace
// - URL is set like: http(s)://<IP>:Port
// - Namespace does not need to be specified in COPs if set in this constuctor
// - There is no server authentication being done. Thus: No need for a truststore
// *****************************
CIMNameSpace nameSpace = new CIMNameSpace(url,NAMESPACE);
// *****************************
// 3. Create CIM Client
// *****************************
_cimClient = new CIMClient(nameSpace,userPr,pwCred);
System.out.println("Established Connection");
}
}