blob: de7a342cf9cbd57fb322c5976a8053d173bb9a7a [file] [log] [blame]
alshabib1f36ab82016-04-29 15:38:19 -07001/*
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
17package org.onosproject.cordconfig.access;
18
19import com.google.common.collect.ImmutableMap;
Jonathan Hart3da5f0c2016-05-23 17:08:15 -070020import org.apache.commons.lang3.tuple.Pair;
alshabib1f36ab82016-04-29 15:38:19 -070021import org.onlab.packet.MacAddress;
22import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.DeviceId;
24
Jonathan Hart3da5f0c2016-05-23 17:08:15 -070025import java.util.List;
alshabib1f36ab82016-04-29 15:38:19 -070026import java.util.Map;
27import java.util.Optional;
Jonathan Hart3da5f0c2016-05-23 17:08:15 -070028import java.util.stream.Collectors;
alshabib1f36ab82016-04-29 15:38:19 -070029
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Information about an access agent.
34 */
35public class AccessAgentData {
Jonathan Hart3da5f0c2016-05-23 17:08:15 -070036
alshabib1f36ab82016-04-29 15:38:19 -070037 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 Hart3da5f0c2016-05-23 17:08:15 -070042 private static final int CHIP_PORT_RANGE_SIZE = 130;
alshabib1f36ab82016-04-29 15:38:19 -070043
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 Hart3da5f0c2016-05-23 17:08:15 -070049 // OLT chip information sorted by ascending MAC address
50 private final List<Pair<ConnectPoint, MacAddress>> sortedOltChips;
alshabib1f36ab82016-04-29 15:38:19 -070051
52 /**
Jonathan Hart3da5f0c2016-05-23 17:08:15 -070053 * Constructs an agent configuration for a given device.
alshabib1f36ab82016-04-29 15:38:19 -070054 *
Jonathan Hart3da5f0c2016-05-23 17:08:15 -070055 * @param deviceId access device ID
alshabib1f36ab82016-04-29 15:38:19 -070056 * @param oltMacInfo a map of olt chips and their mac address
Jonathan Hart3da5f0c2016-05-23 17:08:15 -070057 * @param agentMac the MAC address of the agent
alshabib1f36ab82016-04-29 15:38:19 -070058 * @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 Hart3da5f0c2016-05-23 17:08:15 -070063 this.oltMacInfo = ImmutableMap.copyOf(checkNotNull(oltMacInfo, OLT_INFO_MISSING));
alshabib1f36ab82016-04-29 15:38:19 -070064 this.agentMac = checkNotNull(agentMac, AGENT_MAC_MISSING);
65 this.vtnLocation = checkNotNull(vtnLocation, VTN_MISSING);
Jonathan Hart3da5f0c2016-05-23 17:08:15 -070066
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());
alshabib1f36ab82016-04-29 15:38:19 -070071 }
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 Hart3da5f0c2016-05-23 17:08:15 -070083 * Returns the mapping of OLT chips to MAC addresses. Each chip is
alshabib1f36ab82016-04-29 15:38:19 -070084 * symbolized by a connect point.
85 *
Jonathan Hart3da5f0c2016-05-23 17:08:15 -070086 * @return a mapping of chips (as connect points) to MAC addresses
alshabib1f36ab82016-04-29 15:38:19 -070087 */
88 public Map<ConnectPoint, MacAddress> getOltMacInfo() {
Jonathan Hart3da5f0c2016-05-23 17:08:15 -070089 return oltMacInfo;
alshabib1f36ab82016-04-29 15:38:19 -070090 }
91
92 /**
Jonathan Hart3da5f0c2016-05-23 17:08:15 -070093 * Returns the agent's MAC address.
alshabib1f36ab82016-04-29 15:38:19 -070094 *
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 Hart3da5f0c2016-05-23 17:08:15 -0700109
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 }
alshabib1f36ab82016-04-29 15:38:19 -0700126}