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()));
}
}
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());
}
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());
}
}
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);
}
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);
}
Aggregations