blob: 0b66710aae6233d7e98e309f4710824ad82e4d7c [file] [log] [blame]
janani bf7060cd2017-03-28 19:06:30 +05301/*
2 * Copyright 2017-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.huawei;
18
19import com.google.common.collect.ImmutableList;
Vidyashree Ramacf117ce2017-04-14 08:54:39 +053020import org.apache.commons.lang.StringUtils;
janani bf7060cd2017-03-28 19:06:30 +053021import org.onosproject.net.Device;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.device.DefaultDeviceDescription;
24import org.onosproject.net.device.DeviceDescription;
Andrea Campanella527f8d02017-06-27 15:22:23 +020025import org.onosproject.net.device.DeviceDescriptionDiscovery;
janani bf7060cd2017-03-28 19:06:30 +053026import org.onosproject.net.device.DeviceService;
27import org.onosproject.net.device.PortDescription;
Gaurav Agrawal9912f292017-03-30 01:22:40 +053028import org.onosproject.net.device.PortStatistics;
29import org.onosproject.net.device.PortStatisticsDiscovery;
janani bf7060cd2017-03-28 19:06:30 +053030import org.onosproject.net.driver.AbstractHandlerBehaviour;
31import org.onosproject.netconf.NetconfController;
32import org.onosproject.netconf.NetconfException;
33import org.onosproject.netconf.NetconfSession;
34import org.slf4j.Logger;
35
36import java.io.IOException;
Gaurav Agrawal9912f292017-03-30 01:22:40 +053037import java.util.Collection;
janani bf7060cd2017-03-28 19:06:30 +053038import java.util.List;
39
40import static com.google.common.base.Preconditions.checkNotNull;
41import static org.onosproject.drivers.huawei.DriverUtil.DEV_INFO_FAILURE;
janani bf7060cd2017-03-28 19:06:30 +053042import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE;
43import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE_FILTER;
44import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE_GET;
Gaurav Agrawal9912f292017-03-30 01:22:40 +053045import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE_IFM;
janani bf7060cd2017-03-28 19:06:30 +053046import static org.onosproject.drivers.huawei.DriverUtil.RPC_FILTER;
47import static org.onosproject.drivers.huawei.DriverUtil.RPC_GET;
48import static org.onosproject.drivers.huawei.DriverUtil.RPC_IFM;
Gaurav Agrawal9912f292017-03-30 01:22:40 +053049import static org.onosproject.drivers.huawei.DriverUtil.RPC_IFS;
janani bf7060cd2017-03-28 19:06:30 +053050import static org.onosproject.drivers.huawei.DriverUtil.RPC_MSG;
51import static org.onosproject.drivers.huawei.DriverUtil.RPC_SYS;
52import static org.onosproject.net.Device.Type.ROUTER;
53import static org.slf4j.LoggerFactory.getLogger;
54
55/**
56 * Representation of device information and ports via NETCONF for huawei
57 * routers.
58 */
59public class HuaweiDeviceDescription extends AbstractHandlerBehaviour
Andrea Campanella527f8d02017-06-27 15:22:23 +020060 implements PortStatisticsDiscovery, DeviceDescriptionDiscovery {
janani bf7060cd2017-03-28 19:06:30 +053061
62 private final Logger log = getLogger(getClass());
63
64 /**
65 * Constructs huawei device description.
66 */
67 public HuaweiDeviceDescription() {
68 }
69
70 /**
71 * Discovers device details, for huawei device by getting the system
72 * information.
73 *
74 * @return device description
75 */
76 @Override
77 public DeviceDescription discoverDeviceDetails() {
78 NetconfSession session = getNetconfSession();
79 String sysInfo;
80 try {
81 sysInfo = session.get(getVersionReq());
82 } catch (IOException e) {
83 throw new IllegalArgumentException(
84 new NetconfException(DEV_INFO_FAILURE));
85 }
86
87 String[] details = parseSysInfoXml(sysInfo);
88 DeviceService devSvc = checkNotNull(handler().get(DeviceService.class));
89 DeviceId devId = handler().data().deviceId();
90 Device dev = devSvc.getDevice(devId);
91 return new DefaultDeviceDescription(dev.id().uri(), ROUTER,
92 details[0], details[1],
93 details[2], details[3],
94 dev.chassisId());
95 }
96
97 /**
98 * Discovers interface details, for huawei device.
99 *
100 * @return port list
101 */
102 @Override
103 public List<PortDescription> discoverPortDetails() {
Gaurav Agrawal9912f292017-03-30 01:22:40 +0530104 return ImmutableList.copyOf(parseInterfaceXml(getInterfaces()));
janani bf7060cd2017-03-28 19:06:30 +0530105 }
106
107 /**
108 * Returns the NETCONF session of the device.
109 *
110 * @return session
111 */
112 private NetconfSession getNetconfSession() {
113 NetconfController controller = checkNotNull(
114 handler().get(NetconfController.class));
115 return controller.getDevicesMap().get(handler().data().deviceId())
116 .getSession();
117 }
118
119 /**
120 * Returns the rpc request message for fetching system details in huawei
121 * device.
122 *
123 * @return rpc request message
124 */
125 private String getVersionReq() {
126 StringBuilder rpc = new StringBuilder(RPC_MSG);
127 rpc.append(RPC_GET);
128 rpc.append(RPC_FILTER);
129 rpc.append(RPC_SYS);
130 rpc.append(RPC_CLOSE_FILTER);
131 rpc.append(RPC_CLOSE_GET);
132 rpc.append(RPC_CLOSE);
133 return rpc.toString();
134 }
135
136 /**
137 * Parses system info received from huawei device.
138 *
139 * @param sysInfo system info
140 * @return parsed values
141 */
142 private String[] parseSysInfoXml(String sysInfo) {
143 HuaweiXmlParser parser = new HuaweiXmlParser(sysInfo);
144 parser.parseSysInfo();
145 return parser.getInfo();
146 }
147
148 /**
149 * Returns the rpc request message for fetching interface details in
150 * huawei device.
151 *
152 * @return rpc request message
153 */
154 private String getInterfacesReq() {
155 StringBuilder rpc = new StringBuilder(RPC_MSG);
156 rpc.append(RPC_GET);
157 rpc.append(RPC_FILTER);
158 rpc.append(RPC_IFM);
159 rpc.append(RPC_IFS);
160 rpc.append(RPC_CLOSE_IFM);
161 rpc.append(RPC_CLOSE_FILTER);
162 rpc.append(RPC_CLOSE_GET);
163 rpc.append(RPC_CLOSE);
164 return rpc.toString();
165 }
166
167 /**
168 * Parses interfaces received from huawei device.
169 *
170 * @param interfaces interfaces
171 * @return port list
172 */
173 private List<PortDescription> parseInterfaceXml(String interfaces) {
174 HuaweiXmlParser parser = new HuaweiXmlParser(interfaces);
175 parser.parseInterfaces();
176 return parser.getPorts();
177 }
Gaurav Agrawal9912f292017-03-30 01:22:40 +0530178
179 @Override
180 public Collection<PortStatistics> discoverPortStatistics() {
Vidyashree Ramacf117ce2017-04-14 08:54:39 +0530181 String interfaces = getInterfaces();
182 if (StringUtils.isNotBlank(interfaces)) {
183 Collection<PortStatistics> portStats = getPortStatistics(interfaces);
184 return ImmutableList.copyOf(portStats);
185 }
186 return null;
Gaurav Agrawal9912f292017-03-30 01:22:40 +0530187 }
188
189 private String getInterfaces() {
190 NetconfSession session = getNetconfSession();
Vidyashree Ramacf117ce2017-04-14 08:54:39 +0530191 String interfaces = null;
Gaurav Agrawal9912f292017-03-30 01:22:40 +0530192 try {
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530193 interfaces = session.get(getInterfacesReq());
Gaurav Agrawal9912f292017-03-30 01:22:40 +0530194 } catch (IOException e) {
Vidyashree Ramacf117ce2017-04-14 08:54:39 +0530195 log.info("Failed to retrive interface {} ", e.getMessage());
Gaurav Agrawal9912f292017-03-30 01:22:40 +0530196 }
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530197 return interfaces;
Gaurav Agrawal9912f292017-03-30 01:22:40 +0530198 }
199
200 private Collection<PortStatistics> getPortStatistics(String ifs) {
201 HuaweiXmlParser parser = new HuaweiXmlParser(ifs);
202 return parser.parsePortsStatistics(handler().data().deviceId());
203 }
janani bf7060cd2017-03-28 19:06:30 +0530204}