New feature: Code changes for project support
[osm/SO.git] / rwlaunchpad / plugins / rwpkgmgr / rift / tasklets / rwpkgmgr / rwpkgmgr.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 rwpkgmgr.py
19 @author Varun Prasad (varun.prasad@riftio.com)
20 @date 18-Sep-2016
21
22 """
23
24 import asyncio
25
26 import gi
27 gi.require_version('RwDts', '1.0')
28 gi.require_version('RwPkgMgmtYang', '1.0')
29
30
31 from gi.repository import (
32 RwDts as rwdts,
33 RwPkgMgmtYang)
34 import rift.tasklets
35 from rift.mano.utils.project import (
36 ManoProject,
37 ProjectHandler,
38 )
39
40 from . import rpc
41 from .proxy import filesystem
42 from . import publisher as pkg_publisher
43
44
45 class PackageManagerProject(ManoProject):
46
47 def __init__(self, name, tasklet, **kw):
48 super(PackageManagerProject, self).__init__(tasklet.log, name)
49 self.update(tasklet)
50
51 self.job_handler = pkg_publisher.DownloadStatusPublisher(
52 self._log, self._dts, self._loop, self)
53
54 @asyncio.coroutine
55 def register (self):
56 yield from self.job_handler.register()
57
58 def deregister (self):
59 yield from self.job_handler.deregister()
60
61
62 class PackageManagerTasklet(rift.tasklets.Tasklet):
63 def __init__(self, *args, **kwargs):
64 try:
65 super().__init__(*args, **kwargs)
66 self.rwlog.set_category("rw-mano-log")
67 self.endpoint_rpc = None
68 self.schema_rpc = None
69
70 self._project_handler = None
71 self.projects = {}
72
73 except Exception as e:
74 self.log.exception(e)
75
76 def start(self):
77 super().start()
78
79 self.log.debug("Registering with dts")
80
81 self.dts = rift.tasklets.DTS(
82 self.tasklet_info,
83 RwPkgMgmtYang.get_schema(),
84 self.loop,
85 self.on_dts_state_change
86 )
87
88 proxy = filesystem.FileSystemProxy(self.loop, self.log)
89
90 args = [self.log, self.dts, self.loop]
91 args.append(proxy)
92 self.endpoint_rpc = rpc.EndpointDiscoveryRpcHandler(*args)
93 self.schema_rpc = rpc.SchemaRpcHandler(*args)
94 self.delete_rpc = rpc.PackageDeleteOperationsRpcHandler(*args)
95
96 args.append(self)
97 self.pkg_op = rpc.PackageOperationsRpcHandler(*args)
98
99 def stop(self):
100 try:
101 self.dts.deinit()
102 except Exception as e:
103 self.log.exception(e)
104
105 @asyncio.coroutine
106 def init(self):
107 yield from self.endpoint_rpc.register()
108 yield from self.schema_rpc.register()
109 yield from self.pkg_op.register()
110 yield from self.delete_rpc.register()
111
112 self.log.debug("creating project handler")
113 self.project_handler = ProjectHandler(self, PackageManagerProject)
114 self.project_handler.register()
115
116 @asyncio.coroutine
117 def run(self):
118 pass
119
120 @asyncio.coroutine
121 def on_dts_state_change(self, state):
122 """Handle DTS state change
123
124 Take action according to current DTS state to transition application
125 into the corresponding application state
126
127 Arguments
128 state - current dts state
129
130 """
131 switch = {
132 rwdts.State.INIT: rwdts.State.REGN_COMPLETE,
133 rwdts.State.CONFIG: rwdts.State.RUN,
134 }
135
136 handlers = {
137 rwdts.State.INIT: self.init,
138 rwdts.State.RUN: self.run,
139 }
140
141 # Transition application to next state
142 handler = handlers.get(state, None)
143 if handler is not None:
144 yield from handler()
145
146 # Transition dts to next state
147 next_state = switch.get(state, None)
148 if next_state is not None:
149 self.dts.handle.set_state(next_state)