blob: 59102027c54d0eeca0c4d9e6b3e57e512e5aa00a [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
149 /**
150 * Build link annotations from pcep link description.the annotations consist
151 * of lots of property of Huawei device.
152 *
153 * @param linkDesc
154 * @return
155 */
156 private DefaultAnnotations buildLinkAnnotations(PcepLink linkDesc) {
157 DefaultAnnotations extendedAttributes = DefaultAnnotations
158 .builder()
159 .set("subType", String.valueOf(linkDesc.linkSubType()))
160 .set("workState", linkDesc.linkState())
161 .set("distance", String.valueOf(linkDesc.linkDistance()))
162 .set("capType", linkDesc.linkCapacityType().toLowerCase())
163 .set("avail_" + linkDesc.linkCapacityType().toLowerCase(),
164 String.valueOf(linkDesc.linkAvailValue()))
165 .set("max_" + linkDesc.linkCapacityType().toLowerCase(),
166 String.valueOf(linkDesc.linkMaxValue())).build();
167
168 return extendedAttributes;
169 }
170
171 /**
172 * Build a LinkDescription from a PCEPLink.
173 *
174 * @param pceLink
175 * @return LinkDescription
176 */
177 private LinkDescription buildLinkDescription(PcepLink pceLink) {
178 LinkDescription ld;
179
180 DeviceId srcDeviceID = deviceId(uri(pceLink.linkSrcDeviceID()));
181 DeviceId dstDeviceID = deviceId(uri(pceLink.linkDstDeviceId()));
182
183 if (deviceService.getDevice(srcDeviceID) == null
184 || deviceService.getDevice(dstDeviceID) == null) {
185 log.info("the device of the link is not exited" + srcDeviceID
186 + dstDeviceID);
187 return null;
188 }
189 // update port info
190 long srcPort = pceLink.linkSrcPort();
191 portSet.add(srcPort);
192 List<Long> srcportList = new ArrayList<Long>();
193 srcportList.addAll(portSet);
194 deviceProviderService
195 .updatePorts(srcDeviceID,
196 buildPortDescriptions(srcportList,
197 pceLink.portType()));
198
199 ConnectPoint src = new ConnectPoint(srcDeviceID,
200 PortNumber.portNumber(pceLink
201 .linkSrcPort()));
202
203 ConnectPoint dst = new ConnectPoint(dstDeviceID,
204 PortNumber.portNumber(pceLink
205 .linkDstPort()));
206 DefaultAnnotations extendedAttributes = buildLinkAnnotations(pceLink);
207
208 // construct the link
209 ld = new DefaultLinkDescription(src, dst, Type.OPTICAL,
210 extendedAttributes);
211 return ld;
212 }
213
214 private void processLinkUpdate(LinkDescription linkDescription) {
215
216 // dst changed, delete the original link,if the dst device is not in
217 // other links ,delete it.
218 if (linkService.getLink(linkDescription.src(), linkDescription.dst()) == null) {
219 // in face,one src one link
220 Set<Link> links = linkService
221 .getIngressLinks(linkDescription.src());
222 for (Link link : links) {
223 linkProviderService.linkVanished((LinkDescription) link);
224 if (linkService.getDeviceLinks(link.dst().deviceId()).size() == 0) {
225 deviceProviderService.deviceDisconnected(link.dst()
226 .deviceId());
227 }
228 }
229
230 }
231 linkProviderService.linkDetected(linkDescription);
232
233 }
234
235 private class InternalLinkProvider
236 implements PcepSwitchListener, PcepLinkListener {
237
238 @Override
239 public void switchAdded(PcepDpid dpid) {
240 // TODO Auto-generated method stub
241
242 if (deviceProviderService == null) {
243 return;
244 }
245 DeviceId devicdId = deviceId(uri(dpid));
246 PcepSwitch sw = controller.getSwitch(dpid);
247 checkNotNull(sw, "device should not null.");
248 // The default device type is switch.
cheng fan48e832c2015-05-29 01:54:47 +0800249 ChassisId cId = new ChassisId(dpid.value());
cheng fan7716ec92015-05-31 01:53:19 +0800250 Device.Type deviceType = null;
cheng fan48e832c2015-05-29 01:54:47 +0800251
cheng fan7716ec92015-05-31 01:53:19 +0800252 switch (sw.getDeviceType()) {
253 case ROADM:
254 deviceType = Device.Type.ROADM;
255 break;
256 case OTN:
257 deviceType = Device.Type.SWITCH;
258 break;
259 case ROUTER:
260 deviceType = Device.Type.ROUTER;
261 break;
262 default:
263 deviceType = Device.Type.OTHER;
264 }
cheng fan48e832c2015-05-29 01:54:47 +0800265
266 DeviceDescription description = new DefaultDeviceDescription(
267 devicdId.uri(),
268 deviceType,
269 sw.manufacturerDescription(),
270 sw.hardwareDescription(),
271 sw.softwareDescription(),
272 sw.serialNumber(),
cheng fan7716ec92015-05-31 01:53:19 +0800273 cId);
cheng fan48e832c2015-05-29 01:54:47 +0800274 deviceProviderService.deviceConnected(devicdId, description);
275
276 }
277
278 @Override
279 public void switchRemoved(PcepDpid dpid) {
280 // TODO Auto-generated method stub
281
282 if (deviceProviderService == null || linkProviderService == null) {
283 return;
284 }
285 deviceProviderService.deviceDisconnected(deviceId(uri(dpid)));
286
287 linkProviderService.linksVanished(DeviceId.deviceId(uri(dpid)));
288 }
289
290 @Override
291 public void switchChanged(PcepDpid dpid) {
292 // TODO Auto-generated method stub
293
294 }
295
296 @Override
297 public void handlePCEPlink(PcepLink link) {
298
299 OperationType operType = link.getOperationType();
300 LinkDescription ld = buildLinkDescription(link);
301 if (ld == null) {
302 log.error("Invalid link info.");
303 return;
304 }
305 switch (operType) {
306 case ADD:
307 linkProviderService.linkDetected(ld);
308 break;
309 case UPDATE:
310 processLinkUpdate(ld);
311 break;
312
313 case DELETE:
314 linkProviderService.linkVanished(ld);
315 break;
316
317 default:
318 break;
319
320 }
321 }
322
323 }
324
325 @Override
326 public void triggerProbe(DeviceId deviceId) {
327 // TODO Auto-generated method stub
cheng fan48e832c2015-05-29 01:54:47 +0800328 }
329
330 @Override
331 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
cheng fan48e832c2015-05-29 01:54:47 +0800332 }
333
334 @Override
335 public boolean isReachable(DeviceId deviceId) {
336 // TODO Auto-generated method stub
cheng fan7716ec92015-05-31 01:53:19 +0800337 return true;
cheng fan48e832c2015-05-29 01:54:47 +0800338 }
339}