Commit 2eb84ad9 by Tomasz Ciesielczyk

model load bug fix

parent aaebc8ed
......@@ -58,8 +58,8 @@ object ServerConfInitializer {
private fun loadConfig() {
Timer.start()
DAO.Settings.loadComponentPowerModel()
ModelMap.load()
DAO.Settings.loadComponentPowerModel()
Timer.stop()
}
......
package psnc.m2dc.rtm.commands.java
val APP_VERSION = "1.0.548_55165"
\ No newline at end of file
val APP_VERSION = "1.0.550_64170"
\ No newline at end of file
package psnc.m2dc.rtm.client
const val APP_VERSION = "1.1.548_55166"
\ No newline at end of file
const val APP_VERSION = "1.1.550_64170"
\ No newline at end of file
......@@ -676,7 +676,8 @@ object ModelMap {
val typeName = s[0]
val profileName = s[1]
val typeMap = map[typeName] ?: HashMap<String, ComponentPowerModel>()
typeMap[profileName] = SettingsDAO.model_map.get("${typeName}_${profileName}")
// typeMap[profileName] = SettingsDAO.model_map.get("${typeName}_${profileName}")
typeMap[profileName] = SettingsDAO.model_map["$modelName"]
map[typeName] = typeMap
}
}
......
......@@ -60,7 +60,7 @@ class RPMApiImpl : RPMApi {
private fun actionTask(result: ResourceActionResult): ResourceActionResult {
printConsole("xxx: start ${result.toJson()}")
// printConsole("xxx: start ${result.toJson()}")
try {
when (result.powerAction) {
ManagementMethodType.suspend -> {
......
......@@ -19,8 +19,8 @@ abstract class RECSApi(val XMLParser: XStream) {
abstract fun put(url: String, body: String?, headers: HashMap<String, String> = HashMap()):
Pair<String, Map<String, String>>
abstract fun <T> postXML(url: String, body: String?, headers: HashMap<String, String> = HashMap()):
Pair<T, Map<String, String>>
// abstract fun <T> postXML(url: String, body: String?, headers: HashMap<String, String> = HashMap()):
// Pair<T, Map<String, String>>
// abstract fun loadRECSHosts(): LDAPRECSDictionary
abstract fun loadRECSHosts(): List<String>
......
......@@ -12,8 +12,8 @@ import psnc.m2dc.mock.rtm.rpm.RECSApiMock
import psnc.m2dc.rtm.model.recs.*
import psnc.m2dc.rtm.properties.RTMProperties
import psnc.m2dc.rtm.properties.keys.Keys
import psnc.m2dc.rtm.utils.logDebug
import psnc.m2dc.rtm.utils.logInfo
import psnc.m2dc.rtm.utils.printConsole
import psnc.m2dc.rtm.utils.toJson
......@@ -84,139 +84,79 @@ class RECSApiImpl private constructor(parser: XStream = initXMLParser() ) : RECS
return xmlParser
}
// fun <T> fromCache(storage: IStorage<String>?, vararg args: String): T? {
// val key = Storage.getKey(args)
// if (storage != null && storage.notNull(key)) {
// return storage.get<T>(key)
// }
// return null
// }
}
// fun <T> fromCache(storage: IStorage<String>?, vararg args: Any): T? {
// return null;
// val l = ArrayList<String>(args.size)
// args.forEach() { arg ->
// if (arg is String) {
// l.add(arg)
// } else {
// l.add(RTMProperties.JSONParser.toJson(arg))
// }
// }
//
//
// val key = Storage.getKey(l)
// if (storage != null && storage.notNull(key)) {
// return storage.get<T>(key)
// }
// return null
// }
// fun setCache(value: Any, storage: IStorage<String>?, vararg args: String) {
// val key = Storage.getKey(args)
// if (storage != null) {
// storage.put(key, value, CACHE_TIMEOUT)
// }
// }
override fun get(url: String, headers: HashMap<String, String>):
Pair<String, Map<String, String>> {
// val agent = "Mozilla/5.0 (X11; U; Linux i586; en-US; rv:1.7.3) Gecko/20040924 Epiphany/1.4.4 (Ubuntu)"
// var fromCache = fromCache<Pair<String, Map<String, String>>>(storage, url, headers.toString())
// if (fromCache == null) {
val authString = "$username:$password"
val base64login = String(Base64.encodeBase64(authString.toByteArray()))
headers["Authorization"] = "Basic $base64login"
val connection = Jsoup.connect(url).timeout(30000)
.method(Connection.Method.GET).headers(headers).execute()
return Pair(connection.body(), connection.headers())
// fromCache = Pair(connection.body(), connection.headers())
// setCache(fromCache, storage, url, headers.toString())
// }
// return fromCache
}
fun testConnection(url: String) {
try {
this.get(url)
} catch (e: Exception) {
log.error("testConnection failed: %s".format(e.message))
}
}
// fun testConnection(url: String) {
// try {
// this.get(url)
// } catch (e: Exception) {
// log.error("testConnection failed: %s".format(e.message))
// }
// }
@Suppress("UNCHECKED_CAST")
override fun <T> getXML(url: String, headers: HashMap<String, String>):
Pair<T, Map<String, String>> {
// var fromCache = fromCache<Pair<T, Map<String, String>>>(storage, url, headers.toString())
// if (fromCache == null) {
val authString = "$username:$password"
val base64login = String(Base64.encodeBase64(authString.toByteArray()))
headers["Authorization"] = "Basic $base64login"
printConsole("GET: $url : ${headers.toJson()}" )
logDebug("GET: $url : ${headers.toJson()}" )
val connection = Jsoup.connect(url).timeout(30000)
.method(Connection.Method.GET).headers(headers).execute()
//TODO: log debug
// logDebug("recs output: ${connection.body()}")
return Pair(XMLParser.fromXML(connection.body()) as T, connection.headers())
//fromCache = Pair(XMLParser.fromXML(connection.body()) as T, connection.headers())
// setCache(fromCache, storage, url, headers.toString())
// }
// return fromCache
}
override fun post(url: String, body: String?, headers: HashMap<String, String>):
Pair<String, Map<String, String>> {
// var fromCache = fromCache<Pair<String, Map<String, String>>>(storage, url, headers.toString())
// if (fromCache == null) {
val authString = "$username:$password"
val base64login = String(Base64.encodeBase64(authString.toByteArray()))
headers["Authorization"] = "Basic $base64login"
logDebug("POST: $url : ${headers.toJson()}" )
val connection = Jsoup.connect(url).timeout(30000)
.method(Connection.Method.POST).requestBody(body).headers(headers).execute()
return Pair(connection.body(), connection.headers())
// fromCache = Pair(connection.body(), connection.headers())
// setCache(fromCache, storage, url, headers.toString())
// }
// return fromCache
}
override fun put(url: String, body: String?, headers: HashMap<String, String>):
Pair<String, Map<String, String>> {
// var fromCache = fromCache<Pair<String, Map<String, String>>>(storage, url, headers.toString())
// if (fromCache == null) {
val authString = "$username:$password"
val base64login = String(Base64.encodeBase64(authString.toByteArray()))
headers["Authorization"] = "Basic $base64login"
logDebug("PUT: $url : ${headers.toJson()}" )
val connection = Jsoup.connect(url).timeout(30000)
.method(Connection.Method.PUT).requestBody(body).headers(headers).execute()
return Pair(connection.body(), connection.headers())
// fromCache = Pair(connection.body(), connection.headers())
// setCache(fromCache, storage, url, headers.toString())
// }
// return fromCache
}
@Suppress("UNCHECKED_CAST")
override fun <T> postXML(url: String, body: String?, headers: HashMap<String, String>):
Pair<T, Map<String, String>> {
// var fromCache = this.fromCache<Pair<T, Map<String, String>>>(storage, url, headers.toString())
// if (fromCache == null) {
val authString = "$username:$password"
val base64login = String(Base64.encodeBase64(authString.toByteArray()))
headers["Authorization"] = "Basic $base64login"
val connection = Jsoup.connect(url).timeout(30000)
.method(Connection.Method.POST).requestBody(body).headers(headers).execute()
return Pair(XMLParser.fromXML(connection.body()) as T, connection.headers())
// fromCache = Pair(XMLParser.fromXML(connection.body()) as T, connection.headers())
// setCache(fromCache, storage, url, headers.toString())
// }
// return fromCache
}
//
// @Suppress("UNCHECKED_CAST")
// override fun <T> postXML(url: String, body: String?, headers: HashMap<String, String>):
// Pair<T, Map<String, String>> {
// val authString = "$username:$password"
// val base64login = String(Base64.encodeBase64(authString.toByteArray()))
// headers["Authorization"] = "Basic $base64login"
// val connection = Jsoup.connect(url).timeout(30000)
// .method(Connection.Method.POST).requestBody(body).headers(headers).execute()
// return Pair(XMLParser.fromXML(connection.body()) as T, connection.headers())
// }
override fun loadRECSHosts(): List<String> /*List<LDAPRECSNode>*/ {
......
......@@ -11,16 +11,16 @@ import psnc.m2dc.rtm.rpm.ManagementExecutor
import psnc.m2dc.rtm.rpm.ManagementWorker
import psnc.m2dc.rtm.rpm.ManagementWorkerImpl
import psnc.m2dc.rtm.rpm.TaskExecutor
import psnc.m2dc.rtm.utils.*
import psnc.m2dc.rtm.utils.Timer
import psnc.m2dc.rtm.utils.logError
import psnc.m2dc.rtm.utils.logInfo
import psnc.m2dc.rtm.utils.logWarn
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
class RECSMonitoringApi private constructor(/*storage: IStorage<String>? = null*/) {
private val RECSApi = psnc.m2dc.rtm.rpm.nativeapi.RECSApiImpl.Instance
init {
}
companion object {
......@@ -33,30 +33,26 @@ class RECSMonitoringApi private constructor(/*storage: IStorage<String>? = null*
val log = LoggerFactory.getLogger(RECSMonitoringApi::class.java.name)!!
}
fun listAllNodes(hosts: List<String>): List<Resource> {
//TODO: set reload param
val reload = true
logInfo("RECS: getnodes from: $hosts")
// println("list nodes, hostsize: " + recsDetails.hostMap.size)
fun listAllNodes(hosts: List<String>, reload: Boolean = true): List<Resource> {
logInfo("RECS: get nodes from: $hosts")
val l = ArrayList<Resource>()
/**
* resources to update via ssh api
*/
val updateList = ArrayList<Resource>()
hosts.forEach { host ->
val map = DAO.Resource.getNodes()
val nodes = listNodes(host).nodeList.filter {
// printConsole("${!map.containsKey(it.id)}/${Keys.TestBed.resource_list.getNullable()?.toJson()}")
(!map.containsKey(it.id) || reload) &&
(!Keys.TestBed.filter_nodes.get() ||
Keys.TestBed.resource_list.getNullable()?.contains(it.id) ?: false)
}
// filterNodes(nodes)
nodes.forEach {
try {
val typeId = DAO.Resource.getType(it.id)
val resource = Resource(it.id, typeId ?: "", null, properties = HashMap())
resource.put(Resource.Property.recs_host, host)
val model = NodeModel.create(it.id, it.architecture ?: "unknown", HashMap())
Keys.SSHHosts.get(resource.id)
......@@ -69,15 +65,12 @@ class RECSMonitoringApi private constructor(/*storage: IStorage<String>? = null*
logWarn("Recs resource type is not set, try reload:${resource.id}")
updateList.add(resource)
}
resource.put(model)
resource.put(Resource.Property.is_node, true)
resource.put(Resource.Property.node_id, resource.id)
l.add(resource)
//resource.resourceType = resourceType
} catch (e: Exception) {
// e.printStackTrace()
logError(e, "CANNOT get details for: " + it.id)
}
}
......@@ -106,30 +99,26 @@ class RECSMonitoringApi private constructor(/*storage: IStorage<String>? = null*
}
}
if (saveModelMap) {
printConsole("Debug: save modelmap")
//update model map file with new mapping obtained via ssh
ModelMap.save()
} else {
printConsole("Debug: not save modelmap")
}
// DAO.ResourcesManagerApi.saveResourcesToFile(resources)
}
private fun submitTasks(resourceList: List<Resource>)
: HashMap<Resource, Future<ManagementWorker<Map<String, String>>>?> {
val map = HashMap<Resource, Future<ManagementWorker<Map<String, String>>>?>()
printConsole("update recs nodes")
// printConsole("update recs nodes")
resourceList.forEach { resource ->
val resourceId = resource.id
try {
val sshHost = DAO.Resource.getRemoteApiEndpoint(ManagementApi.ssh, resourceId)
?: { logError("endpoint not found for $resourceId");null }()!!
// LightStorage.instance.put(listOf("resources", resourceId, "properties", "ssh_host"), sshHost)
resource.put(Resource.Property.ssh_host,sshHost)
resource.put(Resource.Property.ssh_host, sshHost)
val sshUsername = LightStorage.instance.get<String>("resources", resourceId, "properties", "ssh_username")
?: Keys.RTM.default_ssh_user.getNullable()
?: throw NullPointerException("sshUsername not provided")
// LightStorage.instance.put(listOf("resources", resourceId, "properties", "ssh_username"), sshUsername)
resource.put(Resource.Property.ssh_username,sshUsername)
resource.put(Resource.Property.ssh_username, sshUsername)
val task = submitTask(sshHost, sshUsername, resource)
map[resource] = task
} catch (e: NullPointerException) {
......@@ -144,7 +133,6 @@ class RECSMonitoringApi private constructor(/*storage: IStorage<String>? = null*
val worker = ManagementWorkerImpl(sshHost, {
Timer.stopwatch("Data download time: $sshHost/${resource.id}") {
try {
val result: Map<String, String> = SSHApiImpl.init(resource).getType()
result["resource_type"]?.let { DAO.Resource.setComponentPowerModel(it, null) }
result
......@@ -180,8 +168,7 @@ class RECSMonitoringApi private constructor(/*storage: IStorage<String>? = null*
fun listNodes(host: String): NodeList {
val result = RECSApi.getXML<NodeList>("""http://$host/REST/node""")
val nl = result.first ?: NodeList()
return nl
return result.first ?: NodeList()
}
/**
......@@ -287,59 +274,3 @@ class RECSMonitoringApi private constructor(/*storage: IStorage<String>? = null*
}
}
//fun listAllNodes(hosts: List<String>): List<Resource> {
//
// logInfo("RECS: getnodes from: $hosts")
// // println("list nodes, hostsize: " + recsDetails.hostMap.size)
// val l = ArrayList<Resource>()
// // val hosts = recsDetails.hostMap.keys
//
// hosts.forEach() { host ->
// // println("current host: " + host)
// val _list = listNodes(host)
// filterNodes(_list.nodeList).forEach() {
// try {
// // println("recs_node: " + it.id + " " + it.actualNodePowerUsage)
// val resource = Resource(it.id, it.id + "_node_type", null, properties = HashMap())
// //TO DO: uncomment// val nodePowerDetails = NodeResourceDetails(it)
// // .id,
// // resource)
// resource.put(Resource.Property.recs_host, host)
// val model = NodeModel.create(it.id, it.architecture ?: "unknown", HashMap())
// // val meta = NodeApiDetails(
// // managementApis = initManagementApis());
// // //to do:)
// resource.put(Resource.Property.ssh_host, Keys.SSHHosts.get(resource.id))
// resource.put(Resource.Property.ssh_username, Keys.RTM.default_ssh_user.get())
//
// // load node's power details
// // if (RTMProperties.getArray(Keys.Default.mock_list).contains("SSH")) {
// // nodePowerDetails = SSHApiMock.getNodeReviewResourceDetails(resource)
// // } else nodePowerDetails = SSHApiImpl.init(resource).getNodeResourceDetails()
// // resource.childResources = nodePowerDetails.resource?.childResources ?: null
// // nodePowerDetails.sync(RPMApi.instance().getNodePowerDetails(it.id), true)
//
// resource.put(model)
// // resource.put(meta)
// resource.put(Resource.Property.is_node, true)
// resource.put(Resource.Property.node_id, resource.id)
//
// // resource.resourceType = ResourceType(resource.id, Resource.Type.recs_node, this.initSensors(meta.managementApis))
//
//
// resource.resourceType = this.getType()
// // val toprint = resource.resourceType?.toJson()
// // println(toprint)
// // resource.resourceType?.properties?.write(meta)
// // RPMApi.instance().putNodePowerDetails(it.id, nodePowerDetails, true)
// l.add(resource)
// } catch (e: Exception) {
// e.printStackTrace();
// println("CANNOT get details for: " + it.id)
// logError(e, "CANNOT get details for recs node: " + it.id)
// }
// }
// }
// return l
//}
\ No newline at end of file
......@@ -231,11 +231,13 @@ fun setWatch(begin: Date, tag: String? = null, print: Boolean = true, timeThresh
object Timer {
const val TIME_THRESHOLD = -1L
inline fun <R> stopwatch(block: () -> R): R {
val name = getRefMethod()
return stopwatch(name, block)
}
// inline fun <R> stopwatch(block: () -> R): R {
// val name = getRefMethod()
// return stopwatch(name, block)
// }
/**
* @param block -> method to measure
*/
inline fun <R> stopwatch(name: String, block: () -> R): R {
val begin = Date().time
try {
......@@ -264,44 +266,22 @@ object Timer {
map.remove(ref)
return diff
}
//
// fun tick() {
// begin = Date()
// }
//
// fun tock(): Long {
// val diff = (Date()).time - begin.time
// if (diff > TIME_THRESHOLD) {
// val info = getRefMethod()
// logInfo("""Execution time: ${_parseTimeDiff(diff)} (${info})""")
// }
// return diff
// }
// fun <T : Any?> measure(f: () -> T, tag: String = getRefMethodName()): Pair<T, Long> {
// val begin = Date()
// val result = f()
// val diff = (Date()).time - begin.time
// if (diff > TIME_THRESHOLD) {
// val info = getRefMethod()
// logInfo("""$tag:Execution time: ${_parseTimeDiff(diff)} ($info)""")
// }
// return Pair(result, diff)
// }
}
fun parseTimeDiff(timeDiffMs: Long): String {
when {
return when {
timeDiffMs < 10000 -> {
return """${timeDiffMs}ms"""
"""${timeDiffMs}ms"""
}
timeDiffMs < 1000000 -> {
return """${timeDiffMs / 1000}s"""
"""${timeDiffMs / 1000}s"""
}
timeDiffMs < 60000000 -> {
return """${timeDiffMs / 60000}min"""
"""${timeDiffMs / 60000}min"""
}
else -> {
return """${timeDiffMs / 3600000}h"""
"""${timeDiffMs / 3600000}h"""
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment