alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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.onosproject.cordconfig.access; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 20 | import org.apache.commons.lang3.tuple.Pair; |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onosproject.net.ConnectPoint; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 25 | import java.util.List; |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 26 | import java.util.Map; |
| 27 | import java.util.Optional; |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 28 | import java.util.stream.Collectors; |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * Information about an access agent. |
| 34 | */ |
| 35 | public class AccessAgentData { |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 36 | |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 37 | private static final String DEVICE_ID_MISSING = "Device ID cannot be null"; |
| 38 | private static final String OLT_INFO_MISSING = "OLT information cannot be null"; |
| 39 | private static final String AGENT_MAC_MISSING = "Agent mac cannot be null"; |
| 40 | private static final String VTN_MISSING = "VTN location cannot be null"; |
| 41 | |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 42 | private static final int CHIP_PORT_RANGE_SIZE = 130; |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 43 | |
| 44 | private final Map<ConnectPoint, MacAddress> oltMacInfo; |
| 45 | private final MacAddress agentMac; |
| 46 | private final Optional<ConnectPoint> vtnLocation; |
| 47 | private final DeviceId deviceId; |
| 48 | |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 49 | // OLT chip information sorted by ascending MAC address |
| 50 | private final List<Pair<ConnectPoint, MacAddress>> sortedOltChips; |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 53 | * Constructs an agent configuration for a given device. |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 54 | * |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 55 | * @param deviceId access device ID |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 56 | * @param oltMacInfo a map of olt chips and their mac address |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 57 | * @param agentMac the MAC address of the agent |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 58 | * @param vtnLocation the location of the agent |
| 59 | */ |
| 60 | public AccessAgentData(DeviceId deviceId, Map<ConnectPoint, MacAddress> oltMacInfo, |
| 61 | MacAddress agentMac, Optional<ConnectPoint> vtnLocation) { |
| 62 | this.deviceId = checkNotNull(deviceId, DEVICE_ID_MISSING); |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 63 | this.oltMacInfo = ImmutableMap.copyOf(checkNotNull(oltMacInfo, OLT_INFO_MISSING)); |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 64 | this.agentMac = checkNotNull(agentMac, AGENT_MAC_MISSING); |
| 65 | this.vtnLocation = checkNotNull(vtnLocation, VTN_MISSING); |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 66 | |
| 67 | this.sortedOltChips = oltMacInfo.entrySet().stream() |
| 68 | .sorted((e1, e2) -> Long.compare(e1.getValue().toLong(), e2.getValue().toLong())) |
| 69 | .map(e -> Pair.of(e.getKey(), e.getValue())) |
| 70 | .collect(Collectors.toList()); |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Retrieves the access device ID. |
| 75 | * |
| 76 | * @return device ID |
| 77 | */ |
| 78 | public DeviceId deviceId() { |
| 79 | return deviceId; |
| 80 | } |
| 81 | |
| 82 | /** |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 83 | * Returns the mapping of OLT chips to MAC addresses. Each chip is |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 84 | * symbolized by a connect point. |
| 85 | * |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 86 | * @return a mapping of chips (as connect points) to MAC addresses |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 87 | */ |
| 88 | public Map<ConnectPoint, MacAddress> getOltMacInfo() { |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 89 | return oltMacInfo; |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 93 | * Returns the agent's MAC address. |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 94 | * |
| 95 | * @return a mac address |
| 96 | */ |
| 97 | public MacAddress getAgentMac() { |
| 98 | return agentMac; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns the location of the agent. |
| 103 | * |
| 104 | * @return a connection point |
| 105 | */ |
| 106 | public Optional<ConnectPoint> getVtnLocation() { |
| 107 | return vtnLocation; |
| 108 | } |
Jonathan Hart | 3da5f0c | 2016-05-23 17:08:15 -0700 | [diff] [blame^] | 109 | |
| 110 | /** |
| 111 | * Returns the point where the OLT is connected to the fabric given a |
| 112 | * connect point on the agent device. |
| 113 | * |
| 114 | * @param agentConnectPoint connect point on the agent device |
| 115 | * @return point were OLT is connected to fabric |
| 116 | */ |
| 117 | public Optional<ConnectPoint> getOltConnectPoint(ConnectPoint agentConnectPoint) { |
| 118 | int index = ((int) agentConnectPoint.port().toLong()) / CHIP_PORT_RANGE_SIZE; |
| 119 | |
| 120 | if (index >= sortedOltChips.size()) { |
| 121 | return Optional.empty(); |
| 122 | } |
| 123 | |
| 124 | return Optional.of(sortedOltChips.get(index).getKey()); |
| 125 | } |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 126 | } |