Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2020-2022 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.exactpro.th2.converter.conversion
import com.exactpro.th2.converter.controllers.ConversionResult
import com.exactpro.th2.converter.controllers.ConversionSummary
import com.exactpro.th2.converter.controllers.ErrorMessage
import com.exactpro.th2.converter.controllers.errors.BadRequestException
import com.exactpro.th2.converter.controllers.errors.ErrorCode
import com.exactpro.th2.converter.`fun`.Convertible
import com.exactpro.th2.converter.`fun`.ConvertibleBoxSpecV1
import com.exactpro.th2.converter.`fun`.ConvertibleBoxSpecV2
import com.exactpro.th2.converter.model.Th2Resource
import com.exactpro.th2.converter.util.Mapper.YAML_MAPPER
import com.exactpro.th2.converter.util.ProjectConstants.API_VERSION_V1
import com.exactpro.th2.converter.util.ProjectConstants.SHORT_API_VERSION_V2
import com.exactpro.th2.infrarepo.ResourceType
import com.exactpro.th2.infrarepo.git.Gitter
import com.exactpro.th2.infrarepo.repo.Repository
import com.exactpro.th2.infrarepo.repo.RepositoryResource
import com.exactpro.th2.model.latest.box.Spec
import com.exactpro.th2.model.v1.box.SpecV1
import com.fasterxml.jackson.module.kotlin.convertValue
object Converter {
fun convertFromGit(
version: String,
gitter: Gitter
): ConversionResult {
val summary = ConversionSummary()
val convertedResources: List
when (version) {
SHORT_API_VERSION_V2 -> {
val boxesToConvert: Set
val links: Set
try {
gitter.lock()
boxesToConvert = HashSet(Repository.getAllBoxesAndStores(gitter))
links = HashSet(Repository.getResourcesByKind(gitter, ResourceType.Th2Link))
} finally {
gitter.unlock()
}
convertedResources = convert(boxesToConvert, API_VERSION_V1, summary)
val linksInserter = LinksInserter()
linksInserter.insertLinksIntoBoxes(convertedResources, links)
linksInserter.addErrorsToSummary(summary)
return ConversionResult(summary, convertedResources)
}
else -> throw BadRequestException(
ErrorCode.VERSION_NOT_ALLOWED,
"Conversion to specified version: '$version' is not supported"
)
}
}
fun convertFromRequest(
targetVersion: String,
resources: Set
): ConversionResult {
val summary = ConversionSummary()
val convertedResources: List
when (targetVersion) {
SHORT_API_VERSION_V2 -> {
val linkKind = ResourceType.Th2Link.kind()
val boxKinds = setOf(
ResourceType.Th2Box.kind(),
ResourceType.Th2CoreBox.kind(),
ResourceType.Th2Estore.kind(),
ResourceType.Th2Mstore.kind()
)
val boxesToConvert = resources.filterTo(HashSet()) { boxKinds.contains(it.kind) }
convertedResources = convert(boxesToConvert, API_VERSION_V1, summary)
val links = resources.filterTo(HashSet()) { it.kind.equals(linkKind) }
val linksInserter = LinksInserter()
linksInserter.insertLinksIntoBoxes(convertedResources, links)
linksInserter.addErrorsToSummary(summary)
}
else -> throw BadRequestException(
ErrorCode.VERSION_NOT_ALLOWED,
"Conversion to specified version: '$targetVersion' is not supported"
)
}
return ConversionResult(summary, convertedResources)
}
fun convertLocal(
version: String,
gitter: Gitter
): ConversionResult {
val summary = ConversionSummary()
val convertedResources: List
when (version) {
SHORT_API_VERSION_V2 -> {
val boxesToConvert = Repository.getAllBoxesAndStores(gitter, false)
val links = Repository.getResourcesByKind(gitter, ResourceType.Th2Link, false)
convertedResources = convert(boxesToConvert, API_VERSION_V1, summary)
val linksInserter = LinksInserter()
linksInserter.insertLinksIntoBoxes(convertedResources, links)
linksInserter.addErrorsToSummary(summary)
return ConversionResult(summary, convertedResources)
}
else -> throw BadRequestException(
ErrorCode.VERSION_NOT_ALLOWED,
"Conversion to specified version: '$version' is not supported"
)
}
}
private inline fun convert(
resources: Set,
fromVersion: String,
summary: ConversionSummary
): List {
val convertedResources: MutableList = ArrayList()
for (resource in resources) {
if (!resource.apiVersion.equals(fromVersion)) {
summary.errorMessages.add(
ErrorMessage(
resource.metadata.name,
"Resource must be of version: $fromVersion"
)
)
continue
}
try {
val specFrom: From = YAML_MAPPER.convertValue(resource.spec)
val resourceFrom = Th2Resource(resource.apiVersion, resource.kind, resource.metadata, wrap(specFrom))
convertedResources.add(resourceFrom.toNextVersion())
summary.convertedResourceNames.add(resource.metadata.name)
} catch (e: Exception) {
summary.errorMessages.add(ErrorMessage(resource.metadata.name, e.message))
}
}
return convertedResources
}
private fun wrap(specFrom: From): Convertible {
return when (specFrom) {
is SpecV1 -> ConvertibleBoxSpecV1(specFrom)
is Spec -> ConvertibleBoxSpecV2(specFrom)
else -> throw AssertionError("Provided spec class is not supported for conversion")
}
}
}