974a421e811354cab31a8abed2a7fee3394ed1a9
1 # Copyright ETSI Contributors and Others.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
17 from osmclient
.common
.exceptions
import ClientException
18 from osmclient
.common
import print_output
19 from osmclient
.cli_commands
import utils
20 from prettytable
import PrettyTable
24 from datetime
import datetime
27 logger
= logging
.getLogger("osmclient")
30 def vnf_list(ctx
, ns
, filter, long, output
):
34 utils
.check_client_version(ctx
.obj
, "--ns")
36 filter = "&".join(filter)
37 utils
.check_client_version(ctx
.obj
, "--filter")
38 resp
= ctx
.obj
.vnf
.list(ns
, filter)
40 resp
= ctx
.obj
.vnf
.list()
41 fullclassname
= ctx
.obj
.__module
__ + "." + ctx
.obj
.__class
__.__name
__
42 if fullclassname
== "osmclient.sol005.client.Client":
64 table
= PrettyTable(field_names
)
66 name
= vnfr
["name"] if "name" in vnfr
else "-"
71 vnfr
["member-vnf-index-ref"],
73 vnfr
["vim-account-id"],
77 date
= datetime
.fromtimestamp(vnfr
["_admin"]["created"]).strftime(
80 last_update
= datetime
.fromtimestamp(
81 vnfr
["_admin"]["modified"]
82 ).strftime("%Y-%m-%dT%H:%M:%S")
83 new_row
.extend([date
, last_update
])
84 table
.add_row(new_row
)
86 table
= PrettyTable(["vnf name", "id", "operational status", "config status"])
88 if "mgmt-interface" not in vnfr
:
89 vnfr
["mgmt-interface"] = {}
90 vnfr
["mgmt-interface"]["ip-address"] = None
95 vnfr
["operational-status"],
96 vnfr
["config-status"],
99 print_output
.print_output(output
, table
.field_names
, table
._rows
)
102 @click.command(name
="vnf-list", short_help
="list all NF instances")
104 "--ns", default
=None, help="NS instance id or name to restrict the NF list"
110 help="restricts the list to the NF instances matching the filter.",
112 @click.option("--long", is_flag
=True, help="get more details")
113 @print_output.output_option
115 def vnf_list1(ctx
, ns
, filter, long, output
):
116 """list all NF instances"""
118 vnf_list(ctx
, ns
, filter, long, output
)
121 @click.command(name
="nf-list", short_help
="list all NF instances")
123 "--ns", default
=None, help="NS instance id or name to restrict the NF list"
129 help="restricts the list to the NF instances matching the filter.",
131 @click.option("--long", is_flag
=True, help="get more details")
132 @print_output.output_option
134 def nf_list(ctx
, ns
, filter, long, output
):
135 """list all NF instances
139 --ns TEXT NS instance id or name to restrict the VNF list
140 --filter filterExpr Restricts the list to the VNF instances matching the filter
143 filterExpr consists of one or more strings formatted according to "simpleFilterExpr",
144 concatenated using the "&" character:
147 filterExpr := <simpleFilterExpr>["&"<simpleFilterExpr>]*
148 simpleFilterExpr := <attrName>["."<attrName>]*["."<op>]"="<value>[","<value>]*
149 op := "eq" | "neq" | "gt" | "lt" | "gte" | "lte" | "cont" | "ncont"
151 value := scalar value
155 * zero or more occurrences
156 ? zero or one occurrence
157 [] grouping of expressions to be used with ? and *
158 "" quotation marks for marking string constants
162 "AttrName" is the name of one attribute in the data type that defines the representation
163 of the resource. The dot (".") character in "simpleFilterExpr" allows concatenation of
164 <attrName> entries to filter by attributes deeper in the hierarchy of a structured document.
165 "Op" stands for the comparison operator. If the expression has concatenated <attrName>
166 entries, it means that the operator "op" is applied to the attribute addressed by the last
167 <attrName> entry included in the concatenation. All simple filter expressions are combined
168 by the "AND" logical operator. In a concatenation of <attrName> entries in a <simpleFilterExpr>,
169 the rightmost "attrName" entry in a "simpleFilterExpr" is called "leaf attribute". The
170 concatenation of all "attrName" entries except the leaf attribute is called the "attribute
171 prefix". If an attribute referenced in an expression is an array, an object that contains a
172 corresponding array shall be considered to match the expression if any of the elements in the
173 array matches all expressions that have the same attribute prefix.
177 --filter vim-account-id=<VIM_ACCOUNT_ID>
178 --filter vnfd-ref=<VNFD_NAME>
179 --filter vdur.ip-address=<IP_ADDRESS>
180 --filter vnfd-ref=<VNFD_NAME>,vdur.ip-address=<IP_ADDRESS>
183 vnf_list(ctx
, ns
, filter, long, output
)
186 @click.command(name
="vnf-show", short_help
="shows the info of a VNF instance")
187 @click.argument("name")
188 @click.option("--literal", is_flag
=True, help="print literally, no pretty table")
192 help="restricts the information to the fields in the filter",
194 @click.option("--kdu", default
=None, help="KDU name (whose status will be shown)")
195 @print_output.output_option
197 def vnf_show(ctx
, name
, literal
, filter, kdu
, output
):
198 """shows the info of a VNF instance
200 NAME: name or ID of the VNF instance
203 def print_kdu_status(op_info_status
):
204 """print KDU status properly formatted"""
206 op_status
= yaml
.safe_load(op_info_status
)
208 "namespace" in op_status
209 and "info" in op_status
210 and "last_deployed" in op_status
["info"]
211 and "status" in op_status
["info"]
212 and "code" in op_status
["info"]["status"]
213 and "resources" in op_status
["info"]["status"]
214 and "seconds" in op_status
["info"]["last_deployed"]
216 last_deployed_time
= datetime
.fromtimestamp(
217 op_status
["info"]["last_deployed"]["seconds"]
218 ).strftime("%a %b %d %I:%M:%S %Y")
219 print("LAST DEPLOYED: {}".format(last_deployed_time
))
220 print("NAMESPACE: {}".format(op_status
["namespace"]))
221 status_code
= "UNKNOWN"
222 if op_status
["info"]["status"]["code"] == 1:
223 status_code
= "DEPLOYED"
224 print("STATUS: {}".format(status_code
))
227 print(op_status
["info"]["status"]["resources"])
228 if "notes" in op_status
["info"]["status"]:
230 print(op_status
["info"]["status"]["notes"])
232 print(op_info_status
)
234 print(op_info_status
)
239 raise ClientException(
240 '"--literal" option is incompatible with "--kdu" option'
243 raise ClientException(
244 '"--filter" option is incompatible with "--kdu" option'
247 utils
.check_client_version(ctx
.obj
, ctx
.command
.name
)
248 resp
= ctx
.obj
.vnf
.get(name
)
251 ns_id
= resp
["nsr-id-ref"]
253 op_data
["member_vnf_index"] = resp
["member-vnf-index-ref"]
254 op_data
["kdu_name"] = kdu
255 op_data
["primitive"] = "status"
256 op_data
["primitive_params"] = {}
257 op_id
= ctx
.obj
.ns
.exec_op(ns_id
, op_name
="action", op_data
=op_data
, wait
=False)
260 op_info
= ctx
.obj
.ns
.get_op(op_id
)
261 if op_info
["operationState"] == "COMPLETED":
262 print_kdu_status(op_info
["detailed-status"])
266 print("Could not determine KDU status")
270 print(yaml
.safe_dump(resp
, indent
=4, default_flow_style
=False))
273 table
= PrettyTable(["field", "value"])
274 for k
, v
in list(resp
.items()):
275 if not filter or k
in filter:
276 table
.add_row([k
, utils
.wrap_text(text
=json
.dumps(v
, indent
=2), width
=100)])
277 print_output
.print_output(output
, table
.field_names
, table
._rows
)