Search in sources :

Example 6 with Inspector

use of com.yahoo.slime.Inspector in project vespa by vespa-engine.

the class BinaryFormat method decode.

private static Predicate decode(Inspector in) {
    switch((int) in.field(NODE_TYPE).asLong()) {
        case TYPE_CONJUNCTION:
            Conjunction conjunction = new Conjunction();
            in = in.field(CHILDREN);
            for (int i = 0, len = in.children(); i < len; ++i) {
                conjunction.addOperand(decode(in.entry(i)));
            }
            return conjunction;
        case TYPE_DISJUNCTION:
            Disjunction disjunction = new Disjunction();
            in = in.field(CHILDREN);
            for (int i = 0, len = in.children(); i < len; ++i) {
                disjunction.addOperand(decode(in.entry(i)));
            }
            return disjunction;
        case TYPE_NEGATION:
            return new Negation(decode(in.field(CHILDREN).entry(0)));
        case TYPE_FEATURE_RANGE:
            FeatureRange featureRange = new FeatureRange(in.field(KEY).asString());
            if (in.field(RANGE_MIN).valid()) {
                featureRange.setFromInclusive(in.field(RANGE_MIN).asLong());
            }
            if (in.field(RANGE_MAX).valid()) {
                featureRange.setToInclusive(in.field(RANGE_MAX).asLong());
            }
            Inspector p_in = in.field(PARTITIONS);
            for (int i = 0, len = p_in.children(); i < len; ++i) {
                featureRange.addPartition(new RangePartition(p_in.entry(i).asString()));
            }
            p_in = in.field(EDGE_PARTITIONS);
            for (int i = 0, len = p_in.children(); i < len; ++i) {
                Inspector obj = p_in.entry(i);
                featureRange.addPartition(new RangeEdgePartition(obj.field(LABEL).asString(), obj.field(VALUE).asLong(), (int) obj.field(LOWER_BOUND).asLong(), (int) obj.field(UPPER_BOUND).asLong()));
            }
            return featureRange;
        case TYPE_FEATURE_SET:
            FeatureSet featureSet = new FeatureSet(in.field(KEY).asString());
            in = in.field(SET);
            for (int i = 0, len = in.children(); i < len; ++i) {
                featureSet.addValue(in.entry(i).asString());
            }
            return featureSet;
        case TYPE_TRUE:
            return new BooleanPredicate(true);
        case TYPE_FALSE:
            return new BooleanPredicate(false);
        default:
            throw new UnsupportedOperationException(String.valueOf(in.field(NODE_TYPE).asLong()));
    }
}
Also used : Inspector(com.yahoo.slime.Inspector)

Example 7 with Inspector

use of com.yahoo.slime.Inspector in project vespa by vespa-engine.

the class BinaryFormatTest method requireThatPartitionedFeatureRangeCanBeSerialized.

@Test
public void requireThatPartitionedFeatureRangeCanBeSerialized() {
    FeatureRange expected = new FeatureRange("foo", 8L, 20L);
    FeatureRange f = new FeatureRange("foo", 8L, 20L);
    f.addPartition(new RangeEdgePartition("foo=0", 0, 8, -1));
    f.addPartition(new RangeEdgePartition("foo=20", 20, 0, 0));
    f.addPartition(new RangePartition("foo", 10, 19, false));
    assertSerializesTo(expected, f);
    Slime slime = com.yahoo.slime.BinaryFormat.decode(BinaryFormat.encode(f));
    assertEquals(BinaryFormat.TYPE_FEATURE_RANGE, slime.get().field(BinaryFormat.NODE_TYPE).asLong());
    Inspector in1 = slime.get().field(BinaryFormat.HASHED_PARTITIONS);
    assertEquals(1, in1.entries());
    assertEquals(0xf2b6d1cc6322cb99L, in1.entry(0).asLong());
    Inspector in2 = slime.get().field(BinaryFormat.HASHED_EDGE_PARTITIONS);
    assertEquals(2, in2.entries());
    Inspector obj1 = in2.entry(0);
    assertEquals(0xb2b301e26efffdc2L, obj1.field(BinaryFormat.HASH).asLong());
    assertEquals(0, obj1.field(BinaryFormat.VALUE).asLong());
    assertEquals(0x80000008L, obj1.field(BinaryFormat.PAYLOAD).asLong());
    Inspector obj2 = in2.entry(1);
    assertEquals(0x22acb2ed72523c36L, obj2.field(BinaryFormat.HASH).asLong());
    assertEquals(20, obj2.field(BinaryFormat.VALUE).asLong());
    assertEquals(0x40000001L, obj2.field(BinaryFormat.PAYLOAD).asLong());
}
Also used : Inspector(com.yahoo.slime.Inspector) Slime(com.yahoo.slime.Slime) Test(org.junit.Test)

Example 8 with Inspector

use of com.yahoo.slime.Inspector in project vespa by vespa-engine.

the class ApplicationApiHandler method setGlobalRotationOverride.

private HttpResponse setGlobalRotationOverride(String tenantName, String applicationName, String instanceName, String environment, String region, boolean inService, HttpRequest request) {
    // Check if request is authorized
    Optional<Tenant> existingTenant = controller.tenants().tenant(new TenantId(tenantName));
    if (!existingTenant.isPresent())
        return ErrorResponse.notFoundError("Tenant '" + tenantName + "' does not exist");
    // Decode payload (reason) and construct parameter to the configserver
    Inspector requestData = toSlime(request.getData()).get();
    String reason = mandatory("reason", requestData).asString();
    String agent = getUserPrincipal(request).getIdentity().getFullName();
    long timestamp = controller.clock().instant().getEpochSecond();
    EndpointStatus.Status status = inService ? EndpointStatus.Status.in : EndpointStatus.Status.out;
    EndpointStatus endPointStatus = new EndpointStatus(status, reason, agent, timestamp);
    // DeploymentId identifies the zone and application we are dealing with
    DeploymentId deploymentId = new DeploymentId(ApplicationId.from(tenantName, applicationName, instanceName), ZoneId.from(environment, region));
    try {
        List<String> rotations = controller.applications().setGlobalRotationStatus(deploymentId, endPointStatus);
        return new MessageResponse(String.format("Rotations %s successfully set to %s service", rotations.toString(), inService ? "in" : "out of"));
    } catch (IOException e) {
        return ErrorResponse.internalServerError("Unable to alter rotation status: " + e.getMessage());
    }
}
Also used : EndpointStatus(com.yahoo.vespa.hosted.controller.api.application.v4.model.EndpointStatus) TenantId(com.yahoo.vespa.hosted.controller.api.identifiers.TenantId) DeploymentId(com.yahoo.vespa.hosted.controller.api.identifiers.DeploymentId) Tenant(com.yahoo.vespa.hosted.controller.api.Tenant) MessageResponse(com.yahoo.vespa.hosted.controller.restapi.MessageResponse) Inspector(com.yahoo.slime.Inspector) IOException(java.io.IOException)

Example 9 with Inspector

use of com.yahoo.slime.Inspector in project vespa by vespa-engine.

the class ApplicationApiHandler method createTenant.

private HttpResponse createTenant(String tenantName, HttpRequest request) {
    if (new TenantId(tenantName).isUser())
        return ErrorResponse.badRequest("Use User API to create user tenants.");
    Inspector requestData = toSlime(request.getData()).get();
    Tenant tenant = new Tenant(new TenantId(tenantName), optional("property", requestData).map(Property::new), optional("athensDomain", requestData).map(AthenzDomain::new), optional("propertyId", requestData).map(PropertyId::new));
    if (tenant.isAthensTenant())
        throwIfNotAthenzDomainAdmin(new AthenzDomain(mandatory("athensDomain", requestData).asString()), request);
    NToken token = getUserPrincipal(request).getNToken().orElseThrow(() -> new IllegalArgumentException("Could not create " + tenant + ": No NToken provided"));
    controller.tenants().createAthenzTenant(tenant, token);
    return tenant(tenant, request, true);
}
Also used : TenantId(com.yahoo.vespa.hosted.controller.api.identifiers.TenantId) Tenant(com.yahoo.vespa.hosted.controller.api.Tenant) AthenzDomain(com.yahoo.vespa.athenz.api.AthenzDomain) NToken(com.yahoo.vespa.athenz.api.NToken) Inspector(com.yahoo.slime.Inspector)

Example 10 with Inspector

use of com.yahoo.slime.Inspector in project vespa by vespa-engine.

the class ApplicationApiHandler method updateTenant.

private HttpResponse updateTenant(String tenantName, HttpRequest request) {
    Optional<Tenant> existingTenant = controller.tenants().tenant(new TenantId(tenantName));
    if (!existingTenant.isPresent())
        return ErrorResponse.notFoundError("Tenant '" + tenantName + "' does not exist");
    ;
    Inspector requestData = toSlime(request.getData()).get();
    Tenant updatedTenant;
    switch(existingTenant.get().tenantType()) {
        case USER:
            {
                throw new BadRequestException("Cannot set property or OpsDB user group for user tenant");
            }
        case ATHENS:
            {
                updatedTenant = Tenant.createAthensTenant(new TenantId(tenantName), new AthenzDomain(mandatory("athensDomain", requestData).asString()), new Property(mandatory("property", requestData).asString()), optional("propertyId", requestData).map(PropertyId::new));
                controller.tenants().updateTenant(updatedTenant, getUserPrincipal(request).getNToken());
                break;
            }
        default:
            {
                throw new BadRequestException("Unknown tenant type: " + existingTenant.get().tenantType());
            }
    }
    return tenant(updatedTenant, request, true);
}
Also used : TenantId(com.yahoo.vespa.hosted.controller.api.identifiers.TenantId) Tenant(com.yahoo.vespa.hosted.controller.api.Tenant) AthenzDomain(com.yahoo.vespa.athenz.api.AthenzDomain) Inspector(com.yahoo.slime.Inspector) BadRequestException(javax.ws.rs.BadRequestException) Property(com.yahoo.vespa.hosted.controller.api.identifiers.Property)

Aggregations

Inspector (com.yahoo.slime.Inspector)27 ArrayTraverser (com.yahoo.slime.ArrayTraverser)10 Slime (com.yahoo.slime.Slime)10 IOException (java.io.IOException)6 Cursor (com.yahoo.slime.Cursor)4 SlimeUtils (com.yahoo.vespa.config.SlimeUtils)4 CompressionType (com.yahoo.compress.CompressionType)3 ApplicationId (com.yahoo.config.provision.ApplicationId)3 JsonDecoder (com.yahoo.slime.JsonDecoder)3 Tenant (com.yahoo.vespa.hosted.controller.api.Tenant)3 TenantId (com.yahoo.vespa.hosted.controller.api.identifiers.TenantId)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 Version (com.yahoo.component.Version)2 ObjectTraverser (com.yahoo.slime.ObjectTraverser)2 Utf8Array (com.yahoo.text.Utf8Array)2 AthenzDomain (com.yahoo.vespa.athenz.api.AthenzDomain)2 Change (com.yahoo.vespa.hosted.controller.application.Change)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2