2 # Copyright 2016-2017 RIFT.IO Inc
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # Author(s): Varun Prasad
17 # Creation Date: 09/25/2016
23 import rift
.mano
.dts
as mano_dts
24 import rift
.package
.store
as store
25 from rift
.package
.convert
import (
30 from gi
.repository
import (
35 class DownloadStatusSubscriber(mano_dts
.AbstractOpdataSubscriber
):
36 def __init__(self
, log
, dts
, loop
, project
, callback
):
37 super().__init
__(log
, dts
, loop
, project
, callback
)
40 return self
._project
.add_project(
41 "D,/rw-pkg-mgmt:download-jobs/rw-pkg-mgmt:job")
44 class VnfdStatusSubscriber(mano_dts
.VnfdCatalogSubscriber
):
45 DOWNLOAD_DIR
= store
.VnfdPackageFilesystemStore
.DEFAULT_ROOT_DIR
47 SERIALIZER
= RwVnfdSerializer()
49 def __init__(self
, log
, dts
, loop
, project
):
50 super().__init
__(log
, dts
, loop
, project
, callback
=self
.on_change
)
52 def on_change(self
, msg
, action
):
53 log_msg
= "1. Vnfd called w/ msg attributes: {} id {} name {} action: {}". \
54 format(repr(msg
), msg
.id, msg
.name
, repr(action
))
55 self
.log
.debug(log_msg
)
56 if action
== RwDts
.QueryAction
.UPDATE
or action
== RwDts
.QueryAction
.CREATE
:
57 actionCreate(self
, msg
, self
.project
.name
)
59 self
.log
.debug("VnfdStatusSubscriber: No action for {}".format(repr(action
)))
63 class NsdStatusSubscriber(mano_dts
.NsdCatalogSubscriber
):
64 DOWNLOAD_DIR
= store
.NsdPackageFilesystemStore
.DEFAULT_ROOT_DIR
66 SERIALIZER
= RwNsdSerializer()
68 def __init__(self
, log
, dts
, loop
, project
):
69 super().__init
__(log
, dts
, loop
, project
, callback
=self
.on_change
)
71 def on_change(self
, msg
, action
):
72 log_msg
= "1. Nsd called w/ msg attributes: {} id {} name {} action: {}". \
73 format(repr(msg
), msg
.id, msg
.name
, repr(action
))
74 self
.log
.debug(log_msg
)
75 if action
== RwDts
.QueryAction
.UPDATE
or action
== RwDts
.QueryAction
.CREATE
:
76 actionCreate(self
, msg
, self
.project
.name
)
78 self
.log
.debug("NsdStatusSubscriber: No action for {}".format(repr(action
)))
82 def actionCreate(descriptor
, msg
, project_name
=None):
83 ''' Create folder structure if it doesn't exist: id/vnf name OR id/nsd name
84 Serialize the Vnfd/Nsd object to yaml and store yaml file in the created folder.
87 download_dir
= os
.path
.join(
88 descriptor
.DOWNLOAD_DIR
,
89 project_name
if project_name
else "",
92 # If a download dir is present with contents, then we know it has been created in the
94 if os
.path
.exists(download_dir
) and os
.listdir(download_dir
):
95 descriptor
.log
.debug("Skpping folder creation, {} already present".format(download_dir
))
98 # Folder structure is based on top-level package-id directory
99 if not os
.path
.exists(download_dir
):
100 os
.makedirs(download_dir
)
101 descriptor
.log
.debug("Created directory {}".format(download_dir
))
102 yaml_path
= "{base}/{name}_{type}.yaml". \
103 format(base
=download_dir
, name
=msg
.name
[0:50], type=descriptor
.DESC_TYPE
)
104 with
open(yaml_path
,"w") as fh
:
105 fh
.write(descriptor
.SERIALIZER
.to_yaml_string(msg
))