blob: 851d7a4270e1bab21df9ac2bd861917219bd4d08 [file] [log] [blame]
HelloONOS137e1fb2019-08-20 16:43:52 +09001/*
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
17package org.onosproject.drivers.dellrest;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
21import org.apache.commons.configuration.HierarchicalConfiguration;
22import org.onosproject.drivers.utilities.XmlConfigParser;
23import org.onosproject.net.AnnotationKeys;
24import org.onosproject.net.DefaultAnnotations;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.Port;
27import org.onosproject.net.PortNumber;
28import org.onosproject.net.device.PortDescription;
29import org.onosproject.net.device.DefaultPortDescription;
30import org.onosproject.net.driver.AbstractHandlerBehaviour;
31import org.onosproject.net.driver.DriverHandler;
32import org.onosproject.protocol.rest.RestSBController;
33import org.slf4j.Logger;
34
35import java.util.List;
36
37import javax.ws.rs.core.MediaType;
38import static com.google.common.base.Preconditions.checkNotNull;
39import static org.slf4j.LoggerFactory.getLogger;
40
41/**
42 * Discovers the ports from a Dell Rest device.
43 */
44@Deprecated
45public 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