Charles Chan | 155ec44 | 2018-09-16 14:30:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.segmentrouting.cli; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.google.common.collect.Sets; |
Ray Milkey | db38bc8 | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Argument; |
| 23 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 52ca4e9 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Charles Chan | 155ec44 | 2018-09-16 14:30:19 -0700 | [diff] [blame] | 25 | import org.onlab.packet.IpPrefix; |
| 26 | import org.onosproject.cli.AbstractShellCommand; |
| 27 | import org.onosproject.net.config.NetworkConfigService; |
| 28 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 29 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
| 30 | |
| 31 | import java.util.Set; |
| 32 | |
| 33 | /** |
| 34 | * CLI command for managing black hole routes. |
| 35 | */ |
Ray Milkey | 52ca4e9 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 36 | @Service |
Charles Chan | 155ec44 | 2018-09-16 14:30:19 -0700 | [diff] [blame] | 37 | @Command(scope = "onos", name = "sr-blackhole", |
| 38 | description = "Manage black hole routes") |
| 39 | public class BlackHoleCommand extends AbstractShellCommand { |
| 40 | @Argument(index = 0, name = "op", |
| 41 | description = "list, add or remove", |
| 42 | required = true, multiValued = false) |
| 43 | private String op; |
| 44 | |
| 45 | @Argument(index = 1, name = "prefix", |
| 46 | description = "IP prefix", |
| 47 | required = false, multiValued = false) |
| 48 | private String prefix; |
| 49 | |
| 50 | @Override |
Ray Milkey | db38bc8 | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 51 | protected void doExecute() { |
Charles Chan | 155ec44 | 2018-09-16 14:30:19 -0700 | [diff] [blame] | 52 | SegmentRoutingService srService = AbstractShellCommand.get(SegmentRoutingService.class); |
| 53 | NetworkConfigService netcfgService = AbstractShellCommand.get(NetworkConfigService.class); |
| 54 | |
| 55 | SegmentRoutingAppConfig appConfig = netcfgService.getConfig(srService.appId(), SegmentRoutingAppConfig.class); |
| 56 | if (appConfig == null) { |
| 57 | JsonNode jsonNode = new ObjectMapper().createObjectNode(); |
| 58 | netcfgService.applyConfig(srService.appId(), SegmentRoutingAppConfig.class, jsonNode); |
| 59 | appConfig = netcfgService.getConfig(srService.appId(), SegmentRoutingAppConfig.class); |
| 60 | } |
| 61 | |
| 62 | Set<IpPrefix> blackHoleIps; |
| 63 | switch (op) { |
| 64 | case "list": |
| 65 | appConfig.blackholeIPs().forEach(prefix -> print(prefix.toString())); |
| 66 | break; |
| 67 | case "add": |
| 68 | blackHoleIps = Sets.newConcurrentHashSet(appConfig.blackholeIPs()); |
| 69 | blackHoleIps.add(IpPrefix.valueOf(prefix)); |
| 70 | appConfig.setBalckholeIps(blackHoleIps); |
| 71 | break; |
| 72 | case "remove": |
| 73 | blackHoleIps = Sets.newConcurrentHashSet(appConfig.blackholeIPs()); |
| 74 | blackHoleIps.remove(IpPrefix.valueOf(prefix)); |
| 75 | appConfig.setBalckholeIps(blackHoleIps); |
| 76 | break; |
| 77 | default: |
| 78 | throw new UnsupportedOperationException("Unknown operation " + op); |
| 79 | } |
| 80 | } |
| 81 | } |