blob: 4071cd83cf8610e5647b7b35f71d1ea4643e6535 [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
18import java.util.Set;
19
Ray Milkey86ad7bb2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
jiangrui800e26f2015-11-28 13:59:51 +080023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.vtnrsc.FloatingIp;
25import org.onosproject.vtnrsc.FloatingIpId;
26import org.onosproject.vtnrsc.floatingip.FloatingIpService;
27
28import com.google.common.collect.Sets;
29
30/**
31 * Supports for remove a floating IP.
32 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070033@Service
jiangrui800e26f2015-11-28 13:59:51 +080034@Command(scope = "onos", name = "floatingip-remove", description = "Supports for removing a floating IP")
35public class FloatingIpRemoveCommand extends AbstractShellCommand {
36 @Option(name = "-I", aliases = "--id", description = "The floating IP identifier",
37 required = false, multiValued = false)
38 String id = null;
39
40 @Option(name = "-i", aliases = "--fixedIp", description = "The fixed IP of floating IP",
41 required = false, multiValued = false)
42 String fixedIp = null;
43
44 @Option(name = "-l", aliases = "--floatingIp", description = "The floating IP of floating IP",
45 required = false, multiValued = false)
46 String floatingIp = null;
47
48 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070049 protected void doExecute() {
jiangrui800e26f2015-11-28 13:59:51 +080050 FloatingIpService service = get(FloatingIpService.class);
51 if (id == null && fixedIp == null && floatingIp == null) {
52 print(null, "one of id, fixedIp, floatingIp should not be null");
53 }
54 try {
55 Set<FloatingIpId> floatingIpSet = Sets.newHashSet();
56 if (id != null) {
57 floatingIpSet.add(FloatingIpId.of(id));
58 service.removeFloatingIps(floatingIpSet);
59 } else {
60 Iterable<FloatingIp> floatingIps = service.getFloatingIps();
61 if (floatingIps == null) {
62 return;
63 }
64 if (fixedIp != null) {
65 for (FloatingIp floatingIp : floatingIps) {
66 if (floatingIp.fixedIp().toString().equals(fixedIp)) {
67 floatingIpSet.add(floatingIp.id());
68 service.removeFloatingIps(floatingIpSet);
69 return;
70 }
71 }
72 print(null, "The fixedIp is not existed");
73 return;
74 }
75 if (floatingIp != null) {
76 for (FloatingIp floatingIpObj : floatingIps) {
77 if (floatingIpObj.fixedIp().toString()
78 .equals(floatingIp)) {
79 floatingIpSet.add(floatingIpObj.id());
80 service.removeFloatingIps(floatingIpSet);
81 return;
82 }
83 }
84 print(null, "The floatingIp is not existed");
85 return;
86 }
87 }
88 } catch (Exception e) {
89 print(null, e.getMessage());
90 }
91 }
92}