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