sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 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 | |
Ray Milkey | db38bc8 | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Command; |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | 52ca4e9 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 21 | import org.glassfish.jersey.internal.guava.Sets; |
| 22 | |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 24 | import org.onosproject.segmentrouting.policy.api.Policy; |
| 25 | import org.onosproject.segmentrouting.policy.api.PolicyData; |
| 26 | import org.onosproject.segmentrouting.policy.api.PolicyService; |
| 27 | |
| 28 | import java.util.Set; |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Command to show the list of policies. |
| 32 | */ |
Ray Milkey | 52ca4e9 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 33 | @Service |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 34 | @Command(scope = "onos", name = "sr-policy", |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 35 | description = "Lists all policies") |
| 36 | public class PolicyListCommand extends AbstractShellCommand { |
| 37 | |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 38 | private static final String FORMAT_MAPPING_POLICY = |
| 39 | " id=%s, state=%s, type=%s"; |
| 40 | private static final String FORMAT_MAPPING_OPERATION = |
| 41 | " op=%s"; |
| 42 | |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 43 | @Option(name = "-t", aliases = "--type", |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 44 | description = "Filter based on policy type", |
| 45 | valueToShowInHelp = "DROP", |
| 46 | multiValued = true) |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 47 | String[] types = null; |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 48 | |
| 49 | @Override |
Ray Milkey | db38bc8 | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 50 | protected void doExecute() { |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 51 | PolicyService policyService = |
| 52 | AbstractShellCommand.get(PolicyService.class); |
| 53 | policyService.policies(policyTypes()).forEach(this::printPolicy); |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 54 | } |
| 55 | |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 56 | private Set<Policy.PolicyType> policyTypes() { |
| 57 | Set<Policy.PolicyType> policyTypes = Sets.newHashSet(); |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 58 | if (types != null) { |
| 59 | for (String filter : types) { |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 60 | policyTypes.add(Policy.PolicyType.valueOf(filter)); |
| 61 | } |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 62 | } |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 63 | return policyTypes; |
| 64 | } |
| 65 | |
| 66 | private void printPolicy(PolicyData policyData) { |
| 67 | print(FORMAT_MAPPING_POLICY, policyData.policy().policyId(), policyData.policyState(), |
| 68 | policyData.policy().policyType()); |
| 69 | policyData.operations().forEach(operation -> print(FORMAT_MAPPING_OPERATION, operation)); |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 70 | } |
| 71 | } |