blob: 7099b1bcd85038ec9edbdbd7371ded95dc23f85e [file] [log] [blame]
Harsh Awasthic1e4bf52022-02-09 14:14:14 +05301/*
2 * Copyright 2021-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 */
16
17package org.opencord.olt.impl;
18
19import org.onlab.packet.EthType;
20import org.onlab.packet.IPv4;
21import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
23import org.onosproject.net.PortNumber;
24import org.onosproject.net.flow.FlowRule;
25import org.onosproject.net.flow.FlowRuleEvent;
26import org.onosproject.net.flow.criteria.Criterion;
27import org.onosproject.net.flow.criteria.EthTypeCriterion;
28import org.onosproject.net.flow.criteria.IPProtocolCriterion;
29import org.onosproject.net.flow.criteria.PortCriterion;
30import org.onosproject.net.flow.criteria.UdpPortCriterion;
31import org.onosproject.net.flow.instructions.L2ModificationInstruction;
32import org.onosproject.net.meter.MeterId;
33import org.opencord.sadis.UniTagInformation;
34
35import java.util.concurrent.atomic.AtomicBoolean;
36
37import static org.onosproject.net.flow.instructions.Instruction.Type.L2MODIFICATION;
38import static org.opencord.olt.impl.OltFlowService.EAPOL_DEFAULT_VLAN;
39import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_TP_ID_DEFAULT;
40
41/**
42 * Utility class for Flow service utility methods.
43 */
44public final class OltFlowServiceUtils {
45
46 public static final int NONE_TP_ID = -1;
47
48 private OltFlowServiceUtils() {
49 }
50
51 /**
52 * Constructs and returns the metadata from cVlan, techProfileId and upstreamOltMeterId.
53 *
54 * @param cVlan the customer vlan
55 * @param techProfileId the technology profile
56 * @param upstreamOltMeterId the upstream olt meter id
57 * @return Metadata
58 */
59 public static Long createTechProfValueForWriteMetadata(VlanId cVlan, int techProfileId,
60 MeterId upstreamOltMeterId) {
61 Long writeMetadata;
62
63 if (cVlan == null || VlanId.NONE.equals(cVlan)) {
64 writeMetadata = (long) techProfileId << 32;
65 } else {
66 writeMetadata = ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
67 }
68 if (upstreamOltMeterId == null) {
69 return writeMetadata;
70 } else {
71 return writeMetadata | upstreamOltMeterId.id();
72 }
73 }
74
75 /**
76 * Converts FlowRuleEvent.Type to OltFlowService.OltFlowsStatus.
77 *
78 * @param type FlowRuleEvent type
79 * @return OltFlowService.OltFlowsStatus
80 */
81 public static OltFlowService.OltFlowsStatus flowRuleStatusToOltFlowStatus(FlowRuleEvent.Type type) {
82 switch (type) {
83 case RULE_ADD_REQUESTED:
84 return OltFlowService.OltFlowsStatus.PENDING_ADD;
85 case RULE_ADDED:
86 return OltFlowService.OltFlowsStatus.ADDED;
87 case RULE_REMOVE_REQUESTED:
88 return OltFlowService.OltFlowsStatus.PENDING_REMOVE;
89 case RULE_REMOVED:
90 return OltFlowService.OltFlowsStatus.REMOVED;
91 default:
92 return OltFlowService.OltFlowsStatus.NONE;
93 }
94 }
95
96 /**
97 * Checks if the configured Mac address is valid for a UniTagInformation.
98 *
99 * @param tagInformation UniTagInformation
100 * @return true if the mac address is valid
101 */
102 public static boolean isMacAddressValid(UniTagInformation tagInformation) {
103 return tagInformation.getConfiguredMacAddress() != null &&
104 !tagInformation.getConfiguredMacAddress().trim().equals("") &&
105 !MacAddress.NONE.equals(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()));
106 }
107
108 /**
109 * Returns true if the flow is a DHCP flow.
110 * Matches both upstream and downstream flows.
111 *
112 * @param flowRule The FlowRule to evaluate
113 * @return boolean
114 */
115 public static boolean isDhcpFlow(FlowRule flowRule) {
116 IPProtocolCriterion ipCriterion = (IPProtocolCriterion) flowRule.selector()
117 .getCriterion(Criterion.Type.IP_PROTO);
118 if (ipCriterion == null) {
119 return false;
120 }
121
122 UdpPortCriterion src = (UdpPortCriterion) flowRule.selector().getCriterion(Criterion.Type.UDP_SRC);
123
124 if (src == null) {
125 return false;
126 }
127 return ipCriterion.protocol() == IPv4.PROTOCOL_UDP &&
128 (src.udpPort().toInt() == 68 || src.udpPort().toInt() == 67);
129 }
130
131 /**
132 * Returns true if the flow is a Pppoe flow.
133 *
134 * @param flowRule The FlowRule to evaluate
135 * @return boolean
136 */
137 public static boolean isPppoeFlow(FlowRule flowRule) {
138 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) flowRule.selector()
139 .getCriterion(Criterion.Type.ETH_TYPE);
140
141 if (ethTypeCriterion == null) {
142 return false;
143 }
144 return EthType.EtherType.PPPoED.ethType().equals(ethTypeCriterion.ethType());
145 }
146
147 /**
148 * Return true if the flow is a Data flow.
149 * @param flowRule The FlowRule to evaluate
150 * @return boolean
151 */
152 public static boolean isDataFlow(FlowRule flowRule) {
153 // we consider subscriber flows the one that matches on VLAN_VID
154 // method is valid only because it's the last check after EAPOL and DHCP.
155 // this matches mcast flows as well, if we want to avoid that we can
156 // filter out the elements that have groups in the treatment or
157 // mcastIp in the selector
158 // IPV4_DST:224.0.0.22/32
159 // treatment=[immediate=[GROUP:0x1]]
160
161 return flowRule.selector().getCriterion(Criterion.Type.VLAN_VID) != null;
162 }
163
164 /**
165 * Extracts and returns inPort selector from the FlowRule.
166 *
167 * @param flowRule The FlowRule to evaluate
168 * @return PortNumber
169 */
170 public static PortNumber getPortNumberFromFlowRule(FlowRule flowRule) {
171 PortCriterion inPort = (PortCriterion) flowRule.selector().getCriterion(Criterion.Type.IN_PORT);
172 if (inPort != null) {
173 return inPort.port();
174 }
175 return null;
176 }
177
178 /**
179 * Constructs and returns the metadata from innerVlan, techProfileId and egressPort.
180 *
181 * @param innerVlan inner vlan tag
182 * @param techProfileId technology profile
183 * @param egressPort outport
184 * @return Metadata
185 */
186 public static Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
187 if (techProfileId == NONE_TP_ID) {
188 techProfileId = DEFAULT_TP_ID_DEFAULT;
189 }
190
191 Long writeMetadata = (long) techProfileId << 32 | egressPort.toLong();
192
193 if (innerVlan != null && !VlanId.NONE.equals(innerVlan)) {
194 writeMetadata |= (long) (innerVlan.id()) << 48;
195 }
196
197 return writeMetadata;
198 }
199
200 /***
201 * Checks if the FlowRule is default eapol.
202 * @param flowRule FlowRule to check.
203 * @return true if FlowRule is default eapol.
204 */
205 public static boolean isDefaultEapolFlow(FlowRule flowRule) {
206 EthTypeCriterion c = (EthTypeCriterion) flowRule.selector().getCriterion(Criterion.Type.ETH_TYPE);
207 if (c == null) {
208 return false;
209 }
210 if (c.ethType().equals(EthType.EtherType.EAPOL.ethType())) {
211 AtomicBoolean isDefault = new AtomicBoolean(false);
212 flowRule.treatment().allInstructions().forEach(instruction -> {
213 if (instruction.type() == L2MODIFICATION) {
214 L2ModificationInstruction modificationInstruction = (L2ModificationInstruction) instruction;
215 if (modificationInstruction.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) {
216 L2ModificationInstruction.ModVlanIdInstruction vlanInstruction =
217 (L2ModificationInstruction.ModVlanIdInstruction) modificationInstruction;
218 if (vlanInstruction.vlanId().id().equals(EAPOL_DEFAULT_VLAN)) {
219 isDefault.set(true);
220 return;
221 }
222 }
223 }
224 });
225 return isDefault.get();
226 }
227 return false;
228 }
229
230 /***
231 * Checks if the FlowRule is Subscriber eapol.
232 * @param flowRule Flow Rule
233 * @return true if FlowRule is Subscriber eapol.
234 */
235 public static boolean isSubscriberEapolFlow(FlowRule flowRule) {
236 EthTypeCriterion c = (EthTypeCriterion) flowRule.selector().getCriterion(Criterion.Type.ETH_TYPE);
237 if (c == null) {
238 return false;
239 }
240 if (c.ethType().equals(EthType.EtherType.EAPOL.ethType())) {
241 AtomicBoolean isSubscriber = new AtomicBoolean(false);
242 flowRule.treatment().allInstructions().forEach(instruction -> {
243 if (instruction.type() == L2MODIFICATION) {
244 L2ModificationInstruction modificationInstruction = (L2ModificationInstruction) instruction;
245 if (modificationInstruction.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) {
246 L2ModificationInstruction.ModVlanIdInstruction vlanInstruction =
247 (L2ModificationInstruction.ModVlanIdInstruction) modificationInstruction;
248 if (!vlanInstruction.vlanId().id().equals(EAPOL_DEFAULT_VLAN)) {
249 isSubscriber.set(true);
250 return;
251 }
252 }
253 }
254 });
255 return isSubscriber.get();
256 }
257 return false;
258 }
259}