Feature 10962 Refactoring of osmclient commands
[osm/osmclient.git] / osmclient / cli_commands / netslice_instance.py
1 # Copyright ETSI Contributors and Others.
2 # All Rights Reserved.
3 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
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
14 # under the License.
15
16 import click
17 from osmclient.common.exceptions import ClientException
18 from osmclient.cli_commands import utils
19 from prettytable import PrettyTable
20 import yaml
21 import json
22 import logging
23
24 logger = logging.getLogger("osmclient")
25
26
27 def nsi_list(ctx, filter):
28 """list all Network Slice Instances"""
29 logger.debug("")
30 utils.check_client_version(ctx.obj, ctx.command.name)
31 if filter:
32 filter = "&".join(filter)
33 resp = ctx.obj.nsi.list(filter)
34 table = PrettyTable(
35 [
36 "netslice instance name",
37 "id",
38 "operational status",
39 "config status",
40 "detailed status",
41 ]
42 )
43 for nsi in resp:
44 nsi_name = nsi["name"]
45 nsi_id = nsi["_id"]
46 opstatus = (
47 nsi["operational-status"] if "operational-status" in nsi else "Not found"
48 )
49 configstatus = nsi["config-status"] if "config-status" in nsi else "Not found"
50 detailed_status = (
51 nsi["detailed-status"] if "detailed-status" in nsi else "Not found"
52 )
53 if configstatus == "config_not_needed":
54 configstatus = "configured (no charms)"
55 table.add_row([nsi_name, nsi_id, opstatus, configstatus, detailed_status])
56 table.align = "l"
57 print(table)
58
59
60 @click.command(name="nsi-list", short_help="list all Network Slice Instances (NSI)")
61 @click.option(
62 "--filter",
63 default=None,
64 multiple=True,
65 help="restricts the list to the Network Slice Instances matching the filter",
66 )
67 @click.pass_context
68 def nsi_list1(ctx, filter):
69 """list all Network Slice Instances (NSI)"""
70 logger.debug("")
71 nsi_list(ctx, filter)
72
73
74 @click.command(
75 name="netslice-instance-list", short_help="list all Network Slice Instances (NSI)"
76 )
77 @click.option(
78 "--filter",
79 default=None,
80 multiple=True,
81 help="restricts the list to the Network Slice Instances matching the filter",
82 )
83 @click.pass_context
84 def nsi_list2(ctx, filter):
85 """list all Network Slice Instances (NSI)"""
86 logger.debug("")
87 nsi_list(ctx, filter)
88
89
90 def nsi_show(ctx, name, literal, filter):
91 logger.debug("")
92 utils.check_client_version(ctx.obj, ctx.command.name)
93 nsi = ctx.obj.nsi.get(name)
94
95 if literal:
96 print(yaml.safe_dump(nsi, indent=4, default_flow_style=False))
97 return
98
99 table = PrettyTable(["field", "value"])
100
101 for k, v in list(nsi.items()):
102 if not filter or k in filter:
103 table.add_row([k, json.dumps(v, indent=2)])
104
105 table.align = "l"
106 print(table)
107
108
109 @click.command(
110 name="nsi-show", short_help="shows the content of a Network Slice Instance (NSI)"
111 )
112 @click.argument("name")
113 @click.option("--literal", is_flag=True, help="print literally, no pretty table")
114 @click.option(
115 "--filter",
116 multiple=True,
117 help="restricts the information to the fields in the filter",
118 )
119 @click.pass_context
120 def nsi_show1(ctx, name, literal, filter):
121 """shows the content of a Network Slice Instance (NSI)
122
123 NAME: name or ID of the Network Slice Instance
124 """
125 logger.debug("")
126 nsi_show(ctx, name, literal, filter)
127
128
129 @click.command(
130 name="netslice-instance-show",
131 short_help="shows the content of a Network Slice Instance (NSI)",
132 )
133 @click.argument("name")
134 @click.option("--literal", is_flag=True, help="print literally, no pretty table")
135 @click.option(
136 "--filter",
137 multiple=True,
138 help="restricts the information to the fields in the filter",
139 )
140 @click.pass_context
141 def nsi_show2(ctx, name, literal, filter):
142 """shows the content of a Network Slice Instance (NSI)
143
144 NAME: name or ID of the Network Slice Instance
145 """
146 logger.debug("")
147 nsi_show(ctx, name, literal, filter)
148
149
150 def nsi_create(
151 ctx, nst_name, nsi_name, vim_account, ssh_keys, config, config_file, wait
152 ):
153 """creates a new Network Slice Instance (NSI)"""
154 logger.debug("")
155 utils.check_client_version(ctx.obj, ctx.command.name)
156 if config_file:
157 if config:
158 raise ClientException(
159 '"--config" option is incompatible with "--config_file" option'
160 )
161 with open(config_file, "r") as cf:
162 config = cf.read()
163 ctx.obj.nsi.create(
164 nst_name,
165 nsi_name,
166 config=config,
167 ssh_keys=ssh_keys,
168 account=vim_account,
169 wait=wait,
170 )
171
172
173 @click.command(name="nsi-create", short_help="creates a new Network Slice Instance")
174 @click.option("--nsi_name", prompt=True, help="name of the Network Slice Instance")
175 @click.option("--nst_name", prompt=True, help="name of the Network Slice Template")
176 @click.option(
177 "--vim_account",
178 prompt=True,
179 help="default VIM account id or name for the deployment",
180 )
181 @click.option(
182 "--ssh_keys", default=None, help="comma separated list of keys to inject to vnfs"
183 )
184 @click.option(
185 "--config",
186 default=None,
187 help="Netslice specific yaml configuration:\n"
188 "netslice_subnet: [\n"
189 "id: TEXT, vim_account: TEXT,\n"
190 "vnf: [member-vnf-index: TEXT, vim_account: TEXT]\n"
191 "vld: [name: TEXT, vim-network-name: TEXT or DICT with vim_account, vim_net entries]\n"
192 "additionalParamsForNsi: {param: value, ...}\n"
193 "additionalParamsForsubnet: [{id: SUBNET_ID, additionalParamsForNs: {}, additionalParamsForVnf: {}}]\n"
194 "],\n"
195 "netslice-vld: [name: TEXT, vim-network-name: TEXT or DICT with vim_account, vim_net entries]",
196 )
197 @click.option(
198 "--config_file", default=None, help="nsi specific yaml configuration file"
199 )
200 @click.option(
201 "--wait",
202 required=False,
203 default=False,
204 is_flag=True,
205 help="do not return the control immediately, but keep it "
206 "until the operation is completed, or timeout",
207 )
208 @click.pass_context
209 def nsi_create1(
210 ctx, nst_name, nsi_name, vim_account, ssh_keys, config, config_file, wait
211 ):
212 """creates a new Network Slice Instance (NSI)"""
213 logger.debug("")
214 nsi_create(
215 ctx, nst_name, nsi_name, vim_account, ssh_keys, config, config_file, wait=wait
216 )
217
218
219 @click.command(
220 name="netslice-instance-create", short_help="creates a new Network Slice Instance"
221 )
222 @click.option("--nsi_name", prompt=True, help="name of the Network Slice Instance")
223 @click.option("--nst_name", prompt=True, help="name of the Network Slice Template")
224 @click.option(
225 "--vim_account",
226 prompt=True,
227 help="default VIM account id or name for the deployment",
228 )
229 @click.option(
230 "--ssh_keys", default=None, help="comma separated list of keys to inject to vnfs"
231 )
232 @click.option(
233 "--config",
234 default=None,
235 help="Netslice specific yaml configuration:\n"
236 "netslice_subnet: [\n"
237 "id: TEXT, vim_account: TEXT,\n"
238 "vnf: [member-vnf-index: TEXT, vim_account: TEXT]\n"
239 "vld: [name: TEXT, vim-network-name: TEXT or DICT with vim_account, vim_net entries]"
240 "],\n"
241 "netslice-vld: [name: TEXT, vim-network-name: TEXT or DICT with vim_account, vim_net entries]",
242 )
243 @click.option(
244 "--config_file", default=None, help="nsi specific yaml configuration file"
245 )
246 @click.option(
247 "--wait",
248 required=False,
249 default=False,
250 is_flag=True,
251 help="do not return the control immediately, but keep it "
252 "until the operation is completed, or timeout",
253 )
254 @click.pass_context
255 def nsi_create2(
256 ctx, nst_name, nsi_name, vim_account, ssh_keys, config, config_file, wait
257 ):
258 """creates a new Network Slice Instance (NSI)"""
259 logger.debug("")
260 nsi_create(
261 ctx, nst_name, nsi_name, vim_account, ssh_keys, config, config_file, wait=wait
262 )
263
264
265 def nsi_delete(ctx, name, force, wait):
266 logger.debug("")
267 utils.check_client_version(ctx.obj, ctx.command.name)
268 ctx.obj.nsi.delete(name, force, wait=wait)
269
270
271 @click.command(name="nsi-delete", short_help="deletes a Network Slice Instance (NSI)")
272 @click.argument("name")
273 @click.option(
274 "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
275 )
276 @click.option(
277 "--wait",
278 required=False,
279 default=False,
280 is_flag=True,
281 help="do not return the control immediately, but keep it "
282 "until the operation is completed, or timeout",
283 )
284 @click.pass_context
285 def nsi_delete1(ctx, name, force, wait):
286 """deletes a Network Slice Instance (NSI)
287
288 NAME: name or ID of the Network Slice instance to be deleted
289 """
290 logger.debug("")
291 nsi_delete(ctx, name, force, wait=wait)
292
293
294 @click.command(
295 name="netslice-instance-delete", short_help="deletes a Network Slice Instance (NSI)"
296 )
297 @click.argument("name")
298 @click.option(
299 "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
300 )
301 @click.pass_context
302 def nsi_delete2(ctx, name, force, wait):
303 """deletes a Network Slice Instance (NSI)
304
305 NAME: name or ID of the Network Slice instance to be deleted
306 """
307 logger.debug("")
308 nsi_delete(ctx, name, force, wait=wait)