blob: 927b77d3e27e986bf46cdbfc1a89b56e813ae22e [file] [log] [blame]
Carmelo Cascone2ea177b2016-02-25 18:38:42 -08001/*
2 * Copyright 2014-2016 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.drivers.bmv2;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070021import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException;
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080022import org.onosproject.bmv2.ctl.Bmv2ThriftClient;
23import org.onosproject.net.DefaultAnnotations;
24import org.onosproject.net.PortNumber;
25import org.onosproject.net.SparseAnnotations;
26import org.onosproject.net.behaviour.PortDiscovery;
27import org.onosproject.net.device.DefaultPortDescription;
28import org.onosproject.net.device.PortDescription;
29import org.onosproject.net.driver.AbstractHandlerBehaviour;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import java.util.Collections;
34import java.util.List;
35
36public class Bmv2PortGetterDriver extends AbstractHandlerBehaviour
37 implements PortDiscovery {
38
39 private final Logger log =
40 LoggerFactory.getLogger(this.getClass());
41
42 @Override
43 public List<PortDescription> getPorts() {
44 Bmv2ThriftClient deviceClient;
45 try {
46 deviceClient = Bmv2ThriftClient.of(handler().data().deviceId());
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070047 } catch (Bmv2RuntimeException e) {
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080048 log.error("Failed to connect to Bmv2 device", e);
49 return Collections.emptyList();
50 }
51
52 List<PortDescription> portDescriptions = Lists.newArrayList();
53
54 try {
55
56 deviceClient.getPortsInfo().forEach(
57 p -> {
58 DefaultAnnotations.Builder builder =
59 DefaultAnnotations.builder();
60 p.getExtraProperties().forEach(builder::set);
61 SparseAnnotations annotations = builder.build();
62
63 portDescriptions.add(new DefaultPortDescription(
64 PortNumber.portNumber(
65 (long) p.portNumber(),
66 p.ifaceName()),
67 p.isUp(),
68 annotations
69 ));
70 });
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070071 } catch (Bmv2RuntimeException e) {
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080072 log.error("Unable to get port description from Bmv2 device", e);
73 }
74
75 return ImmutableList.copyOf(portDescriptions);
76 }
77}