Merge from OSM SO master
[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 from . import subscriber
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 proxy = kw["proxy"]
51
52 args = [self.log, self.dts, self.loop, self]
53 self.job_handler = pkg_publisher.DownloadStatusPublisher(*args)
54 self.copy_publisher = pkg_publisher.CopyStatusPublisher(*args + [self.tasklet.tasklet_info])
55
56 # create catalog subscribers
57 self.vnfd_catalog_sub = subscriber.VnfdStatusSubscriber(*args)
58 self.nsd_catalog_sub = subscriber.NsdStatusSubscriber(*args)
59
60 args.append(proxy)
61 self.copy_rpc = rpc.PackageCopyOperationsRpcHandler(*(args + [self.copy_publisher]))
62
63 @asyncio.coroutine
64 def register (self):
65 yield from self.vnfd_catalog_sub.register()
66 yield from self.nsd_catalog_sub.register()
67 yield from self.copy_rpc.register()
68 yield from self.copy_publisher.register()
69 yield from self.job_handler.register()
70
71 def deregister (self):
72 yield from self.job_handler.deregister()
73 yield from self.copy_rpc.deregister()
74 yield from self.copy_publisher.deregister()
75 yield from self.vnfd_catalog_sub.deregister()
76 yield from self.nsd_catalog_sub.deregister()
77
78
79 class PackageManagerTasklet(rift.tasklets.Tasklet):
80 def __init__(self, *args, **kwargs):
81 try:
82 super().__init__(*args, **kwargs)
83 self.rwlog.set_category("rw-mano-log")
84 self.endpoint_rpc = None
85 self.schema_rpc = None
86
87 self._project_handler = None
88 self.projects = {}
89
90 except Exception as e:
91 self.log.exception(e)
92
93 def start(self):
94
95 self.log.debug("Registering with dts")
96
97 try:
98 super().start()
99 self.dts = rift.tasklets.DTS(
100 self.tasklet_info,
101 RwPkgMgmtYang.get_schema(),
102 self.loop,
103 self.on_dts_state_change
104 )
105
106 proxy = filesystem.FileSystemProxy(self.loop, self.log)
107 args = [self.log, self.dts, self.loop]
108
109 args.append(proxy)
110 self.endpoint_rpc = rpc.EndpointDiscoveryRpcHandler(*args)
111 self.schema_rpc = rpc.SchemaRpcHandler(*args)
112 self.delete_rpc = rpc.PackageDeleteOperationsRpcHandler(*args)
113
114 args.append(self)
115 self.pkg_op = rpc.PackageOperationsRpcHandler(*args)
116
117 self.project_handler = ProjectHandler(self, PackageManagerProject,
118 proxy=proxy,)
119 except Exception as e:
120 self.log.exception("Exception caught rwpkgmgr start: %s", str(e))
121 else:
122 self.log.debug("rwpkgmgr started successfully!")
123
124 def stop(self):
125 try:
126 self.dts.deinit()
127 except Exception as e:
128 self.log.exception(e)
129
130 @asyncio.coroutine
131 def init(self):
132 yield from self.endpoint_rpc.register()
133 yield from self.schema_rpc.register()
134 yield from self.pkg_op.register()
135 yield from self.delete_rpc.register()
136
137 self.log.debug("creating project handler")
138 self.project_handler = ProjectHandler(self, PackageManagerProject)
139 self.project_handler.register()
140
141 @asyncio.coroutine
142 def run(self):
143 pass
144
145 @asyncio.coroutine
146 def on_dts_state_change(self, state):
147 """Handle DTS state change
148
149 Take action according to current DTS state to transition application
150 into the corresponding application state
151
152 Arguments
153 state - current dts state
154
155 """
156 switch = {
157 rwdts.State.INIT: rwdts.State.REGN_COMPLETE,
158 rwdts.State.CONFIG: rwdts.State.RUN,
159 }
160
161 handlers = {
162 rwdts.State.INIT: self.init,
163 rwdts.State.RUN: self.run,
164 }
165
166 # Transition application to next state
167 handler = handlers.get(state, None)
168 if handler is not None:
169 yield from handler()
170
171 # Transition dts to next state
172 next_state = switch.get(state, None)
173 if next_state is not None:
174 self.dts.handle.set_state(next_state)