Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add models for objects and requests #144

Merged
merged 1 commit into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.observability.model

import org.opensearch.action.ActionRequest
import org.opensearch.action.ActionRequestValidationException
import org.opensearch.common.io.stream.StreamInput
import org.opensearch.common.io.stream.StreamOutput
import org.opensearch.common.io.stream.Writeable
import org.opensearch.common.xcontent.ToXContent
import org.opensearch.common.xcontent.ToXContentObject
import org.opensearch.common.xcontent.XContentBuilder
import org.opensearch.common.xcontent.XContentParser
import org.opensearch.common.xcontent.XContentParserUtils
import org.opensearch.commons.utils.fieldIfNotNull
import org.opensearch.commons.utils.logger
import org.opensearch.observability.model.RestTag.OBJECT_ID_FIELD
import java.io.IOException

/**
* Action request for creating new configuration.
*/
internal class CreateObservabilityObjectRequest : ActionRequest, ToXContentObject {
val objectId: String?
val type: ObservabilityObjectType
val objectData: BaseObjectData?

companion object {
private val log by logger(CreateObservabilityObjectRequest::class.java)

/**
* reader to create instance of class from writable.
*/
val reader = Writeable.Reader { CreateObservabilityObjectRequest(it) }

/**
* Creator used in REST communication.
* @param parser XContentParser to deserialize data from.
* @param id optional id to use if missed in XContent
*/
@JvmStatic
@Throws(IOException::class)
fun parse(parser: XContentParser, id: String? = null): CreateObservabilityObjectRequest {
var objectId: String? = id
var type: ObservabilityObjectType? = null
var baseObjectData: BaseObjectData? = null

XContentParserUtils.ensureExpectedToken(
XContentParser.Token.START_OBJECT,
parser.currentToken(),
parser
)
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
val fieldName = parser.currentName()
parser.nextToken()
when (fieldName) {
OBJECT_ID_FIELD -> objectId = parser.text()
else -> {
val objectTypeForTag = ObservabilityObjectType.fromTagOrDefault(fieldName)
if (objectTypeForTag != ObservabilityObjectType.NONE && baseObjectData == null) {
baseObjectData =
ObservabilityObjectDataProperties.createObjectData(objectTypeForTag, parser)
type = objectTypeForTag
} else {
parser.skipChildren()
log.info("Unexpected field: $fieldName, while parsing CreateObservabilityObjectRequest")
}
}
}
}
type ?: throw IllegalArgumentException("Object data field absent")
baseObjectData ?: throw IllegalArgumentException("Object data field absent")
return CreateObservabilityObjectRequest(objectId, type, baseObjectData)
}
}

/**
* {@inheritDoc}
*/
override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder {
builder!!
return builder.startObject()
.fieldIfNotNull(OBJECT_ID_FIELD, objectId)
.field(type.tag, objectData)
.endObject()
}

/**
* constructor for creating the class
* @param objectId optional id to use for ObservabilityObject
* @param type type of ObservabilityObject
* @param objectData the ObservabilityObject
*/
constructor(objectId: String? = null, type: ObservabilityObjectType, objectData: BaseObjectData) {
this.objectId = objectId
this.type = type
this.objectData = objectData
}

/**
* {@inheritDoc}
*/
@Throws(IOException::class)
constructor(input: StreamInput) : super(input) {
objectId = input.readOptionalString()
type = input.readEnum(ObservabilityObjectType::class.java)
objectData = input.readOptionalWriteable(
ObservabilityObjectDataProperties.getReaderForObjectType(
input.readEnum(
ObservabilityObjectType::class.java
)
)
)
}

/**
* {@inheritDoc}
*/
@Throws(IOException::class)
override fun writeTo(output: StreamOutput) {
super.writeTo(output)
output.writeOptionalString(objectId)
output.writeEnum(type)
output.writeOptionalWriteable(objectData)
}

/**
* {@inheritDoc}
*/
override fun validate(): ActionRequestValidationException? {
return null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.observability.model

import org.opensearch.common.io.stream.StreamInput
import org.opensearch.common.io.stream.StreamOutput
import org.opensearch.common.io.stream.Writeable
import org.opensearch.common.xcontent.ToXContent
import org.opensearch.common.xcontent.XContentBuilder
import org.opensearch.common.xcontent.XContentParser
import org.opensearch.common.xcontent.XContentParserUtils
import org.opensearch.commons.utils.logger
import org.opensearch.observability.model.RestTag.OBJECT_ID_FIELD
import java.io.IOException

/**
* Action Response for creating new configuration.
*/
internal class CreateObservabilityObjectResponse : BaseResponse {
val objectId: String

companion object {
private val log by logger(CreateObservabilityObjectResponse::class.java)

/**
* reader to create instance of class from writable.
*/
val reader = Writeable.Reader { CreateObservabilityObjectResponse(it) }

/**
* Creator used in REST communication.
* @param parser XContentParser to deserialize data from.
*/
@JvmStatic
@Throws(IOException::class)
fun parse(parser: XContentParser): CreateObservabilityObjectResponse {
var objectId: String? = null

XContentParserUtils.ensureExpectedToken(
XContentParser.Token.START_OBJECT,
parser.currentToken(),
parser
)
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
val fieldName = parser.currentName()
parser.nextToken()
when (fieldName) {
OBJECT_ID_FIELD -> objectId = parser.text()
else -> {
parser.skipChildren()
log.info("Unexpected field: $fieldName, while parsing CreateObservabilityObjectResponse")
}
}
}
objectId ?: throw IllegalArgumentException("$OBJECT_ID_FIELD field absent")
return CreateObservabilityObjectResponse(objectId)
}
}

/**
* constructor for creating the class
* @param id the id of the created ObservabilityObject
*/
constructor(id: String) {
this.objectId = id
}

/**
* {@inheritDoc}
*/
@Throws(IOException::class)
constructor(input: StreamInput) : super(input) {
objectId = input.readString()
}

/**
* {@inheritDoc}
*/
@Throws(IOException::class)
override fun writeTo(output: StreamOutput) {
output.writeString(objectId)
}

/**
* {@inheritDoc}
*/
override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder {
builder!!
return builder.startObject()
.field(OBJECT_ID_FIELD, objectId)
.endObject()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.observability.model

import org.opensearch.action.ActionRequest
import org.opensearch.action.ActionRequestValidationException
import org.opensearch.action.ValidateActions
import org.opensearch.common.io.stream.StreamInput
import org.opensearch.common.io.stream.StreamOutput
import org.opensearch.common.io.stream.Writeable
import org.opensearch.common.xcontent.ToXContent
import org.opensearch.common.xcontent.ToXContentObject
import org.opensearch.common.xcontent.XContentBuilder
import org.opensearch.common.xcontent.XContentParser
import org.opensearch.common.xcontent.XContentParserUtils
import org.opensearch.commons.utils.logger
import org.opensearch.commons.utils.stringList
import org.opensearch.observability.ObservabilityPlugin.Companion.LOG_PREFIX
import org.opensearch.observability.model.RestTag.OBJECT_ID_FIELD
import org.opensearch.observability.model.RestTag.OBJECT_ID_LIST_FIELD
import java.io.IOException

/**
* Action request for creating new configuration.
*/
internal class DeleteObservabilityObjectRequest : ActionRequest, ToXContentObject {
val objectIds: Set<String>

companion object {
private val log by logger(DeleteObservabilityObjectRequest::class.java)

/**
* reader to create instance of class from writable.
*/
val reader = Writeable.Reader { DeleteObservabilityObjectRequest(it) }

/**
* Creator used in REST communication.
* @param parser XContentParser to deserialize data from.
* @param id optional id to use if missed in XContent
*/
@JvmStatic
@Throws(IOException::class)
fun parse(parser: XContentParser): DeleteObservabilityObjectRequest {
var objectIds: Set<String>? = null

XContentParserUtils.ensureExpectedToken(
XContentParser.Token.START_OBJECT,
parser.currentToken(),
parser
)
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
val fieldName = parser.currentName()
parser.nextToken()
when (fieldName) {
OBJECT_ID_LIST_FIELD -> objectIds = parser.stringList().toSet()
else -> {
parser.skipChildren()
log.info("$LOG_PREFIX:Skipping Unknown field $fieldName")
}
}
}
objectIds ?: throw IllegalArgumentException("$OBJECT_ID_FIELD field absent")
return DeleteObservabilityObjectRequest(objectIds)
}
}

/**
* constructor for creating the class
* @param objectIds the id of the observability object
*/
constructor(objectIds: Set<String>) {
this.objectIds = objectIds
}

/**
* {@inheritDoc}
*/
@Throws(IOException::class)
constructor(input: StreamInput) : super(input) {
objectIds = input.readStringList().toSet()
}

/**
* {@inheritDoc}
*/
@Throws(IOException::class)
override fun writeTo(output: StreamOutput) {
super.writeTo(output)
output.writeStringCollection(objectIds)
}

/**
* {@inheritDoc}
*/
override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder {
builder!!
return builder.startObject()
.field(OBJECT_ID_LIST_FIELD, objectIds)
.endObject()
}

/**
* {@inheritDoc}
*/
override fun validate(): ActionRequestValidationException? {
var validationException: ActionRequestValidationException? = null
if (objectIds.isNullOrEmpty()) {
validationException = ValidateActions.addValidationError("objectIds is null or empty", validationException)
}
return validationException
}
}
Loading