Add MANO project role support
[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.mano.utils.project import (
39 ManoProject,
40 )
41
42 from .projectmano import (
43 ProjectHandler,
44 )
45
46 from .rolesmano import (
47 RoleConfigPublisher,
48 ProjectConfigSubscriber,
49 )
50
51
52 class ProjectMgrManoProject(ManoProject):
53
54 def __init__(self, name, tasklet):
55 super(ProjectMgrManoProject, self).__init__(tasklet.log, name)
56 self.update(tasklet)
57
58 self.project_sub = ProjectConfigSubscriber(self)
59
60 @asyncio.coroutine
61 def register (self):
62 self._log.info("Initializing the ProjectMgrMano for %s", self.name)
63 yield from self.project_sub.register()
64
65 def deregister(self):
66 self._log.debug("De-register project %s", self.name)
67 self.project_sub.deregister()
68
69
70 class ProjectMgrManoTasklet(rift.tasklets.Tasklet):
71 """Tasklet that manages the Project config
72 """
73 def __init__(self, *args, **kwargs):
74 """Constructs a ProjectManager tasklet"""
75 try:
76 super().__init__(*args, **kwargs)
77 self.rwlog.set_category("rw-mano-log")
78
79 self.projects = {}
80
81 except Exception as e:
82 self.log.exception(e)
83
84
85 def start(self):
86 """Callback that gets invoked when a Tasklet is started"""
87 super().start()
88 self.log.info("Starting Mano Project Manager Tasklet")
89
90 self.log.debug("Registering with dts")
91 self.dts = rift.tasklets.DTS(
92 self.tasklet_info,
93 RwProjectManoYang.get_schema(),
94 self.loop,
95 self.on_dts_state_change
96 )
97
98 self.log.debug("Created DTS Api Object: %s", self.dts)
99
100 def stop(self):
101 """Callback that gets invoked when Tasklet is stopped"""
102 try:
103 self.dts.deinit()
104 except Exception as e:
105 self.log.exception(e)
106
107 @asyncio.coroutine
108 def init(self):
109 """DTS Init state handler"""
110 try:
111 self.log.info("Registering for Project Config")
112 self.project_handler = ProjectHandler(self, ProjectMgrManoProject)
113
114 self.project_handler.register()
115
116 except Exception as e:
117 self.log.exception("Registering for project failed: {}".format(e))
118
119 @asyncio.coroutine
120 def run(self):
121 """DTS run state handler"""
122 pass
123
124 @asyncio.coroutine
125 def on_dts_state_change(self, state):
126 """Handle DTS state change
127
128 Take action according to current DTS state to transition application
129 into the corresponding application state
130
131 Arguments
132 state - current dts state
133
134 """
135 switch = {
136 rwdts.State.INIT: rwdts.State.REGN_COMPLETE,
137 rwdts.State.CONFIG: rwdts.State.RUN,
138 }
139
140 handlers = {
141 rwdts.State.INIT: self.init,
142 rwdts.State.RUN: self.run,
143 }
144
145 # Transition application to next state
146 handler = handlers.get(state, None)
147 if handler is not None:
148 yield from handler()
149
150 # Transition dts to next state
151 next_state = switch.get(state, None)
152 if next_state is not None:
153 self.dts.handle.set_state(next_state)
154
155 def config_ready(self):
156 """Subscription is complete and ready to start publishing."""
157 self.log.debug("Configuration Ready")
158
159
160 # vim: ts=4 sw=4 et