blob: 08258831c15bfe6d14c4aaf00b3abc05846e0ded [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 Cascone37d5dbf2016-04-18 15:15:48 -070021import org.onosproject.bmv2.api.runtime.Bmv2Client;
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070022import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException;
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080023import org.onosproject.bmv2.ctl.Bmv2ThriftClient;
24import org.onosproject.net.DefaultAnnotations;
25import org.onosproject.net.PortNumber;
26import org.onosproject.net.SparseAnnotations;
27import org.onosproject.net.behaviour.PortDiscovery;
28import org.onosproject.net.device.DefaultPortDescription;
29import org.onosproject.net.device.PortDescription;
30import org.onosproject.net.driver.AbstractHandlerBehaviour;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34import java.util.Collections;
35import java.util.List;
36
Carmelo Casconee9121642016-04-27 17:02:38 -070037public class Bmv2PortDiscovery extends AbstractHandlerBehaviour
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080038 implements PortDiscovery {
39
40 private final Logger log =
41 LoggerFactory.getLogger(this.getClass());
42
43 @Override
44 public List<PortDescription> getPorts() {
Carmelo Cascone37d5dbf2016-04-18 15:15:48 -070045 Bmv2Client deviceClient;
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080046 try {
47 deviceClient = Bmv2ThriftClient.of(handler().data().deviceId());
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070048 } catch (Bmv2RuntimeException e) {
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080049 log.error("Failed to connect to Bmv2 device", e);
50 return Collections.emptyList();
51 }
52
53 List<PortDescription> portDescriptions = Lists.newArrayList();
54
55 try {
56
57 deviceClient.getPortsInfo().forEach(
58 p -> {
59 DefaultAnnotations.Builder builder =
60 DefaultAnnotations.builder();
61 p.getExtraProperties().forEach(builder::set);
62 SparseAnnotations annotations = builder.build();
63
64 portDescriptions.add(new DefaultPortDescription(
65 PortNumber.portNumber(
66 (long) p.portNumber(),
67 p.ifaceName()),
68 p.isUp(),
69 annotations
70 ));
71 });
Carmelo Casconeaa8b6292016-04-13 14:27:06 -070072 } catch (Bmv2RuntimeException e) {
Carmelo Cascone2ea177b2016-02-25 18:38:42 -080073 log.error("Unable to get port description from Bmv2 device", e);
74 }
75
76 return ImmutableList.copyOf(portDescriptions);
77 }
78}