Debian Codegraph @

neutron_lib/callbacks/events.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 # String literals representing events associated to data store operations
14 BEFORE_CREATE = 'before_create'
15 BEFORE_READ = 'before_read'
16 BEFORE_UPDATE = 'before_update'
17 BEFORE_DELETE = 'before_delete'
18
19 PRECOMMIT_CREATE = 'precommit_create'
20 PRECOMMIT_UPDATE = 'precommit_update'
21 PRECOMMIT_DELETE = 'precommit_delete'
22 PRECOMMIT_ADD_ASSOCIATION = 'precommit_add_association'
23 PRECOMMIT_DELETE_ASSOCIATIONS = 'precommit_delete_associations'
24
25 AFTER_CREATE = 'after_create'
26 AFTER_READ = 'after_read'
27 AFTER_UPDATE = 'after_update'
28 AFTER_DELETE = 'after_delete'
29 AFTER_STATUS_UPDATE = 'after_status_update'
30
31 # String literals representing events associated to API operations
32 BEFORE_RESPONSE = 'before_response'
33 AFTER_REQUEST = 'after_request'
34
35 # String literals representing events associated to process operations
36 BEFORE_INIT = 'before_init'
37 BEFORE_SPAWN = 'before_spawn' # sent per process
38 AFTER_SPAWN = 'after_spawn' # sent per process
39 AFTER_INIT = 'after_init' # sent per worker
40
41 # String literals representing events associated to error conditions
42 ABORT_CREATE = 'abort_create'
43 ABORT_READ = 'abort_read'
44 ABORT_UPDATE = 'abort_update'
45 ABORT_DELETE = 'abort_delete'
46
47 ABORT = 'abort_'
48 BEFORE = 'before_'
49 PRECOMMIT = 'precommit_'
50
51 OVS_RESTARTED = 'ovs_restarted'
52
53
54 def is_cancellable_event(event):
55 """Return if an event is cancellable by definition"""
56 return (event.startswith(BEFORE) or
57 event.startswith(PRECOMMIT))
58
59
60 class EventPayload:
61 """Base event payload object.
62
63 This class is intended to be the super class for all event payloads. As
64 such, it defines common attributes many events are likely to use in their
65 payload. Note that event attributes are passed by reference; no copying
66 of states, metadata or request_body is performed and thus consumers should
67 not modify payload references.
68
69 For more information, see the callbacks dev-ref documentation for this
70 project.
71 """
72
73 def __init__(self, context, metadata=None, request_body=None,
74 states=None, resource_id=None):
75 # the event context
76 self.context = context
77
78 # NOTE(boden): longer term we should consider removing metadata
79 # optional 'unstructured' (key,value) pairs for special needs
80 self.metadata = metadata if metadata else {}
81
82 # the request body associated to the resource
83 self.request_body = request_body
84
85 # an iterable of states for the resource from the newest to the oldest
86 # for example db states or api request/response
87 # the actual object type for states will vary depending on event caller
88 self.states = states if states else []
89
90 # a unique ID for the event resource; may be None if the resource
91 # isn't created yet
92 self.resource_id = resource_id
93
94 @property
95 def has_states(self):
96 """Determines if this event payload has any states.
97
98 :returns: True if this event payload has states, otherwise False.
99 """
100 return len(self.states) > 0
101
102 @property
103 def latest_state(self):
104 """Returns the latest state for the event payload.
105
106 :returns: The last state of this event payload if has_state else None.
107 """
108 return self.states[-1] if self.has_states else None
109
110
111 class DBEventPayload(EventPayload):
112 """The payload for data store events payloads."""
113
114 def __init__(self, context, metadata=None, request_body=None,
115 states=None, resource_id=None, desired_state=None):
116
117 super().__init__(
118 context, metadata=metadata, request_body=request_body,
119 states=states, resource_id=resource_id)
120
121 # the model object to be persisted in pre create/commit payloads
122 self.desired_state = desired_state
123
124 @property
125 def is_persisted(self):
126 """Determine if the resource for this event payload is persisted.
127
128 :returns: True if this payload's resource is persisted, otherwise
129 False.
130 """
131 return self.resource_id is not None and self.has_states
132
133 @property
134 def is_to_be_committed(self):
135 """"Determine if the event payload resource is to be committed.
136
137 :returns: True if the desired state has been populated, else False.
138 """
139 return self.desired_state is not None
140
141 @property
142 def latest_state(self):
143 """Returns the latest state for the event payload resource.
144
145 :returns: If this payload has a desired_state its returned, otherwise
146 latest_state is returned.
147 """
148 return (self.desired_state or super().latest_state)
149
150
151 class APIEventPayload(EventPayload):
152 """The payload for API events."""
153
154 def __init__(self, context, method_name, action,
155 metadata=None, request_body=None, states=None,
156 resource_id=None, collection_name=None):
157
158 super().__init__(
159 context, metadata=metadata, request_body=request_body,
160 states=states, resource_id=resource_id)
161
162 self.method_name = method_name
163 self.action = action
164 self.collection_name = collection_name