3 # Copyright 2016 RIFT.IO Inc
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 @author Varun Prasad (varun.prasad@riftio.com)
27 import tornado
.httpserver
28 import tornado
.httputil
29 import tornado
.platform
.asyncio
30 import tornadostreamform
.multipart_streamer
as multipart_streamer
33 gi
.require_version('RwDts', '1.0')
34 gi
.require_version('RwStagingMgmtYang', '1.0')
35 from gi
.repository
import (
39 from rift
.mano
.utils
.project
import (
46 from .server
import StagingApplication
47 from .publisher
import StagingStorePublisher
50 class StagingManagerProject(ManoProject
):
52 def __init__(self
, name
, tasklet
, **kw
):
53 super(StagingManagerProject
, self
).__init
__(tasklet
.log
, name
)
56 self
.publisher
= StagingStorePublisher(self
)
58 self
.publisher
.delegate
= tasklet
.store
62 yield from self
.publisher
.register()
65 self
.publisher
.deregister()
68 class StagingManagerTasklet(rift
.tasklets
.Tasklet
):
69 """Tasklet to handle all staging related operations
71 def __init__(self
, *args
, **kwargs
):
73 super().__init
__(*args
, **kwargs
)
74 self
._project
_handler
= None
77 except Exception as e
:
78 self
.log
.exception("Staging Manager tasklet init: {}".
84 self
.log
.debug("Registering with dts")
86 self
.dts
= rift
.tasklets
.DTS(
88 RwStagingMgmtYang
.get_schema(),
90 self
.on_dts_state_change
96 except Exception as e
:
101 self
.store
= store
.StagingFileStore(self
)
103 io_loop
= rift
.tasklets
.tornado
.TaskletAsyncIOLoop(asyncio_loop
=self
.loop
)
104 self
.app
= StagingApplication(self
.store
)
106 manifest
= self
.tasklet_info
.get_pb_manifest()
107 ssl_cert
= manifest
.bootstrap_phase
.rwsecurity
.cert
108 ssl_key
= manifest
.bootstrap_phase
.rwsecurity
.key
109 ssl_options
= {"certfile": ssl_cert
, "keyfile": ssl_key
}
111 if manifest
.bootstrap_phase
.rwsecurity
.use_ssl
:
112 self
.server
= tornado
.httpserver
.HTTPServer(
114 max_body_size
=self
.app
.MAX_BODY_SIZE
,
116 ssl_options
=ssl_options
)
118 self
.server
= tornado
.httpserver
.HTTPServer(
120 max_body_size
=self
.app
.MAX_BODY_SIZE
,
124 self
.create_stg_rpc
= rpc
.StagingAreaCreateRpcHandler(
129 yield from self
.create_stg_rpc
.register()
131 self
.log
.debug("creating project handler")
132 self
.project_handler
= ProjectHandler(self
, StagingManagerProject
)
133 self
.project_handler
.register()
137 self
.server
.listen(self
.app
.PORT
)
140 def on_dts_state_change(self
, state
):
141 """Handle DTS state change
143 Take action according to current DTS state to transition application
144 into the corresponding application state
147 state - current dts state
151 rwdts
.State
.INIT
: rwdts
.State
.REGN_COMPLETE
,
152 rwdts
.State
.CONFIG
: rwdts
.State
.RUN
,
156 rwdts
.State
.INIT
: self
.init
,
157 rwdts
.State
.RUN
: self
.run
,
160 # Transition application to next state
161 handler
= handlers
.get(state
, None)
162 if handler
is not None:
165 # Transition dts to next state
166 next_state
= switch
.get(state
, None)
167 if next_state
is not None:
168 self
.dts
.handle
.set_state(next_state
)