blob: d4b08a734b9c2872f6279ea810134ea360a54606 [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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
jiangrui800e26f2015-11-28 13:59:51 +080021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.vtnrsc.FloatingIpId;
23import org.onosproject.vtnrsc.FloatingIp;
24import org.onosproject.vtnrsc.floatingip.FloatingIpService;
25
26/**
27 * Supports for query a floating IP.
28 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070029@Service
jiangrui800e26f2015-11-28 13:59:51 +080030@Command(scope = "onos", name = "floatingips", description = "Supports for querying a floating IP")
31public class FloatingIpQueryCommand extends AbstractShellCommand {
32 @Option(name = "-I", aliases = "--id", description = "The floating IP identifier",
33 required = false, multiValued = false)
34 String id = null;
35
36 @Option(name = "-i", aliases = "--fixedIp", description = "The fixed IP of floating IP",
37 required = false, multiValued = false)
38 String fixedIp = null;
39
40 @Option(name = "-l", aliases = "--floatingIp", description = "The floating IP of floating IP",
41 required = false, multiValued = false)
42 String floatingIp = null;
43
44 private static final String FMT = "floatingIpId=%s, networkId=%s, tenantId=%s, portId=%s,"
45 + "routerId=%s, fixedIp=%s, floatingIp=%s, status=%s";
46
47 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070048 protected void doExecute() {
jiangrui800e26f2015-11-28 13:59:51 +080049 FloatingIpService service = get(FloatingIpService.class);
50 if (id != null) {
51 FloatingIp floatingIp = service.getFloatingIp(FloatingIpId
52 .of(id));
53 printFloatingIp(floatingIp);
54 } else if (fixedIp != null || floatingIp != null) {
55 Iterable<FloatingIp> floatingIps = service.getFloatingIps();
56 if (floatingIps == null) {
57 return;
58 }
59 if (fixedIp != null) {
60 for (FloatingIp floatingIp : floatingIps) {
61 if (floatingIp.fixedIp().toString().equals(fixedIp)) {
62 printFloatingIp(floatingIp);
63 return;
64 }
65 }
66 print(null, "The fixedIp is not existed");
67 }
68 if (floatingIp != null) {
69 for (FloatingIp floatingIpObj : floatingIps) {
70 if (floatingIpObj.fixedIp().toString().equals(floatingIp)) {
71 printFloatingIp(floatingIpObj);
72 return;
73 }
74 }
75 print(null, "The floatingIp is not existed");
76 }
77 } else {
78 Iterable<FloatingIp> floatingIps = service.getFloatingIps();
79 if (floatingIps == null) {
80 return;
81 }
82 for (FloatingIp floatingIp : floatingIps) {
83 printFloatingIp(floatingIp);
84 }
85 }
86 }
87
88 private void printFloatingIp(FloatingIp floatingIp) {
89 print(FMT, floatingIp.id(), floatingIp.networkId(),
90 floatingIp.tenantId(), floatingIp.portId(),
91 floatingIp.routerId(), floatingIp.fixedIp(),
92 floatingIp.floatingIp(), floatingIp.status());
93 }
94}