pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.segmentrouting.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.api.action.Command; |
| 19 | import org.apache.karaf.shell.api.action.Completion; |
| 20 | import org.apache.karaf.shell.api.action.Option; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 22 | import org.glassfish.jersey.internal.guava.Sets; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.cli.net.DeviceIdCompleter; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.segmentrouting.policy.api.PolicyId; |
| 27 | import org.onosproject.segmentrouting.policy.api.PolicyService; |
| 28 | import org.onosproject.segmentrouting.policy.api.RedirectPolicy; |
| 29 | |
| 30 | import 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") |
| 38 | public 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 | } |