1 # Copyright 2017 Sandvine
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):
34 """Returns a list of ns's"""
35 resp
= self
._http
.get_cmd(
36 "api/running/{}ns-instance-config".format(self
._client
.so_rbac_project_path
)
38 if not resp
or "nsr:ns-instance-config" not in resp
:
41 if "nsr" not in resp
["nsr:ns-instance-config"]:
44 return resp
["nsr:ns-instance-config"]["nsr"]
47 """Returns an ns based on name"""
48 for ns
in self
.list():
49 if name
== ns
["name"]:
51 raise NotFound("ns {} not found".format(name
))
53 def scale(self
, ns_name
, ns_scale_group
, instance_index
):
55 postdata
["instance"] = list()
57 instance
["id"] = instance_index
58 postdata
["instance"].append(instance
)
60 ns
= self
.get(ns_name
)
61 resp
= self
._http
.post_cmd(
62 "v1/api/config/{}ns-instance-config/nsr/{}/scaling-group/{}/instance".format(
63 self
._client
.so_rbac_project_path
, ns
["id"], ns_scale_group
67 if "success" not in resp
:
68 raise ClientException(
69 "failed to scale ns: {} result: {}".format(ns_name
, resp
)
79 description
="default description",
80 admin_status
="ENABLED",
83 postdata
["nsr"] = list()
85 nsr
["id"] = str(uuid
.uuid1())
87 nsd
= self
._client
.nsd
.get(nsd_name
)
89 if self
._client
._so
_version
== "v3":
90 datacenter
, resource_orchestrator
= self
._client
.vim
.get_datacenter(account
)
91 if datacenter
is None or resource_orchestrator
is None:
92 raise NotFound("cannot find datacenter account {}".format(account
))
93 if "uuid" not in datacenter
:
95 "The RO Datacenter - {} is invalid. Please select another".format(
100 # Backwards Compatiility
101 datacenter
= self
._client
.vim
.get_datacenter(account
)
102 if datacenter
is None:
103 raise NotFound("cannot find datacenter account {}".format(account
))
106 nsr
["name"] = nsr_name
107 nsr
["short-name"] = nsr_name
108 nsr
["description"] = description
109 nsr
["admin-status"] = admin_status
111 if self
._client
._so
_version
== "v3":
113 nsr
["resource-orchestrator"] = resource_orchestrator
114 nsr
["datacenter"] = datacenter
["name"]
116 # Backwards Compatiility
117 nsr
["om-datacenter"] = datacenter
["uuid"]
119 if ssh_keys
is not None:
120 # ssh_keys is comma separate list
122 for key
in ssh_keys
.split(","):
123 ssh_keys_format
.append({"key-pair-ref": key
})
125 nsr
["ssh-authorized-key"] = ssh_keys_format
130 ns_config
= yaml
.safe_load(config
)
132 if ns_config
and "vim-network-name" in ns_config
:
133 for network
in ns_config
["vim-network-name"]:
134 # now find this network
135 vld_name
= network
["name"]
136 # vim_vld_name = network['vim-network-name']
138 for index
, vld
in enumerate(nsr
["nsd"]["vld"]):
139 if vld
["name"] == vld_name
:
140 nsr
["nsd"]["vld"][index
]["vim-network-name"] = network
[
144 postdata
["nsr"].append(nsr
)
146 resp
= self
._http
.post_cmd(
147 "api/config/{}ns-instance-config/nsr".format(
148 self
._client
.so_rbac_project_path
153 if "success" not in resp
:
154 raise ClientException(
155 "failed to create ns: {} nsd: {} result: {}".format(
156 nsr_name
, nsd_name
, resp
160 def get_opdata(self
, id):
161 return self
._http
.get_cmd(
162 "api/operational/{}ns-instance-opdata/nsr/{}?deep".format(
163 self
._client
.so_rbac_project_path
, id
167 def get_field(self
, ns_name
, field
):
168 nsr
= self
.get(ns_name
)
170 raise NotFound("failed to retrieve ns {}".format(ns_name
))
175 nsopdata
= self
.get_opdata(nsr
["id"])
177 if field
in nsopdata
["nsr:nsr"]:
178 return nsopdata
["nsr:nsr"][field
]
180 raise NotFound("failed to find {} in ns {}".format(field
, ns_name
))
182 def _terminate(self
, ns_name
):
183 ns
= self
.get(ns_name
)
185 raise NotFound("cannot find ns {}".format(ns_name
))
187 return self
._http
.delete_cmd(
188 "api/config/{}ns-instance-config/nsr/{}".format(
189 self
._client
.so_rbac_project_path
, ns
["id"]
193 def delete(self
, ns_name
, wait
=True):
194 vnfs
= self
.get_field(ns_name
, "constituent-vnfr-ref")
196 resp
= self
._terminate
(ns_name
)
197 if "success" not in resp
:
198 raise ClientException("failed to delete ns {}".format(ns_name
))
200 # helper method to check if pkg exists
201 def check_not_exists(func
):
209 if not utils
.wait_for_value(
210 lambda: check_not_exists(lambda: self
._client
.vnf
.get(vnf
["vnfr-id"]))
212 raise ClientException("vnf {} failed to delete".format(vnf
["vnfr-id"]))
213 if not utils
.wait_for_value(
214 lambda: check_not_exists(lambda: self
.get(ns_name
))
216 raise ClientException("ns {} failed to delete".format(ns_name
))
218 def get_monitoring(self
, ns_name
):
219 ns
= self
.get(ns_name
)
224 vnfs
= self
._client
.vnf
.list()
226 if ns
["id"] == vnf
["nsr-id-ref"]:
227 if "monitoring-param" in vnf
:
228 mon_list
[vnf
["name"]] = vnf
["monitoring-param"]