/*
* 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.
*/
/**
* @author Prashant Baliga <prabalig@in.ibm.com>
*
*/
package org.apache.imperius.cimspl.client;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.imperius.spl.core.Actuator;
import org.apache.imperius.spl.core.DataCollector;
import org.apache.imperius.spl.core.InternalClient;
import org.apache.imperius.spl.core.PolicyInfo;
import org.apache.imperius.spl.manager.PolicyManager;
import org.apache.imperius.spl.manager.impl.PolicyManagerImpl;
import org.apache.imperius.spl.parser.exceptions.SPLException;
import org.apache.imperius.spl.parser.util.InternalClientFactory;
import org.apache.imperius.util.SPLLogger;
import org.sblim.wbem.cim.CIMException;
import org.sblim.wbem.cim.CIMNameSpace;
import org.sblim.wbem.cim.CIMObjectPath;
import org.sblim.wbem.client.CIMClient;
import org.sblim.wbem.client.PasswordCredential;
import org.sblim.wbem.client.UserPrincipal;
public class CIM_SPLPolicyRuleClientProvider
{
private PolicyManager _policyManager= null;
private CIMClient _client=null;
public InternalClient _internalClient=null;
private static Logger logger = SPLLogger.getSPLLogger().getLogger();
private static String sourceClass = "CIM_SPLPolicyRuleClientProvider";
public static void main(String[] args) throws SPLException
{
}
public CIM_SPLPolicyRuleClientProvider(String url, String namespace, String username, String password) throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"CIM_SPLPolicyRuleClientProvider");
initialize(url, namespace, username, password);
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"CIM_SPLPolicyRuleClientProvider");
}
public void cleanup() throws SPLException {
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"cleanup");
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"cleanup");
}
public void initialize(String url, String namespace, String username, String password) throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"initialize");
CIMClient a=getCIMClient(url, namespace, username, password);
DataCollector dc = new CIMClientSideDataCollectorImpl(a);
Actuator ac = new CIMClientSideActuatorImpl(a);
_policyManager= new PolicyManagerImpl(dc,ac);
_client=a;
_internalClient=new InternalClient(_policyManager);
InternalClientFactory.registerInternalClient(_internalClient);
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"initialize");
}
public boolean createPolicy(String name, String policyString) throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"createPolicy");
System.out.println("creating new client SPL policy");
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" Creating Policy instance :"+name);
boolean result = _policyManager.createPolicy(name,policyString);
System.out.println("done creating new client SPL policy");
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"createPolicy");
return result;
}
public void deletePolicy(String name) throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"deletePolicy");
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" deleting Policy instance :"+name);
System.out.println("deleting client SPL policy");
_policyManager.deletePolicy(name);
System.out.println("done deleting client SPL policy");
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"deletePolicy");
return;
}
public List enumeratePolicyNames() throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"enumeratePolicyNames");
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" enumerating Policy names ");
List policyNames = _policyManager.getAllPolicyNames();
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"enumeratePolicyNames");
return policyNames;
}
public Map enumeratePolicies() throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"enumeratePolicies");
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" enumerating Policy instances ");
Map policyInstances = _policyManager.getAllPolicies();
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"enumeratePolicyNames");
return policyInstances;
}
public String getPolicy(String policyName) throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"getPolicy");
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" get Policy instances ");
String policy = _policyManager.getPolicy(policyName);
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"getPolicy");
return policy;
}
public void modifyPolicy(String name, String policyString) throws SPLException {
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"modifyPolicy");
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" modify Policy instances :"+name);
_policyManager.updatePolicy(name,policyString);
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"modifyPolicy");
}
public Object executePolicy(String policyName, Map instanceObjectMap) throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"executePolicy");
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" execute Policy :"+policyName);
Integer retVal=new Integer(-1);
System.out.println("executePolicy client SPL policy "+policyName);
System.out.println(" no instances passed in , evaluating against all instances");
PolicyInfo policyInfo=this._policyManager.getPolicyInfo(policyName);
Map classQuaifierMap=policyInfo.getClassQualifierMap();
Iterator classit=classQuaifierMap.keySet().iterator();
System.out.println("number of classes in policy = "+classQuaifierMap.size());
if(classQuaifierMap.size()==0 || classQuaifierMap.size()>1)
{
System.out.println("number of classes in policy should be one "+classQuaifierMap.toString());
}
while(classit.hasNext())
{
Map objMap = new Hashtable();
String className=(String)classit.next();
String nameSpace=(String)classQuaifierMap.get(className);
CIMObjectPath classCop=new CIMObjectPath(className,nameSpace);
Enumeration copEnum=null;
try{
copEnum=_client.enumerateInstanceNames(classCop);
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" enumerating instances of :"+classCop);
}catch(CIMException e){
System.out.println("caught a CIMException "+e.getMessage());
e.printStackTrace();
}
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" iteratig over instances of :"+classCop);
while(copEnum.hasMoreElements())
{
CIMObjectPath copI=(CIMObjectPath)copEnum.nextElement();
System.out.println("instance objectpath= "+copI.toString());
String objectName=copI.getObjectName();
if(objectName.equalsIgnoreCase(className))
{
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" object name matches classname :"+objectName +" "+className);
// InstanceInfo iI=new InstanceInfo(objectName,copI);
//List instanceInfoList = new ArrayList();
//instanceInfoList.add(iI);
objMap.put(objectName, copI);
retVal=(Integer)_policyManager.evaluatePolicy(policyName,objMap);
if (logger.isLoggable(Level.FINE))
logger
.fine(Thread.currentThread().getName()+" return value is = "+retVal);
System.out.println(" policy evaluation complete "+ policyName +" on "+copI.toString()+" and return value= "+retVal);
objMap.remove(objectName);
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"invokeMethod");
}
else
{
if (logger.isLoggable(Level.FINE))
logger
.fine(Thread.currentThread().getName()+" objectName.equalsIgnoreCase(className) "+ objectName+ " "+className);
System.out.println(" SKIPPED >>>>>>> "+ objectName+ " "+className);
}
}
System.out.println(" policy provider finished iterating over all instances ");
break;
}
System.out.println("executePolicy client SPL policy "+policyName);
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"executePolicy");
return retVal;
}
private CIMClient getCIMClient(String url, String namespace, String username, String password) {
CIMClient client = null;
try {
// *****************************
// 1. Create user credentials
// *****************************
UserPrincipal userPr = new UserPrincipal(username);
char [] pwdCharArray = password.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 ns = new CIMNameSpace(url,namespace);
// *****************************
// 3. Create CIM Client
// *****************************
client = new CIMClient(ns,userPr,pwCred);
System.out.println("got new cim client");
} catch(Exception e) {
// PolicyPluginLogUtil.logError("Error while connecting CIM Server", e);
}
return client;
}
}