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