blob: d8b7a0ae5a7c76826540281d14db9380a279f14a [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;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
21
22import org.onlab.packet.IPv4;
23import org.onlab.packet.IpPrefix;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.TpPort;
26import org.onlab.packet.VlanId;
27import org.onosproject.cli.AbstractShellCommand;
28import org.onosproject.cli.net.IpProtocol;
29import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.segmentrouting.policy.api.PolicyId;
32import org.onosproject.segmentrouting.policy.api.PolicyService;
33import org.onosproject.segmentrouting.policy.api.TrafficMatch;
34import org.onosproject.segmentrouting.policy.api.TrafficMatchId;
35
36/**
37 * Command to add a traffic match.
38 */
39@Service
40@Command(scope = "onos", name = "sr-tmatch-add",
41 description = "Create a new traffic match")
42public class TrafficMatchAddCommand extends AbstractShellCommand {
43
44 @Argument(index = 0, name = "policyId",
45 description = "policy id",
46 required = true, multiValued = false)
47 String policyId;
48
49 @Argument(index = 1, name = "srcIp",
50 description = "src IP",
51 required = false, multiValued = false)
52 String srcIp;
53
54 @Argument(index = 2, name = "srcPort",
55 description = "src port",
56 required = false, multiValued = false)
57 short srcPort;
58
59 @Argument(index = 3, name = "dstIp",
60 description = "dst IP",
61 required = false, multiValued = false)
62 String dstIp;
63
64 @Argument(index = 4, name = "dstPort",
65 description = "dst port",
66 required = false, multiValued = false)
67 short dstPort;
68
69 @Argument(index = 5, name = "proto",
70 description = "IP protocol",
71 required = false, multiValued = false)
72 String proto;
73
74 @Argument(index = 6, name = "srcMac",
75 description = "src MAC",
76 required = false, multiValued = false)
77 String srcMac;
78
79 @Argument(index = 7, name = "dstMac",
80 description = "dst MAC",
81 required = false, multiValued = false)
82 String dstMac;
83
84 @Argument(index = 8, name = "vlanId",
85 description = "VLAN id",
86 required = false, multiValued = false)
87 short vlanId = -1;
88
89 @Override
90 protected void doExecute() {
91 TrafficSelector trafficSelector = parseArguments();
92 if (trafficSelector.equals(DefaultTrafficSelector.emptySelector())) {
93 print("Empty traffic selector is not allowed");
94 return;
95 }
96
97 PolicyService policyService = AbstractShellCommand.get(PolicyService.class);
98 TrafficMatchId trafficMatchId = policyService.addOrUpdateTrafficMatch(
99 new TrafficMatch(trafficSelector, PolicyId.of(policyId)));
100 print("Traffic match %s has been submitted", trafficMatchId);
101 }
102
103 private TrafficSelector parseArguments() {
104 TrafficSelector.Builder trafficSelectorBuilder = DefaultTrafficSelector.builder();
105 if (srcIp != null) {
106 trafficSelectorBuilder.matchIPSrc(IpPrefix.valueOf(srcIp));
107 }
108 if (dstIp != null) {
109 trafficSelectorBuilder.matchIPDst(IpPrefix.valueOf(dstIp));
110 }
111 byte ipProtocol = 0;
112 if (proto != null) {
113 ipProtocol = (byte) (0xFF & IpProtocol.parseFromString(proto));
114 trafficSelectorBuilder.matchIPProtocol(ipProtocol);
115 }
116 if (srcPort != 0) {
117 if (ipProtocol == IPv4.PROTOCOL_TCP) {
118 trafficSelectorBuilder.matchTcpSrc(TpPort.tpPort(srcPort));
119 } else if (ipProtocol == IPv4.PROTOCOL_UDP) {
120 trafficSelectorBuilder.matchUdpSrc(TpPort.tpPort(srcPort));
121 }
122 }
123 if (dstPort != 0) {
124 if (ipProtocol == IPv4.PROTOCOL_TCP) {
125 trafficSelectorBuilder.matchTcpDst(TpPort.tpPort(dstPort));
126 } else if (ipProtocol == IPv4.PROTOCOL_UDP) {
127 trafficSelectorBuilder.matchUdpDst(TpPort.tpPort(dstPort));
128 }
129 }
130 if (srcMac != null) {
131 trafficSelectorBuilder.matchEthSrc(MacAddress.valueOf(srcMac));
132 }
133 if (dstMac != null) {
134 trafficSelectorBuilder.matchEthDst(MacAddress.valueOf(dstMac));
135 }
136 if (vlanId != -1) {
137 trafficSelectorBuilder.matchVlanId(VlanId.vlanId(vlanId));
138 }
139 return trafficSelectorBuilder.build();
140 }
141}