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
28 class SdnController(object):
29 def __init__(self
, http
=None, client
=None):
32 self
._apiName
= '/admin'
33 self
._apiVersion
= '/v1'
34 self
._apiResource
= '/sdns'
35 self
._apiBase
= '{}{}{}'.format(self
._apiName
,
36 self
._apiVersion
, self
._apiResource
)
38 # SDNC '--wait' option
39 def _wait(self
, id, deleteFlag
=False):
40 self
._client
.get_token()
41 # Endpoint to get operation status
42 apiUrlStatus
= '{}{}{}'.format(self
._apiName
, self
._apiVersion
, '/sdns')
43 # Wait for status for SDN instance creation/update/deletion
44 WaitForStatus
.wait_for_status(
47 WaitForStatus
.TIMEOUT_SDNC_OPERATION
,
50 deleteFlag
=deleteFlag
)
52 def _get_id_for_wait(self
, name
):
53 # Returns id of name, or the id itself if given as argument
54 for sdnc
in self
.list():
55 if name
== sdnc
['_id']:
57 for sdnc
in self
.list():
58 if name
== sdnc
['name']:
62 def create(self
, name
, sdn_controller
, wait
=False):
63 self
._client
.get_token()
64 http_code
, resp
= self
._http
.post_cmd(endpoint
=self
._apiBase
, postfields_dict
=sdn_controller
)
65 # print('HTTP CODE: {}'.format(http_code))
66 # print('RESP: {}'.format(resp))
67 if http_code
in (200, 201, 202, 204):
69 resp
= json
.loads(resp
)
70 if not resp
or 'id' not in resp
:
71 raise ClientException('unexpected response from server - {}'.format(resp
))
73 # Wait for status for SDNC instance creation
74 self
._wait
(resp
.get('id'))
80 msg
= json
.loads(resp
)
83 raise ClientException("failed to create SDN controller {} - {}".format(name
, msg
))
85 def update(self
, name
, sdn_controller
, wait
=False):
86 self
._client
.get_token()
88 sdnc_id_for_wait
= self
._get
_id
_for
_wait
(name
)
89 http_code
, resp
= self
._http
.patch_cmd(endpoint
='{}/{}'.format(self
._apiBase
,sdnc
['_id']),
90 postfields_dict
=sdn_controller
)
91 # print('HTTP CODE: {}'.format(http_code))
92 # print('RESP: {}'.format(resp))
93 if http_code
in (200, 201, 202, 204):
95 # In this case, 'resp' always returns None, so 'resp['id']' cannot be used.
96 # Use the previously obtained id instead.
97 wait_id
= sdnc_id_for_wait
98 # Wait for status for VI instance update
106 msg
= json
.loads(resp
)
109 raise ClientException("failed to update SDN controller {} - {}".format(name
, msg
))
111 def delete(self
, name
, force
=False, wait
=False):
112 self
._client
.get_token()
113 sdn_controller
= self
.get(name
)
114 sdnc_id_for_wait
= self
._get
_id
_for
_wait
(name
)
117 querystring
= '?FORCE=True'
118 http_code
, resp
= self
._http
.delete_cmd('{}/{}{}'.format(self
._apiBase
,
119 sdn_controller
['_id'], querystring
))
120 # print('HTTP CODE: {}'.format(http_code))
121 # print('RESP: {}'.format(resp))
124 # Wait for status for SDNC instance deletion
125 self
._wait
(sdnc_id_for_wait
, deleteFlag
=True)
127 print('Deletion in progress')
128 elif http_code
== 204:
130 elif resp
and 'result' in resp
:
136 msg
= json
.loads(resp
)
139 raise ClientException("failed to delete SDN controller {} - {}".format(name
, msg
))
141 def list(self
, filter=None):
142 """Returns a list of SDN controllers
144 self
._client
.get_token()
147 filter_string
= '?{}'.format(filter)
148 resp
= self
._http
.get_cmd('{}{}'.format(self
._apiBase
, filter_string
))
149 # print('RESP: {}'.format(resp))
155 """Returns an SDN controller based on name or id
157 self
._client
.get_token()
158 if utils
.validate_uuid4(name
):
159 for sdnc
in self
.list():
160 if name
== sdnc
['_id']:
163 for sdnc
in self
.list():
164 if name
== sdnc
['name']:
166 raise NotFound("SDN controller {} not found".format(name
))