ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present 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.cisco; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 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.NetconfSession; |
| 25 | import org.slf4j.Logger; |
| 26 | |
| 27 | import java.io.IOException; |
| 28 | import java.util.List; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | import static org.slf4j.LoggerFactory.getLogger; |
| 32 | |
| 33 | /** |
| 34 | * Retrieves the ports from Cisco IOS devices via netconf. |
| 35 | */ |
| 36 | public class PortGetterCiscoIosImpl extends AbstractHandlerBehaviour |
| 37 | implements PortDiscovery { |
| 38 | |
| 39 | private final Logger log = getLogger(getClass()); |
| 40 | private String interfaces; |
| 41 | |
| 42 | @Override |
| 43 | public List<PortDescription> getPorts() { |
| 44 | NetconfController controller = checkNotNull(handler().get(NetconfController.class)); |
| 45 | NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession(); |
| 46 | try { |
| 47 | interfaces = session.get(showInterfacesRequestBuilder()); |
| 48 | } catch (IOException e) { |
| 49 | log.error("Failed to retrieve Interfaces"); |
| 50 | return ImmutableList.of(); |
| 51 | } |
| 52 | return ImmutableList.copyOf(TextBlockParserCisco.parseCiscoIosPorts(interfaces)); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Builds a request crafted to get the configuration required to create port |
| 57 | * descriptions for the device. |
| 58 | * |
| 59 | * @return The request string. |
| 60 | */ |
| 61 | private String showInterfacesRequestBuilder() { |
| 62 | //Message ID is injected later. |
| 63 | StringBuilder rpc = new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"); |
| 64 | rpc.append("<get>"); |
| 65 | rpc.append("<filter>"); |
| 66 | rpc.append("<config-format-text-block>"); |
| 67 | rpc.append("<text-filter-spec> | include exp_to_match_run_conf </text-filter-spec>"); |
| 68 | rpc.append("</config-format-text-block>"); |
| 69 | rpc.append("<oper-data-format-text-block>"); |
| 70 | rpc.append("<show>interfaces</show>"); |
| 71 | rpc.append("</oper-data-format-text-block>"); |
| 72 | rpc.append("</filter>"); |
| 73 | rpc.append("</get>"); |
| 74 | rpc.append("</rpc>]]>]]>"); |
| 75 | return rpc.toString(); |
| 76 | } |
| 77 | |
| 78 | } |