blob: ddab52e5be87e2cfa58d866500d5e43d148a520e [file] [log] [blame]
pierventre30368ab2021-02-24 23:23:22 +01001/*
2 * Copyright 2015-present Open Networking Foundation
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.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
pierventre4f68ffa2021-03-09 22:52:14 +010020import org.apache.karaf.shell.api.action.Option;
pierventre30368ab2021-02-24 23:23:22 +010021import org.apache.karaf.shell.api.action.lifecycle.Service;
22
23import org.onlab.packet.IPv4;
24import org.onlab.packet.IpPrefix;
25import org.onlab.packet.MacAddress;
26import org.onlab.packet.TpPort;
27import org.onlab.packet.VlanId;
28import org.onosproject.cli.AbstractShellCommand;
29import org.onosproject.cli.net.IpProtocol;
30import org.onosproject.net.flow.DefaultTrafficSelector;
31import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.segmentrouting.policy.api.PolicyId;
33import org.onosproject.segmentrouting.policy.api.PolicyService;
34import org.onosproject.segmentrouting.policy.api.TrafficMatch;
35import org.onosproject.segmentrouting.policy.api.TrafficMatchId;
Wailok Shum37dd29a2021-04-27 18:13:55 +080036import org.onosproject.segmentrouting.policy.api.TrafficMatchPriority;
pierventre30368ab2021-02-24 23:23:22 +010037
38/**
39 * Command to add a traffic match.
40 */
41@Service
pierventre4f68ffa2021-03-09 22:52:14 +010042@Command(scope = "onos", name = "sr-tm-add",
pierventre30368ab2021-02-24 23:23:22 +010043 description = "Create a new traffic match")
44public class TrafficMatchAddCommand extends AbstractShellCommand {
45
46 @Argument(index = 0, name = "policyId",
47 description = "policy id",
48 required = true, multiValued = false)
49 String policyId;
50
Wailok Shum37dd29a2021-04-27 18:13:55 +080051 @Argument(index = 1, name = "priority",
52 description = "priority",
53 required = true, multiValued = false)
54 int priority;
55
pierventre4f68ffa2021-03-09 22:52:14 +010056 @Option(name = "-sip", aliases = "--srcIp",
pierventre30368ab2021-02-24 23:23:22 +010057 description = "src IP",
pierventre4f68ffa2021-03-09 22:52:14 +010058 valueToShowInHelp = "10.0.0.1",
59 multiValued = false)
pierventre30368ab2021-02-24 23:23:22 +010060 String srcIp;
61
pierventre4f68ffa2021-03-09 22:52:14 +010062 @Option(name = "-sp", aliases = "--srcPort",
pierventre30368ab2021-02-24 23:23:22 +010063 description = "src port",
pierventre4f68ffa2021-03-09 22:52:14 +010064 valueToShowInHelp = "1001",
65 multiValued = false)
pierventre30368ab2021-02-24 23:23:22 +010066 short srcPort;
67
pierventre4f68ffa2021-03-09 22:52:14 +010068 @Option(name = "-dip", aliases = "--dstIp",
pierventre30368ab2021-02-24 23:23:22 +010069 description = "dst IP",
pierventre4f68ffa2021-03-09 22:52:14 +010070 valueToShowInHelp = "10.0.0.2",
71 multiValued = false)
pierventre30368ab2021-02-24 23:23:22 +010072 String dstIp;
73
pierventre4f68ffa2021-03-09 22:52:14 +010074 @Option(name = "-dp", aliases = "--dstPort",
pierventre30368ab2021-02-24 23:23:22 +010075 description = "dst port",
pierventre4f68ffa2021-03-09 22:52:14 +010076 valueToShowInHelp = "1002",
77 multiValued = false)
pierventre30368ab2021-02-24 23:23:22 +010078 short dstPort;
79
pierventre4f68ffa2021-03-09 22:52:14 +010080 @Option(name = "-p", aliases = "--proto",
pierventre30368ab2021-02-24 23:23:22 +010081 description = "IP protocol",
pierventre4f68ffa2021-03-09 22:52:14 +010082 valueToShowInHelp = "0x11",
83 multiValued = false)
pierventre30368ab2021-02-24 23:23:22 +010084 String proto;
85
pierventre4f68ffa2021-03-09 22:52:14 +010086 // TODO Consider to filter out the following fields for red policies
87 @Option(name = "-smac", aliases = "--srcMac",
pierventre30368ab2021-02-24 23:23:22 +010088 description = "src MAC",
pierventre4f68ffa2021-03-09 22:52:14 +010089 valueToShowInHelp = "00:00:00:00:00:01",
90 multiValued = false)
pierventre30368ab2021-02-24 23:23:22 +010091 String srcMac;
92
pierventre4f68ffa2021-03-09 22:52:14 +010093 @Option(name = "-dmac", aliases = "--dstMac",
pierventre30368ab2021-02-24 23:23:22 +010094 description = "dst MAC",
pierventre4f68ffa2021-03-09 22:52:14 +010095 valueToShowInHelp = "00:00:00:00:00:02",
96 multiValued = false)
pierventre30368ab2021-02-24 23:23:22 +010097 String dstMac;
98
pierventre4f68ffa2021-03-09 22:52:14 +010099 @Option(name = "-vid", aliases = "--VlanId",
100 description = "vlan ID",
101 valueToShowInHelp = "10",
102 multiValued = false)
pierventre30368ab2021-02-24 23:23:22 +0100103 short vlanId = -1;
104
105 @Override
106 protected void doExecute() {
107 TrafficSelector trafficSelector = parseArguments();
108 if (trafficSelector.equals(DefaultTrafficSelector.emptySelector())) {
109 print("Empty traffic selector is not allowed");
110 return;
111 }
Wailok Shum37dd29a2021-04-27 18:13:55 +0800112 TrafficMatchPriority trafficMatchPriority;
113 try {
114 trafficMatchPriority = new TrafficMatchPriority(priority);
115 } catch (IllegalArgumentException ex) {
116 print(ex.getMessage());
117 return;
118 }
pierventre30368ab2021-02-24 23:23:22 +0100119
120 PolicyService policyService = AbstractShellCommand.get(PolicyService.class);
121 TrafficMatchId trafficMatchId = policyService.addOrUpdateTrafficMatch(
Wailok Shum37dd29a2021-04-27 18:13:55 +0800122 new TrafficMatch(trafficSelector, PolicyId.of(policyId), trafficMatchPriority));
pierventre30368ab2021-02-24 23:23:22 +0100123 print("Traffic match %s has been submitted", trafficMatchId);
124 }
125
126 private TrafficSelector parseArguments() {
127 TrafficSelector.Builder trafficSelectorBuilder = DefaultTrafficSelector.builder();
128 if (srcIp != null) {
129 trafficSelectorBuilder.matchIPSrc(IpPrefix.valueOf(srcIp));
130 }
131 if (dstIp != null) {
132 trafficSelectorBuilder.matchIPDst(IpPrefix.valueOf(dstIp));
133 }
134 byte ipProtocol = 0;
135 if (proto != null) {
136 ipProtocol = (byte) (0xFF & IpProtocol.parseFromString(proto));
137 trafficSelectorBuilder.matchIPProtocol(ipProtocol);
138 }
139 if (srcPort != 0) {
140 if (ipProtocol == IPv4.PROTOCOL_TCP) {
141 trafficSelectorBuilder.matchTcpSrc(TpPort.tpPort(srcPort));
142 } else if (ipProtocol == IPv4.PROTOCOL_UDP) {
143 trafficSelectorBuilder.matchUdpSrc(TpPort.tpPort(srcPort));
144 }
145 }
146 if (dstPort != 0) {
147 if (ipProtocol == IPv4.PROTOCOL_TCP) {
148 trafficSelectorBuilder.matchTcpDst(TpPort.tpPort(dstPort));
149 } else if (ipProtocol == IPv4.PROTOCOL_UDP) {
150 trafficSelectorBuilder.matchUdpDst(TpPort.tpPort(dstPort));
151 }
152 }
153 if (srcMac != null) {
154 trafficSelectorBuilder.matchEthSrc(MacAddress.valueOf(srcMac));
155 }
156 if (dstMac != null) {
157 trafficSelectorBuilder.matchEthDst(MacAddress.valueOf(dstMac));
158 }
159 if (vlanId != -1) {
160 trafficSelectorBuilder.matchVlanId(VlanId.vlanId(vlanId));
161 }
162 return trafficSelectorBuilder.build();
163 }
164}