BUG#: 4577
[tpot/pegasus/.git] / src / Pegasus / ProviderManager2 / JMPI / org / pegasus / jmpi / tests / Indication / JMPI_IndicationProvider.java
1 //%2006////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 // Author:      Mark Hamzy, IBM (hamzy@us.ibm.com)
33 //
34 // Modified By: Mark Hamzy, IBM (hamzy@us.ibm.com)
35 //
36 //%/////////////////////////////////////////////////////////////////////////////
37 package org.pegasus.jmpi.tests.Indication;
38
39 import java.util.Vector;
40 import org.pegasus.jmpi.CIMClass;
41 import org.pegasus.jmpi.CIMDataType;
42 import org.pegasus.jmpi.CIMException;
43 import org.pegasus.jmpi.CIMInstance;
44 import org.pegasus.jmpi.CIMOMHandle;
45 import org.pegasus.jmpi.CIMObjectPath;
46 import org.pegasus.jmpi.CIMProperty;
47 import org.pegasus.jmpi.CIMValue;
48 import org.pegasus.jmpi.EventProvider2;
49 import org.pegasus.jmpi.InstanceProvider2;
50 import org.pegasus.jmpi.MethodProvider2;
51 import org.pegasus.jmpi.OperationContext;
52 import org.pegasus.jmpi.SelectExp;
53 import org.pegasus.jmpi.SelectList;
54 import org.pegasus.jmpi.NonJoinExp;
55 import org.pegasus.jmpi.QueryExp;
56 import org.pegasus.jmpi.UnsignedInt32;
57 import org.pegasus.jmpi.UnsignedInt64;
58
59 public class JMPI_IndicationProvider
60              implements InstanceProvider2,
61                         MethodProvider2,
62                         EventProvider2
63 {
64    private static String CLASSNAME = "TestIndication";
65    private static String NAMESPACE = "test/static";
66    private CIMOMHandle   ch        = null;
67    private Vector        paths     = new Vector ();
68    private Vector        instances = new Vector ();
69
70    public void initialize (CIMOMHandle ch)
71       throws CIMException
72    {
73       System.out.println ("JMPI_IndicationProvider::initialize: ch = " + ch);
74
75       this.ch = ch;
76    }
77
78    public void cleanup ()
79       throws CIMException
80    {
81       System.out.println ("JMPI_IndicationProvider::cleanup");
82
83       ch        = null;
84       paths     = null;
85       instances = null;
86    }
87
88    public CIMObjectPath createInstance (OperationContext oc,
89                                         CIMObjectPath    cop,
90                                         CIMInstance      cimInstance)
91       throws CIMException
92    {
93       System.out.println ("JMPI_IndicationProvider::createInstance: oc          = " + oc);
94       System.out.println ("JMPI_IndicationProvider::createInstance: cop         = " + cop);
95 // @HACK
96 String nameSpaceCop = cop.getNameSpace ();
97 cop = new CIMObjectPath (cimInstance.getClassName (),
98                          cimInstance.getKeyValuePairs ());
99 cop.setNameSpace (nameSpaceCop);
100 System.out.println ("JMPI_IndicationProvider::createInstance: cop         = " + cop);
101       System.out.println ("JMPI_IndicationProvider::createInstance: cimInstance = " + cimInstance);
102
103       // Ensure the Namespace is valid
104       if (!cop.getNameSpace ().equalsIgnoreCase (NAMESPACE))
105          throw new CIMException (CIMException.CIM_ERR_INVALID_NAMESPACE);
106
107       // Ensure the class existing in the specified namespace
108       if (!cop.getObjectName ().equalsIgnoreCase (CLASSNAME))
109          throw new CIMException (CIMException.CIM_ERR_INVALID_CLASS);
110
111       // Ensure that all of the properties exists in the instance
112       if (  cimInstance.getProperty ("InstanceId") == null
113          || cimInstance.getProperty ("PropertyString") == null
114          )
115          throw new CIMException (CIMException.CIM_ERR_INVALID_PARAMETER);
116
117       // Ensure that all of the key properties exists in the object path
118       if (cop.getKeyValue ("InstanceId") == null)
119          throw new CIMException (CIMException.CIM_ERR_INVALID_PARAMETER);
120
121       // Ensure the requested object do not exist
122       if (findObjectPath (cop) >= 0)
123          throw new CIMException (CIMException.CIM_ERR_ALREADY_EXISTS);
124
125       paths.addElement (cop);
126       instances.addElement (cimInstance);
127
128       return cop;
129    }
130
131    public CIMInstance getInstance (OperationContext oc,
132                                    CIMObjectPath    cop,
133                                    CIMClass         cimClass,
134                                    boolean          includeQualifiers,
135                                    boolean          includeClassOrigin,
136                                    String           propertyList[])
137       throws CIMException
138    {
139       System.out.println ("JMPI_IndicationProvider::getInstance: oc                 = " + oc);
140       System.out.println ("JMPI_IndicationProvider::getInstance: cop                = " + cop);
141       System.out.println ("JMPI_IndicationProvider::getInstance: cimClass           = " + cimClass);
142       System.out.println ("JMPI_IndicationProvider::getInstance: includeQualifiers  = " + includeQualifiers);
143       System.out.println ("JMPI_IndicationProvider::getInstance: includeClassOrigin = " + includeClassOrigin);
144       System.out.println ("JMPI_IndicationProvider::getInstance: propertyList       = " + propertyList);
145
146       // ensure the InstanceId key is valid
147       Vector keys = cop.getKeys ();
148       int    i;
149
150       for (i = 0;
151            i < keys.size () && ! ((CIMProperty)keys.elementAt (i)).getName ().equalsIgnoreCase ("InstanceId");
152            i++)
153       {
154       }
155
156       if (i == keys.size ())
157          throw new CIMException (CIMException.CIM_ERR_INVALID_PARAMETER);
158
159       // ensure the Namespace is valid
160       if (!cop.getNameSpace ().equalsIgnoreCase (NAMESPACE))
161          throw new CIMException (CIMException.CIM_ERR_INVALID_NAMESPACE);
162
163       // ensure the class existing in the specified namespace
164       if (!cop.getObjectName ().equalsIgnoreCase (CLASSNAME))
165          throw new CIMException (CIMException.CIM_ERR_INVALID_CLASS);
166
167       // ensure the request object exists
168       int index = findObjectPath (cop);
169
170       if (index < 0)
171          throw new CIMException (CIMException.CIM_ERR_NOT_FOUND);
172
173       return (CIMInstance)instances.elementAt (index);
174    }
175
176    public void setInstance (OperationContext oc,
177                             CIMObjectPath    cop,
178                             CIMInstance      cimInstance)
179       throws CIMException
180    {
181       System.out.println ("JMPI_IndicationProvider::setInstance: oc          = " + oc);
182       System.out.println ("JMPI_IndicationProvider::setInstance: cop         = " + cop);
183       System.out.println ("JMPI_IndicationProvider::setInstance: cimInstance = " + cimInstance);
184
185       // ensure the Namespace is valid
186       if (!cop.getNameSpace ().equalsIgnoreCase (NAMESPACE))
187          throw new CIMException (CIMException.CIM_ERR_INVALID_NAMESPACE);
188
189       // ensure the class existing in the specified namespace
190       if (!cop.getObjectName ().equalsIgnoreCase (CLASSNAME))
191          throw new CIMException (CIMException.CIM_ERR_INVALID_CLASS);
192
193       // ensure the request object exists
194       int index = findObjectPath (cop);
195
196       if (index < 0)
197          throw new CIMException (CIMException.CIM_ERR_NOT_FOUND);
198    }
199
200    public void deleteInstance (OperationContext oc,
201                                CIMObjectPath    cop)
202       throws CIMException
203    {
204       System.out.println ("JMPI_IndicationProvider::deleteInstance: oc  = " + oc);
205       System.out.println ("JMPI_IndicationProvider::deleteInstance: cop = " + cop);
206
207       // ensure the Namespace is valid
208       if (!cop.getNameSpace ().equalsIgnoreCase (NAMESPACE))
209          throw new CIMException (CIMException.CIM_ERR_INVALID_NAMESPACE);
210
211       // ensure the class existing in the specified namespace
212       if (!cop.getObjectName ().equalsIgnoreCase (CLASSNAME))
213          throw new CIMException (CIMException.CIM_ERR_INVALID_CLASS);
214
215       // ensure the request object exists
216       int index = findObjectPath (cop);
217
218       if (index < 0)
219          throw new CIMException (CIMException.CIM_ERR_NOT_FOUND);
220    }
221
222    public Vector enumerateInstanceNames (OperationContext oc,
223                                          CIMObjectPath    cop,
224                                          CIMClass         cimClass)
225       throws CIMException
226    {
227       System.out.println ("JMPI_IndicationProvider::enumerateInstanceNames: oc       = " + oc);
228       System.out.println ("JMPI_IndicationProvider::enumerateInstanceNames: cop      = " + cop);
229       System.out.println ("JMPI_IndicationProvider::enumerateInstanceNames: cimClass = " + cimClass);
230
231       // ensure the Namespace is valid
232       if (!cop.getNameSpace ().equalsIgnoreCase (NAMESPACE))
233          throw new CIMException (CIMException.CIM_ERR_INVALID_NAMESPACE);
234
235       // ensure the class existing in the specified namespace
236       if (!cop.getObjectName ().equalsIgnoreCase (CLASSNAME))
237          throw new CIMException (CIMException.CIM_ERR_INVALID_CLASS);
238
239       return paths;
240    }
241
242    public Vector enumerateInstances (OperationContext oc,
243                                      CIMObjectPath    cop,
244                                      CIMClass         cimClass,
245                                      boolean          includeQualifiers,
246                                      boolean          includeClassOrigin,
247                                      String           propertyList[])
248       throws CIMException
249    {
250       System.out.println ("JMPI_IndicationProvider::enumerateInstances: oc                 = " + oc);
251       System.out.println ("JMPI_IndicationProvider::enumerateInstances: cop                = " + cop);
252       System.out.println ("JMPI_IndicationProvider::enumerateInstances: cimClass           = " + cimClass);
253       System.out.println ("JMPI_IndicationProvider::enumerateInstances: includeQualifiers  = " + includeQualifiers);
254       System.out.println ("JMPI_IndicationProvider::enumerateInstances: includeClassOrigin = " + includeClassOrigin);
255       System.out.println ("JMPI_IndicationProvider::enumerateInstances: propertyList       = " + propertyList);
256
257       // ensure the Namespace is valid
258       if (!cop.getNameSpace ().equalsIgnoreCase (NAMESPACE))
259          throw new CIMException (CIMException.CIM_ERR_INVALID_NAMESPACE);
260
261       // ensure the class existing in the specified namespace
262       if (!cop.getObjectName ().equalsIgnoreCase (CLASSNAME))
263          throw new CIMException (CIMException.CIM_ERR_INVALID_CLASS);
264
265       return instances;
266    }
267
268    public Vector execQuery (OperationContext oc,
269                             CIMObjectPath    cop,
270                             CIMClass         cimClass,
271                             String           queryStatement,
272                             String           queryLanguage)
273       throws CIMException
274    {
275       System.out.println ("JMPI_IndicationProvider::execQuery: oc             = " + oc);
276       System.out.println ("JMPI_IndicationProvider::execQuery: cop            = " + cop);
277       System.out.println ("JMPI_IndicationProvider::execQuery: cimClass       = " + cimClass);
278       System.out.println ("JMPI_IndicationProvider::execQuery: queryStatement = " + queryStatement);
279       System.out.println ("JMPI_IndicationProvider::execQuery: queryLanguage  = " + queryLanguage);
280
281       if (!queryLanguage.equals ("WQL"))
282       {
283          throw new CIMException (CIMException.CIM_ERR_NOT_SUPPORTED);
284       }
285
286       SelectExp     q         = new SelectExp (queryStatement);
287       SelectList    attrs     = q.getSelectList ();
288       NonJoinExp    from      = (NonJoinExp)q.getFromClause ();
289       QueryExp      where     = q.getWhereClause ();
290       Vector        instances = enumerateInstances (oc,
291                                                     cop,
292                                                     cimClass,
293                                                     true,
294                                                     true,
295                                                     null);
296       Vector        ret       = new Vector ();
297
298       // filter the instances
299       for (int i = 0; i < instances.size (); i++)
300       {
301          if (  where == null
302             || where.apply ((CIMInstance)instances.elementAt (i))
303             )
304          {
305             ret.addElement (attrs.apply ((CIMInstance)instances.elementAt (i)));
306          }
307       }
308
309       return ret;
310    }
311
312    public CIMValue invokeMethod (OperationContext oc,
313                                  CIMObjectPath    cop,
314                                  String           methodName,
315                                  Vector           inArgs,
316                                  Vector           outArgs)
317       throws CIMException
318    {
319       System.out.println ("JMPI_IndicationProvider::invokeMethod: oc         = " + oc);
320       System.out.println ("JMPI_IndicationProvider::invokeMethod: cop        = " + cop);
321       System.out.println ("JMPI_IndicationProvider::invokeMethod: methodName = " + methodName);
322       System.out.println ("JMPI_IndicationProvider::invokeMethod: inArgs     = " + inArgs);
323       System.out.println ("JMPI_IndicationProvider::invokeMethod: outArgs    = " + outArgs);
324
325       // ensure the Namespace is valid
326       if (!cop.getNameSpace ().equalsIgnoreCase (NAMESPACE))
327       {
328          throw new CIMException (CIMException.CIM_ERR_INVALID_NAMESPACE);
329       }
330
331       // ensure the class existing in the specified namespace
332       if (!cop.getObjectName ().equalsIgnoreCase (CLASSNAME))
333       {
334          throw new CIMException (CIMException.CIM_ERR_INVALID_CLASS);
335       }
336
337       if (methodName.equalsIgnoreCase ("SendTestIndicationNormal"))
338       {
339          if (inArgs == null)
340          {
341             throw new CIMException (CIMException.CIM_ERR_INVALID_PARAMETER);
342          }
343          else if (inArgs.size () != 1)
344          {
345             throw new CIMException (CIMException.CIM_ERR_INVALID_PARAMETER);
346          }
347
348          CIMProperty cp = (CIMProperty)inArgs.elementAt (0);
349
350          if (!cp.getName ().equalsIgnoreCase ("indicationSendCount"))
351          {
352             throw new CIMException (CIMException.CIM_ERR_INVALID_PARAMETER);
353          }
354          else if (cp.getType ().getType () != CIMDataType.UINT64)
355          {
356             throw new CIMException (CIMException.CIM_ERR_INVALID_PARAMETER);
357          }
358
359          CIMValue      cvIndicationSendCount = cp.getValue ();
360          UnsignedInt64 uiIndicationSendCount = (UnsignedInt64)cvIndicationSendCount.getValue ();
361
362          System.out.println ("JMPI_IndicationProvider::invokeMethod: uiIndicationSendCount = " + uiIndicationSendCount);
363
364          CIMClass      ccIndication;
365          CIMInstance   ciIndication;
366          CIMObjectPath copIndication;
367
368          ccIndication = ch.getClass (cop,
369                                      true,
370                                      true,
371                                      true,
372                                      null);
373          ciIndication = ccIndication.newInstance ();
374
375          ciIndication.setProperty ("InstanceId", new CIMValue (uiIndicationSendCount));
376          ciIndication.setProperty ("PropertyString", new CIMValue ("Hello"));
377
378          copIndication = createInstance (oc,
379                                          cop,
380                                          ciIndication);
381
382          System.out.println ("JMPI_IndicationProvider::invokeMethod: deliverEvent: copIndication = " + copIndication);
383          System.out.println ("JMPI_IndicationProvider::invokeMethod: deliverEvent: ciIndication  = " + ciIndication);
384
385          ch.deliverEvent (copIndication.getNameSpace (),
386                           ciIndication);
387
388          return new CIMValue (new UnsignedInt32 ("0"));
389       }
390       else
391       {
392          throw new CIMException (CIMException.CIM_ERR_METHOD_NOT_AVAILABLE);
393       }
394    }
395
396    public void authorizeFilter (OperationContext oc,
397                                 SelectExp        filter,
398                                 String           eventType,
399                                 CIMObjectPath    classPath,
400                                 String           owner)
401       throws CIMException
402    {
403       System.out.println ("JMPI_IndicationProvider::authorizeFilter: oc        = " + oc);
404       System.out.println ("JMPI_IndicationProvider::authorizeFilter: filter    = " + filter);
405       System.out.println ("JMPI_IndicationProvider::authorizeFilter: eventType = " + eventType);
406       System.out.println ("JMPI_IndicationProvider::authorizeFilter: classPath = " + classPath);
407       System.out.println ("JMPI_IndicationProvider::authorizeFilter: owner     = " + owner);
408
409       if (filter != null)
410       {
411          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getSelectString () = " + filter.getSelectString ());
412          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getWhereClause ()  = " + filter.getWhereClause ());
413          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getSelectList ()   = " + filter.getSelectList ());
414          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getFromClause ()   = " + filter.getFromClause ());
415       }
416    }
417
418    public boolean mustPoll (OperationContext oc,
419                             SelectExp        filter,
420                             String           eventType,
421                             CIMObjectPath    classPath)
422       throws CIMException
423    {
424       System.out.println ("JMPI_IndicationProvider::mustPoll: oc        = " + oc);
425       System.out.println ("JMPI_IndicationProvider::mustPoll: filter    = " + filter);
426       System.out.println ("JMPI_IndicationProvider::mustPoll: eventType = " + eventType);
427       System.out.println ("JMPI_IndicationProvider::mustPoll: classPath = " + classPath);
428
429       if (filter != null)
430       {
431          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getSelectString () = " + filter.getSelectString ());
432          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getWhereClause ()  = " + filter.getWhereClause ());
433          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getSelectList ()   = " + filter.getSelectList ());
434          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getFromClause ()   = " + filter.getFromClause ());
435       }
436
437       return false;
438    }
439
440    public void activateFilter (OperationContext oc,
441                                SelectExp        filter,
442                                String           eventType,
443                                CIMObjectPath    classPath,
444                                boolean          firstActivation)
445       throws CIMException
446    {
447       System.out.println ("JMPI_IndicationProvider::activateFilter: oc              = " + oc);
448       System.out.println ("JMPI_IndicationProvider::activateFilter: filter          = " + filter);
449       System.out.println ("JMPI_IndicationProvider::activateFilter: eventType       = " + eventType);
450       System.out.println ("JMPI_IndicationProvider::activateFilter: classPath       = " + classPath);
451       System.out.println ("JMPI_IndicationProvider::activateFilter: firstActivation = " + firstActivation);
452
453       if (filter != null)
454       {
455          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getSelectString () = " + filter.getSelectString ());
456          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getWhereClause ()  = " + filter.getWhereClause ());
457          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getSelectList ()   = " + filter.getSelectList ());
458          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getFromClause ()   = " + filter.getFromClause ());
459       }
460    }
461
462    public void deActivateFilter (OperationContext oc,
463                                  SelectExp        filter,
464                                  String           eventType,
465                                  CIMObjectPath    classPath,
466                                  boolean          lastActivation)
467       throws CIMException
468    {
469       System.out.println ("JMPI_IndicationProvider::deActivateFilter: oc             = " + oc);
470       System.out.println ("JMPI_IndicationProvider::deActivateFilter: filter         = " + filter);
471       System.out.println ("JMPI_IndicationProvider::deActivateFilter: eventType      = " + eventType);
472       System.out.println ("JMPI_IndicationProvider::deActivateFilter: classPath      = " + classPath);
473       System.out.println ("JMPI_IndicationProvider::deActivateFilter: lastActivation = " + lastActivation);
474
475       if (filter != null)
476       {
477          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getSelectString () = " + filter.getSelectString ());
478          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getWhereClause ()  = " + filter.getWhereClause ());
479          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getSelectList ()   = " + filter.getSelectList ());
480          System.out.println ("JMPI_IndicationProvider::activateFilter: filter.getFromClause ()   = " + filter.getFromClause ());
481       }
482    }
483
484    private int findObjectPath (CIMObjectPath path)
485    {
486       String p = path.toString ();
487
488       for (int i = 0; i < paths.size (); i++)
489       {
490          System.out.println ("testing " + paths.elementAt (i) + " against " + path);
491
492          if (((CIMObjectPath)paths.elementAt (i)).toString ().equalsIgnoreCase (p))
493             return i;
494       }
495
496       return -1;
497    }
498 }