diff options
author | Einat Vinouze <einat.vinouze@intl.att.com> | 2019-07-16 17:17:36 +0300 |
---|---|---|
committer | Ittay Stern <ittay.stern@att.com> | 2019-07-30 06:01:44 +0300 |
commit | e601bbdc43bae9a08e2e10c5139a6f76b47860d7 (patch) | |
tree | 1913f0b369ead3f2ea5557e5649d8281eca9871c /vid-app-common/src/main/java/org/onap/vid/services/VersionService.kt | |
parent | 76c6ee4a697617ec4cdee2f3b48bc83136c858c5 (diff) |
Implant vid-app-common org.onap.vid.job (main and test)
Issue-ID: VID-378
Change-Id: I41b0bdc2c4e3635f3f3319b1cd63cefc61912dfc
Signed-off-by: Einat Vinouze <einat.vinouze@intl.att.com>
Signed-off-by: Ittay Stern <ittay.stern@att.com>
Diffstat (limited to 'vid-app-common/src/main/java/org/onap/vid/services/VersionService.kt')
-rw-r--r-- | vid-app-common/src/main/java/org/onap/vid/services/VersionService.kt | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/services/VersionService.kt b/vid-app-common/src/main/java/org/onap/vid/services/VersionService.kt new file mode 100644 index 000000000..2fb3e0c08 --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/services/VersionService.kt @@ -0,0 +1,66 @@ +package org.onap.vid.services + +import com.fasterxml.jackson.module.kotlin.readValue +import org.apache.commons.lang3.StringUtils +import org.apache.commons.lang3.StringUtils.substringAfterLast +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate +import org.onap.portalsdk.core.util.SystemProperties +import org.onap.vid.model.VersionAndFeatures +import org.onap.vid.utils.JACKSON_OBJECT_MAPPER +import org.springframework.stereotype.Service +import java.util.regex.Pattern +import javax.inject.Inject +import javax.servlet.ServletContext + +@Service +class VersionService +@Inject +constructor(internal var servletContext: ServletContext) { + + private val Logger = EELFLoggerDelegate.getLogger(VersionService::class.java) + + private val versionAndFeatures: VersionAndFeatures by lazy { + loadVersionAndFeatures() + } + + private fun readBuildNumber(): String { + val resource = servletContext.getResource("/app/vid/scripts/constants/version.json") + return JACKSON_OBJECT_MAPPER.readValue<Map<String, String>>(resource)["Version"]!! + } + + //protected so can be easily tested in UT + protected fun readFeatureSet(): String { + return SystemProperties.getProperty("features.set.filename") + } + + //protected so can be easily tested in UT + protected fun buildDisplayVersion(features: String, build: String): String { + val matcher = Pattern.compile("([^/]+?)(\\.features|$)").matcher(features) + val majorByFeatures = if (matcher.find()) matcher.group(1) else features + + val buildByVersion = StringUtils.defaultIfBlank(substringAfterLast(build, "."), build) + + return StringUtils.join(majorByFeatures, ".", buildByVersion) + } + + fun retrieveVersionAndFeatures(): VersionAndFeatures { + return try { + versionAndFeatures + } catch (exception: Exception) { + Logger.error("Failed to read build number or feature properties settings from version file", exception) + VersionAndFeatures.unknown + } + } + + private fun loadVersionAndFeatures(): VersionAndFeatures { + val featureSet = readFeatureSet(); + val build = readBuildNumber(); + val displayVersion = buildDisplayVersion(featureSet, build) + return VersionAndFeatures(featureSet, build, displayVersion) + } + + //might throw an exception + fun retrieveBuildNumber(): String { + return versionAndFeatures.build + } +} |