blob: 1c619128d9b47523cebc79009ae013500913a62e [file] [log] [blame]
sangho2eae4c62015-06-11 14:49:59 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho2eae4c62015-06-11 14:49:59 -07003 *
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
Ray Milkeydb38bc82018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Command;
pierventre30368ab2021-02-24 23:23:22 +010019import org.apache.karaf.shell.api.action.Option;
Ray Milkey52ca4e92018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
pierventre30368ab2021-02-24 23:23:22 +010021import org.glassfish.jersey.internal.guava.Sets;
22
sangho2eae4c62015-06-11 14:49:59 -070023import org.onosproject.cli.AbstractShellCommand;
pierventre30368ab2021-02-24 23:23:22 +010024import org.onosproject.segmentrouting.policy.api.Policy;
25import org.onosproject.segmentrouting.policy.api.PolicyData;
26import org.onosproject.segmentrouting.policy.api.PolicyService;
27
28import java.util.Set;
sangho2eae4c62015-06-11 14:49:59 -070029
30/**
31 * Command to show the list of policies.
32 */
Ray Milkey52ca4e92018-09-28 10:58:28 -070033@Service
Saurav Das07c74602016-04-27 18:35:50 -070034@Command(scope = "onos", name = "sr-policy-list",
sangho2eae4c62015-06-11 14:49:59 -070035 description = "Lists all policies")
36public class PolicyListCommand extends AbstractShellCommand {
37
pierventre30368ab2021-02-24 23:23:22 +010038 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
43 @Option(name = "-filt", aliases = "--filter",
44 description = "Filter based on policy type",
45 valueToShowInHelp = "DROP",
46 multiValued = true)
47 String[] filters = null;
sangho2eae4c62015-06-11 14:49:59 -070048
49 @Override
Ray Milkeydb38bc82018-09-27 12:32:28 -070050 protected void doExecute() {
pierventre30368ab2021-02-24 23:23:22 +010051 PolicyService policyService =
52 AbstractShellCommand.get(PolicyService.class);
53 policyService.policies(policyTypes()).forEach(this::printPolicy);
sangho2eae4c62015-06-11 14:49:59 -070054 }
55
pierventre30368ab2021-02-24 23:23:22 +010056 private Set<Policy.PolicyType> policyTypes() {
57 Set<Policy.PolicyType> policyTypes = Sets.newHashSet();
58 if (filters != null) {
59 for (String filter : filters) {
60 policyTypes.add(Policy.PolicyType.valueOf(filter));
61 }
sangho2eae4c62015-06-11 14:49:59 -070062 }
pierventre30368ab2021-02-24 23:23:22 +010063 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));
sangho2eae4c62015-06-11 14:49:59 -070070 }
71}