blob: 1f75317d78ee3c72eae3dd67957385d1a16ccce5 [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.Argument;
19import org.apache.karaf.shell.api.action.Command;
Ray Milkey52ca4e92018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
sangho2eae4c62015-06-11 14:49:59 -070021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.segmentrouting.Policy;
sanghobd812f82015-06-29 14:58:47 -070023import org.onosproject.segmentrouting.PolicyHandler;
sangho2eae4c62015-06-11 14:49:59 -070024import org.onosproject.segmentrouting.SegmentRoutingService;
25import org.onosproject.segmentrouting.TunnelPolicy;
26
27/**
28 * Command to add a new policy.
29 */
Ray Milkey52ca4e92018-09-28 10:58:28 -070030@Service
Saurav Das07c74602016-04-27 18:35:50 -070031@Command(scope = "onos", name = "sr-policy-add",
sangho2eae4c62015-06-11 14:49:59 -070032 description = "Create a new policy")
33public class PolicyAddCommand extends AbstractShellCommand {
34
35 // TODO: Need to support skipping some parameters
36
sanghobd812f82015-06-29 14:58:47 -070037 @Argument(index = 0, name = "ID",
sangho2eae4c62015-06-11 14:49:59 -070038 description = "policy ID",
39 required = true, multiValued = false)
40 String policyId;
41
42 @Argument(index = 1, name = "priority",
43 description = "priority",
44 required = true, multiValued = false)
45 int priority;
46
sanghobd812f82015-06-29 14:58:47 -070047 @Argument(index = 2, name = "src_IP",
sangho2eae4c62015-06-11 14:49:59 -070048 description = "src IP",
49 required = false, multiValued = false)
50 String srcIp;
51
sanghobd812f82015-06-29 14:58:47 -070052 @Argument(index = 3, name = "src_port",
sangho8d953f92015-06-22 15:10:19 -070053 description = "src port",
54 required = false, multiValued = false)
55 short srcPort;
56
sanghobd812f82015-06-29 14:58:47 -070057 @Argument(index = 4, name = "dst_IP",
sangho2eae4c62015-06-11 14:49:59 -070058 description = "dst IP",
59 required = false, multiValued = false)
60 String dstIp;
61
sanghobd812f82015-06-29 14:58:47 -070062 @Argument(index = 5, name = "dst_port",
sangho8d953f92015-06-22 15:10:19 -070063 description = "dst port",
64 required = false, multiValued = false)
65 short dstPort;
66
67 @Argument(index = 6, name = "proto",
sanghobd812f82015-06-29 14:58:47 -070068 description = "IP protocol",
sangho8d953f92015-06-22 15:10:19 -070069 required = false, multiValued = false)
70 String proto;
71
sanghobd812f82015-06-29 14:58:47 -070072 @Argument(index = 7, name = "policy_type",
sangho2eae4c62015-06-11 14:49:59 -070073 description = "policy type",
74 required = true, multiValued = false)
75 String policyType;
76
sanghobd812f82015-06-29 14:58:47 -070077 @Argument(index = 8, name = "tunnel_ID",
sangho2eae4c62015-06-11 14:49:59 -070078 description = "tunnel ID",
79 required = false, multiValued = false)
80 String tunnelId;
81
82 @Override
Ray Milkeydb38bc82018-09-27 12:32:28 -070083 protected void doExecute() {
sangho2eae4c62015-06-11 14:49:59 -070084
85 SegmentRoutingService srService =
86 AbstractShellCommand.get(SegmentRoutingService.class);
87
88 TunnelPolicy.Builder tpb = TunnelPolicy.builder().setPolicyId(policyId);
89 tpb.setPriority(priority);
90 tpb.setType(Policy.Type.valueOf(policyType));
91
92 if (srcIp != null) {
93 tpb.setSrcIp(srcIp);
94 }
95 if (dstIp != null) {
96 tpb.setDstIp(dstIp);
97 }
sangho8d953f92015-06-22 15:10:19 -070098 if (srcPort != 0) {
99 tpb.setSrcPort(srcPort);
100 }
101 if (dstPort != 0) {
102 tpb.setDstPort(dstPort);
103 }
Jon Hall8b954f92017-03-28 16:53:22 -0700104 if (!"ip".equals(proto)) {
sangho8d953f92015-06-22 15:10:19 -0700105 tpb.setIpProto(proto);
106 }
sangho2eae4c62015-06-11 14:49:59 -0700107 if (Policy.Type.valueOf(policyType) == Policy.Type.TUNNEL_FLOW) {
108 if (tunnelId == null) {
sanghobd812f82015-06-29 14:58:47 -0700109 error("tunnel ID must be specified for TUNNEL_FLOW policy");
sangho2eae4c62015-06-11 14:49:59 -0700110 return;
111 }
112 tpb.setTunnelId(tunnelId);
113 }
sanghobd812f82015-06-29 14:58:47 -0700114 PolicyHandler.Result result = srService.createPolicy(tpb.build());
115
116 switch (result) {
117 case POLICY_EXISTS:
118 error("the same policy exists");
119 break;
120 case ID_EXISTS:
121 error("the same policy ID exists");
122 break;
123 case TUNNEL_NOT_FOUND:
124 error("the tunnel is not found");
125 break;
126 case UNSUPPORTED_TYPE:
127 error("the policy type specified is not supported");
128 break;
129 default:
130 break;
131 }
132
sangho2eae4c62015-06-11 14:49:59 -0700133 }
134}