All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.kafka.common.requests.AlterClientQuotasRequest Maven / Gradle / Ivy

There is a newer version: 1.2.2.1-jre17
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF 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.apache.kafka.common.requests;

import org.apache.kafka.common.message.AlterClientQuotasRequestData;
import org.apache.kafka.common.message.AlterClientQuotasRequestData.EntityData;
import org.apache.kafka.common.message.AlterClientQuotasRequestData.EntryData;
import org.apache.kafka.common.message.AlterClientQuotasRequestData.OpData;
import org.apache.kafka.common.protocol.ApiKeys;
import org.apache.kafka.common.protocol.types.Struct;
import org.apache.kafka.common.quota.ClientQuotaAlteration;
import org.apache.kafka.common.quota.ClientQuotaEntity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AlterClientQuotasRequest extends AbstractRequest {

    public static class Builder extends AbstractRequest.Builder {

        private final AlterClientQuotasRequestData data;

        public Builder(Collection entries, boolean validateOnly) {
            super(ApiKeys.ALTER_CLIENT_QUOTAS);

            List entryData = new ArrayList<>(entries.size());
            for (ClientQuotaAlteration entry : entries) {
                List entityData = new ArrayList<>(entry.entity().entries().size());
                for (Map.Entry entityEntries : entry.entity().entries().entrySet()) {
                    entityData.add(new EntityData()
                            .setEntityType(entityEntries.getKey())
                            .setEntityName(entityEntries.getValue()));
                }

                List opData = new ArrayList<>(entry.ops().size());
                for (ClientQuotaAlteration.Op op : entry.ops()) {
                    opData.add(new OpData()
                            .setKey(op.key())
                            .setValue(op.value() == null ? 0.0 : op.value())
                            .setRemove(op.value() == null));
                }

                entryData.add(new EntryData()
                        .setEntity(entityData)
                        .setOps(opData));
            }

            this.data = new AlterClientQuotasRequestData()
                    .setEntries(entryData)
                    .setValidateOnly(validateOnly);
        }

        @Override
        public AlterClientQuotasRequest build(short version) {
            return new AlterClientQuotasRequest(data, version);
        }

        @Override
        public String toString() {
            return data.toString();
        }
    }

    private final AlterClientQuotasRequestData data;

    public AlterClientQuotasRequest(AlterClientQuotasRequestData data, short version) {
        super(ApiKeys.ALTER_CLIENT_QUOTAS, version);
        this.data = data;
    }

    public AlterClientQuotasRequest(Struct struct, short version) {
        super(ApiKeys.ALTER_CLIENT_QUOTAS, version);
        this.data = new AlterClientQuotasRequestData(struct, version);
    }

    public Collection entries() {
        List entries = new ArrayList<>(data.entries().size());
        for (EntryData entryData : data.entries()) {
            Map entity = new HashMap<>(entryData.entity().size());
            for (EntityData entityData : entryData.entity()) {
                entity.put(entityData.entityType(), entityData.entityName());
            }

            List ops = new ArrayList<>(entryData.ops().size());
            for (OpData opData : entryData.ops()) {
                Double value = opData.remove() ? null : opData.value();
                ops.add(new ClientQuotaAlteration.Op(opData.key(), value));
            }

            entries.add(new ClientQuotaAlteration(new ClientQuotaEntity(entity), ops));
        }
        return entries;
    }

    public boolean validateOnly() {
        return data.validateOnly();
    }

    @Override
    public AlterClientQuotasResponse getErrorResponse(int throttleTimeMs, Throwable e) {
        ArrayList entities = new ArrayList<>(data.entries().size());
        for (EntryData entryData : data.entries()) {
            Map entity = new HashMap<>(entryData.entity().size());
            for (EntityData entityData : entryData.entity()) {
                entity.put(entityData.entityType(), entityData.entityName());
            }
            entities.add(new ClientQuotaEntity(entity));
        }
        return new AlterClientQuotasResponse(entities, throttleTimeMs, e);
    }

    @Override
    protected Struct toStruct() {
        return data.toStruct(version());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy