Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request tumblr#93 from cburroughs/optional-vlan-name
Browse files Browse the repository at this point in the history
optionally make vlan name optional
  • Loading branch information
bmatheny committed Jun 7, 2013
2 parents 8f08030 + cc8960d commit 09e35b3
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 4 deletions.
13 changes: 13 additions & 0 deletions app/util/config/LldpConfig.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package util
package config

object LldpConfig extends Configurable {
override val namespace = "lldp"
override val referenceConfigFilename = "lldp_reference.conf"

def requireVlanName = getBoolean("requireVlanName", true)

override protected def validateConfig() {
requireVlanName
}
}
7 changes: 6 additions & 1 deletion app/util/parsers/LldpParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util
package parsers

import models.lldp._
import config.LldpConfig
import scala.xml.{Elem, MalformedAttributeException, Node, NodeSeq, XML}

class LldpParser(txt: String) extends CommonParser[LldpRepresentation](txt) {
Expand Down Expand Up @@ -61,7 +62,11 @@ class LldpParser(txt: String) extends CommonParser[LldpRepresentation](txt) {
(seq \\ "vlan").foldLeft(Seq[Vlan]()) { case(vseq, vlan) =>
val id = Option(vlan \ "@vlan-id" text).filter(_.nonEmpty).getOrElse("0")
val name = vlan.text
requireNonEmpty((id -> "vlan-id"), (name -> "vlan name"))
if (LldpConfig.requireVlanName) {
requireNonEmpty((id -> "vlan-id"), (name -> "vlan name"))
} else {
requireNonEmpty((id -> "vlan-id"))
}
Vlan(id.toInt, name) +: vseq
}
}
Expand Down
5 changes: 5 additions & 0 deletions conf/reference/lldp_reference.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
lldp {
# Refuse to accept lldp information with no name (aka
# description)
requireVlanName = true
}
1 change: 1 addition & 0 deletions conf/validations.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ config.validations = [
util.config.Feature,
util.config.IpmiConfig,
util.config.LshwConfig,
util.config.LldpConfig,
util.config.MultiCollinsConfig,
util.config.NodeclassifierConfig,
util.power.PowerConfiguration,
Expand Down
53 changes: 53 additions & 0 deletions test/resources/lldpctl-no-name.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<lldp label="LLDP neighbors">
<interface label="Interface" name="eth0" via="LLDP" rid="1" age="0 day, 00:00:44">
<chassis label="Chassis">
<id label="ChassisID" type="mac">00:00:00:00:00:00</id>
<name label="SysName">laxb-101</name>
<descr label="SysDescr">HP Switch </descr>
<capability label="Capability" type="Bridge" enabled="on"/>
<capability label="Capability" type="Router" enabled="off"/>
</chassis>
<port label="Port">
<id label="PortID" type="local">9</id>
<descr label="PortDescr">A9</descr>
<auto-negotiation label="PMD autoneg" supported="yes" enabled="yes">
<advertised label="Adv" type="10Base-T" hd="yes" fd="yes"/>
<advertised label="Adv" type="100Base-TX" hd="yes" fd="yes"/>
<advertised label="Adv" type="1000Base-T" hd="no" fd="yes"/>
<current label="MAU oper type">1000BaseTFD - Four-pair Category 5 UTP, full duplex mode</current>
</auto-negotiation>
<power label="MDI Power" supported="yes" enabled="yes" paircontrol="no">
<device-type label="Device type">PSE</device-type>
<pairs label="Power pairs">signal</pairs>
<class label="Class">class 0</class>
</power>
</port>
<vlan label="VLAN" vlan-id="100" pvid="yes"/>
</interface>
<interface label="Interface" name="eth1" via="LLDP" rid="1" age="0 day, 00:00:44">
<chassis label="Chassis">
<id label="ChassisID" type="mac">00:00:00:00:00:00</id>
<name label="SysName">laxb-101</name>
<descr label="SysDescr">HP Switch</descr>
<capability label="Capability" type="Bridge" enabled="on"/>
<capability label="Capability" type="Router" enabled="off"/>
</chassis>
<port label="Port">
<id label="PortID" type="local">65</id>
<descr label="PortDescr">C17</descr>
<auto-negotiation label="PMD autoneg" supported="yes" enabled="yes">
<advertised label="Adv" type="10Base-T" hd="yes" fd="yes"/>
<advertised label="Adv" type="100Base-TX" hd="yes" fd="yes"/>
<advertised label="Adv" type="1000Base-T" hd="no" fd="yes"/>
<current label="MAU oper type">1000BaseTFD - Four-pair Category 5 UTP, full duplex mode</current>
</auto-negotiation>
<power label="MDI Power" supported="yes" enabled="yes" paircontrol="no">
<device-type label="Device type">PSE</device-type>
<pairs label="Power pairs">signal</pairs>
<class label="Class">class 0</class>
</power>
</port>
<vlan label="VLAN" vlan-id="101" pvid="yes"/>
</interface>
</lldp>
2 changes: 2 additions & 0 deletions test/util/parsers/CommonParserSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
package parsers

import play.api.Configuration
import _root_.util.config.LldpConfig
import _root_.util.config.LshwConfig
import org.specs2._
import specification._
Expand All @@ -19,6 +20,7 @@ trait CommonParserSpec[REP] extends ResourceFinder {
_root_.util.config.AppConfig.globalConfig = None;
}
LshwConfig.initialize
LldpConfig.initialize
val parser = getParser(data)
parser.parse()
}
Expand Down
24 changes: 21 additions & 3 deletions test/util/parsers/LldpParserSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class LldpParserSpec extends mutable.Specification {

class LldpParserHelper(val filename: String) extends Scope with CommonParserSpec[LldpRepresentation] {
override def getParser(txt: String) = new LldpParser(txt)
def parsed() = getParseResults(filename)
def parsed(options: Map[String,String] = Map.empty) = getParseResults(filename, options)
}

"The Lldp Parser" should {
Expand Down Expand Up @@ -42,6 +42,24 @@ class LldpParserSpec extends mutable.Specification {
}
}

"missing vlan config fail" in new LldpParserHelper("lldpctl-no-name.xml") {
val parseResult = parsed()
parseResult must beLeft
}

"missing vlan config ok" in new LldpParserHelper("lldpctl-no-name.xml") {
val config = Map(
"lshw.requireVlanName" -> "false"
)
val parseResult = parsed(config)
parseResult must beRight
parseResult.right.toOption must beSome.which { rep =>
rep.interfaceCount mustEqual(2)
rep.vlanNames.toSet mustEqual(Set(""))
rep.vlanIds.toSet mustEqual(Set(100,101))
}
}

"Parse XML with four network interfaces" in new LldpParserHelper("lldpctl-four-nic.xml") {
val parseResult = parsed()
parseResult must beRight
Expand All @@ -59,11 +77,11 @@ class LldpParserSpec extends mutable.Specification {
}

"Parse a generated XML file" in new LldpParserHelper("lldpctl-empty.xml") {
parsed must beRight
parsed() must beRight
}

"Fail to parse wrong XML" in new LldpParserHelper("lshw-basic.xml") {
parsed must beLeft
parsed() must beLeft
}

"Fail to parse text" in new LldpParserHelper("hello world") {
Expand Down

0 comments on commit 09e35b3

Please sign in to comment.