Committed by
Gerrit Code Review
Add CLI commands to create/list/remove tunnels and policies
Change-Id: I8cffb0feceb07e7f090e6a51fa41dde680b4956a
Showing
6 changed files
with
330 additions
and
0 deletions
apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/cli/PolicyAddCommand.java
0 → 100644
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
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.commands.Argument; | ||
19 | +import org.apache.karaf.shell.commands.Command; | ||
20 | +import org.onosproject.cli.AbstractShellCommand; | ||
21 | +import org.onosproject.segmentrouting.Policy; | ||
22 | +import org.onosproject.segmentrouting.SegmentRoutingService; | ||
23 | +import org.onosproject.segmentrouting.TunnelPolicy; | ||
24 | + | ||
25 | +/** | ||
26 | + * Command to add a new policy. | ||
27 | + */ | ||
28 | +@Command(scope = "onos", name = "srpolicy-add", | ||
29 | + description = "Create a new policy") | ||
30 | +public class PolicyAddCommand extends AbstractShellCommand { | ||
31 | + | ||
32 | + // TODO: Need to support skipping some parameters | ||
33 | + | ||
34 | + @Argument(index = 0, name = "policy ID", | ||
35 | + description = "policy ID", | ||
36 | + required = true, multiValued = false) | ||
37 | + String policyId; | ||
38 | + | ||
39 | + @Argument(index = 1, name = "priority", | ||
40 | + description = "priority", | ||
41 | + required = true, multiValued = false) | ||
42 | + int priority; | ||
43 | + | ||
44 | + @Argument(index = 2, name = "src IP", | ||
45 | + description = "src IP", | ||
46 | + required = false, multiValued = false) | ||
47 | + String srcIp; | ||
48 | + | ||
49 | + @Argument(index = 3, name = "dst IP", | ||
50 | + description = "dst IP", | ||
51 | + required = false, multiValued = false) | ||
52 | + String dstIp; | ||
53 | + | ||
54 | + @Argument(index = 4, name = "policy type", | ||
55 | + description = "policy type", | ||
56 | + required = true, multiValued = false) | ||
57 | + String policyType; | ||
58 | + | ||
59 | + @Argument(index = 5, name = "tunnel ID", | ||
60 | + description = "tunnel ID", | ||
61 | + required = false, multiValued = false) | ||
62 | + String tunnelId; | ||
63 | + | ||
64 | + @Override | ||
65 | + protected void execute() { | ||
66 | + | ||
67 | + SegmentRoutingService srService = | ||
68 | + AbstractShellCommand.get(SegmentRoutingService.class); | ||
69 | + | ||
70 | + TunnelPolicy.Builder tpb = TunnelPolicy.builder().setPolicyId(policyId); | ||
71 | + tpb.setPriority(priority); | ||
72 | + tpb.setType(Policy.Type.valueOf(policyType)); | ||
73 | + | ||
74 | + if (srcIp != null) { | ||
75 | + tpb.setSrcIp(srcIp); | ||
76 | + } | ||
77 | + if (dstIp != null) { | ||
78 | + tpb.setDstIp(dstIp); | ||
79 | + } | ||
80 | + if (Policy.Type.valueOf(policyType) == Policy.Type.TUNNEL_FLOW) { | ||
81 | + if (tunnelId == null) { | ||
82 | + // TODO: handle errors | ||
83 | + return; | ||
84 | + } | ||
85 | + tpb.setTunnelId(tunnelId); | ||
86 | + } | ||
87 | + srService.createPolicy(tpb.build()); | ||
88 | + } | ||
89 | +} |
apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/cli/PolicyListCommand.java
0 → 100644
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
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.commands.Command; | ||
19 | +import org.onosproject.cli.AbstractShellCommand; | ||
20 | +import org.onosproject.segmentrouting.Policy; | ||
21 | +import org.onosproject.segmentrouting.SegmentRoutingService; | ||
22 | +import org.onosproject.segmentrouting.TunnelPolicy; | ||
23 | + | ||
24 | +/** | ||
25 | + * Command to show the list of policies. | ||
26 | + */ | ||
27 | +@Command(scope = "onos", name = "srpolicy-list", | ||
28 | + description = "Lists all policies") | ||
29 | +public class PolicyListCommand extends AbstractShellCommand { | ||
30 | + | ||
31 | + private static final String FORMAT_MAPPING_TUNNEL = | ||
32 | + " id=%s, type=%s, prio=%d, src=%s, dst=%s, proto=%s, tunnel=%s"; | ||
33 | + | ||
34 | + @Override | ||
35 | + protected void execute() { | ||
36 | + | ||
37 | + SegmentRoutingService srService = | ||
38 | + AbstractShellCommand.get(SegmentRoutingService.class); | ||
39 | + | ||
40 | + srService.getPolicies().forEach(policy -> printPolicy(policy)); | ||
41 | + } | ||
42 | + | ||
43 | + private void printPolicy(Policy policy) { | ||
44 | + if (policy.type() == Policy.Type.TUNNEL_FLOW) { | ||
45 | + print(FORMAT_MAPPING_TUNNEL, policy.id(), policy.type(), policy.priority(), | ||
46 | + policy.srcIp(), policy.dstIp(), (policy.ipProto() == null) ? "" : policy.ipProto(), | ||
47 | + ((TunnelPolicy) policy).tunnelId()); | ||
48 | + } | ||
49 | + } | ||
50 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/cli/PolicyRemoveCommand.java
0 → 100644
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
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 | + | ||
19 | +import org.apache.karaf.shell.commands.Argument; | ||
20 | +import org.apache.karaf.shell.commands.Command; | ||
21 | +import org.onosproject.cli.AbstractShellCommand; | ||
22 | +import org.onosproject.segmentrouting.SegmentRoutingService; | ||
23 | +import org.onosproject.segmentrouting.TunnelPolicy; | ||
24 | + | ||
25 | +/** | ||
26 | + * Command to add a new policy. | ||
27 | + */ | ||
28 | +@Command(scope = "onos", name = "srpolicy-remove", | ||
29 | + description = "Remove a policy") | ||
30 | +public class PolicyRemoveCommand extends AbstractShellCommand { | ||
31 | + | ||
32 | + @Argument(index = 0, name = "policy ID", | ||
33 | + description = "policy ID", | ||
34 | + required = true, multiValued = false) | ||
35 | + String policyId; | ||
36 | + | ||
37 | + @Override | ||
38 | + protected void execute() { | ||
39 | + | ||
40 | + SegmentRoutingService srService = | ||
41 | + AbstractShellCommand.get(SegmentRoutingService.class); | ||
42 | + | ||
43 | + TunnelPolicy.Builder tpb = TunnelPolicy.builder().setPolicyId(policyId); | ||
44 | + srService.removePolicy(tpb.build()); | ||
45 | + } | ||
46 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/cli/TunnelAddCommand.java
0 → 100644
1 | + | ||
2 | +/* | ||
3 | + * Copyright 2015 Open Networking Laboratory | ||
4 | + * | ||
5 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
6 | + * you may not use this file except in compliance with the License. | ||
7 | + * You may obtain a copy of the License at | ||
8 | + * | ||
9 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
10 | + * | ||
11 | + * Unless required by applicable law or agreed to in writing, software | ||
12 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
14 | + * See the License for the specific language governing permissions and | ||
15 | + * limitations under the License. | ||
16 | + */ | ||
17 | +package org.onosproject.segmentrouting.cli; | ||
18 | + | ||
19 | +import org.apache.karaf.shell.commands.Argument; | ||
20 | +import org.apache.karaf.shell.commands.Command; | ||
21 | +import org.onosproject.cli.AbstractShellCommand; | ||
22 | +import org.onosproject.segmentrouting.DefaultTunnel; | ||
23 | +import org.onosproject.segmentrouting.SegmentRoutingService; | ||
24 | +import org.onosproject.segmentrouting.Tunnel; | ||
25 | + | ||
26 | +import java.util.ArrayList; | ||
27 | +import java.util.List; | ||
28 | +import java.util.StringTokenizer; | ||
29 | + | ||
30 | +/** | ||
31 | + * Command to add a new tunnel. | ||
32 | + */ | ||
33 | +@Command(scope = "onos", name = "srtunnel-add", | ||
34 | + description = "Create a new tunnel") | ||
35 | +public class TunnelAddCommand extends AbstractShellCommand { | ||
36 | + | ||
37 | + @Argument(index = 0, name = "tunnel ID", | ||
38 | + description = "tunnel ID", | ||
39 | + required = true, multiValued = false) | ||
40 | + String tunnelId; | ||
41 | + | ||
42 | + @Argument(index = 1, name = "label path", | ||
43 | + description = "label path", | ||
44 | + required = true, multiValued = false) | ||
45 | + String labels; | ||
46 | + | ||
47 | + | ||
48 | + @Override | ||
49 | + protected void execute() { | ||
50 | + | ||
51 | + SegmentRoutingService srService = | ||
52 | + AbstractShellCommand.get(SegmentRoutingService.class); | ||
53 | + | ||
54 | + List<Integer> labelIds = new ArrayList<>(); | ||
55 | + StringTokenizer strToken = new StringTokenizer(labels, ","); | ||
56 | + while (strToken.hasMoreTokens()) { | ||
57 | + labelIds.add(Integer.valueOf(strToken.nextToken())); | ||
58 | + } | ||
59 | + Tunnel tunnel = new DefaultTunnel(tunnelId, labelIds); | ||
60 | + | ||
61 | + srService.createTunnel(tunnel); | ||
62 | + } | ||
63 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/cli/TunnelListCommand.java
0 → 100644
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
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.commands.Command; | ||
19 | +import org.onosproject.cli.AbstractShellCommand; | ||
20 | +import org.onosproject.segmentrouting.SegmentRoutingService; | ||
21 | +import org.onosproject.segmentrouting.Tunnel; | ||
22 | + | ||
23 | +/** | ||
24 | + * Command to show the list of tunnels. | ||
25 | + */ | ||
26 | +@Command(scope = "onos", name = "srtunnel-list", | ||
27 | + description = "Lists all tunnels") | ||
28 | +public class TunnelListCommand extends AbstractShellCommand { | ||
29 | + | ||
30 | + private static final String FORMAT_MAPPING = | ||
31 | + " id=%s, path=%s"; | ||
32 | + | ||
33 | + @Override | ||
34 | + protected void execute() { | ||
35 | + | ||
36 | + SegmentRoutingService srService = | ||
37 | + AbstractShellCommand.get(SegmentRoutingService.class); | ||
38 | + | ||
39 | + srService.getTunnels().forEach(tunnel -> printTunnel(tunnel)); | ||
40 | + } | ||
41 | + | ||
42 | + private void printTunnel(Tunnel tunnel) { | ||
43 | + print(FORMAT_MAPPING, tunnel.id(), tunnel.labelIds()); | ||
44 | + } | ||
45 | +} |
1 | +<!-- | ||
2 | + ~ Copyright 2015 Open Networking Laboratory | ||
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 | +<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> | ||
17 | + | ||
18 | + <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0"> | ||
19 | + <command> | ||
20 | + <action class="org.onosproject.segmentrouting.cli.TunnelListCommand"/> | ||
21 | + </command> | ||
22 | + <command> | ||
23 | + <action class="org.onosproject.segmentrouting.cli.PolicyListCommand"/> | ||
24 | + </command> | ||
25 | + <command> | ||
26 | + <action class="org.onosproject.segmentrouting.cli.PolicyAddCommand"/> | ||
27 | + </command> | ||
28 | + <command> | ||
29 | + <action class="org.onosproject.segmentrouting.cli.PolicyRemoveCommand"/> | ||
30 | + </command> | ||
31 | + <command> | ||
32 | + <action class="org.onosproject.segmentrouting.cli.TunnelAddCommand"/> | ||
33 | + </command> | ||
34 | + </command-bundle> | ||
35 | +</blueprint> | ||
36 | + | ||
37 | + |
-
Please register or login to post a comment