blob: 63066b3edfaaa7fbf21fc7056843a5125b749538 [file] [log] [blame]
Aaron Kruglikov17b4c852016-01-15 16:37:04 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Aaron Kruglikov17b4c852016-01-15 16:37:04 -08003 *
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.fujitsu;
18
19import org.onosproject.drivers.utilities.XmlConfigParser;
20import org.onosproject.net.behaviour.PortDiscovery;
21import org.onosproject.net.device.PortDescription;
22import org.onosproject.net.driver.AbstractHandlerBehaviour;
23import org.onosproject.netconf.NetconfController;
24import org.onosproject.netconf.NetconfException;
25import org.onosproject.netconf.NetconfSession;
26import org.slf4j.Logger;
27
28import java.io.ByteArrayInputStream;
29import java.io.IOException;
30import java.util.List;
31
32import static com.google.common.base.Preconditions.checkNotNull;
33import static org.slf4j.LoggerFactory.getLogger;
34
35/**
36 * Retrieves the ports from a Fujitsu T100 device via netconf.
37 */
38public class PortGetterFujitsuImpl extends AbstractHandlerBehaviour
39 implements PortDiscovery {
40
41 private final Logger log = getLogger(getClass());
42
43 @Override
44 public List<PortDescription> getPorts() {
45 NetconfController controller = checkNotNull(handler().get(NetconfController.class));
46 NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession();
47 String reply;
48 try {
49 reply = session.get(requestBuilder());
50 } catch (IOException e) {
51 throw new RuntimeException(new NetconfException("Failed to retrieve configuration.", e));
52 }
53 List<PortDescription> descriptions = XmlConfigParser.
54 parseFujitsuT100Ports(XmlConfigParser.
55 loadXml(new ByteArrayInputStream(reply.getBytes())));
56 return descriptions;
57 }
58
59 /**
60 * Builds a request crafted to get the configuration required to create port
61 * descriptions for the device.
62 * @return The request string.
63 */
64 private String requestBuilder() {
65 StringBuilder rpc = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
66 //Message ID is injected later.
67 rpc.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">");
68 rpc.append("<get>");
69 rpc.append("<filter type=\"subtree\">");
70 rpc.append("<interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">");
71 rpc.append("</interfaces>");
72 rpc.append("</filter>");
73 rpc.append("</get>");
74 rpc.append("</rpc>");
75 return rpc.toString();
76 }
77}