blob: e9f0279e5f13ffb34ea9f105342e7c17ad6769a5 [file] [log] [blame]
pierventre4f68ffa2021-03-09 22:52:14 +01001/*
2 * Copyright 2015-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 */
16package org.onosproject.segmentrouting.cli;
17
18import org.apache.karaf.shell.api.action.Command;
19import org.apache.karaf.shell.api.action.Completion;
20import org.apache.karaf.shell.api.action.Option;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.glassfish.jersey.internal.guava.Sets;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.cli.net.DeviceIdCompleter;
25import org.onosproject.net.DeviceId;
26import org.onosproject.segmentrouting.policy.api.PolicyId;
27import org.onosproject.segmentrouting.policy.api.PolicyService;
28import org.onosproject.segmentrouting.policy.api.RedirectPolicy;
29
30import java.util.Set;
31
32/**
33 * Command to add a new redirect policy.
34 */
35@Service
36@Command(scope = "onos", name = "sr-policy-redirect-add",
37 description = "Create a new redirect policy")
38public class PolicyRedirectAddCommand extends AbstractShellCommand {
39
40 @Option(name = "-s", aliases = "--spine",
41 description = "Pin to spine",
42 valueToShowInHelp = "device:spine1",
43 multiValued = true)
44 @Completion(DeviceIdCompleter.class)
45 String[] spines = null;
46
47 @Override
48 protected void doExecute() {
49 Set<DeviceId> spinesToEnforce = spinesToEnforce();
50 if (spinesToEnforce.isEmpty()) {
51 print("Unable to submit redirect policy");
52 return;
53 }
54 PolicyService policyService = AbstractShellCommand.get(PolicyService.class);
55 PolicyId policyId = policyService.addOrUpdatePolicy(new RedirectPolicy(spinesToEnforce));
56 print("Policy %s has been submitted", policyId);
57 }
58
59 private Set<DeviceId> spinesToEnforce() {
60 Set<DeviceId> spinesToEnforce = Sets.newHashSet();
61 if (spines != null) {
62 for (String spine : spines) {
63 spinesToEnforce.add(DeviceId.deviceId(spine));
64 }
65 }
66 return spinesToEnforce;
67 }
68}