PEP 55 Update license on source files to current license text and date
[tpot/pegasus/.git] / src / tools / providerreg / providerreg.cpp
1 //%2003////////////////////////////////////////////////////////////////////////
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 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 // 
15 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
16 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
17 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
18 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 //==============================================================================
25 //
26 // Author: Karl Schopmeyer (k.schopmeyer@opengroup.org)
27 //
28 // Modified By: Carol Ann Krug Graves, Hewlett-Packard Company
29 //                (carolann_graves@hp.com)
30 //
31 //%/////////////////////////////////////////////////////////////////////////////
32
33 #include <cassert>
34 #include <fstream>
35 #include <fstream>
36 #include <iostream>
37 #include <iomanip>
38
39 #include <Pegasus/Repository/CIMRepository.h>
40 //#include <Pegasus/Client/CIMClient.h>
41 //#include <Pegasus/Common/Selector.h>
42 #include <Pegasus/Common/OptionManager.h>
43 #include <Pegasus/Common/FileSystem.h>
44
45
46 using namespace Pegasus;
47 using namespace std;
48
49
50 /** RegisterProvider - Sets Registration for Provider
51 This executable sets the appropriate registration parameters
52 to register a provider or unregister a provider.
53 It can be used to register a provider against a single class and
54 define it as a property, method, or instance provider.
55
56 It can be used either to register or unregister the provider.
57
58 The parameters of the call are
59
60 Options
61     -U - If set, tries to unregister the provider (default is register)
62     -D - Simply show the current state of registration
63     -M - Method provider registraion
64     -P - Property provider registration
65     -V - View all current provider registrations
66     
67 Class - CIMReference with the Namespace and classname to register.
68
69 Method or property name.
70
71 Registration type is defined through the CIMReference.
72 If the reference is a class name only, the registration is on the
73 class.  If it includes a method name, it is on the method, etc.
74 */
75
76 /** ErrorExit - Print out the error message as an
77     and get out.
78     @param - Text for error message
79     @return - None, Terminates the program
80     @execption - This function terminates the program
81     ATTN: Should write to stderr
82 */
83 void ErrorExit(const String& message)
84 {
85
86     cout << message << endl;
87     exit(1);
88 }
89
90 void GetOptions(
91     OptionManager& om,
92     int& argc, 
93     char** argv, 
94     const String& pegasusHome)
95 {
96     static struct OptionRow options[] =
97     {
98         {"unregister", "false", false, Option::BOOLEAN, 0, 0, "u"},
99         {"display", "false", false, Option::BOOLEAN, 0, 0, "d"},
100         {"method", "false", false, Option::BOOLEAN, 0, 0, "m"},
101         {"property", "false", false, Option::BOOLEAN, 0, 0, "p"},
102         {"version", "false", false, Option::BOOLEAN, 0, 0, "v"},
103         {"help", "false", false, Option::BOOLEAN, 0, 0, "h"}
104     };
105     const Uint32 NUM_OPTIONS = sizeof(options) / sizeof(options[0]);
106
107     om.registerOptions(options, NUM_OPTIONS);
108
109     String configFile = pegasusHome + "/cimserver.conf";
110
111     if (FileSystem::exists(configFile))
112         om.mergeFile(configFile);
113
114     om.mergeCommandLine(argc, argv);
115
116     om.checkRequiredOptions();
117 }
118
119 void PrintHelp(const char* arg0)
120 {
121     Uint32 PROGRAM_VERSION = 1;
122     cout << '\n';
123     cout << "Provider Registration  Version " << PROGRAM_VERSION << endl;
124     cout << '\n';
125     cout << "Usage: " << arg0 << " [options] [namespace] [class]"; 
126     cout << " [providerID]\n" << '\n';
127     cout << " Registers ProviderIDs for classes in repository\n\n";
128     cout << "    -h prints this help message\n";
129     cout << "    -u unregister the class defined from the provider\n";
130     cout << "    -m method registration (Not Enabled)\n";
131     cout << "    -p property registration provider (Not Enabled)\n";
132     cout << "    -v print out the program version number\n";
133     cout << "   Parameters required are\n";
134     cout << "         Namespace ClassName  ProviderID\n";
135     cout << "\n providerreg root/cimv2 process MyProvider\n";
136     cout << "            registers class process to provider MyProvider \n";
137     cout << "\n providerreg -u root/cimv2 process\n";
138     cout << "            unregisters class process\n";
139     cout << "   NOTE: unregistration does not require providerID param\n";
140     cout << endl;
141 }
142
143
144
145 // Functions to do
146 void registerInstanceProvider(
147             CIMRepository& r, 
148             Boolean registering,
149             String& nameSpace,
150             String& className, 
151             String& providerName)
152 {
153       CIMClass c1;
154     
155     // Create the new qualifier
156     try
157     {
158       
159         c1 = r.getClass(
160             nameSpace, className, false, false, true);
161         /*
162         virtual CIMClass getClass(
163         const String& nameSpace,
164         const String& className,
165         Boolean localOnly = true,
166         Boolean includeQualifiers = true,
167         Boolean includeClassOrigin = false,
168         const Array<String>& propertyList = EmptyStringArray());
169         */
170         // CIMClass c2 = client.getClass(NAMESPACE, "SubClass", false);
171         /*
172         CIMConstClass cc1;
173         cc1 = r.getClass(NAMESPACE, "MyClass");
174         */
175
176     }
177     catch (Exception& e)
178     {
179         // Catch when class does not exist.
180         cout << "Class does not exist " << e.getMessage() << endl;
181     }
182
183     if (registering)
184     {
185         //test if already registered
186
187         cout << "registering " << className << " for " 
188                 << providerName << endl;
189         Uint32 pos = c1.findQualifier("Provider");
190         if (pos != PEG_NOT_FOUND)
191             {
192                 // NOTE that we should modify   
193                 cout << "Provider exists" << endl;
194                 c1.removeQualifier(pos);
195             }
196         c1.addQualifier(CIMQualifier("Provider", providerName));
197         cout << "Provider " << providerName << " changed." << endl;
198         r.modifyClass(nameSpace, c1);
199
200 }
201     else  // Here we deregister
202     {
203         // test not registered
204         cout << "unregistering "<< endl;
205         Uint32 qualifierIndex;
206         qualifierIndex = c1.findQualifier("Provider");
207         if (qualifierIndex != PEG_NOT_FOUND)
208         {
209             c1.removeQualifier(qualifierIndex);
210         }
211         else
212         {
213             // Does not exist.  Issue message
214             cout << "Not Registered" << endl;
215         }
216
217     }
218 }
219
220 void registerMethodProvider(
221                    CIMRepository& r, 
222                    Boolean toRegister,
223                    String& nameSpace,
224                    String& className, 
225                    String& providerName)
226
227 {
228     CIMClass c = r.getClass(
229         nameSpace, "CIM_ComputerSystem", false, false, true);
230
231     c.print();
232 }
233
234 void registerPropertyProvider(
235                     CIMRepository& r, 
236                     Boolean toRegister,
237                     String& nameSpace,
238                     String& className, 
239                     String& providerName)
240
241 {
242     CIMClass c = r.getClass(
243         nameSpace, "CIM_ComputerSystem", false, false, true);
244
245     c.print();
246 }
247
248 void showOneProviderRegistration(
249     CIMRepository& r,
250     String& nameSpace,
251     CIMClass& c1,
252     String& providerName)
253 {
254     try
255     {
256         Uint32 pos;
257         pos = c1.findQualifier("provider");
258         if (pos != PEG_NOT_FOUND)
259         {
260               // Display the qualifier.
261               CIMQualifier q1 = c1.getQualifier(pos);
262
263               String providerId;
264               q1.getValue().get(providerId);
265               String fill = "                       ";
266               String name = c1.getClassName();
267               cout << name
268                    << fill.subString(0, 20-name.size())
269                    << providerId
270                    << endl;
271       }
272     }
273     catch (Exception& e)
274     {
275         // Catch when class does not exist.
276         cout << "Class does not exist " << e.getMessage() << endl;
277     }
278
279
280 }
281
282 void showProviderRegistrations (
283                     CIMRepository& r,
284                     String& nameSpace,
285                     String& className,
286                     String& providerName)
287 {
288     
289     cout << "Show Provider Registrations for Namespace " 
290          << nameSpace << endl;
291     try
292     {
293         
294         /* enumerateClasses
295         virtual Array<CIMClass> enumerateClasses(
296             const String& nameSpace,
297             const String& className = String::EMPTY,
298             Boolean deepInheritance = false,
299             Boolean localOnly = true,
300             Boolean includeQualifiers  = true,
301             Boolean includeClassOrigin = false);
302         */
303         cout << "Class               Provider" << endl;
304         cout << "----------------------------" << endl;
305
306
307         Uint32 providerCount = 0;
308         Array<CIMClass> classList = r.enumerateClasses(
309                 nameSpace, className, true, false, true);
310
311
312         for (Uint32 i = 0, n = classList.size(); i < n; i++)
313         {
314             if (classList[i].findQualifier("provider") != PEG_NOT_FOUND)
315             {
316                 showOneProviderRegistration(
317                             r,
318                             nameSpace,
319                             classList[i],
320                             providerName);
321                 providerCount++;
322             }
323         }
324         cout << endl << providerCount << " Classes found" << endl;
325
326     }
327     catch (Exception& e)
328     {
329         // Catch when class does not exist.
330         cout << "Class does not exist " << e.getMessage() << endl;
331     }
332
333
334 }
335
336 // Questions: if registered already for class, do we have to
337 // register for property or method?
338 //
339
340  int main(int argc, char** argv)
341 {
342     
343     // Get the parameters from the command line
344     // arg2 is objectpath
345     // arg3 is provider
346     // make - parameter to deregister -d
347     if (argc < 2)
348         ErrorExit("unexpected command line arguments");
349     Boolean toRegister = true;
350     String className;
351     className = "Process";
352     String providerName;
353     providerName = "MyProvider";
354     String nameSpace;
355     nameSpace = "root/cimv2";
356     String pegasusHome;
357
358     OptionManager om;
359     try
360     {
361         
362         GetOptions(om, argc, argv, pegasusHome);
363         // om.print();
364     }
365     catch (Exception& e)
366     {
367         cerr << argv[0] << ": " << e.getMessage() << endl;
368         exit(1);
369     }
370
371     String versionOption;
372     if (om.lookupValue("version", versionOption) && versionOption == "true")
373     {
374         cerr << "Registration Version 1.0" << endl;
375         exit(0);
376     }
377
378     // Check to see if user asked for help (-h otpion):
379
380     String helpOption;
381
382     if (om.lookupValue("help", helpOption) && helpOption == "true")
383     {
384         PrintHelp(argv[0]);
385         exit(0);
386     }
387     // At this point, all options should have been extracted; print an
388     // error if there are any remaining:
389
390     //if (argc != 1)
391     //{
392     //    cerr << argv[0] << ": unrecognized options: ";
393     //
394     //    for (int i = 1; i < argc; i++)
395     //        cerr << argv[i] << ' ';
396     //    cout << endl;
397     //    // exit(1);
398     //}
399
400     try
401     {
402         const char *tmp = getenv("PEGASUS_HOME");
403         if (!tmp)
404             ErrorExit("PEGASUS_HOME environment variable missing");
405
406         pegasusHome = tmp;
407         FileSystem::translateSlashes(pegasusHome);
408         
409         String repositoryDir = pegasusHome;
410         repositoryDir += "/repository";
411
412         // Test for legal repository here.
413
414         CIMRepository r(repositoryDir);
415
416         // Process the display option
417         if (om.valueEquals("display", "true")) 
418         {
419             if (argc > 1)
420                 nameSpace = argv[1];
421             if (argc == 2)
422                 className = argv[2];
423             else className = "";
424             showProviderRegistrations (
425                     r, nameSpace, className,providerName);
426         }
427         else
428         {   
429             if (argc < 2)
430                 ErrorExit("Namespace and classname required");
431             nameSpace = argv[1];
432             className = argv[2];
433             if (om.lookupValue("unregister", helpOption) && helpOption == 
434                     "false")
435             {
436                 // if registering, get the provider name parameter
437                 if (argc < 3)
438                     ErrorExit("Provider name parameter required");
439                 else
440                 {
441                     providerName = argv[3];
442                 }
443             }
444             else  // set to unregister
445                 toRegister = false;
446
447             cout << (toRegister ? "Registering " :  "Unregistering ")
448                  << "Class " << nameSpace << ":" <<className
449                  << " for Provider " << providerName << endl;
450             
451             registerInstanceProvider(r, 
452                                  toRegister, 
453                                  nameSpace,  
454                                  className, 
455                                  providerName);
456         }
457
458
459     }
460     catch(Exception& e)
461     {
462         std::cerr << "Error: " << e.getMessage() << std::endl;
463         exit(1);
464     }
465
466     std::cout << "OK" << std::endl;
467
468     return 0;
469 }
470