blob: 62a8e050f030031acc39552f4c8a5cedf2e063dd [file] [log] [blame]
jiangrui800e26f2015-11-28 13:59:51 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
jiangrui800e26f2015-11-28 13:59:51 +08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.vtnrsc.cli.floatingip;
17
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Command;
19import org.apache.karaf.shell.api.action.Option;
jiangrui800e26f2015-11-28 13:59:51 +080020import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.vtnrsc.FloatingIpId;
22import org.onosproject.vtnrsc.FloatingIp;
23import org.onosproject.vtnrsc.floatingip.FloatingIpService;
24
25/**
26 * Supports for query a floating IP.
27 */
28@Command(scope = "onos", name = "floatingips", description = "Supports for querying a floating IP")
29public class FloatingIpQueryCommand extends AbstractShellCommand {
30 @Option(name = "-I", aliases = "--id", description = "The floating IP identifier",
31 required = false, multiValued = false)
32 String id = null;
33
34 @Option(name = "-i", aliases = "--fixedIp", description = "The fixed IP of floating IP",
35 required = false, multiValued = false)
36 String fixedIp = null;
37
38 @Option(name = "-l", aliases = "--floatingIp", description = "The floating IP of floating IP",
39 required = false, multiValued = false)
40 String floatingIp = null;
41
42 private static final String FMT = "floatingIpId=%s, networkId=%s, tenantId=%s, portId=%s,"
43 + "routerId=%s, fixedIp=%s, floatingIp=%s, status=%s";
44
45 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070046 protected void doExecute() {
jiangrui800e26f2015-11-28 13:59:51 +080047 FloatingIpService service = get(FloatingIpService.class);
48 if (id != null) {
49 FloatingIp floatingIp = service.getFloatingIp(FloatingIpId
50 .of(id));
51 printFloatingIp(floatingIp);
52 } else if (fixedIp != null || floatingIp != null) {
53 Iterable<FloatingIp> floatingIps = service.getFloatingIps();
54 if (floatingIps == null) {
55 return;
56 }
57 if (fixedIp != null) {
58 for (FloatingIp floatingIp : floatingIps) {
59 if (floatingIp.fixedIp().toString().equals(fixedIp)) {
60 printFloatingIp(floatingIp);
61 return;
62 }
63 }
64 print(null, "The fixedIp is not existed");
65 }
66 if (floatingIp != null) {
67 for (FloatingIp floatingIpObj : floatingIps) {
68 if (floatingIpObj.fixedIp().toString().equals(floatingIp)) {
69 printFloatingIp(floatingIpObj);
70 return;
71 }
72 }
73 print(null, "The floatingIp is not existed");
74 }
75 } else {
76 Iterable<FloatingIp> floatingIps = service.getFloatingIps();
77 if (floatingIps == null) {
78 return;
79 }
80 for (FloatingIp floatingIp : floatingIps) {
81 printFloatingIp(floatingIp);
82 }
83 }
84 }
85
86 private void printFloatingIp(FloatingIp floatingIp) {
87 print(FMT, floatingIp.id(), floatingIp.networkId(),
88 floatingIp.tenantId(), floatingIp.portId(),
89 floatingIp.routerId(), floatingIp.fixedIp(),
90 floatingIp.floatingIp(), floatingIp.status());
91 }
92}