update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwprojectmano / plugins / rwprojectmano / rift / tasklets / rwprojectmano / tasklet.py
1 #
2 # Copyright 2017 RIFT.IO Inc
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17 """
18 Mano Project Manager tasklet is responsible for managing the Projects
19 configurations required for Role Based Access Control feature.
20 """
21
22 import asyncio
23
24 import gi
25 gi.require_version('RwDts', '1.0')
26 gi.require_version('RwLog', '1.0')
27 gi.require_version('RwProjectYang', '1.0')
28 gi.require_version('RwProjectManoYang', '1.0')
29 from gi.repository import (
30 RwDts as rwdts,
31 RwLog as rwlog,
32 RwProjectYang,
33 RwProjectManoYang,
34 )
35
36 import rift.tasklets
37
38 from rift.tasklets.rwidmgr.rbac import (
39 RbacNotification,
40 )
41
42 from rift.mano.utils.project import (
43 ManoProject,
44 )
45
46 from .projectmano import (
47 ProjectHandler,
48 ProjectStateRolePublisher,
49 )
50
51 from .rolesmano import (
52 ProjectMgrManoRoleConfigPublisher,
53 ProjectConfigSubscriber,
54 )
55
56
57 class ProjectMgrManoProject(ManoProject):
58
59 def __init__(self, name, tasklet):
60 super(ProjectMgrManoProject, self).__init__(tasklet.log, name)
61 self.update(tasklet)
62
63 self.project_sub = ProjectConfigSubscriber(self)
64
65 @asyncio.coroutine
66 def register (self):
67 self._log.info("Initializing the ProjectMgrMano for %s", self.name)
68 yield from self.project_sub.register()
69 self.tasklet.project_state_role_pub.publish_roles(self.name)
70
71 def deregister(self):
72 self._log.info("De-register project %s", self.name)
73 self.tasklet.project_state_role_pub.unpublish_roles(self.name)
74 self.project_sub.deregister()
75
76
77 class ProjectMgrManoTasklet(rift.tasklets.Tasklet):
78 """Tasklet that manages the Project config
79 """
80 def __init__(self, *args, **kwargs):
81 """Constructs a ProjectManager tasklet"""
82 try:
83 super().__init__(*args, **kwargs)
84 self.rwlog.set_category("rw-mano-log")
85 self.notify = RbacNotification(self)
86
87 self.projects = {}
88
89 except Exception as e:
90 self.log.exception(e)
91
92
93 def start(self):
94 """Callback that gets invoked when a Tasklet is started"""
95 super().start()
96 self.log.info("Starting Mano Project Manager Tasklet")
97
98 self.log.debug("Registering with dts")
99 self.dts = rift.tasklets.DTS(
100 self.tasklet_info,
101 RwProjectManoYang.get_schema(),
102 self.loop,
103 self.on_dts_state_change
104 )
105
106 self.log.debug("Created DTS Api Object: %s", self.dts)
107
108 def stop(self):
109 """Callback that gets invoked when Tasklet is stopped"""
110 try:
111 self.dts.deinit()
112 except Exception as e:
113 self.log.exception(e)
114
115 @asyncio.coroutine
116 def init(self):
117 """DTS Init state handler"""
118 try:
119 self.log.info("Registering for Project Config")
120 self.project_handler = ProjectHandler(self, ProjectMgrManoProject)
121 self.project_handler.register()
122
123 self.project_state_role_pub = ProjectStateRolePublisher(self)
124 yield from self.project_state_role_pub.register()
125
126 except Exception as e:
127 self.log.exception("Registering for project failed: {}".format(e))
128
129 @asyncio.coroutine
130 def run(self):
131 """DTS run state handler"""
132 pass
133
134 @asyncio.coroutine
135 def on_dts_state_change(self, state):
136 """Handle DTS state change
137
138 Take action according to current DTS state to transition application
139 into the corresponding application state
140
141 Arguments
142 state - current dts state
143
144 """
145 switch = {
146 rwdts.State.INIT: rwdts.State.REGN_COMPLETE,
147 rwdts.State.CONFIG: rwdts.State.RUN,
148 }
149
150 handlers = {
151 rwdts.State.INIT: self.init,
152 rwdts.State.RUN: self.run,
153 }
154
155 # Transition application to next state
156 handler = handlers.get(state, None)
157 if handler is not None:
158 yield from handler()
159
160 # Transition dts to next state
161 next_state = switch.get(state, None)
162 if next_state is not None:
163 self.dts.handle.set_state(next_state)
164
165 def config_ready(self):
166 """Subscription is complete and ready to start publishing."""
167 self.log.debug("Configuration Ready")
168
169
170 # vim: ts=4 sw=4 et