Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 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.juniper; |
| 18 | |
Kieran McPeake | bc00cb5 | 2019-05-23 13:07:25 +0100 | [diff] [blame^] | 19 | import com.google.common.base.MoreObjects; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 20 | import org.apache.commons.configuration.HierarchicalConfiguration; |
Michele Santuari | ce25649 | 2017-06-23 14:44:01 +0200 | [diff] [blame] | 21 | import org.apache.commons.lang.StringUtils; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 22 | import org.onlab.packet.ChassisId; |
Michele Santuari | ce25649 | 2017-06-23 14:44:01 +0200 | [diff] [blame] | 23 | import org.onlab.packet.Ip4Address; |
| 24 | import org.onlab.packet.Ip4Prefix; |
DongRyeol Cha | 5f87003 | 2018-07-12 18:36:20 +0900 | [diff] [blame] | 25 | import org.onlab.packet.IpAddress; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onosproject.net.AnnotationKeys; |
| 28 | import org.onosproject.net.ConnectPoint; |
| 29 | import org.onosproject.net.DefaultAnnotations; |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 30 | import org.onosproject.net.DefaultAnnotations.Builder; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.Link; |
| 33 | import org.onosproject.net.Port; |
| 34 | import org.onosproject.net.PortNumber; |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 35 | import org.onosproject.net.Port.Type; |
DongRyeol Cha | 5f87003 | 2018-07-12 18:36:20 +0900 | [diff] [blame] | 36 | import org.onosproject.net.behaviour.ControllerInfo; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 37 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 38 | import org.onosproject.net.device.DefaultPortDescription; |
| 39 | import org.onosproject.net.device.DeviceDescription; |
| 40 | import org.onosproject.net.device.PortDescription; |
| 41 | import org.onosproject.net.link.DefaultLinkDescription; |
| 42 | import org.onosproject.net.link.LinkDescription; |
Yuta HIGUCHI | 0184a7b | 2017-03-31 13:13:58 -0700 | [diff] [blame] | 43 | import org.slf4j.Logger; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 44 | |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 45 | import com.google.common.base.Strings; |
| 46 | |
| 47 | import java.util.ArrayList; |
Michele Santuari | ce25649 | 2017-06-23 14:44:01 +0200 | [diff] [blame] | 48 | import java.util.Collection; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 49 | import java.util.HashSet; |
| 50 | import java.util.List; |
Michele Santuari | ce25649 | 2017-06-23 14:44:01 +0200 | [diff] [blame] | 51 | import java.util.Optional; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 52 | import java.util.Set; |
| 53 | import java.util.regex.Matcher; |
| 54 | import java.util.regex.Pattern; |
| 55 | |
Michele Santuari | ce25649 | 2017-06-23 14:44:01 +0200 | [diff] [blame] | 56 | import static org.onosproject.drivers.juniper.StaticRoute.DEFAULT_METRIC_STATIC_ROUTE; |
| 57 | import static org.onosproject.drivers.juniper.StaticRoute.toFlowRulePriority; |
Kieran McPeake | bc00cb5 | 2019-05-23 13:07:25 +0100 | [diff] [blame^] | 58 | import static org.onosproject.drivers.utilities.XmlConfigParser.loadXmlString; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 59 | import static org.onosproject.net.Device.Type.ROUTER; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 60 | import static org.onosproject.net.PortNumber.portNumber; |
Yuta HIGUCHI | 0184a7b | 2017-03-31 13:13:58 -0700 | [diff] [blame] | 61 | import static org.slf4j.LoggerFactory.getLogger; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 62 | |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 63 | // Ref: Junos YANG: |
| 64 | // https://github.com/Juniper/yang |
Michele Santuari | ce25649 | 2017-06-23 14:44:01 +0200 | [diff] [blame] | 65 | |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 66 | /** |
| 67 | * Utility class for Netconf XML for Juniper. |
| 68 | * Tested with MX240 junos 14.2 |
| 69 | */ |
| 70 | public final class JuniperUtils { |
| 71 | |
Yuta HIGUCHI | 0184a7b | 2017-03-31 13:13:58 -0700 | [diff] [blame] | 72 | private static final Logger log = getLogger(JuniperUtils.class); |
| 73 | |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 74 | 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 Santuari | f594537 | 2017-01-19 16:39:21 +0100 | [diff] [blame] | 86 | private static final String LLDP_LIST_NBR_INFO = "lldp-neighbors-information"; |
| 87 | private static final String LLDP_NBR_INFO = "lldp-neighbor-information"; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 88 | 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 HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 95 | |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 96 | private static final String IF_TYPE = "if-type"; |
DongRyeol Cha | e23b18b | 2018-11-20 15:09:50 +0900 | [diff] [blame] | 97 | private static final String IF_MEDIA_TYPE = "if-media-type"; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 98 | private static final String SPEED = "speed"; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 99 | private static final String NAME = "name"; |
DongRyeol Cha | 5f87003 | 2018-07-12 18:36:20 +0900 | [diff] [blame] | 100 | private static final String PORT = "port"; |
| 101 | private static final String PROTOCOL = "protocol"; |
| 102 | |
DongRyeol Cha | e23b18b | 2018-11-20 15:09:50 +0900 | [diff] [blame] | 103 | private static final String FIBER = "fiber"; |
| 104 | private static final String COPPER = "copper"; |
| 105 | |
DongRyeol Cha | 5f87003 | 2018-07-12 18:36:20 +0900 | [diff] [blame] | 106 | private static final String TCP = "tcp"; |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 107 | |
| 108 | // seems to be unique index within device |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 109 | private static final String SNMP_INDEX = "snmp-index"; |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 110 | |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 111 | private static final String LLDP_LO_PORT = "lldp-local-port-id"; |
| 112 | private static final String LLDP_REM_CHASS = "lldp-remote-chassis-id"; |
donghyeok.ho | e6d7187 | 2018-07-25 18:57:44 +0900 | [diff] [blame] | 113 | private static final String LLDP_REM_PORT_SUBTYPE = "lldp-remote-port-id-subtype"; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 114 | private static final String LLDP_REM_PORT = "lldp-remote-port-id"; |
donghyeok.ho | 82749aa | 2018-07-16 19:57:03 +0900 | [diff] [blame] | 115 | private static final String LLDP_REM_PORT_DES = "lldp-remote-port-description"; |
donghyeok.ho | e6d7187 | 2018-07-25 18:57:44 +0900 | [diff] [blame] | 116 | private static final String LLDP_SUBTYPE_MAC = "Mac address"; |
| 117 | private static final String LLDP_SUBTYPE_INTERFACE_NAME = "Interface name"; |
Kieran McPeake | bc00cb5 | 2019-05-23 13:07:25 +0100 | [diff] [blame^] | 118 | // 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 Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 122 | |
Michele Santuari | ce25649 | 2017-06-23 14:44:01 +0200 | [diff] [blame] | 123 | public static final String PROTOCOL_NAME = "protocol-name"; |
| 124 | |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 125 | private static final String JUNIPER = "JUNIPER"; |
| 126 | private static final String UNKNOWN = "UNKNOWN"; |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 127 | |
| 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 Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 171 | 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 Santuari | ce25649 | 2017-06-23 14:44:01 +0200 | [diff] [blame] | 190 | * 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 Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 264 | * Parses device configuration and returns the device description. |
| 265 | * |
| 266 | * @param deviceId the id of the device |
| 267 | * @param sysInfoCfg system configuration |
Kieran McPeake | bc00cb5 | 2019-05-23 13:07:25 +0100 | [diff] [blame^] | 268 | * @param chassisMacAddresses chassis MAC addresses response. Its format depends on JUNOS version of device. |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 269 | * @return device description |
| 270 | */ |
| 271 | public static DeviceDescription parseJuniperDescription(DeviceId deviceId, |
| 272 | HierarchicalConfiguration sysInfoCfg, |
Kieran McPeake | bc00cb5 | 2019-05-23 13:07:25 +0100 | [diff] [blame^] | 273 | String chassisMacAddresses) { |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 274 | 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 Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 283 | return new DefaultDeviceDescription(deviceId.uri(), ROUTER, |
Kieran McPeake | bc00cb5 | 2019-05-23 13:07:25 +0100 | [diff] [blame^] | 284 | 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 Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 313 | } |
| 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 HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 325 | List<PortDescription> portDescriptions = new ArrayList<>(); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 326 | List<HierarchicalConfiguration> subtrees = |
| 327 | cfg.configurationsAt(IF_INFO); |
| 328 | for (HierarchicalConfiguration interfInfo : subtrees) { |
| 329 | List<HierarchicalConfiguration> interfaceTree = |
| 330 | interfInfo.configurationsAt(IF_PHY); |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 331 | for (HierarchicalConfiguration phyIntf : interfaceTree) { |
| 332 | if (phyIntf == null) { |
| 333 | continue; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 334 | } |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 335 | // parse physical Interface |
| 336 | parsePhysicalInterface(portDescriptions, phyIntf); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | return portDescriptions; |
| 340 | } |
| 341 | |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 342 | /** |
| 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 Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 360 | setIfNonNull(annotations, |
| 361 | AnnotationKeys.PORT_NAME, |
| 362 | phyPortName); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 363 | |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 364 | setIfNonNull(annotations, |
| 365 | AnnotationKeys.PORT_MAC, |
| 366 | phyIntf.getString("current-physical-address")); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 367 | |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 368 | setIfNonNull(annotations, |
| 369 | AK_IF_TYPE, |
| 370 | phyIntf.getString(IF_TYPE)); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 371 | |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 372 | setIfNonNull(annotations, |
| 373 | AK_DESCRIPTION, |
| 374 | phyIntf.getString("description")); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 375 | |
Michele Santuari | 0b83265 | 2017-06-05 16:59:11 +0200 | [diff] [blame] | 376 | boolean opUp = phyIntf.getString("oper-status", "down").equals("up"); |
| 377 | annotations.set(AK_OPER_STATUS, toUpDown(opUp)); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 378 | |
Michele Santuari | 0b83265 | 2017-06-05 16:59:11 +0200 | [diff] [blame] | 379 | boolean admUp = phyIntf.getString("admin-status", "down").equals("up"); |
| 380 | annotations.set(AK_ADMIN_STATUS, toUpDown(admUp)); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 381 | |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 382 | long portSpeed = toMbps(phyIntf.getString(SPEED)); |
DongRyeol Cha | e23b18b | 2018-11-20 15:09:50 +0900 | [diff] [blame] | 383 | Type portType = phyIntf.getString(IF_MEDIA_TYPE, COPPER).equalsIgnoreCase(FIBER) ? Type.FIBER : Type.COPPER; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 384 | |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 385 | portDescriptions.add(DefaultPortDescription.builder() |
| 386 | .withPortNumber(portNumber) |
| 387 | .isEnabled(admUp && opUp) |
DongRyeol Cha | e23b18b | 2018-11-20 15:09:50 +0900 | [diff] [blame] | 388 | .type(portType) |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 389 | .portSpeed(portSpeed) |
| 390 | .annotations(annotations.build()).build()); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 391 | |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 392 | // 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 HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 433 | portDescriptions.add(DefaultPortDescription.builder() |
| 434 | .withPortNumber(lPortNumber) |
| 435 | .isEnabled(admUp && opUp && lEnabled) |
DongRyeol Cha | e23b18b | 2018-11-20 15:09:50 +0900 | [diff] [blame] | 436 | .type(portType) |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 437 | .portSpeed(portSpeed).annotations(lannotations.build()) |
| 438 | .build()); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 439 | } |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 442 | /** |
| 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 Milkey | 3717e60 | 2018-02-01 13:49:47 -0800 | [diff] [blame] | 466 | long n = Long.parseLong(matcher.group(1)); |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 467 | 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 Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 475 | } |
Yuta HIGUCHI | ebff1b5 | 2017-04-21 20:41:42 -0700 | [diff] [blame] | 476 | 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 Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 509 | } |
| 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 Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 530 | .set(AnnotationKeys.LAYER, "ETHERNET") |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 531 | .build(); |
| 532 | descs.add(new DefaultLinkDescription( |
DongRyeol Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 533 | local, remote, Link.Type.DIRECT, true, annotations)); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 534 | descs.add(new DefaultLinkDescription( |
DongRyeol Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 535 | 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 Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 561 | } |
| 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 Santuari | f594537 | 2017-01-19 16:39:21 +0100 | [diff] [blame] | 573 | info.configurationsAt(LLDP_LIST_NBR_INFO); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 574 | 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.ho | e6d7187 | 2018-07-25 18:57:44 +0900 | [diff] [blame] | 579 | 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.ho | 82749aa | 2018-07-16 19:57:03 +0900 | [diff] [blame] | 591 | String remotePortDescription = neighbor.getString(LLDP_REM_PORT_DES, null); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 592 | LinkAbstraction link = new LinkAbstraction( |
| 593 | localPortName, |
| 594 | mac.toLong(), |
donghyeok.ho | 82749aa | 2018-07-16 19:57:03 +0900 | [diff] [blame] | 595 | remotePortIndex, |
donghyeok.ho | e6d7187 | 2018-07-25 18:57:44 +0900 | [diff] [blame] | 596 | remotePortId, |
donghyeok.ho | 82749aa | 2018-07-16 19:57:03 +0900 | [diff] [blame] | 597 | remotePortDescription); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 598 | neighbour.add(link); |
| 599 | } |
| 600 | } |
| 601 | return neighbour; |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * Device representation of the adjacency at the IP Layer. |
| 606 | */ |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 607 | static final class LinkAbstraction { |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 608 | protected String localPortName; |
| 609 | protected ChassisId remoteChassisId; |
Michele Santuari | a85bd3b | 2017-05-05 12:57:40 +0200 | [diff] [blame] | 610 | protected long remotePortIndex; |
donghyeok.ho | e6d7187 | 2018-07-25 18:57:44 +0900 | [diff] [blame] | 611 | protected String remotePortId; |
donghyeok.ho | 82749aa | 2018-07-16 19:57:03 +0900 | [diff] [blame] | 612 | protected String remotePortDescription; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 613 | |
donghyeok.ho | e6d7187 | 2018-07-25 18:57:44 +0900 | [diff] [blame] | 614 | protected LinkAbstraction(String pName, long chassisId, long pIndex, String pPortId, String pDescription) { |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 615 | this.localPortName = pName; |
| 616 | this.remoteChassisId = new ChassisId(chassisId); |
| 617 | this.remotePortIndex = pIndex; |
donghyeok.ho | e6d7187 | 2018-07-25 18:57:44 +0900 | [diff] [blame] | 618 | this.remotePortId = pPortId; |
donghyeok.ho | 82749aa | 2018-07-16 19:57:03 +0900 | [diff] [blame] | 619 | this.remotePortDescription = pDescription; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 620 | } |
Kieran McPeake | bc00cb5 | 2019-05-23 13:07:25 +0100 | [diff] [blame^] | 621 | @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 Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 631 | } |
Michele Santuari | ce25649 | 2017-06-23 14:44:01 +0200 | [diff] [blame] | 632 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 633 | enum OperationType { |
Michele Santuari | ce25649 | 2017-06-23 14:44:01 +0200 | [diff] [blame] | 634 | 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 Campanella | 248cc24 | 2018-09-21 13:58:38 +0200 | [diff] [blame] | 655 | 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 Santuari | ce25649 | 2017-06-23 14:44:01 +0200 | [diff] [blame] | 663 | |
Andrea Campanella | 248cc24 | 2018-09-21 13:58:38 +0200 | [diff] [blame] | 664 | } |
| 665 | }); |
Michele Santuari | ce25649 | 2017-06-23 14:44:01 +0200 | [diff] [blame] | 666 | } |
| 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 Cha | 5f87003 | 2018-07-12 18:36:20 +0900 | [diff] [blame] | 698 | |
| 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 Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 782 | } |