summaryrefslogtreecommitdiffstats
path: root/src/software/lmi/software/core/InstMethodCall.py
blob: 82b7b1ecab764106d740d0772494bb2738409927 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- encoding: utf-8 -*-
# Software Management Providers
#
# Copyright (C) 2012-2013 Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
CIM values for enumeration types of CIM_InstMethodCall indication class.
"""

import pywbem
import socket

from lmi.common import cmpi_logging
from lmi.software.core import Job
from lmi.software.core import InstallationService
from lmi.software.yumdb import jobs, errors

class Values(object):
    class ReturnValueType(object):
        boolean = pywbem.Uint16(2)
        string = pywbem.Uint16(3)
        char16 = pywbem.Uint16(4)
        uint8 = pywbem.Uint16(5)
        sint8 = pywbem.Uint16(6)
        uint16 = pywbem.Uint16(7)
        sint16 = pywbem.Uint16(8)
        uint32 = pywbem.Uint16(9)
        sint32 = pywbem.Uint16(10)
        uint64 = pywbem.Uint16(11)
        sint64 = pywbem.Uint16(12)
        datetime = pywbem.Uint16(13)
        real32 = pywbem.Uint16(14)
        real64 = pywbem.Uint16(15)
        reference = pywbem.Uint16(16)
        # DMTF_Reserved = ..
        _reverse_map = {
                2: 'boolean',
                3: 'string',
                4: 'char16',
                5: 'uint8',
                6: 'sint8',
                7: 'uint16',
                8: 'sint16',
                9: 'uint32',
                10: 'sint32',
                11: 'uint64',
                12: 'sint64',
                13: 'datetime',
                14: 'real32',
                15: 'real64',
                16: 'reference'
        }

    class PerceivedSeverity(object):
        Unknown = pywbem.Uint16(0)
        Other = pywbem.Uint16(1)
        Information = pywbem.Uint16(2)
        Degraded_Warning = pywbem.Uint16(3)
        Minor = pywbem.Uint16(4)
        Major = pywbem.Uint16(5)
        Critical = pywbem.Uint16(6)
        Fatal_NonRecoverable = pywbem.Uint16(7)
        # DMTF_Reserved = ..
        _reverse_map = {
                0: 'Unknown',
                1: 'Other',
                2: 'Information',
                3: 'Degraded/Warning',
                4: 'Minor',
                5: 'Major',
                6: 'Critical',
                7: 'Fatal/NonRecoverable'
        }

@cmpi_logging.trace_function
def job2model(job, pre=True):
    """
    Create post or pre indication instance used by clients to subscribe
    to job's state changes.

    :param job: (``YumJob``) Instance of job created as a result of method
        invocation.
    :param pre: (``bool``) says, whether to make pre or post indication
        instance.
    :rtype CIMInstance of CIM_InstMethodCall.
    """
    if not isinstance(job, jobs.YumJob):
        raise TypeError("job must be a YumJob")
    if not pre and job.state == job.NEW or job.state == job.RUNNING:
        raise ValueError("job must be finished to make a post indication"
            " instance")
    path = pywbem.CIMInstanceName(
            classname="CIM_InstMethodCall",
            host=socket.gethostname(),
            namespace="root/cimv2")
    inst = pywbem.CIMInstance(classname="CIM_InstMethodCall", path=path)
    src_instance = Job.job2model(job, keys_only=False)
    inst['SourceInstance'] = pywbem.CIMProperty("SourceInstance",
            type="instance", value=src_instance)
    inst['SourceInstanceModelPath'] = \
            str(src_instance.path)    #pylint: disable=E1103
    inst['MethodName'] = Job.JOB_METHOD_NAMES[
            job.metadata["method"]]
    inst['MethodParameters'] = Job.make_method_params(
            job, '__MethodParameters', True, not pre)
    inst['PreCall'] = pre

    if not pre:
        inst["Error"] = pywbem.CIMProperty("Error", type="instance",
                is_array=True, value=[])
        error = Job.job2error(job)
        if error is not None:
            inst["Error"].append(error)
        inst["ReturnValueType"] = Values.ReturnValueType.uint32
        if job.state == job.COMPLETED:
            inst["ReturnValue"] = str(InstallationService. \
                    Values.InstallFromURI.Job_Completed_with_No_Error)
        elif job.state == job.EXCEPTION:
            if issubclass(job.result_data[0], (
                    errors.InvalidNevra, errors.InvalidURI,
                    errors.PackageNotFound)):
                inst["ReturnValue"] = str(InstallationService.Values. \
                        InstallFromURI.Invalid_Parameter)
            else:
                inst["ReturnValue"] = str(InstallationService.Values. \
                        InstallFromURI.Failed)
        else:
            inst["ReturnValue"] = str(InstallationService.Values. \
                    InstallFromURI.Unspecified_Error)
    return inst