Aaron Kruglikov | 17b4c85 | 2016-01-15 16:37:04 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 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.fujitsu; |
| 18 | |
| 19 | import org.onosproject.drivers.utilities.XmlConfigParser; |
| 20 | import org.onosproject.net.behaviour.PortDiscovery; |
| 21 | import org.onosproject.net.device.PortDescription; |
| 22 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 23 | import org.onosproject.netconf.NetconfController; |
| 24 | import org.onosproject.netconf.NetconfException; |
| 25 | import org.onosproject.netconf.NetconfSession; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import java.io.ByteArrayInputStream; |
| 29 | import java.io.IOException; |
| 30 | import java.util.List; |
| 31 | |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | import static org.slf4j.LoggerFactory.getLogger; |
| 34 | |
| 35 | /** |
| 36 | * Retrieves the ports from a Fujitsu T100 device via netconf. |
| 37 | */ |
| 38 | public 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 | } |