blob: 88b7d4f3f22ad6ac4cf058bdd49f861bf5c7d209 [file] [log] [blame]
Charles Chan155ec442018-09-16 14:30:19 -07001/*
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
17package org.onosproject.segmentrouting.cli;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.google.common.collect.Sets;
Ray Milkeydb38bc82018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Argument;
23import org.apache.karaf.shell.api.action.Command;
Ray Milkey52ca4e92018-09-28 10:58:28 -070024import org.apache.karaf.shell.api.action.lifecycle.Service;
Charles Chan155ec442018-09-16 14:30:19 -070025import org.onlab.packet.IpPrefix;
26import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.net.config.NetworkConfigService;
28import org.onosproject.segmentrouting.SegmentRoutingService;
29import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
30
31import java.util.Set;
32
33/**
34 * CLI command for managing black hole routes.
35 */
Ray Milkey52ca4e92018-09-28 10:58:28 -070036@Service
Charles Chan155ec442018-09-16 14:30:19 -070037@Command(scope = "onos", name = "sr-blackhole",
38 description = "Manage black hole routes")
39public 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 Milkeydb38bc82018-09-27 12:32:28 -070051 protected void doExecute() {
Charles Chan155ec442018-09-16 14:30:19 -070052 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);
Wailok Shumfc2cbaf2022-01-11 05:04:24 +080071 appConfig.apply();
Charles Chan155ec442018-09-16 14:30:19 -070072 break;
73 case "remove":
74 blackHoleIps = Sets.newConcurrentHashSet(appConfig.blackholeIPs());
75 blackHoleIps.remove(IpPrefix.valueOf(prefix));
76 appConfig.setBalckholeIps(blackHoleIps);
Wailok Shumfc2cbaf2022-01-11 05:04:24 +080077 appConfig.apply();
Charles Chan155ec442018-09-16 14:30:19 -070078 break;
79 default:
80 throw new UnsupportedOperationException("Unknown operation " + op);
81 }
82 }
83}