blob: b776500593c92be747fdc408762d1cb70615ffff [file] [log] [blame]
Charles Chana17b3a12018-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 Milkey86ad7bb2018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Argument;
23import org.apache.karaf.shell.api.action.Command;
Charles Chana17b3a12018-09-16 14:30:19 -070024import org.onlab.packet.IpPrefix;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.net.config.NetworkConfigService;
27import org.onosproject.segmentrouting.SegmentRoutingService;
28import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
29
30import 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")
37public 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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070049 protected void doExecute() {
Charles Chana17b3a12018-09-16 14:30:19 -070050 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}