registration_service.py 9.4 KB

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