Skip to content

Commit

Permalink
Add models for objects and requests (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuali925 authored Oct 19, 2021
1 parent 796be2e commit 938e5b6
Show file tree
Hide file tree
Showing 20 changed files with 3,395 additions and 0 deletions.
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

0 comments on commit 938e5b6

Please sign in to comment.