Feature 10962 Refactoring of osmclient commands
[osm/osmclient.git] / osmclient / cli_commands / sdnc.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.cli_commands import utils
18 from prettytable import PrettyTable
19 import json
20 import logging
21
22 logger = logging.getLogger("osmclient")
23
24
25 @click.command(name="sdnc-create", short_help="creates a new SDN controller")
26 @click.option("--name", prompt=True, help="Name to create sdn controller")
27 @click.option("--type", prompt=True, help="SDN controller type")
28 @click.option(
29 "--sdn_controller_version", # hidden=True,
30 help="Deprecated. Use --config {version: sdn_controller_version}",
31 )
32 @click.option("--url", help="URL in format http[s]://HOST:IP/")
33 @click.option("--ip_address", help="Deprecated. Use --url") # hidden=True,
34 @click.option("--port", help="Deprecated. Use --url") # hidden=True,
35 @click.option(
36 "--switch_dpid", help="Deprecated. Use --config {switch_id: DPID}" # hidden=True,
37 )
38 @click.option(
39 "--config",
40 help="Extra information for SDN in yaml format, as {switch_id: identity used for the plugin (e.g. DPID: "
41 "Openflow Datapath ID), version: version}",
42 )
43 @click.option("--user", help="SDN controller username")
44 @click.option(
45 "--password",
46 hide_input=True,
47 confirmation_prompt=True,
48 help="SDN controller password",
49 )
50 @click.option("--description", default=None, help="human readable description")
51 @click.option(
52 "--wait",
53 required=False,
54 default=False,
55 is_flag=True,
56 help="do not return the control immediately, but keep it until the operation is completed, or timeout",
57 )
58 @click.pass_context
59 def sdnc_create(ctx, **kwargs):
60 """creates a new SDN controller"""
61 logger.debug("")
62 sdncontroller = {
63 x: kwargs[x]
64 for x in kwargs
65 if kwargs[x] and x not in ("wait", "ip_address", "port", "switch_dpid")
66 }
67 if kwargs.get("port"):
68 print("option '--port' is deprecated, use '--url' instead")
69 sdncontroller["port"] = int(kwargs["port"])
70 if kwargs.get("ip_address"):
71 print("option '--ip_address' is deprecated, use '--url' instead")
72 sdncontroller["ip"] = kwargs["ip_address"]
73 if kwargs.get("switch_dpid"):
74 print(
75 "option '--switch_dpid' is deprecated, use '--config={switch_id: id|DPID}' instead"
76 )
77 sdncontroller["dpid"] = kwargs["switch_dpid"]
78 if kwargs.get("sdn_controller_version"):
79 print(
80 "option '--sdn_controller_version' is deprecated, use '--config={version: SDN_CONTROLLER_VERSION}'"
81 " instead"
82 )
83 utils.check_client_version(ctx.obj, ctx.command.name)
84 ctx.obj.sdnc.create(kwargs["name"], sdncontroller, wait=kwargs["wait"])
85
86
87 @click.command(name="sdnc-update", short_help="updates an SDN controller")
88 @click.argument("name")
89 @click.option("--newname", help="New name for the SDN controller")
90 @click.option("--description", default=None, help="human readable description")
91 @click.option("--type", help="SDN controller type")
92 @click.option("--url", help="URL in format http[s]://HOST:IP/")
93 @click.option(
94 "--config",
95 help="Extra information for SDN in yaml format, as "
96 "{switch_id: identity used for the plugin (e.g. DPID: "
97 "Openflow Datapath ID), version: version}",
98 )
99 @click.option("--user", help="SDN controller username")
100 @click.option("--password", help="SDN controller password")
101 @click.option("--ip_address", help="Deprecated. Use --url") # hidden=True
102 @click.option("--port", help="Deprecated. Use --url") # hidden=True
103 @click.option(
104 "--switch_dpid", help="Deprecated. Use --config {switch_dpid: DPID}"
105 ) # hidden=True
106 @click.option(
107 "--sdn_controller_version", help="Deprecated. Use --config {version: VERSION}"
108 ) # hidden=True
109 @click.option(
110 "--wait",
111 required=False,
112 default=False,
113 is_flag=True,
114 help="do not return the control immediately, but keep it until the operation is completed, or timeout",
115 )
116 @click.pass_context
117 def sdnc_update(ctx, **kwargs):
118 """updates an SDN controller
119
120 NAME: name or ID of the SDN controller
121 """
122 logger.debug("")
123 sdncontroller = {
124 x: kwargs[x]
125 for x in kwargs
126 if kwargs[x]
127 and x not in ("wait", "ip_address", "port", "switch_dpid", "new_name")
128 }
129 if kwargs.get("newname"):
130 sdncontroller["name"] = kwargs["newname"]
131 if kwargs.get("port"):
132 print("option '--port' is deprecated, use '--url' instead")
133 sdncontroller["port"] = int(kwargs["port"])
134 if kwargs.get("ip_address"):
135 print("option '--ip_address' is deprecated, use '--url' instead")
136 sdncontroller["ip"] = kwargs["ip_address"]
137 if kwargs.get("switch_dpid"):
138 print(
139 "option '--switch_dpid' is deprecated, use '--config={switch_id: id|DPID}' instead"
140 )
141 sdncontroller["dpid"] = kwargs["switch_dpid"]
142 if kwargs.get("sdn_controller_version"):
143 print(
144 "option '--sdn_controller_version' is deprecated, use '---config={version: SDN_CONTROLLER_VERSION}'"
145 " instead"
146 )
147
148 utils.check_client_version(ctx.obj, ctx.command.name)
149 ctx.obj.sdnc.update(kwargs["name"], sdncontroller, wait=kwargs["wait"])
150
151
152 @click.command(name="sdnc-delete", short_help="deletes an SDN controller")
153 @click.argument("name")
154 @click.option(
155 "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
156 )
157 @click.option(
158 "--wait",
159 required=False,
160 default=False,
161 is_flag=True,
162 help="do not return the control immediately, but keep it until the operation is completed, or timeout",
163 )
164 @click.pass_context
165 def sdnc_delete(ctx, name, force, wait):
166 """deletes an SDN controller
167
168 NAME: name or ID of the SDN controller to be deleted
169 """
170 logger.debug("")
171 utils.check_client_version(ctx.obj, ctx.command.name)
172 ctx.obj.sdnc.delete(name, force, wait=wait)
173
174
175 @click.command(name="sdnc-list", short_help="list all SDN controllers")
176 @click.option(
177 "--filter",
178 default=None,
179 multiple=True,
180 help="restricts the list to the SDN controllers matching the filter with format: 'k[.k..]=v[&k[.k]=v2]'",
181 )
182 @click.pass_context
183 def sdnc_list(ctx, filter):
184 """list all SDN controllers"""
185 logger.debug("")
186 utils.check_client_version(ctx.obj, ctx.command.name)
187 if filter:
188 filter = "&".join(filter)
189 resp = ctx.obj.sdnc.list(filter)
190 table = PrettyTable(["sdnc name", "id"])
191 for sdnc in resp:
192 table.add_row([sdnc["name"], sdnc["_id"]])
193 table.align = "l"
194 print(table)
195
196
197 @click.command(name="sdnc-show", short_help="shows the details of an SDN controller")
198 @click.argument("name")
199 @click.pass_context
200 def sdnc_show(ctx, name):
201 """shows the details of an SDN controller
202
203 NAME: name or ID of the SDN controller
204 """
205 logger.debug("")
206 utils.check_client_version(ctx.obj, ctx.command.name)
207 resp = ctx.obj.sdnc.get(name)
208
209 table = PrettyTable(["key", "attribute"])
210 for k, v in list(resp.items()):
211 table.add_row([k, json.dumps(v, indent=2)])
212 table.align = "l"
213 print(table)