blob: 3a14a29ff3f8a02590fe2fd45bc4562ca836dfd4 [file] [log] [blame]
ivoutsas69b97632016-05-25 13:36:44 +03001/*
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
17package org.onosproject.drivers.cisco;
18
19import com.google.common.collect.ImmutableList;
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.NetconfSession;
25import org.slf4j.Logger;
26
27import java.io.IOException;
28import java.util.List;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31import static org.slf4j.LoggerFactory.getLogger;
32
33/**
34 * Retrieves the ports from Cisco IOS devices via netconf.
35 */
36public 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}