Revert "Create folder structure in backend for onboarded descriptor - RIFT 15317"
[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
36
37 from . import rpc
38 from .proxy import filesystem
39 from . import publisher as pkg_publisher
40
41
42 class PackageManagerTasklet(rift.tasklets.Tasklet):
43 def __init__(self, *args, **kwargs):
44 try:
45 super().__init__(*args, **kwargs)
46 self.rwlog.set_category("rw-mano-log")
47 self.endpoint_rpc = None
48 self.schema_rpc = None
49 except Exception as e:
50 self.log.exception(e)
51
52 def start(self):
53 super().start()
54
55 self.log.debug("Registering with dts")
56
57 self.dts = rift.tasklets.DTS(
58 self.tasklet_info,
59 RwPkgMgmtYang.get_schema(),
60 self.loop,
61 self.on_dts_state_change
62 )
63
64 proxy = filesystem.FileSystemProxy(self.loop, self.log)
65
66 args = [self.log, self.dts, self.loop]
67 self.job_handler = pkg_publisher.DownloadStatusPublisher(*args)
68
69 args.append(proxy)
70 self.endpoint_rpc = rpc.EndpointDiscoveryRpcHandler(*args)
71 self.schema_rpc = rpc.SchemaRpcHandler(*args)
72 self.delete_rpc = rpc.PackageDeleteOperationsRpcHandler(*args)
73
74 args.append(self.job_handler)
75 self.pkg_op = rpc.PackageOperationsRpcHandler(*args)
76
77 def stop(self):
78 try:
79 self.dts.deinit()
80 except Exception as e:
81 self.log.exception(e)
82
83 @asyncio.coroutine
84 def init(self):
85 yield from self.endpoint_rpc.register()
86 yield from self.schema_rpc.register()
87 yield from self.pkg_op.register()
88 yield from self.job_handler.register()
89 yield from self.delete_rpc.register()
90
91 @asyncio.coroutine
92 def run(self):
93 pass
94
95 @asyncio.coroutine
96 def on_dts_state_change(self, state):
97 """Handle DTS state change
98
99 Take action according to current DTS state to transition application
100 into the corresponding application state
101
102 Arguments
103 state - current dts state
104
105 """
106 switch = {
107 rwdts.State.INIT: rwdts.State.REGN_COMPLETE,
108 rwdts.State.CONFIG: rwdts.State.RUN,
109 }
110
111 handlers = {
112 rwdts.State.INIT: self.init,
113 rwdts.State.RUN: self.run,
114 }
115
116 # Transition application to next state
117 handler = handlers.get(state, None)
118 if handler is not None:
119 yield from handler()
120
121 # Transition dts to next state
122 next_state = switch.get(state, None)
123 if next_state is not None:
124 self.dts.handle.set_state(next_state)