poll_service.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 warnings
  15. # ╔══════════════════════════════════════════════════════════════════════════╗
  16. # ║ POLLING SERVICE ║
  17. # ╚══════════════════════════════════════════════════════════════════════════╝
  18. #
  19. # oadrPoll is a service independent polling mechanism used by VENs in a PULL
  20. # model to request pending service operations from the VTN. The VEN queries
  21. # the poll endpoint and the VTN re- sponds with the same message that it would
  22. # have “pushed” had it been a PUSH VEN. If there are multiple messages pending
  23. # a “push,” the VEN will continue to query the poll endpoint until there are
  24. # no new messages and the VTN responds with an eiResponse payload.
  25. #
  26. # ┌──────────────────────────────────────────────────────────────────────────┐
  27. # │ The VEN can poll for any messages that we have for them. If we have no │
  28. # │ (more) messages, we send a generic oadrResponse: │
  29. # │ ┌────┐ ┌────┐ │
  30. # │ │VEN │ │VTN │ │
  31. # │ └─┬──┘ └─┬──┘ │
  32. # │ │───────────────────────────oadrPoll()───────────────────────────▶│ │
  33. # │ │ │ │
  34. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrResponse() ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  35. # │ │ │ │
  36. # │ │
  37. # └──────────────────────────────────────────────────────────────────────────┘
  38. # ┌──────────────────────────────────────────────────────────────────────────┐
  39. # │ If we have an Event, we expect the following: │
  40. # │ │
  41. # │ ┌────┐ ┌────┐ │
  42. # │ │VEN │ │VTN │ │
  43. # │ └─┬──┘ └─┬──┘ │
  44. # │ │───────────────────────────oadrPoll()───────────────────────────▶│ │
  45. # │ │ │ │
  46. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ oadrCreateEvent() ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  47. # │ │ │ │
  48. # │ │───────────────────────oadrCreatedEvent()───────────────────────▶│ │
  49. # │ │ │ │
  50. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrResponse() ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  51. # │ │ │ │
  52. # │ │
  53. # └──────────────────────────────────────────────────────────────────────────┘
  54. # ┌──────────────────────────────────────────────────────────────────────────┐
  55. # │ For Reports: │
  56. # │ │
  57. # │ ┌────┐ ┌────┐ │
  58. # │ │VEN │ │VTN │ │
  59. # │ └─┬──┘ └─┬──┘ │
  60. # │ │───────────────────────────oadrPoll()───────────────────────────▶│ │
  61. # │ │ │ │
  62. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrCreateReport() ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  63. # │ │ │ │
  64. # │ │───────────────────────oadrCreatedReport()──────────────────────▶│ │
  65. # │ │ │ │
  66. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrResponse() ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  67. # │ │ │ │
  68. # │ │
  69. # └──────────────────────────────────────────────────────────────────────────┘
  70. # ┌──────────────────────────────────────────────────────────────────────────┐
  71. # │ If re-registration is neccessary: │
  72. # │ │
  73. # │ ┌────┐ ┌────┐ │
  74. # │ │VEN │ │VTN │ │
  75. # │ └─┬──┘ └─┬──┘ │
  76. # │ │───────────────────────────oadrPoll()───────────────────────────▶│ │
  77. # │ │ │ │
  78. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrRequestReregistration()─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  79. # │ │ │ │
  80. # │ │─────────────────────────oadrResponse()─────────────────────────▶│ │
  81. # │ │ │ │
  82. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ HTTP 200─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  83. # │ │ │ │
  84. # │ │
  85. # │ │──────────────────oadrCreatePartyRegistration()─────────────────▶│ │
  86. # │ │ │ │
  87. # │ │◀ ─ ─ ─ ─ ─ ─ ─ ─ ─oadrRequestReregistration()─ ─ ─ ─ ─ ─ ─ ─ ─ ─│ │
  88. # │ │ │ │
  89. # │ │
  90. # └──────────────────────────────────────────────────────────────────────────┘
  91. @service('OadrPoll')
  92. class PollService(VTNService):
  93. @handler('oadrPoll')
  94. async def poll(self, payload):
  95. """
  96. Retrieve the messages that we have for this VEN in order.
  97. The backend get_next_message
  98. """
  99. result = self.on_poll(ven_id=payload['ven_id'])
  100. if iscoroutine(result):
  101. result = await result
  102. if result is None:
  103. return result
  104. if isinstance(result, tuple):
  105. return result
  106. if isinstance(result, list):
  107. return 'oadrDistributeEvent', result
  108. if isinstance(result, dict) and 'event_descriptor' in result:
  109. return 'oadrDistributeEvent', {'events': [result]}
  110. warnings.warn(f"Could not determine type of message in response to oadrPoll: {result}")
  111. return 'oadrResponse', result