Charles Chan | a17b3a1 | 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; |
| 22 | import org.apache.karaf.shell.commands.Argument; |
| 23 | import org.apache.karaf.shell.commands.Command; |
| 24 | import org.onlab.packet.IpPrefix; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.net.config.NetworkConfigService; |
| 27 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 28 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
| 29 | |
| 30 | import java.util.Set; |
| 31 | |
| 32 | /** |
| 33 | * CLI command for managing black hole routes. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "sr-blackhole", |
| 36 | description = "Manage black hole routes") |
| 37 | public class BlackHoleCommand extends AbstractShellCommand { |
| 38 | @Argument(index = 0, name = "op", |
| 39 | description = "list, add or remove", |
| 40 | required = true, multiValued = false) |
| 41 | private String op; |
| 42 | |
| 43 | @Argument(index = 1, name = "prefix", |
| 44 | description = "IP prefix", |
| 45 | required = false, multiValued = false) |
| 46 | private String prefix; |
| 47 | |
| 48 | @Override |
| 49 | protected void execute() { |
| 50 | SegmentRoutingService srService = AbstractShellCommand.get(SegmentRoutingService.class); |
| 51 | NetworkConfigService netcfgService = AbstractShellCommand.get(NetworkConfigService.class); |
| 52 | |
| 53 | SegmentRoutingAppConfig appConfig = netcfgService.getConfig(srService.appId(), SegmentRoutingAppConfig.class); |
| 54 | if (appConfig == null) { |
| 55 | JsonNode jsonNode = new ObjectMapper().createObjectNode(); |
| 56 | netcfgService.applyConfig(srService.appId(), SegmentRoutingAppConfig.class, jsonNode); |
| 57 | appConfig = netcfgService.getConfig(srService.appId(), SegmentRoutingAppConfig.class); |
| 58 | } |
| 59 | |
| 60 | Set<IpPrefix> blackHoleIps; |
| 61 | switch (op) { |
| 62 | case "list": |
| 63 | appConfig.blackholeIPs().forEach(prefix -> print(prefix.toString())); |
| 64 | break; |
| 65 | case "add": |
| 66 | blackHoleIps = Sets.newConcurrentHashSet(appConfig.blackholeIPs()); |
| 67 | blackHoleIps.add(IpPrefix.valueOf(prefix)); |
| 68 | appConfig.setBalckholeIps(blackHoleIps); |
| 69 | break; |
| 70 | case "remove": |
| 71 | blackHoleIps = Sets.newConcurrentHashSet(appConfig.blackholeIPs()); |
| 72 | blackHoleIps.remove(IpPrefix.valueOf(prefix)); |
| 73 | appConfig.setBalckholeIps(blackHoleIps); |
| 74 | break; |
| 75 | default: |
| 76 | throw new UnsupportedOperationException("Unknown operation " + op); |
| 77 | } |
| 78 | } |
| 79 | } |