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
.exceptions
import ClientException
23 from osmclient
.common
.exceptions
import NotFound
27 class SdnController(object):
28 def __init__(self
, http
=None, client
=None):
31 self
._apiName
= '/admin'
32 self
._apiVersion
= '/v1'
33 self
._apiResource
= '/sdns'
34 self
._apiBase
= '{}{}{}'.format(self
._apiName
,
35 self
._apiVersion
, self
._apiResource
)
36 def create(self
, name
, sdn_controller
):
37 http_code
, resp
= self
._http
.post_cmd(endpoint
=self
._apiBase
,
38 postfields_dict
=sdn_controller
)
39 #print 'HTTP CODE: {}'.format(http_code)
40 #print 'RESP: {}'.format(resp)
41 if http_code
in (200, 201, 202, 204):
43 resp
= json
.loads(resp
)
44 if not resp
or 'id' not in resp
:
45 raise ClientException('unexpected response from server - {}'.format(
52 msg
= json
.loads(resp
)
55 raise ClientException("failed to create SDN controller {} - {}".format(name
, msg
))
57 def update(self
, name
, sdn_controller
):
59 http_code
, resp
= self
._http
.put_cmd(endpoint
='{}/{}'.format(self
._apiBase
,sdnc
['_id']),
60 postfields_dict
=sdn_controller
)
61 #print 'HTTP CODE: {}'.format(http_code)
62 #print 'RESP: {}'.format(resp)
63 if http_code
in (200, 201, 202, 204):
65 resp
= json
.loads(resp
)
66 if not resp
or 'id' not in resp
:
67 raise ClientException('unexpected response from server - {}'.format(
74 msg
= json
.loads(resp
)
77 raise ClientException("failed to update SDN controller {} - {}".format(name
, msg
))
79 def delete(self
, name
, force
=False):
80 sdn_controller
= self
.get(name
)
83 querystring
= '?FORCE=True'
84 http_code
, resp
= self
._http
.delete_cmd('{}/{}{}'.format(self
._apiBase
,
85 sdn_controller
['_id'], querystring
))
86 #print 'HTTP CODE: {}'.format(http_code)
87 #print 'RESP: {}'.format(resp)
89 print('Deletion in progress')
90 elif http_code
== 204:
92 elif resp
and 'result' in resp
:
98 msg
= json
.loads(resp
)
101 raise ClientException("failed to delete SDN controller {} - {}".format(name
, msg
))
103 def list(self
, filter=None):
104 """Returns a list of SDN controllers
108 filter_string
= '?{}'.format(filter)
109 resp
= self
._http
.get_cmd('{}{}'.format(self
._apiBase
,filter_string
))
110 #print 'RESP: {}'.format(resp)
116 """Returns an SDN controller based on name or id
118 if utils
.validate_uuid4(name
):
119 for sdnc
in self
.list():
120 if name
== sdnc
['_id']:
123 for sdnc
in self
.list():
124 if name
== sdnc
['name']:
126 raise NotFound("SDN controller {} not found".format(name
))