blob: adcb9364c2156fb20c1d2d4b68ea1b848bfb5d0b [file] [log] [blame]
sangho6703da22015-06-11 14:49:59 -07001/*
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 */
16package org.onosproject.segmentrouting.cli;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.segmentrouting.Policy;
22import org.onosproject.segmentrouting.SegmentRoutingService;
23import 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")
30public 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}