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 | |
| 34 | import java.util.Optional; |
| 35 | |
| 36 | /** |
| 37 | * Utility class for holding FTTB constants and utility methods. |
| 38 | */ |
| 39 | public final class FttbUtils { |
| 40 | |
| 41 | public static final String FTTB_FLOW_DIRECTION = "fttbFlowDirection"; |
| 42 | public static final String FTTB_FLOW_UPSTREAM = "fttbFlowUpstream"; |
| 43 | public static final String FTTB_FLOW_DOWNSTREAM = "fttbFlowDownstream"; |
| 44 | |
| 45 | public static final String FTTB_SERVICE_NAME = "fttbServiceName"; |
| 46 | public static final String FTTB_SERVICE_DPU_MGMT_TRAFFIC = "DPU_MGMT_TRAFFIC"; |
| 47 | public static final String FTTB_SERVICE_DPU_ANCP_TRAFFIC = "DPU_ANCP_TRAFFIC"; |
| 48 | public static final String FTTB_SERVICE_SUBSCRIBER_TRAFFIC = "FTTB_SUBSCRIBER_TRAFFIC"; |
| 49 | |
| 50 | private static final Logger log = LoggerFactory.getLogger(FttbUtils.class); |
| 51 | |
| 52 | private FttbUtils() { |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Checks if the FlowObjective qualifies as FTTB rule. |
| 57 | * |
| 58 | * @param fwd ForwardingObjective rule. |
| 59 | * @return true if the fwd is FTTB rule. |
| 60 | */ |
| 61 | public static boolean isFttbRule(ForwardingObjective fwd) { |
| 62 | String serviceName = fwd.annotations().value(FTTB_SERVICE_NAME); |
| 63 | |
| 64 | if (serviceName == null) { |
| 65 | if (log.isTraceEnabled()) { |
| 66 | log.trace("Service name not found for : {} ", fwd); |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | return isFttbService(serviceName); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Checks if the UniTagInformation is a FTTB subscriber. |
| 76 | * |
| 77 | * @param uti The UniTagInformation to check for. |
| 78 | * @return true if the uti is FTTB subscriber. |
| 79 | */ |
| 80 | public static boolean isFttbService(UniTagInformation uti) { |
| 81 | String serviceName = uti.getServiceName(); |
| 82 | |
| 83 | if (serviceName == null) { |
| 84 | log.warn("Could not find service name for {}", uti); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | return isFttbService(serviceName); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Checks if the UniTagInformation is FTTB DPU or ANCP service. |
| 93 | * |
| 94 | * @param uti The UniTagInformation to check for. |
| 95 | * @return true if the uti is FTTB DPU or ANCP service. |
| 96 | */ |
| 97 | public static boolean isFttbDpuOrAncpService(UniTagInformation uti) { |
| 98 | String serviceName = uti.getServiceName(); |
| 99 | |
| 100 | if (serviceName == null) { |
| 101 | log.trace("Could not find service name for {}", uti); |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | switch (serviceName) { |
| 106 | case FTTB_SERVICE_DPU_MGMT_TRAFFIC: |
| 107 | case FTTB_SERVICE_DPU_ANCP_TRAFFIC: |
| 108 | return true; |
| 109 | default: |
| 110 | return false; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Adds match conditions to FilteringObjective.Builder for FTTB. |
| 116 | * @param dhcpBuilder FilteringObjective.Builder |
| 117 | * @param uti UniTagInformation |
| 118 | */ |
| 119 | public static void addUpstreamDhcpCondition(FilteringObjective.Builder dhcpBuilder, |
| 120 | UniTagInformation uti) { |
| 121 | dhcpBuilder.addCondition(Criteria.matchVlanId(uti.getPonCTag())); |
| 122 | if (uti.getUsPonCTagPriority() != -1) { |
| 123 | dhcpBuilder.addCondition(Criteria.matchVlanPcp((byte) uti.getUsPonCTagPriority())); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Adds Instructions to TrafficTreatment.Builder for FTTB. |
| 129 | * @param treatmentBuilder TrafficTreatment.Builder |
| 130 | * @param uti UniTagInformation |
| 131 | */ |
| 132 | public static void addUpstreamDhcpTreatment(TrafficTreatment.Builder treatmentBuilder, UniTagInformation uti) { |
| 133 | treatmentBuilder.setVlanId(uti.getPonSTag()); |
| 134 | |
| 135 | if (uti.getUsPonSTagPriority() != -1) { |
| 136 | treatmentBuilder.setVlanPcp((byte) uti.getUsPonSTagPriority()); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | private static boolean isFttbService(String serviceName) { |
| 141 | if (serviceName == null) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | switch (serviceName) { |
| 146 | case FTTB_SERVICE_DPU_MGMT_TRAFFIC: |
| 147 | case FTTB_SERVICE_DPU_ANCP_TRAFFIC: |
| 148 | case FTTB_SERVICE_SUBSCRIBER_TRAFFIC: |
| 149 | return true; |
| 150 | default: |
| 151 | if (log.isTraceEnabled()) { |
| 152 | log.trace("Service name {} is not one for FTTB", serviceName); |
| 153 | } |
| 154 | return false; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns mac address from the Dhcp Enabled UniTagInformation for a FTTB service. |
| 160 | * |
| 161 | * @param hostService Service for interacting with the inventory of end-station hosts |
| 162 | * @param si Information about a subscriber |
| 163 | * @param deviceId Device id for mac lookup. |
| 164 | * @param port Uni port on the device for mac lookup. |
| 165 | * @return Mac address of the subscriber. |
| 166 | */ |
| 167 | public static MacAddress getMacAddressFromDhcpEnabledUti(HostService hostService, |
| 168 | SubscriberAndDeviceInformation si, |
| 169 | DeviceId deviceId, |
| 170 | Port port) { |
| 171 | for (UniTagInformation uniTagInfo : si.uniTagList()) { |
| 172 | boolean isMacLearningEnabled = uniTagInfo.getEnableMacLearning(); |
| 173 | if (isMacLearningEnabled) { |
| 174 | Optional<Host> optHost = hostService.getConnectedHosts(new ConnectPoint(deviceId, port.number())) |
| 175 | .stream().filter(host -> host.vlan().equals(uniTagInfo.getPonSTag())).findFirst(); |
| 176 | if (optHost.isPresent()) { |
| 177 | if (optHost.get().mac() != null) { |
| 178 | return optHost.get().mac(); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | return null; |
| 184 | } |
| 185 | } |