blob: 31a7ab6506891a423f1c39ebeddc7e9fa81571c2 [file] [log] [blame]
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001/*
Joey Armstrong7f6d6d22023-01-09 17:09:50 -05002 * Copyright 2021-2023 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07003 *
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
Harsh Awasthic1e4bf52022-02-09 14:14:14 +053019import org.onlab.packet.VlanId;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070020import org.onosproject.net.AnnotationKeys;
21import org.onosproject.net.Port;
Gustavo Silva89e2f042022-08-01 09:58:04 -030022import org.opencord.olt.AccessDevicePort;
Gustavo Silva29fb20e2022-05-26 09:59:54 -030023import org.opencord.olt.FlowOperation;
Gustavo Silva89e2f042022-08-01 09:58:04 -030024import org.opencord.olt.OltFlowServiceInterface;
25import org.opencord.olt.ServiceKey;
Harsh Awasthic1e4bf52022-02-09 14:14:14 +053026import org.opencord.sadis.SubscriberAndDeviceInformation;
27import org.opencord.sadis.UniTagInformation;
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070028
Gustavo Silva89e2f042022-08-01 09:58:04 -030029import java.util.List;
30import java.util.Map;
31import java.util.stream.Collectors;
32
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070033/**
34 * Utility class for OLT app.
35 */
Gustavo Silva29fb20e2022-05-26 09:59:54 -030036public final class OltUtils {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070037
38 private OltUtils() {
39 }
40
41 /**
42 * Returns the port name if present in the annotations.
43 * @param port the port
44 * @return the annotated port name
45 */
Gustavo Silva29fb20e2022-05-26 09:59:54 -030046 public static String getPortName(Port port) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070047 String name = port.annotations().value(AnnotationKeys.PORT_NAME);
48 return name == null ? "" : name;
49 }
50
51 /**
52 * Returns a port printed as a connect point and with the name appended.
53 * @param port the port
54 * @return the formatted string
55 */
Gustavo Silva29fb20e2022-05-26 09:59:54 -030056 public static String portWithName(Port port) {
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070057 return port.element().id().toString() + '/' +
58 port.number() + '[' +
59 getPortName(port) + ']';
60 }
61
Gustavo Silva29fb20e2022-05-26 09:59:54 -030062 public static String flowOpToString(FlowOperation op) {
63 return op == FlowOperation.ADD ? "Adding" : "Removing";
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070064 }
65
Gustavo Silva29fb20e2022-05-26 09:59:54 -030066 public static String completeFlowOpToString(FlowOperation op) {
67 return op == FlowOperation.ADD ? "Added" : "Removed";
Matteo Scandoloaa2adde2021-09-13 12:45:32 -070068 }
Harsh Awasthic1e4bf52022-02-09 14:14:14 +053069
70 /**
71 * Search and return the matching UniTagInfomation from the list of UniTagInfomation in the
72 * SubscriberAndDeviceInformation.
73 * For the match : cvlan, svlan and tpId are used.
74 *
75 * @param subInfo Subscriber information.
76 * @param innerVlan cTag
77 * @param outerVlan sTag
78 * @param tpId Techprofile Id
79 * @return UniTagInformation
80 */
Gustavo Silva29fb20e2022-05-26 09:59:54 -030081 public static UniTagInformation getUniTagInformation(SubscriberAndDeviceInformation subInfo, VlanId innerVlan,
Harsh Awasthic1e4bf52022-02-09 14:14:14 +053082 VlanId outerVlan, int tpId) {
83 UniTagInformation service = null;
84 for (UniTagInformation tagInfo : subInfo.uniTagList()) {
85 if (innerVlan.equals(tagInfo.getPonCTag()) && outerVlan.equals(tagInfo.getPonSTag())
86 && tpId == tagInfo.getTechnologyProfileId()) {
87 service = tagInfo;
88 break;
89 }
90 }
91 return service;
92 }
Gustavo Silva89e2f042022-08-01 09:58:04 -030093
94 public static SubscriberAndDeviceInformation getProgrammedSubscriber(
95 OltFlowServiceInterface service, AccessDevicePort accessDevicePort) {
96 List<Map.Entry<ServiceKey, UniTagInformation>> entries =
97 service.getProgrammedSubscribers().entrySet().stream()
98 .filter(entry -> entry.getKey().getPort().equals(accessDevicePort))
99 .collect(Collectors.toList());
100 if (!entries.isEmpty()) {
101 List<UniTagInformation> programmedList = entries.stream()
102 .map(entry -> entry.getKey().getService())
103 .collect(Collectors.toList());
104 SubscriberAndDeviceInformation si = new SubscriberAndDeviceInformation();
105 si.setUniTagList(programmedList);
106 return si;
107 }
108 return null;
109 }
Matteo Scandoloaa2adde2021-09-13 12:45:32 -0700110}