jiangrui | 800e26f | 2015-11-28 13:59:51 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
jiangrui | 800e26f | 2015-11-28 13:59:51 +0800 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.vtnrsc.cli.floatingip; |
| 17 | |
| 18 | import java.util.Set; |
| 19 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
jiangrui | 800e26f | 2015-11-28 13:59:51 +0800 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.vtnrsc.FloatingIp; |
| 25 | import org.onosproject.vtnrsc.FloatingIpId; |
| 26 | import org.onosproject.vtnrsc.floatingip.FloatingIpService; |
| 27 | |
| 28 | import com.google.common.collect.Sets; |
| 29 | |
| 30 | /** |
| 31 | * Supports for remove a floating IP. |
| 32 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 33 | @Service |
jiangrui | 800e26f | 2015-11-28 13:59:51 +0800 | [diff] [blame] | 34 | @Command(scope = "onos", name = "floatingip-remove", description = "Supports for removing a floating IP") |
| 35 | public 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 Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 49 | protected void doExecute() { |
jiangrui | 800e26f | 2015-11-28 13:59:51 +0800 | [diff] [blame] | 50 | 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 | } |