/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.action.bulk;

import org.elasticsearch.Version;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;

import java.io.IOException;

public class BulkItemRequest implements Streamable {

    private int id;
    private DocWriteRequest request;
    private volatile BulkItemResponse primaryResponse;

    BulkItemRequest() {

    }

    // NOTE: public for testing only
    public BulkItemRequest(int id, DocWriteRequest request) {
        this.id = id;
        this.request = request;
    }

    public int id() {
        return id;
    }

    public DocWriteRequest request() {
        return request;
    }

    public String index() {
        assert request.indices().length == 1;
        return request.indices()[0];
    }

    BulkItemResponse getPrimaryResponse() {
        return primaryResponse;
    }

    void setPrimaryResponse(BulkItemResponse primaryResponse) {
        this.primaryResponse = primaryResponse;
    }

    public static BulkItemRequest readBulkItem(StreamInput in) throws IOException {
        BulkItemRequest item = new BulkItemRequest();
        item.readFrom(in);
        return item;
    }

    @Override
    public void readFrom(StreamInput in) throws IOException {
        id = in.readVInt();
        request = DocWriteRequest.readDocumentRequest(in);
        if (in.readBoolean()) {
            primaryResponse = BulkItemResponse.readBulkItem(in);
        }
        if (in.getVersion().before(Version.V_6_0_0_alpha1)) { // TODO remove once backported
            boolean ignoreOnReplica = in.readBoolean();
            if (ignoreOnReplica == false && primaryResponse != null) {
                assert primaryResponse.isFailed() == false : "expected no failure on the primary response";
            }
        }
    }

    @Override
    public void writeTo(StreamOutput out) throws IOException {
        out.writeVInt(id);
        if (out.getVersion().before(Version.V_6_0_0_alpha1)) { // TODO remove once backported
            // old nodes expect updated version and version type on the request
            if (primaryResponse != null) {
                request.version(primaryResponse.getVersion());
                request.versionType(request.versionType().versionTypeForReplicationAndRecovery());
                DocWriteRequest.writeDocumentRequest(out, request);
            } else {
                DocWriteRequest.writeDocumentRequest(out, request);
            }
        } else {
            DocWriteRequest.writeDocumentRequest(out, request);
        }
        out.writeOptionalStreamable(primaryResponse);
        if (out.getVersion().before(Version.V_6_0_0_alpha1)) { // TODO remove once backported
            if (primaryResponse != null) {
                out.writeBoolean(primaryResponse.isFailed()
                        || primaryResponse.getResponse().getResult() == DocWriteResponse.Result.NOOP);
            } else {
                out.writeBoolean(false);
            }
        }
    }
}

@@ -852,114 +852,30 @@
 rch.
-Version;%0Aimport org.elasticsearch.action.DocWriteRequest;%0Aimport org.elasticsearch.action.DocWriteResponse
+action.DocWriteRequest
 ;%0Aim
@@ -2335,1547 +2335,228 @@
     
-    if (in.getVersion().before(Version.V_6_0_0_alpha1)) %7B // TODO remove once backported%0A            boolean ignoreOnReplica = in.readBoolean();%0A            if (ignoreOnReplica == false && primaryResponse != null) %7B%0A                assert primaryResponse.isFailed() == false : %22expected no failure on the primary response%22;%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void writeTo(StreamOutput out) throws IOException %7B%0A        out.writeVInt(id);%0A        if (out.getVersion().before(Version.V_6_0_0_alpha1)) %7B // TODO remove once backported%0A            // old nodes expect updated version and version type on the request%0A            if (primaryResponse != null) %7B%0A                request.version(primaryResponse.getVersion());%0A                request.versionType(request.versionType().versionTypeForReplicationAndRecovery());%0A                DocWriteRequest.writeDocumentRequest(out, request);%0A            %7D else %7B%0A                DocWriteRequest.writeDocumentRequest(out, request);%0A            %7D%0A        %7D else %7B%0A            DocWriteRequest.writeDocumentRequest(out, request);%0A        %7D%0A        out.writeOptionalStreamable(primaryResponse);%0A        if (out.getVersion().before(Version.V_6_0_0_alpha1)) %7B // TODO remove once backported%0A            if (primaryResponse != null) %7B%0A                out.writeBoolean(primaryResponse.isFailed()%0A                        %7C%7C primaryResponse.getResponse().getResult() == DocWriteResponse.Result.NOOP);%0A            %7D else %7B%0A                out.writeBoolean(false);%0A            %7D%0A        %7D
+%7D%0A%0A    @Override%0A    public void writeTo(StreamOutput out) throws IOException %7B%0A        out.writeVInt(id);%0A        DocWriteRequest.writeDocumentRequest(out, request);%0A        out.writeOptionalStreamable(primaryResponse);
 %0A   

Move `BulkItemRequest` BWC to 5.x (#25511)