sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.segmentrouting.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.segmentrouting.Policy; |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 22 | import org.onosproject.segmentrouting.PolicyHandler; |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 23 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 24 | import org.onosproject.segmentrouting.TunnelPolicy; |
| 25 | |
| 26 | /** |
| 27 | * Command to add a new policy. |
| 28 | */ |
| 29 | @Command(scope = "onos", name = "srpolicy-add", |
| 30 | description = "Create a new policy") |
| 31 | public class PolicyAddCommand extends AbstractShellCommand { |
| 32 | |
| 33 | // TODO: Need to support skipping some parameters |
| 34 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 35 | @Argument(index = 0, name = "ID", |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 36 | 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 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 45 | @Argument(index = 2, name = "src_IP", |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 46 | description = "src IP", |
| 47 | required = false, multiValued = false) |
| 48 | String srcIp; |
| 49 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 50 | @Argument(index = 3, name = "src_port", |
sangho | 8d953f9 | 2015-06-22 15:10:19 -0700 | [diff] [blame] | 51 | description = "src port", |
| 52 | required = false, multiValued = false) |
| 53 | short srcPort; |
| 54 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 55 | @Argument(index = 4, name = "dst_IP", |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 56 | description = "dst IP", |
| 57 | required = false, multiValued = false) |
| 58 | String dstIp; |
| 59 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 60 | @Argument(index = 5, name = "dst_port", |
sangho | 8d953f9 | 2015-06-22 15:10:19 -0700 | [diff] [blame] | 61 | description = "dst port", |
| 62 | required = false, multiValued = false) |
| 63 | short dstPort; |
| 64 | |
| 65 | @Argument(index = 6, name = "proto", |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 66 | description = "IP protocol", |
sangho | 8d953f9 | 2015-06-22 15:10:19 -0700 | [diff] [blame] | 67 | required = false, multiValued = false) |
| 68 | String proto; |
| 69 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 70 | @Argument(index = 7, name = "policy_type", |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 71 | description = "policy type", |
| 72 | required = true, multiValued = false) |
| 73 | String policyType; |
| 74 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 75 | @Argument(index = 8, name = "tunnel_ID", |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 76 | 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 | } |
sangho | 8d953f9 | 2015-06-22 15:10:19 -0700 | [diff] [blame] | 96 | 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 | } |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 105 | if (Policy.Type.valueOf(policyType) == Policy.Type.TUNNEL_FLOW) { |
| 106 | if (tunnelId == null) { |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 107 | error("tunnel ID must be specified for TUNNEL_FLOW policy"); |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | tpb.setTunnelId(tunnelId); |
| 111 | } |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 112 | 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 | |
sangho | 2eae4c6 | 2015-06-11 14:49:59 -0700 | [diff] [blame] | 131 | } |
| 132 | } |