update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / common / python / rift / mano / dts / rpc / core.py
1 """
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16 #
17
18 @file core.py
19 @author Varun Prasad (varun.prasad@riftio.com)
20 @date 28-Sep-2016
21
22 """
23
24 import abc
25 import asyncio
26
27 import gi
28 gi.require_version("RwDts", "1.0")
29
30 from gi.repository import RwDts as rwdts
31 import rift.tasklets
32
33 from ..core import DtsHandler
34
35
36 class AbstractRpcHandler(DtsHandler):
37 """Base class to simplify RPC implementation
38 """
39 def __init__(self, log, dts, loop, project=None):
40 super().__init__(log, dts, loop, project)
41
42 if not asyncio.iscoroutinefunction(self.callback):
43 raise ValueError('%s has to be a coroutine' % (self.callback))
44
45 @abc.abstractproperty
46 def xpath(self):
47 pass
48
49 @property
50 def input_xpath(self):
51 return "I,{}".format(self.xpath)
52
53 @property
54 def output_xpath(self):
55 return "O,{}".format(self.xpath)
56
57 def flags(self):
58 return rwdts.Flag.PUBLISHER
59
60 @asyncio.coroutine
61 def on_prepare(self, xact_info, action, ks_path, msg):
62 assert action == rwdts.QueryAction.RPC
63
64 if self.project and not self.project.rpc_check(msg, xact_info=xact_info):
65 return
66
67 try:
68 rpc_op = yield from self.callback(ks_path, msg)
69 xact_info.respond_xpath(
70 rwdts.XactRspCode.ACK,
71 self.output_xpath,
72 rpc_op)
73
74 except Exception as e:
75 self.log.exception(e)
76 xact_info.respond_xpath(
77 rwdts.XactRspCode.NACK,
78 self.output_xpath)
79
80 @asyncio.coroutine
81 def register(self):
82 if self.reg:
83 self._log.warning("RPC already registered for project {}".
84 format(self._project.name))
85 return
86
87 reg_event = asyncio.Event(loop=self.loop)
88
89 @asyncio.coroutine
90 def on_ready(regh, status):
91 reg_event.set()
92
93 handler = rift.tasklets.DTS.RegistrationHandler(
94 on_prepare=self.on_prepare,
95 on_ready=on_ready)
96
97 with self.dts.group_create() as group:
98 self.reg = group.register(
99 xpath=self.input_xpath,
100 handler=handler,
101 flags=self.flags())
102
103 yield from reg_event.wait()
104
105 def deregister(self):
106 self.reg.deregister()
107 self.reg = None
108
109 @abc.abstractmethod
110 @asyncio.coroutine
111 def callback(self, ks_path, msg):
112 """Subclass needs to override this method
113
114 Args:
115 ks_path : Key spec path
116 msg : RPC input
117 """
118 pass
119