/*
* 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;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.pegasus.jmpi.CIMArgument;
import org.pegasus.jmpi.CIMClass;
import org.pegasus.jmpi.CIMException;
import org.pegasus.jmpi.CIMInstance;
import org.pegasus.jmpi.CIMMethodProvider2;
import org.pegasus.jmpi.CIMOMHandle;
import org.pegasus.jmpi.CIMObjectPath;
import org.pegasus.jmpi.CIMProperty;
import org.pegasus.jmpi.CIMValue;
import org.pegasus.jmpi.InstanceProvider2;
import org.pegasus.jmpi.OperationContext;
import org.apache.imperius.spl.core.Actuator;
import org.apache.imperius.spl.core.DataCollector;
import org.apache.imperius.spl.core.InstanceInfo;
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.statements.impl.PolicyInfo;
import org.apache.imperius.spl.parser.util.InternalClientFactory;
import org.apache.imperius.util.SPLLogger;
import org.pegasus.jmpi.CIMOMHandle;
public class CIM_SPLPolicyRuleProvider implements InstanceProvider2,CIMMethodProvider2
{
public static final String POLICY_CLASS_NAME = "CIM_SPLPolicyRule";
public static final String NAMESPACE = "root/cimv2";
private static Logger logger = null;
private InternalClient _internalClient=null;
private PolicyManager _policyManager= null;
private static String sourceClass = "CIM_SPLPolicyRuleProvider";
private static CIMOMHandle _handle;
// private static Logger auditLogger = null;
public static boolean ENABLE_SPL_LOGGING = false;
public void cleanup() throws CIMException { }
public void initialize(CIMOMHandle ch) throws CIMException
{
_handle=ch;
String home = System.getProperty("user.dir");
System.out.println("SPL Home Set to" + home);
SPLLogger.SPL_HOME = home;
logger = SPLLogger.getSPLLogger().getLogger();
// auditLogger = SPLLogger.getSPLLogger().getAuditLogger();
//
// if(auditLogger.isLoggable(Level.FINE))
// logger.fine(Thread.currentThread().getName()+" Initializing Policy Provider" );
DataCollector dc = new CIMDataCollectorImpl(ch);
Actuator ac = new CIMActuatorImpl(ch);
_policyManager= new PolicyManagerImpl(dc,ac);
_internalClient=new InternalClient(_policyManager);
InternalClientFactory.registerInternalClient(_internalClient);
}
public CIMObjectPath createInstance(OperationContext oc,
CIMObjectPath cop, CIMInstance cimInstance)
throws CIMException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"createInstance");
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" Creating Policy instance :"+cop);
PolicyInstance pi = _createPolicyInstance(cop, cimInstance);
try
{
boolean instcreated = _policyManager.createPolicy(pi.getPolicyRuleName(),pi.getPolicyString());
if(instcreated)
{
CIMObjectPath cimop=_createPolicyObjectPath(pi.getPolicyRuleName());
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"createInstance");
return cimop;
}
else
{
logger.severe("instance not created");
throw new CIMException("instance not created");
}
}
catch(SPLException e)
{
logger.severe("instance not created");
throw new CIMException("instance not created" +e.getMessage());
}
}
public void deleteInstance(OperationContext oc, CIMObjectPath cop) throws CIMException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"deleteInstance");
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" Deleting Policy instance :"+cop);
String policyRuleName=cop.getKeyValue("PolicyRuleName");
try
{
_policyManager.deletePolicy(policyRuleName);
}
catch (SPLException e)
{
logger.severe(Thread.currentThread().getName()+" "+"deleteInstance failed :"
+ e.getMessage());
throw new CIMException("deleteInstance failed :"+e.getMessage());
}
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"deleteInstance");
return;
}
public Vector enumerateInstanceNames(OperationContext oc, CIMObjectPath cop,
CIMClass cimClass) throws CIMException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"enumerateInstanceNames");
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" Enumerating Policy instance names :"+cop);
String nameSpace = cop.getNameSpace();
String className = cimClass.getName();
// retrieving policy Names from Repository
if (logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName() + " retrieving Policies Names from policyRepository");
if ((className.equals(POLICY_CLASS_NAME))&& (nameSpace.equals(NAMESPACE)))
{
try
{
List policyNames=_policyManager.getAllPolicyNames();
Iterator it=policyNames.iterator();
Vector instances=new Vector();
while(it.hasNext())
{
String pi=(String)it.next();
CIMObjectPath cimInst=_createPolicyObjectPath(pi);
instances.add(cimInst);
}
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"enumerateInstanceNames");
return instances;
}
catch (SPLException e)
{
logger.severe(Thread.currentThread().getName()+" "+"enumerateInstances failed :"+e.getMessage());
throw new CIMException("enumerateInstances failed :"+e.getMessage());
}
}
else
{
logger
.severe(Thread.currentThread().getName()+" "+"This CIM class is not handled by the Policy Provider");
throw new CIMException(
"This CIM class is not handled by the Policy Provider");
}
}
public Vector enumerateInstances(OperationContext oc, CIMObjectPath cop, CIMClass cimClass,
boolean includeQualifiers, boolean includeClassOrigin, String[] propertyList)
throws CIMException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"enumerateInstances");
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" Enumerating Policy instances :"+cop);
String nameSpace = cop.getNameSpace();
String className = cimClass.getName();
// enumerating Policies from policyRepository
if (logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" enumerating Policies from policyRepository");
// USE equalscase instead
if (className.equals(POLICY_CLASS_NAME) && nameSpace.equals(NAMESPACE))
{
try
{
Map policyInstances=_policyManager.getAllPolicies();
Set polNames=policyInstances.keySet();
Iterator it=polNames.iterator();
Vector instances=new Vector();
while(it.hasNext())
{
String pi=(String)it.next();
CIMInstance cimInst=_createPolicyCIMInstance(pi, (String)policyInstances.get(pi));
instances.add(cimInst);
}
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"enumerateInstances");
return instances;
}
catch (SPLException e)
{
logger.severe(Thread.currentThread().getName()+" "+"enumerateInstances failed :"+e.getMessage());
throw new CIMException("enumerateInstances failed :"+e.getMessage());
}
}
else
{
logger
.severe(Thread.currentThread().getName()+" "+"This CIM class is not handled by the Policy Provider");
throw new CIMException("This CIM class is not handled by the Policy Provider");
}
}
public CIMInstance getInstance(OperationContext oc, CIMObjectPath cop, CIMClass cimClass,
boolean includeQualifiers, boolean includeClassOrigin, String[] propertyList)
throws CIMException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"getInstance");
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" Getting Policy instance :"+cop);
String nameSpace = cop.getNameSpace();
String PolicyRuleName = cop.getKeyValue("PolicyRuleName");
String className = cimClass.getName();
if ((className.equals(POLICY_CLASS_NAME))
&& (nameSpace.equals(NAMESPACE)))
{
try
{
String pi=_policyManager.getPolicy(PolicyRuleName);
CIMInstance cimInst=_createPolicyCIMInstance(PolicyRuleName, pi);
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"getInstance");
return cimInst;
}
catch (SPLException e)
{
logger.severe(Thread.currentThread().getName()+" "+"getInstance failed :"+e.getMessage());
throw new CIMException("getInstance failed :"+e.getMessage());
}
}
else
{
logger
.severe(Thread.currentThread().getName()+" "+"This CIM class is not handled by the Policy Provider");
throw new CIMException("This CIM class is not handled by the Policy Provider");
}
}
public Vector execQuery(OperationContext oc, CIMObjectPath cop, CIMClass cimClass,
String queryStatement, String queryLanguage) throws CIMException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"execQuery");
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" not implemented");
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"execQuery");
return null;
}
public void setInstance(OperationContext oc, CIMObjectPath cop, CIMInstance ci) throws CIMException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"setInstance");
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" Setting Policy instance :"+cop);
if ((ci.getClassName().equals(POLICY_CLASS_NAME))
&& (cop.getNameSpace().equals(NAMESPACE)))
{
String policyRuleName = ci.getProperty("PolicyRuleName").getValue()
.getValue().toString();
String policyString = ((((CIMProperty) ci
.getProperty("PolicyString")).getValue()).getValue())
.toString();
try
{
_policyManager.updatePolicy(policyRuleName,policyString);
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"setInstance");
}
catch (SPLException e)
{
logger.severe(Thread.currentThread().getName()+" "+"setInstance failed :"+e.getMessage());
throw new CIMException("setInstance failed :"+e.getMessage());
}
}
else
{
logger
.severe(Thread.currentThread().getName()+" "+"This CIM class is not handled by the Policy Provider");
throw new CIMException(
"This CIM class is not handled by the Policy Provider");
}
}
public CIMValue invokeMethod(OperationContext oc, CIMObjectPath cop, String name,
CIMArgument[] in, CIMArgument[] out) throws CIMException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"invokeMethod");
if(logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" Invoking Policy method :"+name +" on :"+ cop);
// if(auditLogger.isLoggable(Level.FINE))
// auditLogger.fine(Thread.currentThread().getName()+" Invoking Policy method :"+name +" on :"+ cop);
//System.out.println(" Invoking Policy method :"+name +" on :"+ cop);
boolean inparamsIsNotNull=false;
boolean inparamsLengthGreaterThanZero=false;
boolean arrayOfInstancesParamFound=false;
boolean arrayOfInstancesParamSizeGreaterThanZero=false;
Integer returnValue = new Integer(-1);
try
{
Map objMap = new Hashtable();
String policyRuleName=cop.getKeyValue("PolicyRuleName");
if (in != null)
{
logger.fine(Thread.currentThread().getName() + "params passed");
inparamsIsNotNull=true;
if(in.length > 0)
{
inparamsLengthGreaterThanZero=true;
for(int i=0;i<in.length;i++)
{
CIMArgument cimarg=in[i];
if(cimarg.getName().equalsIgnoreCase("arrayOfInstances"))
{
arrayOfInstancesParamFound=true;
CIMValue cv=cimarg.getValue();
List arrayCop=(List)cv.getValue();
Iterator it=arrayCop.iterator();
while(it.hasNext())
{
arrayOfInstancesParamSizeGreaterThanZero=true;
CIMObjectPath copI=(CIMObjectPath)it.next();
String objectName=copI.getObjectName();
//InstanceInfo iI=new InstanceInfo(objectName,copI);
//List instanceInfoList = new ArrayList();
//instanceInfoList.add(iI);
objMap.put(objectName, copI);
Integer retVal=(Integer)_policyManager.evaluatePolicy(policyRuleName,objMap);
if (logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" return value is = "+retVal);
objMap.remove(objectName);
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"invokeMethod");
}
}
break;
}
}
}
if( !inparamsIsNotNull || !inparamsLengthGreaterThanZero || !arrayOfInstancesParamFound ||
!arrayOfInstancesParamSizeGreaterThanZero)
{
logger.fine(Thread.currentThread().getName() + "no instances passed evaluating against all");
PolicyInfo policyInfo=this._policyManager.getPolicyInfo(policyRuleName);
Map classQuaifierMap=policyInfo.getClassQualifierMap();
Iterator classit=classQuaifierMap.keySet().iterator();
if(classQuaifierMap.size()==0 || classQuaifierMap.size()>1)
{
logger.severe(Thread.currentThread().getName() + "Multiple Import classes not supported in CIM-SPL");
}
while(classit.hasNext())
{
String className=(String)classit.next();
String nameSpace=(String)classQuaifierMap.get(className);
CIMObjectPath classCop=new CIMObjectPath(className,nameSpace);
Enumeration copEnum=_handle.enumerateInstanceNames(classCop);
while(copEnum.hasMoreElements())
{
CIMObjectPath copI=(CIMObjectPath)copEnum.nextElement();
logger.fine(Thread.currentThread().getName() + "instance objectpath= "+copI.toString());
String objectName=copI.getObjectName();
if(objectName.equalsIgnoreCase(className))
{
// InstanceInfo iI=new InstanceInfo(objectName,copI);
//
// List instanceInfoList = new ArrayList();
// instanceInfoList.add(iI);
objMap.put(objectName, copI);
logger.fine(Thread.currentThread().getName() + "about to invoke evaluate policy");
returnValue=(Integer)_policyManager.evaluatePolicy(policyRuleName,objMap);
if (logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" return value is = "+returnValue);
objMap.remove(objectName);
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"invokeMethod");
}
else{
}
}
break;
}
}
return new CIMValue(returnValue);
}
catch (SPLException e)
{
logger.severe(Thread.currentThread().getName()+" "+"getInstance failed :"+e.getMessage());
throw new CIMException("getInstance failed :"+e.getMessage());
}
}
private PolicyInstance _createPolicyInstance(CIMObjectPath op, CIMInstance ci) throws CIMException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+"createPolicyInstance");
if ((ci.getClassName().equals(POLICY_CLASS_NAME))
&& (op.getNameSpace().equals(NAMESPACE)))
{
String PolicyRuleName = ci.getProperty("PolicyRuleName").getValue().getValue().toString();
String PolicyString = ((((CIMProperty) ci
.getProperty("PolicyString")).getValue()).getValue())
.toString();
// check to see if PolicyRuleName and PolicyString are valid
if (logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" PolicyRuleName and PolicyString " + PolicyRuleName
+ " " + PolicyString);
PolicyInstance pi= new PolicyInstance(PolicyRuleName,PolicyString);
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+"createPolicyInstance");
return pi;
}
else
{
logger
.severe(Thread.currentThread().getName()+" "+"This CIM class is not handled by the Policy Provider");
throw new CIMException(
"This CIM class is not handled by the Policy Provider");
}
}
private static CIMInstance _createPolicyCIMInstance(String policyRuleName,
String PolicyString) throws CIMException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "_createPolicyCIMInstance");
if ((policyRuleName != null) && (PolicyString != null))
{
if (logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" create a CIMPolicy Object out of the PolicyString and PolicyRuleName");
// create a CIMPolicy Object out of the PolicyString and
// PolicyRuleName
CIMInstance policyInstance = new CIMInstance(POLICY_CLASS_NAME);
policyInstance.setProperty("SystemCreationClassName",
new CIMValue("cim_splpolicyrule"));
policyInstance.setProperty("SystemName", new CIMValue("localhost.localdomain"));
policyInstance.setProperty("CreationClassName", new CIMValue(POLICY_CLASS_NAME));
policyInstance.setProperty("PolicyRuleName", new CIMValue(policyRuleName));
policyInstance.setProperty("PolicyString", new CIMValue(PolicyString));
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "_createPolicyCIMInstance");
return policyInstance;
}
else
{
logger.severe(Thread.currentThread().getName()+" "+"The Policy cannot be found due to Invalid Policy Parameters");
throw new CIMException("The Policy cannot be found due to Invalid Policy Parameters");
}
}
private static CIMObjectPath _createPolicyObjectPath(String policyRuleName)
throws CIMException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "_createPolicyObjectPath");
if (policyRuleName != null)
{
if (logger.isLoggable(Level.FINE))
logger.fine(Thread.currentThread().getName()+" create a CIMObjectPath using the PolicyRuleName");
// create a CIMObjectPath using the PolicyRuleName
CIMObjectPath cop = new CIMObjectPath(POLICY_CLASS_NAME, NAMESPACE);
cop.addKey("SystemName", new CIMValue("localhost.localdomain"));
cop.addKey("CreationClassName", new CIMValue(POLICY_CLASS_NAME));
cop.addKey("PolicyRuleName", new CIMValue(policyRuleName));
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "_createPolicyObjectPath");
return cop;
}
else
{
logger
.severe(Thread.currentThread().getName()+" "+"The Policy cannot be found due to Invalid Policy Parameters");
throw new CIMException(
"The Policy cannot be found due to Invalid Policy Parameters");
}
}
}