blob: 29313079db858f9df13416ec1128ed55a76335e0 [file] [log] [blame]
sangho2eae4c62015-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
sangho8d953f92015-06-22 15:10:19 -070049 @Argument(index = 3, name = "src port",
50 description = "src port",
51 required = false, multiValued = false)
52 short srcPort;
53
54 @Argument(index = 4, name = "dst IP",
sangho2eae4c62015-06-11 14:49:59 -070055 description = "dst IP",
56 required = false, multiValued = false)
57 String dstIp;
58
sangho8d953f92015-06-22 15:10:19 -070059 @Argument(index = 5, name = "dst port",
60 description = "dst port",
61 required = false, multiValued = false)
62 short dstPort;
63
64 @Argument(index = 6, name = "proto",
65 description = "proto",
66 required = false, multiValued = false)
67 String proto;
68
69 @Argument(index = 7, name = "policy type",
sangho2eae4c62015-06-11 14:49:59 -070070 description = "policy type",
71 required = true, multiValued = false)
72 String policyType;
73
sangho8d953f92015-06-22 15:10:19 -070074 @Argument(index = 8, name = "tunnel ID",
sangho2eae4c62015-06-11 14:49:59 -070075 description = "tunnel ID",
76 required = false, multiValued = false)
77 String tunnelId;
78
79 @Override
80 protected void execute() {
81
82 SegmentRoutingService srService =
83 AbstractShellCommand.get(SegmentRoutingService.class);
84
85 TunnelPolicy.Builder tpb = TunnelPolicy.builder().setPolicyId(policyId);
86 tpb.setPriority(priority);
87 tpb.setType(Policy.Type.valueOf(policyType));
88
89 if (srcIp != null) {
90 tpb.setSrcIp(srcIp);
91 }
92 if (dstIp != null) {
93 tpb.setDstIp(dstIp);
94 }
sangho8d953f92015-06-22 15:10:19 -070095 if (srcPort != 0) {
96 tpb.setSrcPort(srcPort);
97 }
98 if (dstPort != 0) {
99 tpb.setDstPort(dstPort);
100 }
101 if (!proto.equals("ip")) {
102 tpb.setIpProto(proto);
103 }
sangho2eae4c62015-06-11 14:49:59 -0700104 if (Policy.Type.valueOf(policyType) == Policy.Type.TUNNEL_FLOW) {
105 if (tunnelId == null) {
106 // TODO: handle errors
107 return;
108 }
109 tpb.setTunnelId(tunnelId);
110 }
111 srService.createPolicy(tpb.build());
112 }
113}