blob: 9ef9412d5b94307721d3826c99c76d740134a6a9 [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
18import java.util.ArrayList;
19import java.util.HashSet;
20import java.util.List;
21import java.util.Set;
22
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
28import org.onlab.packet.ChassisId;
29import org.onosproject.cluster.ClusterService;
30import org.onosproject.cluster.NodeId;
31import org.onosproject.mastership.MastershipAdminService;
32import org.onosproject.mastership.MastershipService;
33import org.onosproject.net.ConnectPoint;
34import org.onosproject.net.DefaultAnnotations;
35import org.onosproject.net.Device;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.Link;
38import org.onosproject.net.Link.Type;
39import org.onosproject.net.MastershipRole;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.device.DefaultDeviceDescription;
42import org.onosproject.net.device.DefaultPortDescription;
43import org.onosproject.net.device.DeviceDescription;
44import org.onosproject.net.device.DeviceProvider;
45import org.onosproject.net.device.DeviceProviderRegistry;
46import org.onosproject.net.device.DeviceProviderService;
47import org.onosproject.net.device.DeviceService;
48import org.onosproject.net.device.PortDescription;
49import org.onosproject.net.link.DefaultLinkDescription;
50import org.onosproject.net.link.LinkDescription;
51import org.onosproject.net.link.LinkProvider;
52import org.onosproject.net.link.LinkProviderRegistry;
53import org.onosproject.net.link.LinkProviderService;
54import org.onosproject.net.link.LinkService;
55import org.onosproject.net.provider.AbstractProvider;
56import org.onosproject.net.provider.ProviderId;
57import org.onosproject.pcep.api.PcepController;
58import org.onosproject.pcep.api.PcepDpid;
59import org.onosproject.pcep.api.PcepLink;
60import org.onosproject.pcep.api.PcepLinkListener;
61import org.onosproject.pcep.api.PcepOperator.OperationType;
62import org.onosproject.pcep.api.PcepSwitch;
63import org.onosproject.pcep.api.PcepSwitchListener;
64import org.slf4j.Logger;
65import org.slf4j.LoggerFactory;
66
67import static com.google.common.base.Preconditions.checkNotNull;
68import static org.onosproject.net.DeviceId.deviceId;
69import static org.onosproject.pcep.api.PcepDpid.uri;
70
71/**
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,
133 String portType) {
134 final List<PortDescription> portDescs = new ArrayList<>();
135 for (long port : ports) {
136 portDescs.add(buildPortDescription(port, portType));
137 }
138 return portDescs;
139 }
140
141 private PortDescription buildPortDescription(long port, String portType) {
142 final PortNumber portNo = PortNumber.portNumber(port);
143 final boolean enabled = true;
144 DefaultAnnotations extendedAttributes = DefaultAnnotations.builder()
145 .set("portType", portType).build();
146 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.
249 Device.Type deviceType = Device.Type.SWITCH;
250 ChassisId cId = new ChassisId(dpid.value());
251
252 // Device subType: ROADM,OTN,ROUTER.
253 DefaultAnnotations extendedAttributes = DefaultAnnotations
254 .builder()
255 .set("subType", String.valueOf(sw.getDeviceSubType()))
256 .build();
257
258 DeviceDescription description = new DefaultDeviceDescription(
259 devicdId.uri(),
260 deviceType,
261 sw.manufacturerDescription(),
262 sw.hardwareDescription(),
263 sw.softwareDescription(),
264 sw.serialNumber(),
265 cId,
266 extendedAttributes);
267 NodeId localNode = clusterService.getLocalNode().id();
268 mastershipAdminService.setRole(localNode, devicdId,
269 MastershipRole.MASTER);
270 mastershipService.relinquishMastership(devicdId);
271 deviceProviderService.deviceConnected(devicdId, description);
272
273 }
274
275 @Override
276 public void switchRemoved(PcepDpid dpid) {
277 // TODO Auto-generated method stub
278
279 if (deviceProviderService == null || linkProviderService == null) {
280 return;
281 }
282 deviceProviderService.deviceDisconnected(deviceId(uri(dpid)));
283
284 linkProviderService.linksVanished(DeviceId.deviceId(uri(dpid)));
285 }
286
287 @Override
288 public void switchChanged(PcepDpid dpid) {
289 // TODO Auto-generated method stub
290
291 }
292
293 @Override
294 public void handlePCEPlink(PcepLink link) {
295
296 OperationType operType = link.getOperationType();
297 LinkDescription ld = buildLinkDescription(link);
298 if (ld == null) {
299 log.error("Invalid link info.");
300 return;
301 }
302 switch (operType) {
303 case ADD:
304 linkProviderService.linkDetected(ld);
305 break;
306 case UPDATE:
307 processLinkUpdate(ld);
308 break;
309
310 case DELETE:
311 linkProviderService.linkVanished(ld);
312 break;
313
314 default:
315 break;
316
317 }
318 }
319
320 }
321
322 @Override
323 public void triggerProbe(DeviceId deviceId) {
324 // TODO Auto-generated method stub
325
326 }
327
328 @Override
329 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
330 // NodeId localNode = clusterService.getLocalNode().id();
331 // mastershipService.setRole(localNode, deviceId, newRole);
332
333 }
334
335 @Override
336 public boolean isReachable(DeviceId deviceId) {
337 // TODO Auto-generated method stub
338 return false;
339 }
340}