blob: 9ce0181a4bf9183841a31483f94e739f076a896f [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.fttb;
18
19import org.onlab.packet.MacAddress;
20import org.onosproject.net.ConnectPoint;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.Host;
23import org.onosproject.net.Port;
24import org.onosproject.net.flow.TrafficTreatment;
25import org.onosproject.net.flow.criteria.Criteria;
26import org.onosproject.net.flowobjective.FilteringObjective;
27import org.onosproject.net.flowobjective.ForwardingObjective;
28import org.onosproject.net.host.HostService;
29import org.opencord.sadis.SubscriberAndDeviceInformation;
30import org.opencord.sadis.UniTagInformation;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
amit.ghosh74a4bb22022-06-14 11:34:52 +020034import java.util.Map;
Harsh Awasthic1e4bf52022-02-09 14:14:14 +053035import java.util.Optional;
36
37/**
38 * Utility class for holding FTTB constants and utility methods.
39 */
40public 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.ghosh74a4bb22022-06-14 11:34:52 +0200162 * @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 Awasthic1e4bf52022-02-09 14:14:14 +0530167 * @return Mac address of the subscriber.
168 */
169 public static MacAddress getMacAddressFromDhcpEnabledUti(HostService hostService,
170 SubscriberAndDeviceInformation si,
171 DeviceId deviceId,
amit.ghosh74a4bb22022-06-14 11:34:52 +0200172 Port port,
173 Map<ConnectPoint, MacAddress> localAddresses) {
Harsh Awasthic1e4bf52022-02-09 14:14:14 +0530174 for (UniTagInformation uniTagInfo : si.uniTagList()) {
175 boolean isMacLearningEnabled = uniTagInfo.getEnableMacLearning();
176 if (isMacLearningEnabled) {
amit.ghosh74a4bb22022-06-14 11:34:52 +0200177 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
178 Optional<Host> optHost = hostService.getConnectedHosts(cp)
Harsh Awasthic1e4bf52022-02-09 14:14:14 +0530179 .stream().filter(host -> host.vlan().equals(uniTagInfo.getPonSTag())).findFirst();
180 if (optHost.isPresent()) {
amit.ghosh74a4bb22022-06-14 11:34:52 +0200181 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 Awasthic1e4bf52022-02-09 14:14:14 +0530192 }
193 }
Andrea Campanellaf734a2a2022-05-16 11:56:55 +0200194 } 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 Awasthic1e4bf52022-02-09 14:14:14 +0530198 }
199 }
200 return null;
201 }
202}