cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | package org.onosproject.provider.pcep.topology.impl; |
| 17 | |
cheng fan | 7716ec9 | 2015-05-31 01:53:19 +0800 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.onosproject.net.DeviceId.deviceId; |
| 20 | import static org.onosproject.pcep.api.PcepDpid.uri; |
| 21 | |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 22 | import java.util.ArrayList; |
| 23 | import java.util.HashSet; |
| 24 | import java.util.List; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import org.apache.felix.scr.annotations.Activate; |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Deactivate; |
| 30 | import org.apache.felix.scr.annotations.Reference; |
| 31 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 32 | import org.onlab.packet.ChassisId; |
| 33 | import org.onosproject.cluster.ClusterService; |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 34 | import org.onosproject.mastership.MastershipAdminService; |
| 35 | import org.onosproject.mastership.MastershipService; |
| 36 | import org.onosproject.net.ConnectPoint; |
| 37 | import org.onosproject.net.DefaultAnnotations; |
| 38 | import org.onosproject.net.Device; |
| 39 | import org.onosproject.net.DeviceId; |
| 40 | import org.onosproject.net.Link; |
| 41 | import org.onosproject.net.Link.Type; |
| 42 | import org.onosproject.net.MastershipRole; |
| 43 | import org.onosproject.net.PortNumber; |
| 44 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 45 | import org.onosproject.net.device.DefaultPortDescription; |
| 46 | import org.onosproject.net.device.DeviceDescription; |
| 47 | import org.onosproject.net.device.DeviceProvider; |
| 48 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 49 | import org.onosproject.net.device.DeviceProviderService; |
| 50 | import org.onosproject.net.device.DeviceService; |
| 51 | import org.onosproject.net.device.PortDescription; |
| 52 | import org.onosproject.net.link.DefaultLinkDescription; |
| 53 | import org.onosproject.net.link.LinkDescription; |
| 54 | import org.onosproject.net.link.LinkProvider; |
| 55 | import org.onosproject.net.link.LinkProviderRegistry; |
| 56 | import org.onosproject.net.link.LinkProviderService; |
| 57 | import org.onosproject.net.link.LinkService; |
| 58 | import org.onosproject.net.provider.AbstractProvider; |
| 59 | import org.onosproject.net.provider.ProviderId; |
| 60 | import org.onosproject.pcep.api.PcepController; |
| 61 | import org.onosproject.pcep.api.PcepDpid; |
| 62 | import org.onosproject.pcep.api.PcepLink; |
cheng fan | 7716ec9 | 2015-05-31 01:53:19 +0800 | [diff] [blame] | 63 | import org.onosproject.pcep.api.PcepLink.PortType; |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 64 | import org.onosproject.pcep.api.PcepLinkListener; |
| 65 | import org.onosproject.pcep.api.PcepOperator.OperationType; |
| 66 | import org.onosproject.pcep.api.PcepSwitch; |
| 67 | import org.onosproject.pcep.api.PcepSwitchListener; |
| 68 | import org.slf4j.Logger; |
| 69 | import org.slf4j.LoggerFactory; |
| 70 | |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 71 | /** |
| 72 | * Provider which uses an PCEP controller to detect network infrastructure |
| 73 | * topology. |
| 74 | */ |
| 75 | @Component(immediate = true) |
| 76 | public class PcepTopologyProvider extends AbstractProvider |
| 77 | implements LinkProvider, DeviceProvider { |
| 78 | |
| 79 | public PcepTopologyProvider() { |
| 80 | super(new ProviderId("pcep", "org.onosproject.provider.pcep")); |
| 81 | } |
| 82 | |
| 83 | private static final Logger log = LoggerFactory |
| 84 | .getLogger(PcepTopologyProvider.class); |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected LinkProviderRegistry linkProviderRegistry; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected DeviceProviderRegistry deviceProviderRegistry; |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected PcepController controller; |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected DeviceService deviceService; |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected LinkService linkService; |
| 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected MastershipAdminService mastershipAdminService; |
| 103 | |
| 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | protected MastershipService mastershipService; |
| 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 108 | protected ClusterService clusterService; |
| 109 | |
| 110 | private DeviceProviderService deviceProviderService; |
| 111 | private LinkProviderService linkProviderService; |
| 112 | // List<Long> srcportList = new ArrayList<Long>(); |
| 113 | HashSet<Long> portSet = new HashSet<>(); |
| 114 | private InternalLinkProvider listener = new InternalLinkProvider(); |
| 115 | |
| 116 | @Activate |
| 117 | public void activate() { |
| 118 | linkProviderService = linkProviderRegistry.register(this); |
| 119 | deviceProviderService = deviceProviderRegistry.register(this); |
| 120 | controller.addListener(listener); |
| 121 | controller.addLinkListener(listener); |
| 122 | } |
| 123 | |
| 124 | @Deactivate |
| 125 | public void deactivate() { |
| 126 | linkProviderRegistry.unregister(this); |
| 127 | linkProviderService = null; |
| 128 | controller.removeListener(listener); |
| 129 | controller.removeLinkListener(listener); |
| 130 | } |
| 131 | |
| 132 | private List<PortDescription> buildPortDescriptions(List<Long> ports, |
cheng fan | 7716ec9 | 2015-05-31 01:53:19 +0800 | [diff] [blame] | 133 | PortType portType) { |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 134 | final List<PortDescription> portDescs = new ArrayList<>(); |
| 135 | for (long port : ports) { |
| 136 | portDescs.add(buildPortDescription(port, portType)); |
| 137 | } |
| 138 | return portDescs; |
| 139 | } |
| 140 | |
cheng fan | 7716ec9 | 2015-05-31 01:53:19 +0800 | [diff] [blame] | 141 | private PortDescription buildPortDescription(long port, PortType portType) { |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 142 | final PortNumber portNo = PortNumber.portNumber(port); |
| 143 | final boolean enabled = true; |
| 144 | DefaultAnnotations extendedAttributes = DefaultAnnotations.builder() |
cheng fan | 7716ec9 | 2015-05-31 01:53:19 +0800 | [diff] [blame] | 145 | .set("portType", String.valueOf(portType)).build(); |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 146 | return new DefaultPortDescription(portNo, enabled, extendedAttributes); |
| 147 | } |
| 148 | |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 149 | private DefaultAnnotations buildLinkAnnotations(PcepLink linkDesc) { |
| 150 | DefaultAnnotations extendedAttributes = DefaultAnnotations |
| 151 | .builder() |
| 152 | .set("subType", String.valueOf(linkDesc.linkSubType())) |
| 153 | .set("workState", linkDesc.linkState()) |
| 154 | .set("distance", String.valueOf(linkDesc.linkDistance())) |
| 155 | .set("capType", linkDesc.linkCapacityType().toLowerCase()) |
| 156 | .set("avail_" + linkDesc.linkCapacityType().toLowerCase(), |
| 157 | String.valueOf(linkDesc.linkAvailValue())) |
| 158 | .set("max_" + linkDesc.linkCapacityType().toLowerCase(), |
| 159 | String.valueOf(linkDesc.linkMaxValue())).build(); |
| 160 | |
| 161 | return extendedAttributes; |
| 162 | } |
| 163 | |
| 164 | /** |
chengfan | 3e61879 | 2015-06-28 21:42:01 +0800 | [diff] [blame^] | 165 | * Build a link description from a pcep link. |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 166 | * |
chengfan | 3e61879 | 2015-06-28 21:42:01 +0800 | [diff] [blame^] | 167 | * @param pceLink pcep link |
| 168 | * @return LinkDescription onos link description |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 169 | */ |
| 170 | private LinkDescription buildLinkDescription(PcepLink pceLink) { |
| 171 | LinkDescription ld; |
| 172 | |
| 173 | DeviceId srcDeviceID = deviceId(uri(pceLink.linkSrcDeviceID())); |
| 174 | DeviceId dstDeviceID = deviceId(uri(pceLink.linkDstDeviceId())); |
| 175 | |
| 176 | if (deviceService.getDevice(srcDeviceID) == null |
| 177 | || deviceService.getDevice(dstDeviceID) == null) { |
| 178 | log.info("the device of the link is not exited" + srcDeviceID |
| 179 | + dstDeviceID); |
| 180 | return null; |
| 181 | } |
| 182 | // update port info |
| 183 | long srcPort = pceLink.linkSrcPort(); |
| 184 | portSet.add(srcPort); |
| 185 | List<Long> srcportList = new ArrayList<Long>(); |
| 186 | srcportList.addAll(portSet); |
| 187 | deviceProviderService |
| 188 | .updatePorts(srcDeviceID, |
| 189 | buildPortDescriptions(srcportList, |
| 190 | pceLink.portType())); |
| 191 | |
| 192 | ConnectPoint src = new ConnectPoint(srcDeviceID, |
| 193 | PortNumber.portNumber(pceLink |
| 194 | .linkSrcPort())); |
| 195 | |
| 196 | ConnectPoint dst = new ConnectPoint(dstDeviceID, |
| 197 | PortNumber.portNumber(pceLink |
| 198 | .linkDstPort())); |
| 199 | DefaultAnnotations extendedAttributes = buildLinkAnnotations(pceLink); |
| 200 | |
| 201 | // construct the link |
| 202 | ld = new DefaultLinkDescription(src, dst, Type.OPTICAL, |
| 203 | extendedAttributes); |
| 204 | return ld; |
| 205 | } |
| 206 | |
| 207 | private void processLinkUpdate(LinkDescription linkDescription) { |
| 208 | |
| 209 | // dst changed, delete the original link,if the dst device is not in |
| 210 | // other links ,delete it. |
| 211 | if (linkService.getLink(linkDescription.src(), linkDescription.dst()) == null) { |
| 212 | // in face,one src one link |
| 213 | Set<Link> links = linkService |
| 214 | .getIngressLinks(linkDescription.src()); |
| 215 | for (Link link : links) { |
| 216 | linkProviderService.linkVanished((LinkDescription) link); |
| 217 | if (linkService.getDeviceLinks(link.dst().deviceId()).size() == 0) { |
| 218 | deviceProviderService.deviceDisconnected(link.dst() |
| 219 | .deviceId()); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | } |
| 224 | linkProviderService.linkDetected(linkDescription); |
| 225 | |
| 226 | } |
| 227 | |
| 228 | private class InternalLinkProvider |
| 229 | implements PcepSwitchListener, PcepLinkListener { |
| 230 | |
| 231 | @Override |
| 232 | public void switchAdded(PcepDpid dpid) { |
| 233 | // TODO Auto-generated method stub |
| 234 | |
| 235 | if (deviceProviderService == null) { |
| 236 | return; |
| 237 | } |
| 238 | DeviceId devicdId = deviceId(uri(dpid)); |
| 239 | PcepSwitch sw = controller.getSwitch(dpid); |
| 240 | checkNotNull(sw, "device should not null."); |
| 241 | // The default device type is switch. |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 242 | ChassisId cId = new ChassisId(dpid.value()); |
cheng fan | 7716ec9 | 2015-05-31 01:53:19 +0800 | [diff] [blame] | 243 | Device.Type deviceType = null; |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 244 | |
cheng fan | 7716ec9 | 2015-05-31 01:53:19 +0800 | [diff] [blame] | 245 | switch (sw.getDeviceType()) { |
| 246 | case ROADM: |
| 247 | deviceType = Device.Type.ROADM; |
| 248 | break; |
| 249 | case OTN: |
| 250 | deviceType = Device.Type.SWITCH; |
| 251 | break; |
| 252 | case ROUTER: |
| 253 | deviceType = Device.Type.ROUTER; |
| 254 | break; |
| 255 | default: |
| 256 | deviceType = Device.Type.OTHER; |
| 257 | } |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 258 | |
| 259 | DeviceDescription description = new DefaultDeviceDescription( |
| 260 | devicdId.uri(), |
| 261 | deviceType, |
| 262 | sw.manufacturerDescription(), |
| 263 | sw.hardwareDescription(), |
| 264 | sw.softwareDescription(), |
| 265 | sw.serialNumber(), |
cheng fan | 7716ec9 | 2015-05-31 01:53:19 +0800 | [diff] [blame] | 266 | cId); |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 267 | deviceProviderService.deviceConnected(devicdId, description); |
| 268 | |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public void switchRemoved(PcepDpid dpid) { |
| 273 | // TODO Auto-generated method stub |
| 274 | |
| 275 | if (deviceProviderService == null || linkProviderService == null) { |
| 276 | return; |
| 277 | } |
| 278 | deviceProviderService.deviceDisconnected(deviceId(uri(dpid))); |
| 279 | |
| 280 | linkProviderService.linksVanished(DeviceId.deviceId(uri(dpid))); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void switchChanged(PcepDpid dpid) { |
| 285 | // TODO Auto-generated method stub |
| 286 | |
| 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public void handlePCEPlink(PcepLink link) { |
| 291 | |
| 292 | OperationType operType = link.getOperationType(); |
| 293 | LinkDescription ld = buildLinkDescription(link); |
| 294 | if (ld == null) { |
| 295 | log.error("Invalid link info."); |
| 296 | return; |
| 297 | } |
| 298 | switch (operType) { |
| 299 | case ADD: |
| 300 | linkProviderService.linkDetected(ld); |
| 301 | break; |
| 302 | case UPDATE: |
| 303 | processLinkUpdate(ld); |
| 304 | break; |
| 305 | |
| 306 | case DELETE: |
| 307 | linkProviderService.linkVanished(ld); |
| 308 | break; |
| 309 | |
| 310 | default: |
| 311 | break; |
| 312 | |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public void triggerProbe(DeviceId deviceId) { |
| 320 | // TODO Auto-generated method stub |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public boolean isReachable(DeviceId deviceId) { |
| 329 | // TODO Auto-generated method stub |
cheng fan | 7716ec9 | 2015-05-31 01:53:19 +0800 | [diff] [blame] | 330 | return true; |
cheng fan | 48e832c | 2015-05-29 01:54:47 +0800 | [diff] [blame] | 331 | } |
| 332 | } |