blob: 14694a924b37b97e7f0f0278292ef6e1083b3f87 [file] [log] [blame]
sangho6703da22015-06-11 14:49:59 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho6703da22015-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
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.segmentrouting.Policy;
sangho71abe1b2015-06-29 14:58:47 -070022import org.onosproject.segmentrouting.PolicyHandler;
sangho6703da22015-06-11 14:49:59 -070023import org.onosproject.segmentrouting.SegmentRoutingService;
24import org.onosproject.segmentrouting.TunnelPolicy;
25
26/**
27 * Command to add a new policy.
28 */
Saurav Das59232cf2016-04-27 18:35:50 -070029@Command(scope = "onos", name = "sr-policy-add",
sangho6703da22015-06-11 14:49:59 -070030 description = "Create a new policy")
31public class PolicyAddCommand extends AbstractShellCommand {
32
33 // TODO: Need to support skipping some parameters
34
sangho71abe1b2015-06-29 14:58:47 -070035 @Argument(index = 0, name = "ID",
sangho6703da22015-06-11 14:49:59 -070036 description = "policy ID",
37 required = true, multiValued = false)
38 String policyId;
39
40 @Argument(index = 1, name = "priority",
41 description = "priority",
42 required = true, multiValued = false)
43 int priority;
44
sangho71abe1b2015-06-29 14:58:47 -070045 @Argument(index = 2, name = "src_IP",
sangho6703da22015-06-11 14:49:59 -070046 description = "src IP",
47 required = false, multiValued = false)
48 String srcIp;
49
sangho71abe1b2015-06-29 14:58:47 -070050 @Argument(index = 3, name = "src_port",
sanghofe4e3082015-06-22 15:10:19 -070051 description = "src port",
52 required = false, multiValued = false)
53 short srcPort;
54
sangho71abe1b2015-06-29 14:58:47 -070055 @Argument(index = 4, name = "dst_IP",
sangho6703da22015-06-11 14:49:59 -070056 description = "dst IP",
57 required = false, multiValued = false)
58 String dstIp;
59
sangho71abe1b2015-06-29 14:58:47 -070060 @Argument(index = 5, name = "dst_port",
sanghofe4e3082015-06-22 15:10:19 -070061 description = "dst port",
62 required = false, multiValued = false)
63 short dstPort;
64
65 @Argument(index = 6, name = "proto",
sangho71abe1b2015-06-29 14:58:47 -070066 description = "IP protocol",
sanghofe4e3082015-06-22 15:10:19 -070067 required = false, multiValued = false)
68 String proto;
69
sangho71abe1b2015-06-29 14:58:47 -070070 @Argument(index = 7, name = "policy_type",
sangho6703da22015-06-11 14:49:59 -070071 description = "policy type",
72 required = true, multiValued = false)
73 String policyType;
74
sangho71abe1b2015-06-29 14:58:47 -070075 @Argument(index = 8, name = "tunnel_ID",
sangho6703da22015-06-11 14:49:59 -070076 description = "tunnel ID",
77 required = false, multiValued = false)
78 String tunnelId;
79
80 @Override
81 protected void execute() {
82
83 SegmentRoutingService srService =
84 AbstractShellCommand.get(SegmentRoutingService.class);
85
86 TunnelPolicy.Builder tpb = TunnelPolicy.builder().setPolicyId(policyId);
87 tpb.setPriority(priority);
88 tpb.setType(Policy.Type.valueOf(policyType));
89
90 if (srcIp != null) {
91 tpb.setSrcIp(srcIp);
92 }
93 if (dstIp != null) {
94 tpb.setDstIp(dstIp);
95 }
sanghofe4e3082015-06-22 15:10:19 -070096 if (srcPort != 0) {
97 tpb.setSrcPort(srcPort);
98 }
99 if (dstPort != 0) {
100 tpb.setDstPort(dstPort);
101 }
102 if (!proto.equals("ip")) {
103 tpb.setIpProto(proto);
104 }
sangho6703da22015-06-11 14:49:59 -0700105 if (Policy.Type.valueOf(policyType) == Policy.Type.TUNNEL_FLOW) {
106 if (tunnelId == null) {
sangho71abe1b2015-06-29 14:58:47 -0700107 error("tunnel ID must be specified for TUNNEL_FLOW policy");
sangho6703da22015-06-11 14:49:59 -0700108 return;
109 }
110 tpb.setTunnelId(tunnelId);
111 }
sangho71abe1b2015-06-29 14:58:47 -0700112 PolicyHandler.Result result = srService.createPolicy(tpb.build());
113
114 switch (result) {
115 case POLICY_EXISTS:
116 error("the same policy exists");
117 break;
118 case ID_EXISTS:
119 error("the same policy ID exists");
120 break;
121 case TUNNEL_NOT_FOUND:
122 error("the tunnel is not found");
123 break;
124 case UNSUPPORTED_TYPE:
125 error("the policy type specified is not supported");
126 break;
127 default:
128 break;
129 }
130
sangho6703da22015-06-11 14:49:59 -0700131 }
132}