Coverage for nova/virt/event.py: 97%
37 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-17 15:08 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-17 15:08 +0000
1# Copyright 2013 Red Hat, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
15"""
16Asynchronous event notifications from virtualization drivers.
18This module defines a set of classes representing data for
19various asynchronous events that can occur in a virtualization
20driver.
21"""
23import time
25from nova.i18n import _
27EVENT_LIFECYCLE_STARTED = 0
28EVENT_LIFECYCLE_STOPPED = 1
29EVENT_LIFECYCLE_PAUSED = 2
30EVENT_LIFECYCLE_RESUMED = 3
31EVENT_LIFECYCLE_SUSPENDED = 4
32EVENT_LIFECYCLE_POSTCOPY_STARTED = 5
33EVENT_LIFECYCLE_MIGRATION_COMPLETED = 6
36NAMES = {
37 EVENT_LIFECYCLE_STARTED: _('Started'),
38 EVENT_LIFECYCLE_STOPPED: _('Stopped'),
39 EVENT_LIFECYCLE_PAUSED: _('Paused'),
40 EVENT_LIFECYCLE_RESUMED: _('Resumed'),
41 EVENT_LIFECYCLE_SUSPENDED: _('Suspended'),
42 EVENT_LIFECYCLE_POSTCOPY_STARTED: _('Postcopy started'),
43 EVENT_LIFECYCLE_MIGRATION_COMPLETED: _('Migration completed'),
44}
47class Event(object):
48 """Base class for all events emitted by a hypervisor.
50 All events emitted by a virtualization driver are
51 subclasses of this base object. The only generic
52 information recorded in the base class is a timestamp
53 indicating when the event first occurred. The timestamp
54 is recorded as fractional seconds since the UNIX epoch.
55 """
57 def __init__(self, timestamp=None):
58 if timestamp is None:
59 self.timestamp = time.time()
60 else:
61 self.timestamp = timestamp
63 def get_timestamp(self):
64 return self.timestamp
66 def __repr__(self):
67 return "<%s: %s>" % (
68 self.__class__.__name__,
69 self.timestamp)
72class InstanceEvent(Event):
73 """Base class for all instance events.
75 All events emitted by a virtualization driver which
76 are associated with a virtual domain instance are
77 subclasses of this base object. This object records
78 the UUID associated with the instance.
79 """
81 def __init__(self, uuid, timestamp=None):
82 super(InstanceEvent, self).__init__(timestamp)
84 self.uuid = uuid
86 def get_instance_uuid(self):
87 return self.uuid
89 def __repr__(self):
90 return "<%s: %s, %s>" % (
91 self.__class__.__name__,
92 self.timestamp,
93 self.uuid)
96class LifecycleEvent(InstanceEvent):
97 """Class for instance lifecycle state change events.
99 When a virtual domain instance lifecycle state changes,
100 events of this class are emitted. The EVENT_LIFECYCLE_XX
101 constants defined why lifecycle change occurred. This
102 event allows detection of an instance starting/stopping
103 without need for polling.
104 """
106 def __init__(self, uuid, transition, timestamp=None):
107 super(LifecycleEvent, self).__init__(uuid, timestamp)
109 self.transition = transition
111 def get_transition(self):
112 return self.transition
114 def get_name(self):
115 return NAMES.get(self.transition, _('Unknown'))
117 def __repr__(self):
118 return "<%s: %s, %s => %s>" % (
119 self.__class__.__name__,
120 self.timestamp,
121 self.uuid,
122 self.get_name())