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