|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.ml.common.memorycontainer; |
| 7 | + |
| 8 | +import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; |
| 9 | +import static org.opensearch.ml.common.CommonValue.TENANT_ID_FIELD; |
| 10 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.CREATED_TIME_FIELD; |
| 11 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.DESCRIPTION_FIELD; |
| 12 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.LAST_UPDATED_TIME_FIELD; |
| 13 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.MEMORY_STORAGE_CONFIG_FIELD; |
| 14 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.NAME_FIELD; |
| 15 | +import static org.opensearch.ml.common.memorycontainer.MemoryContainerConstants.OWNER_FIELD; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.time.Instant; |
| 19 | + |
| 20 | +import org.opensearch.commons.authuser.User; |
| 21 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 22 | +import org.opensearch.core.common.io.stream.StreamOutput; |
| 23 | +import org.opensearch.core.common.io.stream.Writeable; |
| 24 | +import org.opensearch.core.xcontent.ToXContent; |
| 25 | +import org.opensearch.core.xcontent.ToXContentObject; |
| 26 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 27 | +import org.opensearch.core.xcontent.XContentParser; |
| 28 | + |
| 29 | +import lombok.Builder; |
| 30 | +import lombok.EqualsAndHashCode; |
| 31 | +import lombok.Getter; |
| 32 | +import lombok.Setter; |
| 33 | + |
| 34 | +/** |
| 35 | + * ML Memory Container data model that stores metadata about memory-related objects |
| 36 | + */ |
| 37 | +@Getter |
| 38 | +@Setter |
| 39 | +@Builder |
| 40 | +@EqualsAndHashCode |
| 41 | +public class MLMemoryContainer implements ToXContentObject, Writeable { |
| 42 | + |
| 43 | + private String name; |
| 44 | + private String description; |
| 45 | + private User owner; |
| 46 | + private String tenantId; |
| 47 | + private Instant createdTime; |
| 48 | + private Instant lastUpdatedTime; |
| 49 | + private MemoryStorageConfig memoryStorageConfig; |
| 50 | + |
| 51 | + public MLMemoryContainer( |
| 52 | + String name, |
| 53 | + String description, |
| 54 | + User owner, |
| 55 | + String tenantId, |
| 56 | + Instant createdTime, |
| 57 | + Instant lastUpdatedTime, |
| 58 | + MemoryStorageConfig memoryStorageConfig |
| 59 | + ) { |
| 60 | + this.name = name; |
| 61 | + this.description = description; |
| 62 | + this.owner = owner; |
| 63 | + this.tenantId = tenantId; |
| 64 | + this.createdTime = createdTime; |
| 65 | + this.lastUpdatedTime = lastUpdatedTime; |
| 66 | + this.memoryStorageConfig = memoryStorageConfig; |
| 67 | + } |
| 68 | + |
| 69 | + public MLMemoryContainer(StreamInput input) throws IOException { |
| 70 | + this.name = input.readOptionalString(); |
| 71 | + this.description = input.readOptionalString(); |
| 72 | + if (input.readBoolean()) { |
| 73 | + this.owner = new User(input); |
| 74 | + } |
| 75 | + this.tenantId = input.readOptionalString(); |
| 76 | + this.createdTime = input.readOptionalInstant(); |
| 77 | + this.lastUpdatedTime = input.readOptionalInstant(); |
| 78 | + if (input.readBoolean()) { |
| 79 | + this.memoryStorageConfig = new MemoryStorageConfig(input); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void writeTo(StreamOutput out) throws IOException { |
| 85 | + out.writeOptionalString(name); |
| 86 | + out.writeOptionalString(description); |
| 87 | + if (owner != null) { |
| 88 | + out.writeBoolean(true); |
| 89 | + owner.writeTo(out); |
| 90 | + } else { |
| 91 | + out.writeBoolean(false); |
| 92 | + } |
| 93 | + out.writeOptionalString(tenantId); |
| 94 | + out.writeOptionalInstant(createdTime); |
| 95 | + out.writeOptionalInstant(lastUpdatedTime); |
| 96 | + if (memoryStorageConfig != null) { |
| 97 | + out.writeBoolean(true); |
| 98 | + memoryStorageConfig.writeTo(out); |
| 99 | + } else { |
| 100 | + out.writeBoolean(false); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { |
| 106 | + builder.startObject(); |
| 107 | + if (name != null) { |
| 108 | + builder.field(NAME_FIELD, name); |
| 109 | + } |
| 110 | + if (description != null) { |
| 111 | + builder.field(DESCRIPTION_FIELD, description); |
| 112 | + } |
| 113 | + if (owner != null) { |
| 114 | + builder.field(OWNER_FIELD, owner); |
| 115 | + } |
| 116 | + if (tenantId != null) { |
| 117 | + builder.field(TENANT_ID_FIELD, tenantId); |
| 118 | + } |
| 119 | + if (createdTime != null) { |
| 120 | + builder.field(CREATED_TIME_FIELD, createdTime.toEpochMilli()); |
| 121 | + } |
| 122 | + if (lastUpdatedTime != null) { |
| 123 | + builder.field(LAST_UPDATED_TIME_FIELD, lastUpdatedTime.toEpochMilli()); |
| 124 | + } |
| 125 | + if (memoryStorageConfig != null) { |
| 126 | + builder.field(MEMORY_STORAGE_CONFIG_FIELD, memoryStorageConfig); |
| 127 | + } |
| 128 | + builder.endObject(); |
| 129 | + return builder; |
| 130 | + } |
| 131 | + |
| 132 | + public static MLMemoryContainer parse(XContentParser parser) throws IOException { |
| 133 | + String name = null; |
| 134 | + String description = null; |
| 135 | + User owner = null; |
| 136 | + String tenantId = null; |
| 137 | + Instant createdTime = null; |
| 138 | + Instant lastUpdatedTime = null; |
| 139 | + MemoryStorageConfig memoryStorageConfig = null; |
| 140 | + |
| 141 | + ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); |
| 142 | + while (parser.nextToken() != XContentParser.Token.END_OBJECT) { |
| 143 | + String fieldName = parser.currentName(); |
| 144 | + parser.nextToken(); |
| 145 | + |
| 146 | + switch (fieldName) { |
| 147 | + case NAME_FIELD: |
| 148 | + name = parser.text(); |
| 149 | + break; |
| 150 | + case DESCRIPTION_FIELD: |
| 151 | + description = parser.text(); |
| 152 | + break; |
| 153 | + case OWNER_FIELD: |
| 154 | + owner = User.parse(parser); |
| 155 | + break; |
| 156 | + case TENANT_ID_FIELD: |
| 157 | + tenantId = parser.text(); |
| 158 | + break; |
| 159 | + case CREATED_TIME_FIELD: |
| 160 | + createdTime = Instant.ofEpochMilli(parser.longValue()); |
| 161 | + break; |
| 162 | + case LAST_UPDATED_TIME_FIELD: |
| 163 | + lastUpdatedTime = Instant.ofEpochMilli(parser.longValue()); |
| 164 | + break; |
| 165 | + case MEMORY_STORAGE_CONFIG_FIELD: |
| 166 | + memoryStorageConfig = MemoryStorageConfig.parse(parser); |
| 167 | + break; |
| 168 | + default: |
| 169 | + parser.skipChildren(); |
| 170 | + break; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return MLMemoryContainer |
| 175 | + .builder() |
| 176 | + .name(name) |
| 177 | + .description(description) |
| 178 | + .owner(owner) |
| 179 | + .tenantId(tenantId) |
| 180 | + .createdTime(createdTime) |
| 181 | + .lastUpdatedTime(lastUpdatedTime) |
| 182 | + .memoryStorageConfig(memoryStorageConfig) |
| 183 | + .build(); |
| 184 | + } |
| 185 | +} |
0 commit comments