sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 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 | |
Ray Milkey | db38bc8 | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 52ca4e9 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.segmentrouting.Policy; |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 23 | import org.onosproject.segmentrouting.PolicyHandler; |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 24 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 25 | import org.onosproject.segmentrouting.TunnelPolicy; |
| 26 | |
| 27 | /** |
| 28 | * Command to add a new policy. |
| 29 | */ |
Ray Milkey | 52ca4e9 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 30 | @Service |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 31 | @Command(scope = "onos", name = "sr-policy-add", |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 32 | description = "Create a new policy") |
| 33 | public class PolicyAddCommand extends AbstractShellCommand { |
| 34 | |
| 35 | // TODO: Need to support skipping some parameters |
| 36 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 37 | @Argument(index = 0, name = "ID", |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 38 | 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 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 47 | @Argument(index = 2, name = "src_IP", |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 48 | description = "src IP", |
| 49 | required = false, multiValued = false) |
| 50 | String srcIp; |
| 51 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 52 | @Argument(index = 3, name = "src_port", |
sangho | 8d953f9 | 2015-06-22 15:10:19 -0700 | [diff] [blame] | 53 | description = "src port", |
| 54 | required = false, multiValued = false) |
| 55 | short srcPort; |
| 56 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 57 | @Argument(index = 4, name = "dst_IP", |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 58 | description = "dst IP", |
| 59 | required = false, multiValued = false) |
| 60 | String dstIp; |
| 61 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 62 | @Argument(index = 5, name = "dst_port", |
sangho | 8d953f9 | 2015-06-22 15:10:19 -0700 | [diff] [blame] | 63 | description = "dst port", |
| 64 | required = false, multiValued = false) |
| 65 | short dstPort; |
| 66 | |
| 67 | @Argument(index = 6, name = "proto", |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 68 | description = "IP protocol", |
sangho | 8d953f9 | 2015-06-22 15:10:19 -0700 | [diff] [blame] | 69 | required = false, multiValued = false) |
| 70 | String proto; |
| 71 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 72 | @Argument(index = 7, name = "policy_type", |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 73 | description = "policy type", |
| 74 | required = true, multiValued = false) |
| 75 | String policyType; |
| 76 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 77 | @Argument(index = 8, name = "tunnel_ID", |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 78 | description = "tunnel ID", |
| 79 | required = false, multiValued = false) |
| 80 | String tunnelId; |
| 81 | |
| 82 | @Override |
Ray Milkey | db38bc8 | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 83 | protected void doExecute() { |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 84 | |
| 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 | } |
sangho | 8d953f9 | 2015-06-22 15:10:19 -0700 | [diff] [blame] | 98 | if (srcPort != 0) { |
| 99 | tpb.setSrcPort(srcPort); |
| 100 | } |
| 101 | if (dstPort != 0) { |
| 102 | tpb.setDstPort(dstPort); |
| 103 | } |
Jon Hall | 8b954f9 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 104 | if (!"ip".equals(proto)) { |
sangho | 8d953f9 | 2015-06-22 15:10:19 -0700 | [diff] [blame] | 105 | tpb.setIpProto(proto); |
| 106 | } |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 107 | if (Policy.Type.valueOf(policyType) == Policy.Type.TUNNEL_FLOW) { |
| 108 | if (tunnelId == null) { |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 109 | error("tunnel ID must be specified for TUNNEL_FLOW policy"); |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | tpb.setTunnelId(tunnelId); |
| 113 | } |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 114 | 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 | |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 133 | } |
| 134 | } |