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.
20 from rift
.package
import convert
21 from gi
.repository
import (
29 class OnboardError(Exception):
33 class UpdateError(Exception):
37 class DescriptorOnboarder(object):
38 """ This class is responsible for onboarding descriptors using Restconf"""
40 NsdYang
.YangData_Nsd_NsdCatalog_Nsd
: "nsd-catalog/nsd",
41 RwNsdYang
.YangData_Nsd_NsdCatalog_Nsd
: "nsd-catalog/nsd",
42 VnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd
: "vnfd-catalog/vnfd",
43 RwVnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd
: "vnfd-catalog/vnfd",
46 DESC_SERIALIZER_MAP
= {
47 NsdYang
.YangData_Nsd_NsdCatalog_Nsd
: convert
.NsdSerializer(),
48 RwNsdYang
.YangData_Nsd_NsdCatalog_Nsd
: convert
.RwNsdSerializer(),
49 VnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd
: convert
.VnfdSerializer(),
50 RwVnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd
: convert
.RwVnfdSerializer(),
53 HEADERS
= {"content-type": "application/vnd.yang.data+json"}
55 AUTH
= ('admin', 'admin')
57 def __init__(self
, log
, host
="127.0.0.1", port
=8008, use_ssl
=False, ssl_cert
=None, ssl_key
=None):
61 self
._use
_ssl
= use_ssl
62 self
._ssl
_cert
= ssl_cert
63 self
._ssl
_key
= ssl_key
65 self
.timeout
= DescriptorOnboarder
.TIMEOUT_SECS
68 def _get_headers(cls
, auth
):
69 headers
= cls
.HEADERS
.copy()
71 headers
['authorization'] = auth
75 def _get_url(self
, descriptor_msg
):
76 if type(descriptor_msg
) not in DescriptorOnboarder
.DESC_SERIALIZER_MAP
:
77 raise TypeError("Invalid descriptor message type")
79 endpoint
= DescriptorOnboarder
.DESC_ENDPOINT_MAP
[type(descriptor_msg
)]
81 url
= "{}://{}:{}/api/config/{}".format(
82 "https" if self
._use
_ssl
else "http",
90 def _make_request_args(self
, descriptor_msg
, auth
=None):
91 if type(descriptor_msg
) not in DescriptorOnboarder
.DESC_SERIALIZER_MAP
:
92 raise TypeError("Invalid descriptor message type")
94 serializer
= DescriptorOnboarder
.DESC_SERIALIZER_MAP
[type(descriptor_msg
)]
95 json_data
= serializer
.to_json_string(descriptor_msg
)
96 url
= self
._get
_url
(descriptor_msg
)
101 headers
=self
._get
_headers
(auth
),
102 auth
=DescriptorOnboarder
.AUTH
,
104 cert
=(self
._ssl
_cert
, self
._ssl
_key
) if self
._use
_ssl
else None,
105 timeout
=self
.timeout
,
110 def update(self
, descriptor_msg
, auth
=None):
111 """ Update the descriptor config
114 descriptor_msg - A descriptor proto-gi msg
115 auth - the authorization header
118 UpdateError - The descriptor config update failed
120 request_args
= self
._make
_request
_args
(descriptor_msg
, auth
)
122 response
= requests
.put(**request_args
)
123 response
.raise_for_status()
124 except requests
.exceptions
.ConnectionError
as e
:
125 msg
= "Could not connect to restconf endpoint: %s" % str(e
)
127 raise UpdateError(msg
) from e
128 except requests
.exceptions
.HTTPError
as e
:
129 msg
= "PUT request to %s error: %s" % (request_args
["url"], response
.text
)
131 raise UpdateError(msg
) from e
132 except requests
.exceptions
.Timeout
as e
:
133 msg
= "Timed out connecting to restconf endpoint: %s", str(e
)
135 raise UpdateError(msg
) from e
137 def onboard(self
, descriptor_msg
, auth
=None):
138 """ Onboard the descriptor config
141 descriptor_msg - A descriptor proto-gi msg
142 auth - the authorization header
145 OnboardError - The descriptor config update failed
148 request_args
= self
._make
_request
_args
(descriptor_msg
, auth
)
150 response
= requests
.post(**request_args
)
151 response
.raise_for_status()
152 except requests
.exceptions
.ConnectionError
as e
:
153 msg
= "Could not connect to restconf endpoint: %s" % str(e
)
155 raise OnboardError(msg
) from e
156 except requests
.exceptions
.HTTPError
as e
:
157 msg
= "POST request to %s error: %s" % (request_args
["url"], response
.text
)
159 raise OnboardError(msg
) from e
160 except requests
.exceptions
.Timeout
as e
:
161 msg
= "Timed out connecting to restconf endpoint: %s", str(e
)
163 raise OnboardError(msg
) from e