Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# SPDX-License-Identifier: Apache-2.0
3# Copyright 2020 Contributors to OpenLEADR
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
9# http://www.apache.org/licenses/LICENSE-2.0
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
17from . import service, handler, VTNService
18from asyncio import iscoroutine
19import logging
20logger = logging.getLogger('openleadr')
22# ╔══════════════════════════════════════════════════════════════════════════╗
23# ║ REGISTRATION SERVICE ║
24# ╚══════════════════════════════════════════════════════════════════════════╝
25# ┌──────────────────────────────────────────────────────────────────────────┐
26# │ The VEN can explore some information about the VTN: │
27# │ │
28# │ ┌────┐ ┌────┐ │
29# │ │VEN │ │VTN │ │
30# │ └─┬──┘ └─┬──┘ │
31# │ │─────────────────────oadrQueryRegistration()────────────────────▶│ │
32# │ │ │ │
33# │ │◀ ─ ─ ─ ─ ─ ─ oadrCreatedPartyRegistration(VTN Info)─ ─ ─ ─ ─ ─ ─│ │
34# │ │ │ │
35# │ │
36# └──────────────────────────────────────────────────────────────────────────┘
37# ┌──────────────────────────────────────────────────────────────────────────┐
38# │ The VEN can then go on and register with the VTN │
39# │ │
40# │ ┌────┐ ┌────┐ │
41# │ │VEN │ │VTN │ │
42# │ └─┬──┘ └─┬──┘ │
43# │ │───────────────oadrCreatePartyRegistration(VEN Info)────────────▶│ │
44# │ │ │ │
45# │ │◀ ─ ─ oadrCreatedPartyRegistration(VTN Info, registrationID)─ ─ ─│ │
46# │ │ │ │
47# │ │
48# └──────────────────────────────────────────────────────────────────────────┘
49# ┌──────────────────────────────────────────────────────────────────────────┐
50# │ The VEN can also choose to cancel the registration │
51# │ │
52# │ ┌────┐ ┌────┐ │
53# │ │VEN │ │VTN │ │
54# │ └─┬──┘ └─┬──┘ │
55# │ │──────────oadrCancelPartyRegistration(registrationID)───────────▶│ │
56# │ │ │ │
57# │ │◀ ─ ─ ─ ─ ─ ─ ─ ─oadrCanceledPartyRegistration()─ ─ ─ ─ ─ ─ ─ ─ ─│ │
58# │ │ │ │
59# │ │
60# └──────────────────────────────────────────────────────────────────────────┘
63@service('EiRegisterParty')
64class RegistrationService(VTNService):
66 def __init__(self, vtn_id, poll_freq):
67 super().__init__(vtn_id)
68 self.poll_freq = poll_freq
70 @handler('oadrQueryRegistration')
71 async def query_registration(self, payload):
72 """
73 Return the profiles we support.
74 """
75 if hasattr(self, 'on_query_registration'):
76 result = self.on_query_registration(payload)
77 if iscoroutine(result):
78 result = await result
79 return result
81 # If you don't provide a default handler, just give out the info
82 response_payload = {'request_id': payload['request_id'],
83 'profiles': [{'profile_name': '2.0b',
84 'transports': [{'transport_name': 'simpleHttp'}]}],
85 'requested_oadr_poll_freq': self.poll_freq}
86 return 'oadrCreatedPartyRegistration', response_payload
88 @handler('oadrCreatePartyRegistration')
89 async def create_party_registration(self, payload):
90 """
91 Handle the registration of a VEN party.
92 """
93 result = self.on_create_party_registration(payload)
94 if iscoroutine(result):
95 result = await result
97 if result is not False and result is not None:
98 if len(result) != 2:
99 logger.error("Your on_create_party_registration handler should return either "
100 "'False' (if the client is rejected) or a (ven_id, registration_id) "
101 "tuple. Will REJECT the client for now.")
102 response_payload = {}
103 else:
104 ven_id, registration_id = result
105 transports = [{'transport_name': payload['transport_name']}]
106 response_payload = {'ven_id': result[0],
107 'registration_id': result[1],
108 'profiles': [{'profile_name': payload['profile_name'],
109 'transports': transports}],
110 'requested_oadr_poll_freq': self.poll_freq}
111 else:
112 transports = [{'transport_name': payload['transport_name']}]
113 response_payload = {'profiles': [{'profile_name': payload['profile_name'],
114 'transports': transports}],
115 'requested_oadr_poll_freq': self.poll_freq}
116 return 'oadrCreatedPartyRegistration', response_payload
118 def on_create_party_registration(self, payload):
119 """
120 Placeholder for the on_create_party_registration handler
121 """
122 logger.warning("You should implement and register your own on_create_party_registration "
123 "handler if you want VENs to be able to connect to you. This handler will "
124 "receive a registration request and should return either 'False' (if the "
125 "registration is denied) or a (ven_id, registration_id) tuple if the "
126 "registration is accepted.")
127 return False
129 @handler('oadrCancelPartyRegistration')
130 async def cancel_party_registration(self, payload):
131 """
132 Cancel the registration of a party.
133 """
134 result = self.on_cancel_party_registration(payload)
135 if iscoroutine(result):
136 result = await result
137 return result
139 def on_cancel_party_registration(self, ven_id):
140 """
141 Placeholder for the on_cancel_party_registration handler.
142 """
143 logger.warning("You should implement and register your own on_cancel_party_registration "
144 "handler that allown VENs to deregister from your VTN. This will receive a "
145 "ven_id as its argument. You don't need to return anything.")
146 return None