b12e192fc4f51a7101578e1b8c085c979438e755
[osm/SO.git] / rwlaunchpad / plugins / rwlaunchpadtasklet / rift / tasklets / rwlaunchpad / onboard.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16 #
17
18 import requests
19
20 from rift.package import convert
21 from gi.repository import (
22 NsdYang,
23 RwNsdYang,
24 VnfdYang,
25 RwVnfdYang,
26 )
27
28
29 class OnboardError(Exception):
30 pass
31
32
33 class UpdateError(Exception):
34 pass
35
36
37 class DescriptorOnboarder(object):
38 """ This class is responsible for onboarding descriptors using Restconf"""
39 DESC_ENDPOINT_MAP = {
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",
44 }
45
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(),
51 }
52
53 HEADERS = {"content-type": "application/vnd.yang.data+json"}
54 TIMEOUT_SECS = 60
55 AUTH = ('admin', 'admin')
56
57 def __init__(self, log, host="127.0.0.1", port=8008, use_ssl=False, ssl_cert=None, ssl_key=None):
58 self._log = log
59 self._host = host
60 self.port = port
61 self._use_ssl = use_ssl
62 self._ssl_cert = ssl_cert
63 self._ssl_key = ssl_key
64
65 self.timeout = DescriptorOnboarder.TIMEOUT_SECS
66
67 @classmethod
68 def _get_headers(cls, auth):
69 headers = cls.HEADERS.copy()
70 if auth is not None:
71 headers['authorization'] = auth
72
73 return headers
74
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")
78
79 endpoint = DescriptorOnboarder.DESC_ENDPOINT_MAP[type(descriptor_msg)]
80
81 url = "{}://{}:{}/api/config/{}".format(
82 "https" if self._use_ssl else "http",
83 self._host,
84 self.port,
85 endpoint,
86 )
87
88 return url
89
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")
93
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)
97
98 request_args = dict(
99 url=url,
100 data=json_data,
101 headers=self._get_headers(auth),
102 auth=DescriptorOnboarder.AUTH,
103 verify=False,
104 cert=(self._ssl_cert, self._ssl_key) if self._use_ssl else None,
105 timeout=self.timeout,
106 )
107
108 return request_args
109
110 def update(self, descriptor_msg, auth=None):
111 """ Update the descriptor config
112
113 Arguments:
114 descriptor_msg - A descriptor proto-gi msg
115 auth - the authorization header
116
117 Raises:
118 UpdateError - The descriptor config update failed
119 """
120 request_args = self._make_request_args(descriptor_msg, auth)
121 try:
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)
126 self._log.error(msg)
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)
130 self._log.error(msg)
131 raise UpdateError(msg) from e
132 except requests.exceptions.Timeout as e:
133 msg = "Timed out connecting to restconf endpoint: %s", str(e)
134 self._log.error(msg)
135 raise UpdateError(msg) from e
136
137 def onboard(self, descriptor_msg, auth=None):
138 """ Onboard the descriptor config
139
140 Arguments:
141 descriptor_msg - A descriptor proto-gi msg
142 auth - the authorization header
143
144 Raises:
145 OnboardError - The descriptor config update failed
146 """
147
148 request_args = self._make_request_args(descriptor_msg, auth)
149 try:
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)
154 self._log.error(msg)
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)
158 self._log.error(msg)
159 raise OnboardError(msg) from e
160 except requests.exceptions.Timeout as e:
161 msg = "Timed out connecting to restconf endpoint: %s", str(e)
162 self._log.error(msg)
163 raise OnboardError(msg) from e
164