blob: 1fc4f6c265c1dd32bb53969f36c4b8bf80cad078 [file] [log] [blame]
Michele Santuari21c14012016-11-14 13:31:33 +01001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Michele Santuari21c14012016-11-14 13:31:33 +01003 *
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.juniper;
18
Kieran McPeakebc00cb52019-05-23 13:07:25 +010019import com.google.common.base.MoreObjects;
Michele Santuari21c14012016-11-14 13:31:33 +010020import org.apache.commons.configuration.HierarchicalConfiguration;
Michele Santuarice256492017-06-23 14:44:01 +020021import org.apache.commons.lang.StringUtils;
Michele Santuari21c14012016-11-14 13:31:33 +010022import org.onlab.packet.ChassisId;
Michele Santuarice256492017-06-23 14:44:01 +020023import org.onlab.packet.Ip4Address;
24import org.onlab.packet.Ip4Prefix;
DongRyeol Cha5f870032018-07-12 18:36:20 +090025import org.onlab.packet.IpAddress;
Michele Santuari21c14012016-11-14 13:31:33 +010026import org.onlab.packet.MacAddress;
27import org.onosproject.net.AnnotationKeys;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.DefaultAnnotations;
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -070030import org.onosproject.net.DefaultAnnotations.Builder;
Michele Santuari21c14012016-11-14 13:31:33 +010031import org.onosproject.net.DeviceId;
32import org.onosproject.net.Link;
33import org.onosproject.net.Port;
34import org.onosproject.net.PortNumber;
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -070035import org.onosproject.net.Port.Type;
DongRyeol Cha5f870032018-07-12 18:36:20 +090036import org.onosproject.net.behaviour.ControllerInfo;
Michele Santuari21c14012016-11-14 13:31:33 +010037import org.onosproject.net.device.DefaultDeviceDescription;
38import org.onosproject.net.device.DefaultPortDescription;
39import org.onosproject.net.device.DeviceDescription;
40import org.onosproject.net.device.PortDescription;
41import org.onosproject.net.link.DefaultLinkDescription;
42import org.onosproject.net.link.LinkDescription;
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070043import org.slf4j.Logger;
Michele Santuari21c14012016-11-14 13:31:33 +010044
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -070045import com.google.common.base.Strings;
46
47import java.util.ArrayList;
Michele Santuarice256492017-06-23 14:44:01 +020048import java.util.Collection;
Michele Santuari21c14012016-11-14 13:31:33 +010049import java.util.HashSet;
50import java.util.List;
Michele Santuarice256492017-06-23 14:44:01 +020051import java.util.Optional;
Michele Santuari21c14012016-11-14 13:31:33 +010052import java.util.Set;
53import java.util.regex.Matcher;
54import java.util.regex.Pattern;
55
Michele Santuarice256492017-06-23 14:44:01 +020056import static org.onosproject.drivers.juniper.StaticRoute.DEFAULT_METRIC_STATIC_ROUTE;
57import static org.onosproject.drivers.juniper.StaticRoute.toFlowRulePriority;
Kieran McPeakebc00cb52019-05-23 13:07:25 +010058import static org.onosproject.drivers.utilities.XmlConfigParser.loadXmlString;
Michele Santuari21c14012016-11-14 13:31:33 +010059import static org.onosproject.net.Device.Type.ROUTER;
Michele Santuari21c14012016-11-14 13:31:33 +010060import static org.onosproject.net.PortNumber.portNumber;
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070061import static org.slf4j.LoggerFactory.getLogger;
Michele Santuari21c14012016-11-14 13:31:33 +010062
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -070063// Ref: Junos YANG:
64// https://github.com/Juniper/yang
Michele Santuarice256492017-06-23 14:44:01 +020065
Michele Santuari21c14012016-11-14 13:31:33 +010066/**
67 * Utility class for Netconf XML for Juniper.
68 * Tested with MX240 junos 14.2
69 */
70public final class JuniperUtils {
71
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070072 private static final Logger log = getLogger(JuniperUtils.class);
73
Michele Santuari21c14012016-11-14 13:31:33 +010074 public static final String FAILED_CFG = "Failed to retrieve configuration.";
75
76 private static final String RPC_TAG_NETCONF_BASE = "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">";
77 private static final String RPC_CLOSE_TAG = "</rpc>";
78
79 //requests
80 public static final String REQ_LLDP_NBR_INFO = "<get-lldp-neighbors-information/>";
81 public static final String REQ_SYS_INFO = "<get-system-information/>";
82 public static final String REQ_MAC_ADD_INFO = "<get-chassis-mac-addresses/>";
83 public static final String REQ_IF_INFO = "<get-interface-information/>";
84
85 //helper strings for parsing
Michele Santuarif5945372017-01-19 16:39:21 +010086 private static final String LLDP_LIST_NBR_INFO = "lldp-neighbors-information";
87 private static final String LLDP_NBR_INFO = "lldp-neighbor-information";
Michele Santuari21c14012016-11-14 13:31:33 +010088 private static final String SYS_INFO = "system-information";
89 private static final String HW_MODEL = "hardware-model";
90 private static final String OS_NAME = "os-name";
91 private static final String OS_VER = "os-version";
92 private static final String SER_NUM = "serial-number";
93 private static final String IF_INFO = "interface-information";
94 private static final String IF_PHY = "physical-interface";
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -070095
Michele Santuari21c14012016-11-14 13:31:33 +010096 private static final String IF_TYPE = "if-type";
DongRyeol Chae23b18b2018-11-20 15:09:50 +090097 private static final String IF_MEDIA_TYPE = "if-media-type";
Michele Santuari21c14012016-11-14 13:31:33 +010098 private static final String SPEED = "speed";
Michele Santuari21c14012016-11-14 13:31:33 +010099 private static final String NAME = "name";
DongRyeol Cha5f870032018-07-12 18:36:20 +0900100 private static final String PORT = "port";
101 private static final String PROTOCOL = "protocol";
102
DongRyeol Chae23b18b2018-11-20 15:09:50 +0900103 private static final String FIBER = "fiber";
104 private static final String COPPER = "copper";
105
DongRyeol Cha5f870032018-07-12 18:36:20 +0900106 private static final String TCP = "tcp";
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700107
108 // seems to be unique index within device
Michele Santuari21c14012016-11-14 13:31:33 +0100109 private static final String SNMP_INDEX = "snmp-index";
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700110
Michele Santuari21c14012016-11-14 13:31:33 +0100111 private static final String LLDP_LO_PORT = "lldp-local-port-id";
112 private static final String LLDP_REM_CHASS = "lldp-remote-chassis-id";
donghyeok.hoe6d71872018-07-25 18:57:44 +0900113 private static final String LLDP_REM_PORT_SUBTYPE = "lldp-remote-port-id-subtype";
Michele Santuari21c14012016-11-14 13:31:33 +0100114 private static final String LLDP_REM_PORT = "lldp-remote-port-id";
donghyeok.ho82749aa2018-07-16 19:57:03 +0900115 private static final String LLDP_REM_PORT_DES = "lldp-remote-port-description";
donghyeok.hoe6d71872018-07-25 18:57:44 +0900116 private static final String LLDP_SUBTYPE_MAC = "Mac address";
117 private static final String LLDP_SUBTYPE_INTERFACE_NAME = "Interface name";
Kieran McPeakebc00cb52019-05-23 13:07:25 +0100118 // For older JUNOS e.g. 15.1
119 private static final Pattern ADD_PATTERN_JUNOS15_1 =
120 Pattern.compile(".*Private base address\\s*([:,0-9,a-f,A-F]*).*", Pattern.DOTALL);
121
Michele Santuari21c14012016-11-14 13:31:33 +0100122
Michele Santuarice256492017-06-23 14:44:01 +0200123 public static final String PROTOCOL_NAME = "protocol-name";
124
Michele Santuari21c14012016-11-14 13:31:33 +0100125 private static final String JUNIPER = "JUNIPER";
126 private static final String UNKNOWN = "UNKNOWN";
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700127
128 /**
129 * Annotation key for interface type.
130 */
131 static final String AK_IF_TYPE = "ifType";
132
133 /**
134 * Annotation key for Logical link-layer encapsulation.
135 */
136 static final String AK_ENCAPSULATION = "encapsulation";
137
138 /**
139 * Annotation key for interface description.
140 */
141 static final String AK_DESCRIPTION = "description";
142
143 /**
144 * Annotation key for interface admin status. "up"/"down"
145 */
146 static final String AK_ADMIN_STATUS = "adminStatus";
147
148 /**
149 * Annotation key for interface operational status. "up"/"down"
150 */
151 static final String AK_OPER_STATUS = "operStatus";
152
153 /**
154 * Annotation key for logical-interfaces parent physical interface name.
155 */
156 static final String AK_PHYSICAL_PORT_NAME = "physicalPortName";
157
158
159 private static final String NUMERIC_SPEED_REGEXP = "(\\d+)([GM])bps";
160
161 /**
162 * {@value #NUMERIC_SPEED_REGEXP} as {@link Pattern}.
163 * Case insensitive
164 */
165 private static final Pattern SPEED_PATTERN =
166 Pattern.compile(NUMERIC_SPEED_REGEXP, Pattern.CASE_INSENSITIVE);
167
168 /**
169 * Default port speed {@value} Mbps.
170 */
Michele Santuari21c14012016-11-14 13:31:33 +0100171 private static final long DEFAULT_PORT_SPEED = 1000;
172
173
174 private JuniperUtils() {
175 //not called, preventing any allocation
176 }
177
178 /**
179 * Helper method to build a XML schema given a request.
180 *
181 * @param request a tag element of the XML schema
182 * @return string containing the XML schema
183 */
184 public static String requestBuilder(String request) {
185 return RPC_TAG_NETCONF_BASE +
186 request + RPC_CLOSE_TAG;
187 }
188
189 /**
Michele Santuarice256492017-06-23 14:44:01 +0200190 * Helper method to commit a config.
191 *
192 * @return string contains the result of the commit
193 */
194 public static String commitBuilder() {
195 return RPC_TAG_NETCONF_BASE +
196 "<commit/>" + RPC_CLOSE_TAG;
197 }
198
199 /**
200 * Helper method to build the schema for returning to a previously
201 * committed configuration.
202 *
203 * @param versionToReturn Configuration to return to. The range of values is from 0 through 49.
204 * The most recently saved configuration is number 0,
205 * and the oldest saved configuration is number 49.
206 * @return string containing the XML schema
207 */
208 public static String rollbackBuilder(int versionToReturn) {
209 return RPC_TAG_NETCONF_BASE +
210 "<get-rollback-information>" +
211 "<rollback>" + versionToReturn + "</rollback>" +
212 "</get-rollback-information>" +
213 RPC_CLOSE_TAG;
214 }
215
216
217 /**
218 * Helper method to build an XML schema to configure a static route
219 * given a {@link StaticRoute}.
220 *
221 * @param staticRoute the static route to be configured
222 * @return string contains the result of the configuration
223 */
224 public static String routeAddBuilder(StaticRoute staticRoute) {
225 StringBuilder rpc = new StringBuilder("<configuration>\n");
226 rpc.append("<routing-options>\n");
227 rpc.append("<static>\n");
228 rpc.append("<route>\n");
229 rpc.append("<destination>" + staticRoute.ipv4Dst().toString() + "</destination>\n");
230 rpc.append("<next-hop>" + staticRoute.nextHop() + "</next-hop>\n");
231
232 if (staticRoute.getMetric() != DEFAULT_METRIC_STATIC_ROUTE) {
233 rpc.append("<metric>" + staticRoute.getMetric() + "</metric>");
234 }
235
236 rpc.append("</route>\n");
237 rpc.append("</static>\n");
238 rpc.append("</routing-options>\n");
239 rpc.append("</configuration>\n");
240
241 return rpc.toString();
242 }
243
244 /**
245 * Helper method to build a XML schema to delete a static route
246 * given a {@link StaticRoute}.
247 * @param staticRoute the static route to be deleted
248 * @return string contains the result of the configuratio
249 */
250 public static String routeDeleteBuilder(StaticRoute staticRoute) {
251 return "<configuration>\n" +
252 "<routing-options>\n" +
253 "<static>\n" +
254 "<route operation=\"delete\">\n" +
255 "<name>" + staticRoute.ipv4Dst().toString() + "</name>\n" +
256 "</route>\n" +
257 "</static>\n" +
258 "</routing-options>\n" +
259 "</configuration>\n";
260 }
261
262
263 /**
Michele Santuari21c14012016-11-14 13:31:33 +0100264 * Parses device configuration and returns the device description.
265 *
266 * @param deviceId the id of the device
267 * @param sysInfoCfg system configuration
Kieran McPeakebc00cb52019-05-23 13:07:25 +0100268 * @param chassisMacAddresses chassis MAC addresses response. Its format depends on JUNOS version of device.
Michele Santuari21c14012016-11-14 13:31:33 +0100269 * @return device description
270 */
271 public static DeviceDescription parseJuniperDescription(DeviceId deviceId,
272 HierarchicalConfiguration sysInfoCfg,
Kieran McPeakebc00cb52019-05-23 13:07:25 +0100273 String chassisMacAddresses) {
Michele Santuari21c14012016-11-14 13:31:33 +0100274 HierarchicalConfiguration info = sysInfoCfg.configurationAt(SYS_INFO);
275
276 String hw = info.getString(HW_MODEL) == null ? UNKNOWN : info.getString(HW_MODEL);
277 String sw = UNKNOWN;
278 if (info.getString(OS_NAME) != null || info.getString(OS_VER) != null) {
279 sw = info.getString(OS_NAME) + " " + info.getString(OS_VER);
280 }
281 String serial = info.getString(SER_NUM) == null ? UNKNOWN : info.getString(SER_NUM);
282
Michele Santuari21c14012016-11-14 13:31:33 +0100283 return new DefaultDeviceDescription(deviceId.uri(), ROUTER,
Kieran McPeakebc00cb52019-05-23 13:07:25 +0100284 JUNIPER, hw, sw, serial,
285 extractChassisId(chassisMacAddresses),
286 DefaultAnnotations.EMPTY);
287 }
288
289 /**
290 * Parses the chassisMacAddresses argument to find the private-base-address and maps it to a chassis id.
291 * @param chassisMacAddresses XML response
292 * @return the corresponding chassisId, or null if supplied chassisMacAddresses could not be parsed.
293 */
294 private static ChassisId extractChassisId(final String chassisMacAddresses) {
295
296 ChassisId result = null;
297
298 // Old JUNOS versions used CLI-style text for chassisMacAddresses, whereas recent versions provide a
299 // chassis-mac-addresses XML.
300 Matcher matcher = ADD_PATTERN_JUNOS15_1.matcher(chassisMacAddresses);
301 if (matcher.lookingAt()) {
302 result = new ChassisId(MacAddress.valueOf(matcher.group(1)).toLong());
303 } else {
304 String pba = loadXmlString(chassisMacAddresses)
305 .configurationAt("chassis-mac-addresses")
306 .configurationAt("mac-address-information")
307 .getString("private-base-address");
308 if (StringUtils.isNotBlank(pba)) {
309 result = new ChassisId(MacAddress.valueOf(pba).toLong());
310 }
311 }
312 return result;
Michele Santuari21c14012016-11-14 13:31:33 +0100313 }
314
315 /**
316 * Parses device ports configuration and returns a list of
317 * port description.
318 *
319 * @param cfg interface configuration
320 * @return list of interface descriptions of the device
321 */
322 public static List<PortDescription> parseJuniperPorts(HierarchicalConfiguration cfg) {
323 //This methods ignores some internal ports
324
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700325 List<PortDescription> portDescriptions = new ArrayList<>();
Michele Santuari21c14012016-11-14 13:31:33 +0100326 List<HierarchicalConfiguration> subtrees =
327 cfg.configurationsAt(IF_INFO);
328 for (HierarchicalConfiguration interfInfo : subtrees) {
329 List<HierarchicalConfiguration> interfaceTree =
330 interfInfo.configurationsAt(IF_PHY);
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700331 for (HierarchicalConfiguration phyIntf : interfaceTree) {
332 if (phyIntf == null) {
333 continue;
Michele Santuari21c14012016-11-14 13:31:33 +0100334 }
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700335 // parse physical Interface
336 parsePhysicalInterface(portDescriptions, phyIntf);
Michele Santuari21c14012016-11-14 13:31:33 +0100337 }
338 }
339 return portDescriptions;
340 }
341
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700342 /**
343 * Parses {@literal physical-interface} tree.
344 *
345 * @param portDescriptions list to populate Ports found parsing configuration
346 * @param phyIntf physical-interface
347 */
348 private static void parsePhysicalInterface(List<PortDescription> portDescriptions,
349 HierarchicalConfiguration phyIntf) {
350 Builder annotations = DefaultAnnotations.builder();
351 PortNumber portNumber = portNumber(phyIntf.getString(SNMP_INDEX));
352 String phyPortName = phyIntf.getString(NAME);
353 if (portNumber == null) {
354 log.debug("Skipping physical-interface {}, no PortNumer",
355 phyPortName);
356 log.trace(" {}", phyIntf);
357 return;
Michele Santuari21c14012016-11-14 13:31:33 +0100358 }
359
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700360 setIfNonNull(annotations,
361 AnnotationKeys.PORT_NAME,
362 phyPortName);
Michele Santuari21c14012016-11-14 13:31:33 +0100363
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700364 setIfNonNull(annotations,
365 AnnotationKeys.PORT_MAC,
366 phyIntf.getString("current-physical-address"));
Michele Santuari21c14012016-11-14 13:31:33 +0100367
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700368 setIfNonNull(annotations,
369 AK_IF_TYPE,
370 phyIntf.getString(IF_TYPE));
Michele Santuari21c14012016-11-14 13:31:33 +0100371
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700372 setIfNonNull(annotations,
373 AK_DESCRIPTION,
374 phyIntf.getString("description"));
Michele Santuari21c14012016-11-14 13:31:33 +0100375
Michele Santuari0b832652017-06-05 16:59:11 +0200376 boolean opUp = phyIntf.getString("oper-status", "down").equals("up");
377 annotations.set(AK_OPER_STATUS, toUpDown(opUp));
Michele Santuari21c14012016-11-14 13:31:33 +0100378
Michele Santuari0b832652017-06-05 16:59:11 +0200379 boolean admUp = phyIntf.getString("admin-status", "down").equals("up");
380 annotations.set(AK_ADMIN_STATUS, toUpDown(admUp));
Michele Santuari21c14012016-11-14 13:31:33 +0100381
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700382 long portSpeed = toMbps(phyIntf.getString(SPEED));
DongRyeol Chae23b18b2018-11-20 15:09:50 +0900383 Type portType = phyIntf.getString(IF_MEDIA_TYPE, COPPER).equalsIgnoreCase(FIBER) ? Type.FIBER : Type.COPPER;
Michele Santuari21c14012016-11-14 13:31:33 +0100384
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800385 portDescriptions.add(DefaultPortDescription.builder()
386 .withPortNumber(portNumber)
387 .isEnabled(admUp && opUp)
DongRyeol Chae23b18b2018-11-20 15:09:50 +0900388 .type(portType)
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800389 .portSpeed(portSpeed)
390 .annotations(annotations.build()).build());
Michele Santuari21c14012016-11-14 13:31:33 +0100391
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700392 // parse each logical Interface
393 for (HierarchicalConfiguration logIntf : phyIntf.configurationsAt("logical-interface")) {
394 if (logIntf == null) {
395 continue;
396 }
397 PortNumber lPortNumber = safePortNumber(logIntf.getString(SNMP_INDEX));
398 if (lPortNumber == null) {
399 log.debug("Skipping logical-interface {} under {}, no PortNumer",
400 logIntf.getString(NAME), phyPortName);
401 log.trace(" {}", logIntf);
402 continue;
403 }
404
405 Builder lannotations = DefaultAnnotations.builder();
406 setIfNonNull(lannotations,
407 AnnotationKeys.PORT_NAME,
408 logIntf.getString(NAME));
409 setIfNonNull(lannotations,
410 AK_PHYSICAL_PORT_NAME,
411 phyPortName);
412
413 String afName = logIntf.getString("address-family.address-family-name");
414 String address = logIntf.getString("address-family.interface-address.ifa-local");
415 if (afName != null && address != null) {
416 // e.g., inet : IPV4, inet6 : IPV6
417 setIfNonNull(lannotations, afName, address);
418 }
419
420 // preserving former behavior
421 setIfNonNull(lannotations,
422 "ip",
423 logIntf.getString("address-family.interface-address.ifa-local"));
424
425 setIfNonNull(lannotations,
426 AK_ENCAPSULATION, logIntf.getString("encapsulation"));
427
428 // TODO confirm if this is correct.
429 // Looking at sample data,
430 // it seemed all logical loop-back interfaces were down
431 boolean lEnabled = logIntf.getString("if-config-flags.iff-up") != null;
432
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800433 portDescriptions.add(DefaultPortDescription.builder()
434 .withPortNumber(lPortNumber)
435 .isEnabled(admUp && opUp && lEnabled)
DongRyeol Chae23b18b2018-11-20 15:09:50 +0900436 .type(portType)
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800437 .portSpeed(portSpeed).annotations(lannotations.build())
438 .build());
Michele Santuari21c14012016-11-14 13:31:33 +0100439 }
Michele Santuari21c14012016-11-14 13:31:33 +0100440 }
441
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700442 /**
443 * Port status as "up"/"down".
444 *
445 * @param portStatus port status
446 * @return "up" if {@code portStats} is {@literal true}, "down" otherwise
447 */
448 static String toUpDown(boolean portStatus) {
449 return portStatus ? "up" : "down";
450 }
451
452 /**
453 * Translate interface {@literal speed} value as Mbps value.
454 *
455 * Note: {@literal Unlimited} and unrecognizable string will be treated as
456 * {@value #DEFAULT_PORT_SPEED} Mbps.
457 *
458 * @param speed in String
459 * @return Mbps
460 */
461 static long toMbps(String speed) {
462 String s = Strings.nullToEmpty(speed).trim().toLowerCase();
463 Matcher matcher = SPEED_PATTERN.matcher(s);
464 if (matcher.matches()) {
465 // numeric
Ray Milkey3717e602018-02-01 13:49:47 -0800466 long n = Long.parseLong(matcher.group(1));
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700467 String unit = matcher.group(2);
468 if ("m".equalsIgnoreCase(unit)) {
469 // Mbps
470 return n;
471 } else {
472 // assume Gbps
473 return 1000 * n;
474 }
Michele Santuari21c14012016-11-14 13:31:33 +0100475 }
Yuta HIGUCHIebff1b52017-04-21 20:41:42 -0700476 log.trace("Treating unknown speed value {} as default", speed);
477 // Unlimited or unrecognizable
478 return DEFAULT_PORT_SPEED;
479 }
480
481 /**
482 * Sets annotation entry if {@literal value} was not {@literal null}.
483 *
484 * @param builder Annotation Builder
485 * @param key Annotation key
486 * @param value Annotation value (can be {@literal null})
487 */
488 static void setIfNonNull(Builder builder, String key, String value) {
489 if (value != null) {
490 builder.set(key, value.trim());
491 }
492 }
493
494 /**
495 * Creates PortNumber instance from String.
496 *
497 * Instead for throwing Exception, it will return null on format error.
498 *
499 * @param s port number as string
500 * @return PortNumber instance or null on error
501 */
502 static PortNumber safePortNumber(String s) {
503 try {
504 return portNumber(s);
505 } catch (RuntimeException e) {
506 log.trace("Failed parsing PortNumber {}", s, e);
507 }
508 return null;
Michele Santuari21c14012016-11-14 13:31:33 +0100509 }
510
511 /**
512 * Create two LinkDescriptions corresponding to the bidirectional links.
513 *
514 * @param localDevId the identity of the local device
515 * @param localPort the port of the local device
516 * @param remoteDevId the identity of the remote device
517 * @param remotePort the port of the remote device
518 * @param descs the collection to which the link descriptions
519 * should be added
520 */
521 public static void createBiDirLinkDescription(DeviceId localDevId,
522 Port localPort,
523 DeviceId remoteDevId,
524 Port remotePort,
525 Set<LinkDescription> descs) {
526
527 ConnectPoint local = new ConnectPoint(localDevId, localPort.number());
528 ConnectPoint remote = new ConnectPoint(remoteDevId, remotePort.number());
529 DefaultAnnotations annotations = DefaultAnnotations.builder()
DongRyeol Chace65cc02018-07-23 15:02:28 +0900530 .set(AnnotationKeys.LAYER, "ETHERNET")
Michele Santuari21c14012016-11-14 13:31:33 +0100531 .build();
532 descs.add(new DefaultLinkDescription(
DongRyeol Chace65cc02018-07-23 15:02:28 +0900533 local, remote, Link.Type.DIRECT, true, annotations));
Michele Santuari21c14012016-11-14 13:31:33 +0100534 descs.add(new DefaultLinkDescription(
DongRyeol Chace65cc02018-07-23 15:02:28 +0900535 remote, local, Link.Type.DIRECT, true, annotations));
536 }
537
538 /**
539 * Create one way LinkDescriptions.
540 *
541 * @param localDevId the identity of the local device
542 * @param localPort the port of the local device
543 * @param remoteDevId the identity of the remote device
544 * @param remotePort the port of the remote device
545 * @param descs the collection to which the link descriptions
546 * should be added
547 */
548 public static void createOneWayLinkDescription(DeviceId localDevId,
549 Port localPort,
550 DeviceId remoteDevId,
551 Port remotePort,
552 Set<LinkDescription> descs) {
553
554 ConnectPoint local = new ConnectPoint(localDevId, localPort.number());
555 ConnectPoint remote = new ConnectPoint(remoteDevId, remotePort.number());
556 DefaultAnnotations annotations = DefaultAnnotations.builder()
557 .set(AnnotationKeys.LAYER, "ETHERNET")
558 .build();
559 descs.add(new DefaultLinkDescription(
560 remote, local, Link.Type.DIRECT, true, annotations));
Michele Santuari21c14012016-11-14 13:31:33 +0100561 }
562
563 /**
564 * Parses neighbours discovery information and returns a list of
565 * link abstractions.
566 *
567 * @param info interface configuration
568 * @return set of link abstractions
569 */
570 public static Set<LinkAbstraction> parseJuniperLldp(HierarchicalConfiguration info) {
571 Set<LinkAbstraction> neighbour = new HashSet<>();
572 List<HierarchicalConfiguration> subtrees =
Michele Santuarif5945372017-01-19 16:39:21 +0100573 info.configurationsAt(LLDP_LIST_NBR_INFO);
Michele Santuari21c14012016-11-14 13:31:33 +0100574 for (HierarchicalConfiguration neighborsInfo : subtrees) {
575 List<HierarchicalConfiguration> neighbors =
576 neighborsInfo.configurationsAt(LLDP_NBR_INFO);
577 for (HierarchicalConfiguration neighbor : neighbors) {
578 String localPortName = neighbor.getString(LLDP_LO_PORT);
donghyeok.hoe6d71872018-07-25 18:57:44 +0900579 MacAddress mac = MacAddress.valueOf(neighbor.getString(LLDP_REM_CHASS));
580 String remotePortId = null;
581 long remotePortIndex = -1;
582 String remotePortIdSubtype = neighbor.getString(LLDP_REM_PORT_SUBTYPE, null);
583 if (remotePortIdSubtype != null) {
584 if (remotePortIdSubtype.equals(LLDP_SUBTYPE_MAC)
585 || remotePortIdSubtype.equals(LLDP_SUBTYPE_INTERFACE_NAME)) {
586 remotePortId = neighbor.getString(LLDP_REM_PORT, null);
587 } else {
588 remotePortIndex = neighbor.getLong(LLDP_REM_PORT, -1);
589 }
590 }
donghyeok.ho82749aa2018-07-16 19:57:03 +0900591 String remotePortDescription = neighbor.getString(LLDP_REM_PORT_DES, null);
Michele Santuari21c14012016-11-14 13:31:33 +0100592 LinkAbstraction link = new LinkAbstraction(
593 localPortName,
594 mac.toLong(),
donghyeok.ho82749aa2018-07-16 19:57:03 +0900595 remotePortIndex,
donghyeok.hoe6d71872018-07-25 18:57:44 +0900596 remotePortId,
donghyeok.ho82749aa2018-07-16 19:57:03 +0900597 remotePortDescription);
Michele Santuari21c14012016-11-14 13:31:33 +0100598 neighbour.add(link);
599 }
600 }
601 return neighbour;
602 }
603
604 /**
605 * Device representation of the adjacency at the IP Layer.
606 */
Ray Milkey9c9cde42018-01-12 14:22:06 -0800607 static final class LinkAbstraction {
Michele Santuari21c14012016-11-14 13:31:33 +0100608 protected String localPortName;
609 protected ChassisId remoteChassisId;
Michele Santuaria85bd3b2017-05-05 12:57:40 +0200610 protected long remotePortIndex;
donghyeok.hoe6d71872018-07-25 18:57:44 +0900611 protected String remotePortId;
donghyeok.ho82749aa2018-07-16 19:57:03 +0900612 protected String remotePortDescription;
Michele Santuari21c14012016-11-14 13:31:33 +0100613
donghyeok.hoe6d71872018-07-25 18:57:44 +0900614 protected LinkAbstraction(String pName, long chassisId, long pIndex, String pPortId, String pDescription) {
Michele Santuari21c14012016-11-14 13:31:33 +0100615 this.localPortName = pName;
616 this.remoteChassisId = new ChassisId(chassisId);
617 this.remotePortIndex = pIndex;
donghyeok.hoe6d71872018-07-25 18:57:44 +0900618 this.remotePortId = pPortId;
donghyeok.ho82749aa2018-07-16 19:57:03 +0900619 this.remotePortDescription = pDescription;
Michele Santuari21c14012016-11-14 13:31:33 +0100620 }
Kieran McPeakebc00cb52019-05-23 13:07:25 +0100621 @Override
622 public String toString() {
623 return MoreObjects.toStringHelper(getClass()).omitNullValues()
624 .add("localPortName", localPortName)
625 .add("remoteChassisId", remoteChassisId)
626 .add("remotePortIndex", remotePortIndex)
627 .add("remotePortId", remotePortId)
628 .add("remotePortDescription", remotePortDescription)
629 .toString();
630 }
Michele Santuari21c14012016-11-14 13:31:33 +0100631 }
Michele Santuarice256492017-06-23 14:44:01 +0200632
Ray Milkey9c9cde42018-01-12 14:22:06 -0800633 enum OperationType {
Michele Santuarice256492017-06-23 14:44:01 +0200634 ADD,
635 REMOVE,
636 }
637
638 /**
639 * Parses {@literal route-information} tree.
640 * This implementation supports only static routes.
641 *
642 * @param cfg route-information
643 * @return a collection of static routes
644 */
645 public static Collection<StaticRoute> parseRoutingTable(HierarchicalConfiguration cfg) {
646
647 Collection<StaticRoute> staticRoutes = new HashSet<>();
648 HierarchicalConfiguration routeInfo =
649 cfg.configurationAt("route-information");
650 List<HierarchicalConfiguration> routeTables = routeInfo.configurationsAt("route-table");
651 for (HierarchicalConfiguration routeTable : routeTables) {
652 List<HierarchicalConfiguration> routes = routeTable.configurationsAt("rt");
653 for (HierarchicalConfiguration route : routes) {
654 if (route != null) {
Andrea Campanella248cc242018-09-21 13:58:38 +0200655 List<HierarchicalConfiguration> rtEntries = route.configurationsAt("rt-entry");
656 rtEntries.forEach(rtEntry -> {
657 if (rtEntry.getString(PROTOCOL_NAME) != null &&
658 rtEntry.getString(PROTOCOL_NAME).contains("Static")) {
659 parseStaticRoute(rtEntry,
660 route.getString("rt-destination"),
661 rtEntry.getString("metric"))
662 .ifPresent(staticRoutes::add);
Michele Santuarice256492017-06-23 14:44:01 +0200663
Andrea Campanella248cc242018-09-21 13:58:38 +0200664 }
665 });
Michele Santuarice256492017-06-23 14:44:01 +0200666 }
667 }
668 }
669 return staticRoutes;
670 }
671
672 /**
673 * Parse the {@literal rt-entry} for static routes.
674 *
675 * @param rtEntry rt-entry filtered by {@literal protocol-name} equals to Static
676 * @param destination rt-destination
677 * @return optional of static route
678 */
679 private static Optional<StaticRoute> parseStaticRoute(HierarchicalConfiguration rtEntry,
680 String destination, String metric) {
681
682 Ip4Prefix ipDst = Ip4Prefix.valueOf(destination);
683
684 HierarchicalConfiguration nextHop = rtEntry.configurationAt("nh");
685 String to = nextHop.getString("to");
686 if (StringUtils.isEmpty(to)) {
687 return Optional.empty();
688 }
689 Ip4Address nextHopIp = Ip4Address.valueOf(to);
690
691 if (metric == null) {
692 return Optional.of(new StaticRoute(ipDst, nextHopIp, false));
693 } else {
694 return Optional.of(new StaticRoute(ipDst, nextHopIp, false,
695 toFlowRulePriority(Integer.parseInt(metric))));
696 }
697 }
DongRyeol Cha5f870032018-07-12 18:36:20 +0900698
699 /**
700 * Helper method to build a XML schema for a given "set/merge" Op inJunOS XML Format.
701 *
702 * @param request a CLI command
703 * @return string containing the XML schema
704 */
705 public static String cliSetRequestBuilder(StringBuilder request) {
706 StringBuilder rpc = new StringBuilder(RPC_TAG_NETCONF_BASE);
707 rpc.append("<edit-config>");
708 rpc.append("<target><candidate/></target><config>");
709 rpc.append("<configuration>");
710 rpc.append(request);
711 rpc.append("</configuration>");
712 rpc.append("</config></edit-config>");
713 rpc.append(RPC_CLOSE_TAG);
714 rpc.append("]]>]]>");
715 return rpc.toString();
716 }
717
718 /**
719 * Helper method to build a XML schema for a given "delete Op" in JunOS XML Format.
720 *
721 * @param request a CLI command
722 * @return string containing the XML schema
723 */
724 public static String cliDeleteRequestBuilder(StringBuilder request) {
725 StringBuilder rpc = new StringBuilder(RPC_TAG_NETCONF_BASE);
726 rpc.append("<edit-config>");
727 rpc.append("<target><candidate/></target>");
728 rpc.append("<default-operation>none</default-operation>");
729 rpc.append("<config>");
730 rpc.append("<configuration>");
731 rpc.append(request);
732 rpc.append("</configuration>");
733 rpc.append("</config></edit-config>");
734 rpc.append(RPC_CLOSE_TAG);
735 rpc.append("]]>]]>");
736 return rpc.toString();
737 }
738
739 public static List<ControllerInfo> getOpenFlowControllersFromConfig(HierarchicalConfiguration cfg) {
740 List<ControllerInfo> controllers = new ArrayList<ControllerInfo>();
741 String ipKey = "configuration.protocols.openflow.mode.ofagent-mode.controller.ip";
742
743 if (!cfg.configurationsAt(ipKey).isEmpty()) {
744 List<HierarchicalConfiguration> ipNodes = cfg.configurationsAt(ipKey);
745
746 ipNodes.forEach(ipNode -> {
747 int port = 0;
748 String proto = UNKNOWN;
749 HierarchicalConfiguration protocolNode = ipNode.configurationAt(PROTOCOL);
750 HierarchicalConfiguration tcpNode = protocolNode.configurationAt(TCP);
751
752 if (!tcpNode.isEmpty()) {
753 String portString = tcpNode.getString(PORT);
754
755 if (portString != null && !portString.isEmpty()) {
756 port = Integer.parseInt(portString);
757 }
758
759 proto = TCP;
760 }
761
762 String ipaddress = ipNode.getString(NAME);
763
764 if (ipaddress == null) {
765 ipaddress = UNKNOWN;
766 }
767
768 if (ipaddress.equals(UNKNOWN) || proto.equals(UNKNOWN) || port == 0) {
769 log.error("Controller infomation is invalid. Skip this controller node." +
770 " ipaddress: {}, proto: {}, port: {}", ipaddress, proto, port);
771 return;
772 }
773
774 controllers.add(new ControllerInfo(IpAddress.valueOf(ipaddress), port, proto));
775 });
776 } else {
777 log.error("Controller not present");
778 }
779
780 return controllers;
781 }
Michele Santuari21c14012016-11-14 13:31:33 +0100782}