blob: b43a912429f7da0890dca0b07ddd32ff3f204067 [file] [log] [blame]
cheng fan48e832c2015-05-29 01:54:47 +08001/*
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 */
16package org.onosproject.provider.pcep.topology.impl;
17
cheng fan7716ec92015-05-31 01:53:19 +080018import static com.google.common.base.Preconditions.checkNotNull;
19import static org.onosproject.net.DeviceId.deviceId;
20import static org.onosproject.pcep.api.PcepDpid.uri;
21
cheng fan48e832c2015-05-29 01:54:47 +080022import java.util.ArrayList;
23import java.util.HashSet;
24import java.util.List;
25import java.util.Set;
26
27import org.apache.felix.scr.annotations.Activate;
28import org.apache.felix.scr.annotations.Component;
29import org.apache.felix.scr.annotations.Deactivate;
30import org.apache.felix.scr.annotations.Reference;
31import org.apache.felix.scr.annotations.ReferenceCardinality;
32import org.onlab.packet.ChassisId;
33import org.onosproject.cluster.ClusterService;
cheng fan48e832c2015-05-29 01:54:47 +080034import org.onosproject.mastership.MastershipAdminService;
35import org.onosproject.mastership.MastershipService;
36import org.onosproject.net.ConnectPoint;
37import org.onosproject.net.DefaultAnnotations;
38import org.onosproject.net.Device;
39import org.onosproject.net.DeviceId;
40import org.onosproject.net.Link;
41import org.onosproject.net.Link.Type;
42import org.onosproject.net.MastershipRole;
43import org.onosproject.net.PortNumber;
44import org.onosproject.net.device.DefaultDeviceDescription;
45import org.onosproject.net.device.DefaultPortDescription;
46import org.onosproject.net.device.DeviceDescription;
47import org.onosproject.net.device.DeviceProvider;
48import org.onosproject.net.device.DeviceProviderRegistry;
49import org.onosproject.net.device.DeviceProviderService;
50import org.onosproject.net.device.DeviceService;
51import org.onosproject.net.device.PortDescription;
52import org.onosproject.net.link.DefaultLinkDescription;
53import org.onosproject.net.link.LinkDescription;
54import org.onosproject.net.link.LinkProvider;
55import org.onosproject.net.link.LinkProviderRegistry;
56import org.onosproject.net.link.LinkProviderService;
57import org.onosproject.net.link.LinkService;
58import org.onosproject.net.provider.AbstractProvider;
59import org.onosproject.net.provider.ProviderId;
60import org.onosproject.pcep.api.PcepController;
61import org.onosproject.pcep.api.PcepDpid;
62import org.onosproject.pcep.api.PcepLink;
cheng fan7716ec92015-05-31 01:53:19 +080063import org.onosproject.pcep.api.PcepLink.PortType;
cheng fan48e832c2015-05-29 01:54:47 +080064import org.onosproject.pcep.api.PcepLinkListener;
65import org.onosproject.pcep.api.PcepOperator.OperationType;
66import org.onosproject.pcep.api.PcepSwitch;
67import org.onosproject.pcep.api.PcepSwitchListener;
68import org.slf4j.Logger;
69import org.slf4j.LoggerFactory;
70
cheng fan48e832c2015-05-29 01:54:47 +080071/**
72 * Provider which uses an PCEP controller to detect network infrastructure
73 * topology.
74 */
75@Component(immediate = true)
76public 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 fan7716ec92015-05-31 01:53:19 +0800133 PortType portType) {
cheng fan48e832c2015-05-29 01:54:47 +0800134 final List<PortDescription> portDescs = new ArrayList<>();
135 for (long port : ports) {
136 portDescs.add(buildPortDescription(port, portType));
137 }
138 return portDescs;
139 }
140
cheng fan7716ec92015-05-31 01:53:19 +0800141 private PortDescription buildPortDescription(long port, PortType portType) {
cheng fan48e832c2015-05-29 01:54:47 +0800142 final PortNumber portNo = PortNumber.portNumber(port);
143 final boolean enabled = true;
144 DefaultAnnotations extendedAttributes = DefaultAnnotations.builder()
cheng fan7716ec92015-05-31 01:53:19 +0800145 .set("portType", String.valueOf(portType)).build();
cheng fan48e832c2015-05-29 01:54:47 +0800146 return new DefaultPortDescription(portNo, enabled, extendedAttributes);
147 }
148
cheng fan48e832c2015-05-29 01:54:47 +0800149 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 /**
chengfan3e618792015-06-28 21:42:01 +0800165 * Build a link description from a pcep link.
cheng fan48e832c2015-05-29 01:54:47 +0800166 *
chengfan3e618792015-06-28 21:42:01 +0800167 * @param pceLink pcep link
168 * @return LinkDescription onos link description
cheng fan48e832c2015-05-29 01:54:47 +0800169 */
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 fan48e832c2015-05-29 01:54:47 +0800242 ChassisId cId = new ChassisId(dpid.value());
cheng fan7716ec92015-05-31 01:53:19 +0800243 Device.Type deviceType = null;
cheng fan48e832c2015-05-29 01:54:47 +0800244
cheng fan7716ec92015-05-31 01:53:19 +0800245 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 fan48e832c2015-05-29 01:54:47 +0800258
259 DeviceDescription description = new DefaultDeviceDescription(
260 devicdId.uri(),
261 deviceType,
262 sw.manufacturerDescription(),
263 sw.hardwareDescription(),
264 sw.softwareDescription(),
265 sw.serialNumber(),
cheng fan7716ec92015-05-31 01:53:19 +0800266 cId);
cheng fan48e832c2015-05-29 01:54:47 +0800267 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 fan48e832c2015-05-29 01:54:47 +0800321 }
322
323 @Override
324 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
cheng fan48e832c2015-05-29 01:54:47 +0800325 }
326
327 @Override
328 public boolean isReachable(DeviceId deviceId) {
329 // TODO Auto-generated method stub
cheng fan7716ec92015-05-31 01:53:19 +0800330 return true;
cheng fan48e832c2015-05-29 01:54:47 +0800331 }
332}