1 # Copyright 2018 Telefonica
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # 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, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
18 OSM SDN controller API handling
21 from osmclient
.common
import utils
22 from osmclient
.common
import wait
as WaitForStatus
23 from osmclient
.common
.exceptions
import ClientException
24 from osmclient
.common
.exceptions
import NotFound
30 class SdnController(object):
31 def __init__(self
, http
=None, client
=None):
34 self
._logger
= logging
.getLogger('osmclient')
35 self
._apiName
= '/admin'
36 self
._apiVersion
= '/v1'
37 self
._apiResource
= '/sdns'
38 self
._apiBase
= '{}{}{}'.format(self
._apiName
,
39 self
._apiVersion
, self
._apiResource
)
41 # SDNC '--wait' option
42 def _wait(self
, id, wait_time
, deleteFlag
=False):
43 self
._logger
.debug("")
44 self
._client
.get_token()
45 # Endpoint to get operation status
46 apiUrlStatus
= '{}{}{}'.format(self
._apiName
, self
._apiVersion
, '/sdns')
47 # Wait for status for SDN instance creation/update/deletion
48 if isinstance(wait_time
, bool):
49 wait_time
= WaitForStatus
.TIMEOUT_SDNC_OPERATION
50 WaitForStatus
.wait_for_status(
56 deleteFlag
=deleteFlag
)
58 def _get_id_for_wait(self
, name
):
59 """Returns id of name, or the id itself if given as argument
61 self
._logger
.debug("")
62 for sdnc
in self
.list():
63 if name
== sdnc
['_id']:
65 for sdnc
in self
.list():
66 if name
== sdnc
['name']:
70 def create(self
, name
, sdn_controller
, wait
=False):
71 self
._logger
.debug("")
72 if 'config' in sdn_controller
and isinstance(sdn_controller
["config"], str):
73 sdn_controller
["config"] = yaml
.safe_load(sdn_controller
["config"])
74 self
._client
.get_token()
75 http_code
, resp
= self
._http
.post_cmd(endpoint
=self
._apiBase
,
76 postfields_dict
=sdn_controller
)
77 #print('HTTP CODE: {}'.format(http_code))
78 #print('RESP: {}'.format(resp))
79 #if http_code in (200, 201, 202, 204):
81 resp
= json
.loads(resp
)
82 if not resp
or 'id' not in resp
:
83 raise ClientException('unexpected response from server - {}'.format(
86 # Wait for status for SDNC instance creation
87 self
._wait
(resp
.get('id'), wait
)
93 # msg = json.loads(resp)
96 # raise ClientException("failed to create SDN controller {} - {}".format(name, msg))
98 def update(self
, name
, sdn_controller
, wait
=False):
99 self
._logger
.debug("")
100 if 'config' in sdn_controller
and isinstance(sdn_controller
["config"], str):
101 sdn_controller
["config"] = yaml
.safe_load(sdn_controller
["config"])
102 self
._client
.get_token()
103 sdnc
= self
.get(name
)
104 sdnc_id_for_wait
= self
._get
_id
_for
_wait
(name
)
105 http_code
, resp
= self
._http
.patch_cmd(endpoint
='{}/{}'.format(self
._apiBase
,sdnc
['_id']),
106 postfields_dict
=sdn_controller
)
107 # print('HTTP CODE: {}'.format(http_code))
108 # print('RESP: {}'.format(resp))
109 #if http_code in (200, 201, 202, 204):
111 # In this case, 'resp' always returns None, so 'resp['id']' cannot be used.
112 # Use the previously obtained id instead.
113 wait_id
= sdnc_id_for_wait
114 # Wait for status for VI instance update
115 self
._wait
(wait_id
, wait
)
122 # msg = json.loads(resp)
125 # raise ClientException("failed to update SDN controller {} - {}".format(name, msg))
127 def delete(self
, name
, force
=False, wait
=False):
128 self
._logger
.debug("")
129 self
._client
.get_token()
130 sdn_controller
= self
.get(name
)
131 sdnc_id_for_wait
= self
._get
_id
_for
_wait
(name
)
134 querystring
= '?FORCE=True'
135 http_code
, resp
= self
._http
.delete_cmd('{}/{}{}'.format(self
._apiBase
,
136 sdn_controller
['_id'], querystring
))
137 # print('HTTP CODE: {}'.format(http_code))
138 # print('RESP: {}'.format(resp))
141 # Wait for status for SDNC instance deletion
142 self
._wait
(sdnc_id_for_wait
, wait
, deleteFlag
=True)
144 print('Deletion in progress')
145 elif http_code
== 204:
147 elif resp
and 'result' in resp
:
153 # msg = json.loads(resp)
156 raise ClientException("failed to delete SDN controller {} - {}".format(name
, msg
))
158 def list(self
, filter=None):
159 """Returns a list of SDN controllers
161 self
._logger
.debug("")
162 self
._client
.get_token()
165 filter_string
= '?{}'.format(filter)
166 _
, resp
= self
._http
.get2_cmd('{}{}'.format(self
._apiBase
,filter_string
))
167 #print('RESP: {}'.format(resp))
169 return json
.loads(resp
)
173 """Returns an SDN controller based on name or id
175 self
._logger
.debug("")
176 self
._client
.get_token()
177 if utils
.validate_uuid4(name
):
178 for sdnc
in self
.list():
179 if name
== sdnc
['_id']:
182 for sdnc
in self
.list():
183 if name
== sdnc
['name']:
185 raise NotFound("SDN controller {} not found".format(name
))