Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.opencord.olt.impl.fttb; |
| 18 | |
| 19 | import org.onlab.packet.MacAddress; |
| 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.Host; |
| 23 | import org.onosproject.net.Port; |
| 24 | import org.onosproject.net.flow.TrafficTreatment; |
| 25 | import org.onosproject.net.flow.criteria.Criteria; |
| 26 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 27 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 28 | import org.onosproject.net.host.HostService; |
| 29 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
| 30 | import org.opencord.sadis.UniTagInformation; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
amit.ghosh | 74a4bb2 | 2022-06-14 11:34:52 +0200 | [diff] [blame] | 34 | import java.util.Map; |
Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 35 | import java.util.Optional; |
| 36 | |
| 37 | /** |
| 38 | * Utility class for holding FTTB constants and utility methods. |
| 39 | */ |
| 40 | public final class FttbUtils { |
| 41 | |
| 42 | public static final String FTTB_FLOW_DIRECTION = "fttbFlowDirection"; |
| 43 | public static final String FTTB_FLOW_UPSTREAM = "fttbFlowUpstream"; |
| 44 | public static final String FTTB_FLOW_DOWNSTREAM = "fttbFlowDownstream"; |
| 45 | |
| 46 | public static final String FTTB_SERVICE_NAME = "fttbServiceName"; |
| 47 | public static final String FTTB_SERVICE_DPU_MGMT_TRAFFIC = "DPU_MGMT_TRAFFIC"; |
| 48 | public static final String FTTB_SERVICE_DPU_ANCP_TRAFFIC = "DPU_ANCP_TRAFFIC"; |
| 49 | public static final String FTTB_SERVICE_SUBSCRIBER_TRAFFIC = "FTTB_SUBSCRIBER_TRAFFIC"; |
| 50 | |
| 51 | private static final Logger log = LoggerFactory.getLogger(FttbUtils.class); |
| 52 | |
| 53 | private FttbUtils() { |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Checks if the FlowObjective qualifies as FTTB rule. |
| 58 | * |
| 59 | * @param fwd ForwardingObjective rule. |
| 60 | * @return true if the fwd is FTTB rule. |
| 61 | */ |
| 62 | public static boolean isFttbRule(ForwardingObjective fwd) { |
| 63 | String serviceName = fwd.annotations().value(FTTB_SERVICE_NAME); |
| 64 | |
| 65 | if (serviceName == null) { |
| 66 | if (log.isTraceEnabled()) { |
| 67 | log.trace("Service name not found for : {} ", fwd); |
| 68 | } |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | return isFttbService(serviceName); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Checks if the UniTagInformation is a FTTB subscriber. |
| 77 | * |
| 78 | * @param uti The UniTagInformation to check for. |
| 79 | * @return true if the uti is FTTB subscriber. |
| 80 | */ |
| 81 | public static boolean isFttbService(UniTagInformation uti) { |
| 82 | String serviceName = uti.getServiceName(); |
| 83 | |
| 84 | if (serviceName == null) { |
| 85 | log.warn("Could not find service name for {}", uti); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | return isFttbService(serviceName); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Checks if the UniTagInformation is FTTB DPU or ANCP service. |
| 94 | * |
| 95 | * @param uti The UniTagInformation to check for. |
| 96 | * @return true if the uti is FTTB DPU or ANCP service. |
| 97 | */ |
| 98 | public static boolean isFttbDpuOrAncpService(UniTagInformation uti) { |
| 99 | String serviceName = uti.getServiceName(); |
| 100 | |
| 101 | if (serviceName == null) { |
| 102 | log.trace("Could not find service name for {}", uti); |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | switch (serviceName) { |
| 107 | case FTTB_SERVICE_DPU_MGMT_TRAFFIC: |
| 108 | case FTTB_SERVICE_DPU_ANCP_TRAFFIC: |
| 109 | return true; |
| 110 | default: |
| 111 | return false; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Adds match conditions to FilteringObjective.Builder for FTTB. |
| 117 | * @param dhcpBuilder FilteringObjective.Builder |
| 118 | * @param uti UniTagInformation |
| 119 | */ |
| 120 | public static void addUpstreamDhcpCondition(FilteringObjective.Builder dhcpBuilder, |
| 121 | UniTagInformation uti) { |
| 122 | dhcpBuilder.addCondition(Criteria.matchVlanId(uti.getPonCTag())); |
| 123 | if (uti.getUsPonCTagPriority() != -1) { |
| 124 | dhcpBuilder.addCondition(Criteria.matchVlanPcp((byte) uti.getUsPonCTagPriority())); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Adds Instructions to TrafficTreatment.Builder for FTTB. |
| 130 | * @param treatmentBuilder TrafficTreatment.Builder |
| 131 | * @param uti UniTagInformation |
| 132 | */ |
| 133 | public static void addUpstreamDhcpTreatment(TrafficTreatment.Builder treatmentBuilder, UniTagInformation uti) { |
| 134 | treatmentBuilder.setVlanId(uti.getPonSTag()); |
| 135 | |
| 136 | if (uti.getUsPonSTagPriority() != -1) { |
| 137 | treatmentBuilder.setVlanPcp((byte) uti.getUsPonSTagPriority()); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | private static boolean isFttbService(String serviceName) { |
| 142 | if (serviceName == null) { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | switch (serviceName) { |
| 147 | case FTTB_SERVICE_DPU_MGMT_TRAFFIC: |
| 148 | case FTTB_SERVICE_DPU_ANCP_TRAFFIC: |
| 149 | case FTTB_SERVICE_SUBSCRIBER_TRAFFIC: |
| 150 | return true; |
| 151 | default: |
| 152 | if (log.isTraceEnabled()) { |
| 153 | log.trace("Service name {} is not one for FTTB", serviceName); |
| 154 | } |
| 155 | return false; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns mac address from the Dhcp Enabled UniTagInformation for a FTTB service. |
| 161 | * |
amit.ghosh | 74a4bb2 | 2022-06-14 11:34:52 +0200 | [diff] [blame] | 162 | * @param hostService Service for interacting with the inventory of end-station hosts |
| 163 | * @param si Information about a subscriber |
| 164 | * @param deviceId Device id for mac lookup. |
| 165 | * @param port Uni port on the device for mac lookup. |
| 166 | * @param localAddresses Map of the addresses that this app has locally |
Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 167 | * @return Mac address of the subscriber. |
| 168 | */ |
| 169 | public static MacAddress getMacAddressFromDhcpEnabledUti(HostService hostService, |
| 170 | SubscriberAndDeviceInformation si, |
| 171 | DeviceId deviceId, |
amit.ghosh | 74a4bb2 | 2022-06-14 11:34:52 +0200 | [diff] [blame] | 172 | Port port, |
| 173 | Map<ConnectPoint, MacAddress> localAddresses) { |
Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 174 | for (UniTagInformation uniTagInfo : si.uniTagList()) { |
| 175 | boolean isMacLearningEnabled = uniTagInfo.getEnableMacLearning(); |
| 176 | if (isMacLearningEnabled) { |
amit.ghosh | 74a4bb2 | 2022-06-14 11:34:52 +0200 | [diff] [blame] | 177 | ConnectPoint cp = new ConnectPoint(deviceId, port.number()); |
| 178 | Optional<Host> optHost = hostService.getConnectedHosts(cp) |
Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 179 | .stream().filter(host -> host.vlan().equals(uniTagInfo.getPonSTag())).findFirst(); |
| 180 | if (optHost.isPresent()) { |
amit.ghosh | 74a4bb2 | 2022-06-14 11:34:52 +0200 | [diff] [blame] | 181 | MacAddress learntMac = optHost.get().mac(); |
| 182 | if (learntMac != null) { |
| 183 | localAddresses.put(cp, learntMac); |
| 184 | log.info("Stored mac {} locally for connectPoint {}", learntMac, cp); |
| 185 | return learntMac; |
| 186 | } |
| 187 | } else { |
| 188 | MacAddress localMac = localAddresses.get(new ConnectPoint(deviceId, port.number())); |
| 189 | if (localMac != null) { |
| 190 | log.debug("Returning local mac {} for connectPoint {}", localMac, cp); |
| 191 | return localMac; |
Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 192 | } |
| 193 | } |
Andrea Campanella | f734a2a | 2022-05-16 11:56:55 +0200 | [diff] [blame] | 194 | } else if (uniTagInfo.getConfiguredMacAddress() != null && |
| 195 | !uniTagInfo.getConfiguredMacAddress().isEmpty()) { |
| 196 | log.info("Using configured mac address for FTTB {}", uniTagInfo.getConfiguredMacAddress()); |
| 197 | return MacAddress.valueOf(uniTagInfo.getConfiguredMacAddress()); |
Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | return null; |
| 201 | } |
| 202 | } |