File: poll_service.py

package info (click to toggle)
python-openleadr-python 0.5.34%2Bdfsg.1-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,496 kB
  • sloc: python: 6,942; xml: 663; makefile: 32; sh: 18
file content (152 lines) | stat: -rw-r--r-- 12,245 bytes parent folder | download
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
145
146
147
148
149
150
151
152
# SPDX-License-Identifier: Apache-2.0

# Copyright 2020 Contributors to OpenLEADR

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from openleadr.service import service, handler, VTNService
from openleadr import objects
import asyncio
from dataclasses import asdict
import logging
logger = logging.getLogger('openleadr')

# ╔══════════════════════════════════════════════════════════════════════════╗
# ║                             POLLING SERVICE                              ║
# ╚══════════════════════════════════════════════════════════════════════════╝
#
# oadrPoll is a service independent polling mechanism used by VENs in a PULL
# model to request pending service operations from the VTN. The VEN queries
# the poll endpoint and the VTN re- sponds with the same message that it would
# have “pushed” had it been a PUSH VEN. If there are multiple messages pending
# a “push,” the VEN will continue to query the poll endpoint until there are
# no new messages and the VTN responds with an eiResponse payload.
#
# ┌──────────────────────────────────────────────────────────────────────────┐
# │ The VEN can poll for any messages that we have for them. If we have no   │
# │ (more) messages, we send a generic oadrResponse:                         │
# │ ┌────┐                                                            ┌────┐ │
# │ │VEN │                                                            │VTN │ │
# │ └─┬──┘                                                            └─┬──┘ │
# │   │───────────────────────────oadrPoll()───────────────────────────▶│    │
# │   │                                                                 │    │
# │   │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrResponse() ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│    │
# │   │                                                                 │    │
# │                                                                          │
# └──────────────────────────────────────────────────────────────────────────┘
# ┌──────────────────────────────────────────────────────────────────────────┐
# │ If we have an Event, we expect the following:                            │
# │                                                                          │
# │ ┌────┐                                                            ┌────┐ │
# │ │VEN │                                                            │VTN │ │
# │ └─┬──┘                                                            └─┬──┘ │
# │   │───────────────────────────oadrPoll()───────────────────────────▶│    │
# │   │                                                                 │    │
# │   │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ oadrCreateEvent() ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│    │
# │   │                                                                 │    │
# │   │───────────────────────oadrCreatedEvent()───────────────────────▶│    │
# │   │                                                                 │    │
# │   │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrResponse() ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│    │
# │   │                                                                 │    │
# │                                                                          │
# └──────────────────────────────────────────────────────────────────────────┘
# ┌──────────────────────────────────────────────────────────────────────────┐
# │ For Reports:                                                             │
# │                                                                          │
# │ ┌────┐                                                            ┌────┐ │
# │ │VEN │                                                            │VTN │ │
# │ └─┬──┘                                                            └─┬──┘ │
# │   │───────────────────────────oadrPoll()───────────────────────────▶│    │
# │   │                                                                 │    │
# │   │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrCreateReport() ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│    │
# │   │                                                                 │    │
# │   │───────────────────────oadrCreatedReport()──────────────────────▶│    │
# │   │                                                                 │    │
# │   │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrResponse() ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│    │
# │   │                                                                 │    │
# │                                                                          │
# └──────────────────────────────────────────────────────────────────────────┘
# ┌──────────────────────────────────────────────────────────────────────────┐
# │ If re-registration is neccessary:                                        │
# │                                                                          │
# │ ┌────┐                                                            ┌────┐ │
# │ │VEN │                                                            │VTN │ │
# │ └─┬──┘                                                            └─┬──┘ │
# │   │───────────────────────────oadrPoll()───────────────────────────▶│    │
# │   │                                                                 │    │
# │   │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrRequestReregistration()─ ─ ─ ─ ─ ─ ─ ─ ─ ─│    │
# │   │                                                                 │    │
# │   │─────────────────────────oadrResponse()─────────────────────────▶│    │
# │   │                                                                 │    │
# │   │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ HTTP 200─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│    │
# │   │                                                                 │    │
# │                                                                          │
# │   │──────────────────oadrCreatePartyRegistration()─────────────────▶│    │
# │   │                                                                 │    │
# │   │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrRequestReregistration()─ ─ ─ ─ ─ ─ ─ ─ ─ ─│    │
# │   │                                                                 │    │
# │                                                                          │
# └──────────────────────────────────────────────────────────────────────────┘


@service('OadrPoll')
class PollService(VTNService):

    def __init__(self, vtn_id, polling_method='internal', event_service=None, report_service=None):
        super().__init__(vtn_id)
        self.polling_method = polling_method
        self.events_updated = {}
        self.report_requests = {}
        self.event_service = event_service
        self.report_service = report_service

    @handler('oadrPoll')
    async def poll(self, payload):
        """
        Handle the request to the oadrPoll service. This either calls a previously registered
        `on_poll` handler, or it retrieves the next message from the internal queue.
        """
        if self.polling_method == 'external':
            result = self.on_poll(ven_id=payload['ven_id'])
        elif self.events_updated.get(payload['ven_id']):
            # Send a oadrDistributeEvent whenever the events were updated
            result = await self.event_service.request_event({'ven_id': payload['ven_id']})
            self.events_updated[payload['ven_id']] = False
        else:
            return 'oadrResponse', {}

        if asyncio.iscoroutine(result):
            result = await result
        if result is None:
            return 'oadrResponse', {}
        if isinstance(result, tuple):
            return result
        if isinstance(result, list):
            return 'oadrDistributeEvent', result
        if isinstance(result, dict) and 'event_descriptor' in result:
            return 'oadrDistributeEvent', {'events': [result]}
        if isinstance(result, objects.Event):
            return 'oadrDistributeEvent', {'events': [asdict(result)]}
        logger.warning(f"Could not determine type of message in response to oadrPoll: {result}")
        return 'oadrResponse', result

    def on_poll(self, ven_id):
        """
        Placeholder for the on_poll handler.
        """
        logger.warning("You should implement and register your own on_poll handler that "
                       "returns the next message for the VEN. This handler receives the "
                       "ven_id as its argument, and should return None (if no messages "
                       "are available), an Event or list of Events, a RequestReregistration "
                       " or RequestReport.")
        return None