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