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
21 from osmclient
.common
import utils
22 from osmclient
.common
.exceptions
import ClientException
23 from osmclient
.common
.exceptions
import NotFound
29 def __init__(self
, http
=None, client
=None):
32 self
._apiName
= '/nslcm'
33 self
._apiVersion
= '/v1'
34 self
._apiResource
= '/ns_instances_content'
35 self
._apiBase
= '{}{}{}'.format(self
._apiName
,
36 self
._apiVersion
, self
._apiResource
)
38 def list(self
, filter=None):
39 """Returns a list of NS
43 filter_string
= '?{}'.format(filter)
44 resp
= self
._http
.get_cmd('{}{}'.format(self
._apiBase
,filter_string
))
50 """Returns an NS based on name or id
52 if utils
.validate_uuid4(name
):
53 for ns
in self
.list():
57 for ns
in self
.list():
58 if name
== ns
['name']:
60 raise NotFound("ns {} not found".format(name
))
62 def get_individual(self
, name
):
64 if not utils
.validate_uuid4(name
):
65 for ns
in self
.list():
66 if name
== ns
['name']:
69 resp
= self
._http
.get_cmd('{}/{}'.format(self
._apiBase
, ns_id
))
70 #resp = self._http.get_cmd('{}/{}/nsd_content'.format(self._apiBase, ns_id))
71 #print yaml.safe_dump(resp)
74 raise NotFound("ns {} not found".format(name
))
76 def delete(self
, name
):
78 http_code
, resp
= self
._http
.delete_cmd('{}/{}'.format(self
._apiBase
,ns
['_id']))
79 #print 'RESP: {}'.format(resp)
81 print 'Deletion in progress'
82 elif http_code
== 204:
85 raise ClientException("failed to delete ns {}: {}".format(name
, resp
))
87 def create(self
, nsd_name
, nsr_name
, account
, config
=None,
88 ssh_keys
=None, description
='default description',
89 admin_status
='ENABLED'):
91 nsd
= self
._client
.nsd
.get(nsd_name
)
95 def get_vim_account_id(vim_account
):
96 if vim_account_id
.get(vim_account
):
97 return vim_account_id
[vim_account
]
99 vim
= self
._client
.vim
.get(vim_account
)
101 raise NotFound("cannot find vim account '{}'".format(vim_account
))
102 vim_account_id
[vim_account
] = vim
['_id']
106 ns
['nsdId'] = nsd
['_id']
107 ns
['nsName'] = nsr_name
108 ns
['nsDescription'] = description
109 ns
['vimAccountId'] = get_vim_account_id(account
)
111 #ns['userdata']['key1']='value1'
112 #ns['userdata']['key2']='value2'
114 if ssh_keys
is not None:
115 # ssh_keys is comma separate list
116 # ssh_keys_format = []
117 # for key in ssh_keys.split(','):
118 # ssh_keys_format.append({'key-pair-ref': key})
120 # ns['ssh-authorized-key'] = ssh_keys_format
121 ns
['ssh-authorized-key'] = ssh_keys
.split(',')
123 ns_config
= yaml
.load(config
)
124 if "vim-network-name" in ns_config
:
125 ns_config
["vld"] = ns_config
.pop("vim-network-name")
126 if "vld" in ns_config
:
127 for vld
in ns_config
["vld"]:
128 if vld
.get("vim-network-name"):
129 if isinstance(vld
["vim-network-name"], dict):
130 vim_network_name_dict
= {}
131 for vim_account
, vim_net
in vld
["vim-network-name"].items():
132 vim_network_name_dict
[get_vim_account_id(vim_account
)] = vim_net
133 vld
["vim-network-name"] = vim_network_name_dict
134 ns
["vld"] = ns_config
["vld"]
135 if "vnf" in ns_config
:
136 for vnf
in ns_config
["vnf"]:
137 if vnf
.get("vim_account"):
138 vnf
["vimAccountId"] = get_vim_account_id(vnf
.pop("vim_account"))
140 ns
["vnf"] = ns_config
["vnf"]
142 #print yaml.safe_dump(ns)
144 self
._apiResource
= '/ns_instances_content'
145 self
._apiBase
= '{}{}{}'.format(self
._apiName
,
146 self
._apiVersion
, self
._apiResource
)
148 resp
= self
._http
.post_cmd(endpoint
=self
._apiBase
,
150 if not resp
or 'id' not in resp
:
151 raise ClientException('unexpected response from server: '.format(
155 except ClientException
as exc
:
156 message
="failed to create ns: {} nsd: {}\nerror:\n{}".format(
160 raise ClientException(message
)