poll_service.py 10.0 KB

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