HelloONOS | 137e1fb | 2019-08-20 16:43:52 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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.dellrest; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import org.apache.commons.configuration.HierarchicalConfiguration; |
| 22 | import org.onosproject.drivers.utilities.XmlConfigParser; |
| 23 | import org.onosproject.net.AnnotationKeys; |
| 24 | import org.onosproject.net.DefaultAnnotations; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.Port; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.device.PortDescription; |
| 29 | import org.onosproject.net.device.DefaultPortDescription; |
| 30 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 31 | import org.onosproject.net.driver.DriverHandler; |
| 32 | import org.onosproject.protocol.rest.RestSBController; |
| 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.util.List; |
| 36 | |
| 37 | import javax.ws.rs.core.MediaType; |
| 38 | import static com.google.common.base.Preconditions.checkNotNull; |
| 39 | import static org.slf4j.LoggerFactory.getLogger; |
| 40 | |
| 41 | /** |
| 42 | * Discovers the ports from a Dell Rest device. |
| 43 | */ |
| 44 | @Deprecated |
| 45 | public class DellRestPortDiscovery extends AbstractHandlerBehaviour { |
| 46 | |
| 47 | private final Logger log = getLogger(getClass()); |
| 48 | private static final String NAME = "name"; |
| 49 | private static final String INTERFACES = "interface"; |
| 50 | private static final String INTERFACES_REQUEST = "/running/dell/interfaces"; |
| 51 | private static final String TENGINTERFACENAME = "Te "; |
| 52 | |
| 53 | |
| 54 | @Deprecated |
| 55 | public List<PortDescription> getPorts() { |
| 56 | List<PortDescription> ports = Lists.newArrayList(); |
| 57 | DriverHandler handler = handler(); |
| 58 | RestSBController controller = checkNotNull(handler.get(RestSBController.class)); |
| 59 | DeviceId deviceId = handler.data().deviceId(); |
| 60 | String remotePortName = ""; |
| 61 | |
| 62 | // read configuration from REST API |
| 63 | HierarchicalConfiguration config = XmlConfigParser. |
| 64 | loadXml(controller.get(deviceId, INTERFACES_REQUEST, MediaType.valueOf("*/*"))); |
| 65 | |
| 66 | // get the interfaces part |
| 67 | List<HierarchicalConfiguration> portsConfig = parseDellPorts(config); |
| 68 | |
| 69 | portsConfig.stream().forEach(sub -> { |
| 70 | String portName = sub.getString(NAME); |
| 71 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder() |
| 72 | .set(AnnotationKeys.PORT_NAME, portName); |
| 73 | // TODO: obtain proper port speed and type |
| 74 | long portSpeed = 10_000; |
| 75 | Port.Type portType = Port.Type.COPPER; |
| 76 | PortNumber portNumber = PortNumber.fromString(remotePortName.replaceAll(TENGINTERFACENAME, "")); |
| 77 | |
| 78 | ports.add(DefaultPortDescription.builder() |
| 79 | .withPortNumber(portNumber) |
| 80 | .isEnabled(true) |
| 81 | .type(portType) |
| 82 | .portSpeed(portSpeed) |
| 83 | .annotations(annotations.build()) |
| 84 | .build()); |
| 85 | }); |
| 86 | return ImmutableList.copyOf(ports); |
| 87 | } |
| 88 | |
| 89 | public static List<HierarchicalConfiguration> parseDellPorts(HierarchicalConfiguration cfg) { |
| 90 | return cfg.configurationsAt(INTERFACES); |
| 91 | } |
| 92 | } |
| 93 | |