#!/usr/bin/python
# Copyright IBM Corp. 2007
#
# indication_tester.py - Tool for testing indication subscription and
#                        delivery against a CIMOM
# Author: Dan Smith <danms@us.ibm.com>

import sys
from optparse import OptionParser
import BaseHTTPServer
import httplib
import base64
from xml.dom.minidom import parse, parseString

def filter_xml(name, type, ns, sysname):
    return """
    <?xml version="1.0" encoding="utf-8"?>
    <CIM CIMVERSION="2.0" DTDVERSION="2.0">
    <MESSAGE ID="4711" PROTOCOLVERSION="1.0">
      <SIMPLEREQ>
        <IMETHODCALL NAME="CreateInstance">
          <LOCALNAMESPACEPATH>
            <NAMESPACE NAME="root"/>
            <NAMESPACE NAME="PG_InterOp"/>
          </LOCALNAMESPACEPATH>
          <IPARAMVALUE NAME="NewInstance">
              <INSTANCE CLASSNAME="CIM_IndicationFilter">
                <PROPERTY NAME="SystemCreationClassName" TYPE="string">
                  <VALUE>CIM_ComputerSystem</VALUE>
                </PROPERTY>
                <PROPERTY NAME="SystemName" TYPE="string">
                  <VALUE>%s</VALUE>
                </PROPERTY>
                <PROPERTY NAME="CreationClassName" TYPE="string">
                  <VALUE>CIM_IndicationFilter</VALUE>
                </PROPERTY>
                <PROPERTY NAME="Name" TYPE="string">
                  <VALUE>%sFilter</VALUE>
                </PROPERTY>
                <PROPERTY NAME="Query" TYPE="string">
                  <VALUE> SELECT * FROM %s
                  </VALUE>
                </PROPERTY>
                <PROPERTY NAME="QueryLanguage" TYPE="string">
                  <VALUE>WQL</VALUE>
                </PROPERTY>
                <PROPERTY NAME="SourceNamespace" TYPE="string">
                  <VALUE>%s</VALUE>
                </PROPERTY>
              </INSTANCE>
            </IPARAMVALUE>
          </IMETHODCALL>
        </SIMPLEREQ>
      </MESSAGE>
    </CIM>
    """ % (sysname, name, type, ns)

def handler_xml(name, port, sysname):
    return """
    <?xml version="1.0" encoding="utf-8"?>
    <CIM CIMVERSION="2.0" DTDVERSION="2.0">
      <MESSAGE ID="4711" PROTOCOLVERSION="1.0">
        <SIMPLEREQ>
        <IMETHODCALL NAME="CreateInstance">
            <LOCALNAMESPACEPATH>
              <NAMESPACE NAME="root"/>
              <NAMESPACE NAME="PG_InterOp"/>
            </LOCALNAMESPACEPATH>
            <IPARAMVALUE NAME="NewInstance">
              <INSTANCE CLASSNAME="CIM_IndicationHandlerCIMXML">
                <PROPERTY N
AME="SystemCreationClassName" TYPE="string">
                  <VALUE>CIM_ComputerSystem</VALUE>
                </PROPERTY>
                <PROPERTY NAME="SystemName" TYPE="string">
                  <VALUE>%s</VALUE>
                </PROPERTY>
                <PROPERTY NAME="CreationClassName" TYPE="string">
                  <VALUE>CIM_IndicationHandlerCIMXML</VALUE>
                </PROPERTY>
                <PROPERTY NAME
="Name" TYPE="string">
                  <VALUE>%sHandler</VALUE>
                </PROPERTY>
                <PROPERTY NAME="Destination" TYPE="string">
                  <VALUE>http://localhost:%i</VALUE>
                </PROPERTY>
              </INSTANCE>
            </IPARAMVALUE>
          </IMETHODCALL>
        </SIMPLEREQ>
      </MESSAGE>
      </CIM>
      """ % (sysname, name, port)

def subscription_xml(name, sysname):
    return """
    <?xml version="1.0" encoding="utf-8"?>
    <CIM CIMVERSION="2.0" DTDVERSION="2.0">
      <MESSAGE ID="4711" PROTOCOLVERSION="1.0">
        <SIMPLEREQ>
          <IMETHODCALL NAME="CreateInstance">
            <LOCALNAMESPACEPATH>
              <NAMESPACE NAME="root"/>
              <NAMESPACE NAME="PG_InterOp"/>
            </LOCALNAMESPACEPATH>
            <IPARAMVALUE NAME="NewInstance">
              <INSTANCE CLASSNAME="CIM_IndicationSubscription">
                <PROPERTY.REFERENCE NAME="Filter"
                                    REFERENCECLASS="CIM_IndicationFilter">
                  <VALUE.REFERENCE>
                    <INSTANCENAME CLASSNAME="CIM_IndicationFilter">
                      <KEYBINDING NAME="SystemCreationClassName">
                        <KEYVALUE VALUETYPE="string">
                        CIM_ComputerSystem
                        </KEYVALUE>
                      </KEYBINDING>
                      <KEYBINDING NAME="SystemName">
                        <KEYVALUE VALUETYPE="string">
                        %s
                        </KEYVALUE>
                      </KEYBINDING>
                      <KEYBINDING NAME="CreationClassName">
                        <KEYVALUE VALUETYPE="string">
                        CIM_IndicationFilter
                        </KEYVALUE>
                      </KEYBINDING>
                      <KEYBINDING NAME="Name">
                        <KEYVALUE VALUETYPE="string">
                        %sFilter
                        </KEYVALUE>
                      </KEYBINDING>
                    </INSTANCENAME>
                  </VALUE.REFERENCE>
                </PROPERTY.REFERENCE>
                <PROPERTY.REFERENCE NAME="Handler"
                                    REFERENCECLASS="CIM_IndicationHandler">
                  <VALUE.REFERENCE>
                    <INSTANCENAME CLASSNAME="CIM_IndicationHandlerCIMXML">
                      <KEYBINDING NAME="SystemCreationClassName">
                        <KEYVALUE VALUETYPE="string">
                        CIM_ComputerSystem
                        </KEYVALUE>
                      </KEYBINDING>
                      <KEYBINDING NAME="SystemName">
                        <KEYVALUE VALUETYPE="string">
                        %s
                        </KEYVALUE>
                      </KEYBINDING>
                      <KEYBINDING NAME="CreationClassName">
                        <KEYVALUE VALUETYPE="string">
                        CIM_IndicationHandlerCIMXML
                        </KEYVALUE>
                      </KEYBINDING>
                      <KEYBINDING NAME="Name">
                        <KEYVALUE VALUETYPE="string">
                        %sHandler
                        </KEYVALUE>
                      </KEYBINDING>
                    </INSTANCENAME>
                  </VALUE.REFERENCE>
                </PROPERTY.REFERENCE>
                <PROPERTY NAME="SubscriptionState" TYPE="uint16">
                  <VALUE> 2 </VALUE>
                </PROPERTY>
              </INSTANCE>
            </IPARAMVALUE>
          </IMETHODCALL>
        </SIMPLEREQ>
      </MESSAGE>
      </CIM>
      """ % (sysname, name, sysname, name)

def delete_inst_xml(name, type, sysname, inst_name):
    return """
    <?xml version="1.0" encoding="utf-8"?>
    <CIM CIMVERSION="2.0" DTDVERSION="2.0">
      <MESSAGE ID="4711" PROTOCOLVERSION="1.0">
        <SIMPLEREQ>
          <IMETHODCALL NAME="DeleteInstance">
            <LOCALNAMESPACEPATH>
              <NAMESPACE NAME="root"/>
              <NAMESPACE NAME="PG_InterOp"/>
            </LOCALNAMESPACEPATH>
            <IPARAMVALUE NAME="InstanceName">
              <INSTANCENAME CLASSNAME="CIM_Indication%s">
                <KEYBINDING NAME="SystemCreationClassName">
                  <KEYVALUE>CIM_ComputerSystem</KEYVALUE>
                </KEYBINDING>
                <KEYBINDING NAME="SystemName">
                  <KEYVALUE>%s</KEYVALUE>
                </KEYBINDING>
                <KEYBINDING NAME="CreationClassName">
                  <KEYVALUE>CIM_Indication%s</KEYVALUE>
                </KEYBINDING>
                <KEYBINDING NAME="Name">
                  <KEYVALUE>%s</KEYVALUE>
                </KEYBINDING>
              </INSTANCENAME>
            </IPARAMVALUE>
          </IMETHODCALL>
        </SIMPLEREQ>
      </MESSAGE>
    </CIM>;
    """ % (type, sysname, type, inst_name);

def delete_sub_xml(name, sysname):
    return """
    <?xml version="1.0" encoding="utf-8"?>
    <CIM CIMVERSION="2.0" DTDVERSION="2.0">
      <MESSAGE ID="4711" PROTOCOLVERSION="1.0">
        <SIMPLEREQ>
          <IMETHODCALL NAME="DeleteInstance">
            <LOCALNAMESPACEPATH>
              <NAMESPACE NAME="root"/>
              <NAMESPACE NAME="