Skip to content

Commit

Permalink
Fixed issue while mibs folder does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Chen committed Nov 29, 2016
1 parent 5524ff6 commit 6725ee9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 63 deletions.
95 changes: 48 additions & 47 deletions gen/src/main/scala/org/snmp4s/gen/Gen.scala
Expand Up @@ -12,35 +12,36 @@ import java.io.File
import scala.collection.JavaConversions._

object Util {
def name2oid(mib:Mib) = {
def name2oid(mib: Mib) = {
val syms = mib.getAllSymbols()
(for {
sym <- syms
if (sym.isInstanceOf[MibValueSymbol])
} yield {
val s = sym.asInstanceOf[MibValueSymbol]
s.getName -> s
}).toMap
sym <- syms
if (sym.isInstanceOf[MibValueSymbol])
} yield {
val s = sym.asInstanceOf[MibValueSymbol]
s.getName -> s
}).toMap
}
def camel(mib:String) = mib.split("-").map(s => s.substring(0, 1).toUpperCase + s.substring(1).toLowerCase).mkString

def camel(mib: String) = mib.split("-").map(s => s.substring(0, 1).toUpperCase + s.substring(1).toLowerCase).mkString
}

class Gen(pkg:String) {
def code(mib:BuiltIn.Value):String = code(load(mib))
def code(dir:File):Map[String,String] = load(dir).map { mib => mib.getName -> code(mib) }.toMap

protected def load(mib:BuiltIn.Value):Mib = {
class Gen(pkg: String) {
def code(mib: BuiltIn.Value): String = code(load(mib))

def code(dir: File): Map[String, String] = load(dir).map { mib => mib.getName -> code(mib) }.toMap

protected def load(mib: BuiltIn.Value): Mib = {
val loader = new MibLoader
loader.load(mib.toString())
}
protected def load(file:File):Seq[Mib] = {

protected def load(file: File): Seq[Mib] = {
require(file.isDirectory(), "The file must be a directory")
require(file.canRead(), "The directory must be readable")
val loader = new MibLoader
loader.addDir(file)

(for {
m <- file.listFiles()
if m isFile
Expand All @@ -54,12 +55,12 @@ class Gen(pkg:String) {
}
}).flatten.toSeq
}
protected def code(mib:Mib):String = {

protected def code(mib: Mib): String = {
val syms = mib.getAllSymbols()
"package " + pkg + "." + Util.camel(mib.getName()) + "\n" +
"import org.snmp4s._\n" +
(for {
"import org.snmp4s._\n" +
(for {
sym <- syms
if sym.isInstanceOf[MibValueSymbol]
if sym.asInstanceOf[MibValueSymbol].getChildCount() == 0
Expand All @@ -68,60 +69,60 @@ class Gen(pkg:String) {
code(s)
}).mkString("\n")
}
private def toObjName(name:String):String =

private def toObjName(name: String): String =
name.split("-").map(s => s.substring(0, 1).toUpperCase + s.substring(1)).mkString
def code(oid:MibValueSymbol):String = {

def code(oid: MibValueSymbol): String = {
val name = oid.getName
val objName = toObjName(name)
if(oid.getType.isInstanceOf[SnmpObjectType]) {
if (oid.getType.isInstanceOf[SnmpObjectType]) {
val snmp = oid.getType.asInstanceOf[SnmpObjectType]
val access = accessMap.get(snmp.getAccess()).get
val octets = oid.getValue.toString.replace(".", ",")
val syntaxTrait = syntaxTraitMap.get(snmp.getSyntax.getName).get
val (scalaType, enumArg, typeCode) = syntax(objName, snmp.getSyntax)
val code = typeCode + "case object "+objName+" extends AccessibleObject["+access+", "+scalaType+"](Seq("+octets+"), \""+name+"\", "+syntaxTrait+enumArg+")"
if(oid.isScalar) code + " with Scalar["+access+", "+scalaType+"]"

val code = typeCode + "case object " + objName + " extends AccessibleObject[" + access + ", " + scalaType + "](Seq(" + octets + "), \"" + name + "\", " + syntaxTrait + enumArg + ")"
if (oid.isScalar) code + " with Scalar[" + access + ", " + scalaType + "]"
else code
} else {
""
}
}
private def syntax(objName:String, syntax:MibType):(String,String,String) = {
if(syntax.isInstanceOf[IntegerType]) {

private def syntax(objName: String, syntax: MibType): (String, String, String) = {
if (syntax.isInstanceOf[IntegerType]) {
val intType = syntax.asInstanceOf[IntegerType]
if(intType.hasSymbols) syntaxEnum(objName, intType)
if (intType.hasSymbols) syntaxEnum(objName, intType)
else syntaxGeneral(syntax)
}
else syntaxGeneral(syntax)
}
private def syntaxEnum(objName:String, syntax:IntegerType):(String,String,String) = {
val scalaType = objName+"_enum.Value"
val enumArg = ", Some("+objName+"_enum)"
val typeHead = "object "+objName+"_enum extends EnumInteger {\n"+
" type "+objName+" = Value\n"

private def syntaxEnum(objName: String, syntax: IntegerType): (String, String, String) = {
val scalaType = objName + "_enum.Value"
val enumArg = ", Some(" + objName + "_enum)"

val typeHead = "object " + objName + "_enum extends EnumInteger {\n" +
" type " + objName + " = Value\n"
val entries = for {
s <- syntax.getAllSymbols.toList
} yield {
val v = s.getValue
val nl = s.getName
val nu = toObjName(nl)
" val "+nu+" = Value("+v+", \""+nl+"\")\n"
" val " + nu + " = Value(" + v + ", \"" + nl + "\")\n"
}
val typeTail = "}\n"

val typeCode = typeHead + entries.mkString + typeTail

(scalaType, enumArg, typeCode)
}
private def syntaxGeneral(syntax:MibType):(String,String,String) = (syntaxScalaMap.get(syntax.getName).get, "", "")

private def syntaxGeneral(syntax: MibType): (String, String, String) = (syntaxScalaMap.get(syntax.getName).get, "", "")

private val accessMap = Map(
SnmpAccess.READ_WRITE -> "ReadWrite",
SnmpAccess.NOT_ACCESSIBLE -> "NotAccessible",
Expand All @@ -130,7 +131,7 @@ class Gen(pkg:String) {
SnmpAccess.READ_ONLY -> "ReadOnly",
SnmpAccess.WRITE_ONLY -> "WriteOnly"
)

private val syntaxScalaMap = Map(
"INTEGER" -> "Int",
"OCTET STRING" -> "String",
Expand Down
35 changes: 19 additions & 16 deletions sbt/Snmp4sSbtPlugin.scala
Expand Up @@ -2,39 +2,42 @@ import sbt._
import sbt.Keys._
import org.snmp4s.gen._

object Snmp4sSbtPlugin extends Plugin
{
object Snmp4sSbtPlugin extends Plugin {
val snmp4sBuiltInMibs = SettingKey[Seq[BuiltIn.Value]]("snmp4s-built-in-mibs")
val snmp4sMibPackage = SettingKey[String]("snmp4s-mib-package")
val snmp4sMibPackage = SettingKey[String]("snmp4s-mib-package")

val snmp4sSettings = Seq(
snmp4sBuiltInMibs := Seq(),
snmp4sMibPackage := "org.snmp4s.mib",
sourceGenerators in Compile <+= (sourceManaged in Compile, sourceDirectory in Compile, snmp4sBuiltInMibs, snmp4sMibPackage, streams) map {
(outDir:File, srcDir:File, mibs:Seq[BuiltIn.Value], pkg:String, s:TaskStreams) =>
sourceGenerators in Compile <+= (sourceManaged in Compile, sourceDirectory in Compile, snmp4sBuiltInMibs, snmp4sMibPackage, streams) map {
(outDir: File, srcDir: File, mibs: Seq[BuiltIn.Value], pkg: String, s: TaskStreams) =>
genMibs(outDir / "mibs", srcDir / "mibs", mibs, pkg, s)
}
)


def genMibs(dst:File, src:File, mibs:Seq[BuiltIn.Value], pkg:String, s:TaskStreams) = {
def genMibs(dst: File, src: File, mibs: Seq[BuiltIn.Value], pkg: String, s: TaskStreams) = {
val g = new Gen(pkg)
val dir = pkg.split("\\.").foldLeft(dst){ case (d, next) => d / next }
val mibFileCount = if(src.list != null) src.list.size else 0
val dir = pkg.split("\\.").foldLeft(dst) { case (d, next) => d / next }
val mibFileCount = if (src.list != null) src.list.size else 0
val mibCount = mibs.size + mibFileCount

s.log.info("Generating Scala source from "+mibCount+" MIB"+(if(mibCount == 1) "" else "s")+" to "+dst+"...")
s.log.info("Generating Scala source from " + mibCount + " MIB" + (if (mibCount == 1) "" else "s") + " to " + dst + "...")

val fileCode = g.code(src) map { case (name, code) =>
val file = dir / (org.snmp4s.gen.Util.camel(name)+".scala")
IO.write(file, code)
file
val fileCode = if (src.exists()) {
g.code(src) map { case (name, code) =>
val file = dir / (org.snmp4s.gen.Util.camel(name) + ".scala")
IO.write(file, code)
file
}
} else {
Seq()
}

val builtInCode = mibs map { mib =>
val file = dir / (org.snmp4s.gen.Util.camel(mib.toString)+".scala")
val builtInCode = mibs map { mib =>
val file = dir / (org.snmp4s.gen.Util.camel(mib.toString) + ".scala")
IO.write(file, g.code(mib))
file
file
}

(fileCode ++ builtInCode).toSeq
Expand Down

0 comments on commit 6725ee9

Please sign in to comment.