Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.bmv2; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame] | 21 | import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException; |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 22 | import org.onosproject.bmv2.ctl.Bmv2ThriftClient; |
| 23 | import org.onosproject.net.DefaultAnnotations; |
| 24 | import org.onosproject.net.PortNumber; |
| 25 | import org.onosproject.net.SparseAnnotations; |
| 26 | import org.onosproject.net.behaviour.PortDiscovery; |
| 27 | import org.onosproject.net.device.DefaultPortDescription; |
| 28 | import org.onosproject.net.device.PortDescription; |
| 29 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import java.util.Collections; |
| 34 | import java.util.List; |
| 35 | |
| 36 | public 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 Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame] | 47 | } catch (Bmv2RuntimeException e) { |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 48 | 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 Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame] | 71 | } catch (Bmv2RuntimeException e) { |
Carmelo Cascone | 2ea177b | 2016-02-25 18:38:42 -0800 | [diff] [blame] | 72 | log.error("Unable to get port description from Bmv2 device", e); |
| 73 | } |
| 74 | |
| 75 | return ImmutableList.copyOf(portDescriptions); |
| 76 | } |
| 77 | } |