blob: 2491058854a453eb6527f910956cb5135f57c5ee [file] [log] [blame]
jiangrui800e26f2015-11-28 13:59:51 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
20import org.apache.karaf.shell.commands.Command;
21import org.apache.karaf.shell.commands.Option;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.vtnrsc.FloatingIp;
24import org.onosproject.vtnrsc.FloatingIpId;
25import org.onosproject.vtnrsc.floatingip.FloatingIpService;
26
27import com.google.common.collect.Sets;
28
29/**
30 * Supports for remove a floating IP.
31 */
32@Command(scope = "onos", name = "floatingip-remove", description = "Supports for removing a floating IP")
33public class FloatingIpRemoveCommand extends AbstractShellCommand {
34 @Option(name = "-I", aliases = "--id", description = "The floating IP identifier",
35 required = false, multiValued = false)
36 String id = null;
37
38 @Option(name = "-i", aliases = "--fixedIp", description = "The fixed IP of floating IP",
39 required = false, multiValued = false)
40 String fixedIp = null;
41
42 @Option(name = "-l", aliases = "--floatingIp", description = "The floating IP of floating IP",
43 required = false, multiValued = false)
44 String floatingIp = null;
45
46 @Override
47 protected void execute() {
48 FloatingIpService service = get(FloatingIpService.class);
49 if (id == null && fixedIp == null && floatingIp == null) {
50 print(null, "one of id, fixedIp, floatingIp should not be null");
51 }
52 try {
53 Set<FloatingIpId> floatingIpSet = Sets.newHashSet();
54 if (id != null) {
55 floatingIpSet.add(FloatingIpId.of(id));
56 service.removeFloatingIps(floatingIpSet);
57 } else {
58 Iterable<FloatingIp> floatingIps = service.getFloatingIps();
59 if (floatingIps == null) {
60 return;
61 }
62 if (fixedIp != null) {
63 for (FloatingIp floatingIp : floatingIps) {
64 if (floatingIp.fixedIp().toString().equals(fixedIp)) {
65 floatingIpSet.add(floatingIp.id());
66 service.removeFloatingIps(floatingIpSet);
67 return;
68 }
69 }
70 print(null, "The fixedIp is not existed");
71 return;
72 }
73 if (floatingIp != null) {
74 for (FloatingIp floatingIpObj : floatingIps) {
75 if (floatingIpObj.fixedIp().toString()
76 .equals(floatingIp)) {
77 floatingIpSet.add(floatingIpObj.id());
78 service.removeFloatingIps(floatingIpSet);
79 return;
80 }
81 }
82 print(null, "The floatingIp is not existed");
83 return;
84 }
85 }
86 } catch (Exception e) {
87 print(null, e.getMessage());
88 }
89 }
90}