restructure package for trial

Original commit: elastic/x-pack-elasticsearch@0ae53fab2e
This commit is contained in:
Areek Zillur 2014-10-14 14:07:21 -04:00
parent 52d29f4fbb
commit 6c16d562bc
8 changed files with 201 additions and 181 deletions

View File

@ -11,18 +11,12 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicenseBuilders;
import org.elasticsearch.license.plugin.core.TrialLicenses;
import org.elasticsearch.license.plugin.core.TrialLicensesBuilder;
import java.io.IOException;
import java.util.*;
import static org.elasticsearch.license.core.ESLicenses.*;
import static org.elasticsearch.license.manager.Utils.getESLicenseFromSignature;
import static org.elasticsearch.license.plugin.core.TrialLicenses.TrialLicense;
import static org.elasticsearch.license.plugin.core.TrialLicensesBuilder.fromEncodedTrialLicense;
import static org.elasticsearch.license.plugin.core.TrialLicensesBuilder.toEncodedTrialLicense;
import static org.elasticsearch.license.plugin.core.TrialLicensesBuilder.trialLicensesBuilder;
public class Utils {
@ -40,41 +34,6 @@ public class Utils {
}
}
public static TrialLicenses readTrialLicensesFromMetaData(StreamInput in) throws IOException {
boolean exists = in.readBoolean();
return exists ? fromEncodedTrialLicenses(in.readStringArray()) : null;
}
public static void writeTrialLicensesToMetaData(TrialLicenses trialLicenses, StreamOutput out) throws IOException {
if (trialLicenses == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeStringArray(toEncodedTrialLicenses(trialLicenses));
}
}
public static String[] toEncodedTrialLicenses(TrialLicenses trialLicenses) {
Set<String> encodedTrialLicenses = new HashSet<>();
for (TrialLicense trialLicense : trialLicenses) {
encodedTrialLicenses.add(toEncodedTrialLicense(trialLicense));
}
return encodedTrialLicenses.toArray(new String[encodedTrialLicenses.size()]);
}
public static TrialLicenses fromEncodedTrialLicenses(String[] encodedTrialLicenses) {
final TrialLicensesBuilder trialLicensesBuilder = trialLicensesBuilder();
for (String encodedTrialLicense : encodedTrialLicenses) {
trialLicensesBuilder.license(fromEncodedTrialLicense(encodedTrialLicense));
}
return trialLicensesBuilder.build();
}
public static TrialLicenses fromEncodedTrialLicenses(Set<String> encodedTrialLicenses) {
return fromEncodedTrialLicenses(encodedTrialLicenses.toArray(new String[encodedTrialLicenses.size()]));
}
public static String[] toSignatures(ESLicenses esLicenses) {
Set<String> signatures = new HashSet<>();
for (ESLicense esLicense : esLicenses) {
@ -120,57 +79,6 @@ public class Utils {
}
}
public static TrialLicenses readTrialLicensesFrom(StreamInput in) throws IOException {
final TrialLicensesBuilder licensesBuilder = TrialLicensesBuilder.trialLicensesBuilder();
boolean exists = in.readBoolean();
if (exists) {
int size = in.readVInt();
for (int i = 0; i < size; i++) {
licensesBuilder.license(trialLicenseFromMap(in.readMap()));
}
return licensesBuilder.build();
}
return null;
}
public static void writeTrialLicensesTo(TrialLicenses trialLicenses, StreamOutput out) throws IOException {
if (trialLicenses == null) {
out.writeBoolean(false);
return;
}
out.writeBoolean(true);
out.writeVInt(trialLicenses.trialLicenses().size());
for (TrialLicense trialLicense : trialLicenses) {
out.writeMap(trialLicenseAsMap(trialLicense));
}
}
// TODO: make sure field order is preserved
public static Map<String, Object> trialLicenseAsMap(TrialLicense trialLicense) {
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
builder.put(TrialLicenseFields.UID, trialLicense.uid());
builder.put(TrialLicenseFields.TYPE, Type.TRIAL.string());
builder.put(TrialLicenseFields.SUBSCRIPTION_TYPE, SubscriptionType.NONE.string());
builder.put(TrialLicenseFields.ISSUE_DATE, trialLicense.issueDate());
builder.put(TrialLicenseFields.FEATURE, trialLicense.feature().string());
builder.put(TrialLicenseFields.EXPIRY_DATE, trialLicense.expiryDate());
builder.put(TrialLicenseFields.MAX_NODES, trialLicense.maxNodes());
builder.put(TrialLicenseFields.ISSUED_TO, trialLicense.issuedTo());
return builder.build();
}
public static TrialLicense trialLicenseFromMap(Map<String, Object> map) {
return TrialLicensesBuilder.trialLicenseBuilder()
.uid((String) map.get(TrialLicenseFields.UID))
.issuedTo((String) map.get(TrialLicenseFields.ISSUED_TO))
.maxNodes((int) map.get(TrialLicenseFields.MAX_NODES))
.feature(FeatureType.fromString((String) map.get(TrialLicenseFields.FEATURE)))
.issueDate((long) map.get(TrialLicenseFields.ISSUE_DATE))
.expiryDate((long) map.get(TrialLicenseFields.EXPIRY_DATE))
.build();
}
// TODO: make sure field order is preserved
public static Map<String, Object> licenseAsMap(ESLicense esLicense) {
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
@ -201,17 +109,6 @@ public class Utils {
}
final static class TrialLicenseFields {
private final static String UID = "uid";
private final static String TYPE = "type";
private final static String SUBSCRIPTION_TYPE = "subscription_type";
private final static String ISSUE_DATE = "issue_date";
private final static String FEATURE = "feature";
private final static String ISSUED_TO = "issued_to";
private final static String MAX_NODES = "max_nodes";
private final static String EXPIRY_DATE = "expiry_date";
}
final static class LicenseFields {
private final static String UID = "uid";
private final static String TYPE = "type";

View File

@ -10,8 +10,9 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.core.LicenseBuilders;
import org.elasticsearch.license.plugin.core.TrialLicenses;
import org.elasticsearch.license.plugin.core.TrialLicensesBuilder;
import org.elasticsearch.license.plugin.core.trial.TrialLicenseUtils;
import org.elasticsearch.license.plugin.core.trial.TrialLicenses;
import org.elasticsearch.license.plugin.core.trial.TrialLicensesBuilder;
import java.io.IOException;
@ -42,14 +43,14 @@ public class GetLicenseResponse extends ActionResponse {
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
licenses = readGeneratedLicensesFrom(in);
trialLicenses = readTrialLicensesFrom(in);
trialLicenses = TrialLicenseUtils.readTrialLicensesFrom(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
writeGeneratedLicensesTo(licenses, out);
writeTrialLicensesTo(trialLicenses, out);
TrialLicenseUtils.writeTrialLicensesTo(trialLicenses, out);
}
}

View File

@ -12,6 +12,8 @@ import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.plugin.core.trial.TrialLicenseUtils;
import org.elasticsearch.license.plugin.core.trial.TrialLicenses;
import java.io.IOException;
import java.util.*;
@ -73,7 +75,7 @@ public class LicensesMetaData implements MetaData.Custom {
TrialLicenses trialLicenses = null;
if (in.readBoolean()) {
esLicenses = readGeneratedLicensesFromMetaData(in);
trialLicenses = readTrialLicensesFromMetaData(in);
trialLicenses = TrialLicenseUtils.readTrialLicensesFromMetaData(in);
}
return new LicensesMetaData(esLicenses, trialLicenses);
}
@ -88,7 +90,7 @@ public class LicensesMetaData implements MetaData.Custom {
} else {
out.writeBoolean(true);
writeGeneratedLicensesToMetaData(licensesMetaData.getLicenses(), out);
writeTrialLicensesToMetaData(licensesMetaData.getTrialLicenses(), out);
TrialLicenseUtils.writeTrialLicensesToMetaData(licensesMetaData.getTrialLicenses(), out);
}
}
@ -120,7 +122,7 @@ public class LicensesMetaData implements MetaData.Custom {
}
}
return new LicensesMetaData(fromSignatures(signatures), fromEncodedTrialLicenses(encodedTrialLicenses));
return new LicensesMetaData(fromSignatures(signatures), TrialLicenseUtils.fromEncodedTrialLicenses(encodedTrialLicenses));
}
/**
@ -130,7 +132,7 @@ public class LicensesMetaData implements MetaData.Custom {
public void toXContent(LicensesMetaData licensesMetaData, XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject();
builder.array(Fields.LICENSES, toSignatures(licensesMetaData.getLicenses()));
builder.array(Fields.TRIAL_LICENSES, toEncodedTrialLicenses(licensesMetaData.getTrialLicenses()));
builder.array(Fields.TRIAL_LICENSES, TrialLicenseUtils.toEncodedTrialLicenses(licensesMetaData.getTrialLicenses()));
builder.endObject();
}

View File

@ -21,13 +21,15 @@ import org.elasticsearch.license.core.LicenseBuilders;
import org.elasticsearch.license.manager.ESLicenseManager;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseRequest;
import org.elasticsearch.license.plugin.action.put.PutLicenseRequest;
import org.elasticsearch.license.plugin.core.trial.TrialLicenses;
import org.elasticsearch.license.plugin.core.trial.TrialLicensesBuilder;
import java.util.HashSet;
import java.util.Set;
import static org.elasticsearch.license.core.ESLicenses.FeatureType;
import static org.elasticsearch.license.plugin.core.TrialLicenses.TrialLicense;
import static org.elasticsearch.license.plugin.core.TrialLicensesBuilder.EMPTY;
import static org.elasticsearch.license.plugin.core.trial.TrialLicenses.TrialLicense;
import static org.elasticsearch.license.plugin.core.trial.TrialLicensesBuilder.EMPTY;
/**
* Service responsible for maintaining and providing access to licenses on nodes.

View File

@ -0,0 +1,181 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.license.plugin.core.trial;
import org.apache.commons.codec.binary.Base64;
import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.license.core.ESLicenses;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static org.elasticsearch.license.plugin.core.trial.TrialLicensesBuilder.trialLicensesBuilder;
public class TrialLicenseUtils {
public static TrialLicenses.TrialLicense fromEncodedTrialLicense(String encodedTrialLicense) {
byte[] encodedBytes = Base64.decodeBase64(encodedTrialLicense);
ByteBuffer byteBuffer = ByteBuffer.wrap(encodedBytes);
int uidLen = byteBuffer.getInt();
byte[] uidBytes = new byte[uidLen];
byteBuffer.get(uidBytes);
String uid = new String(uidBytes, Charset.forName("UTF-8"));
int issuedToLen = byteBuffer.getInt();
byte[] issuedToBytes = new byte[issuedToLen];
byteBuffer.get(issuedToBytes);
String issuedTo = new String(issuedToBytes, Charset.forName("UTF-8"));
int featureLen = byteBuffer.getInt();
byte[] featureBytes = new byte[featureLen];
byteBuffer.get(featureBytes);
String feature = new String(featureBytes, Charset.forName("UTF-8"));
int maxNodes = byteBuffer.getInt();
long issueDate = byteBuffer.getLong();
long expiryDate = byteBuffer.getLong();
return TrialLicensesBuilder.trialLicenseBuilder()
.uid(uid)
.issuedTo(issuedTo)
.feature(ESLicenses.FeatureType.fromString(feature))
.maxNodes(maxNodes)
.issueDate(issueDate)
.expiryDate(expiryDate)
.build();
}
public static String toEncodedTrialLicense(TrialLicenses.TrialLicense trialLicense) {
byte[] uidBytes = trialLicense.uid().getBytes(Charset.forName("UTF-8"));
String feature = trialLicense.feature().string();
byte[] featureBytes = feature.getBytes(Charset.forName("UTF-8"));
byte[] issuedToBytes = trialLicense.issuedTo().getBytes(Charset.forName("UTF-8"));
// uid len + uid bytes + issuedTo len + issuedTo bytes + feature bytes length + feature bytes + maxNodes + issueDate + expiryDate
int len = 4 + uidBytes.length + issuedToBytes.length + featureBytes.length + 4 + 8 + 8;
final byte[] encodedLicense = new byte[len];
ByteBuffer byteBuffer = ByteBuffer.wrap(encodedLicense);
byteBuffer.putInt(uidBytes.length);
byteBuffer.put(uidBytes);
byteBuffer.putInt(issuedToBytes.length);
byteBuffer.put(issuedToBytes);
byteBuffer.putInt(featureBytes.length);
byteBuffer.put(featureBytes);
byteBuffer.putInt(trialLicense.maxNodes());
byteBuffer.putLong(trialLicense.issueDate());
byteBuffer.putLong(trialLicense.expiryDate());
return Base64.encodeBase64String(encodedLicense);
}
public static TrialLicenses readTrialLicensesFromMetaData(StreamInput in) throws IOException {
boolean exists = in.readBoolean();
return exists ? fromEncodedTrialLicenses(in.readStringArray()) : null;
}
public static void writeTrialLicensesToMetaData(TrialLicenses trialLicenses, StreamOutput out) throws IOException {
if (trialLicenses == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeStringArray(toEncodedTrialLicenses(trialLicenses));
}
}
public static String[] toEncodedTrialLicenses(TrialLicenses trialLicenses) {
Set<String> encodedTrialLicenses = new HashSet<>();
for (TrialLicenses.TrialLicense trialLicense : trialLicenses) {
encodedTrialLicenses.add(toEncodedTrialLicense(trialLicense));
}
return encodedTrialLicenses.toArray(new String[encodedTrialLicenses.size()]);
}
public static TrialLicenses fromEncodedTrialLicenses(String[] encodedTrialLicenses) {
final TrialLicensesBuilder trialLicensesBuilder = trialLicensesBuilder();
for (String encodedTrialLicense : encodedTrialLicenses) {
trialLicensesBuilder.license(fromEncodedTrialLicense(encodedTrialLicense));
}
return trialLicensesBuilder.build();
}
public static TrialLicenses fromEncodedTrialLicenses(Set<String> encodedTrialLicenses) {
return fromEncodedTrialLicenses(encodedTrialLicenses.toArray(new String[encodedTrialLicenses.size()]));
}
public static TrialLicenses readTrialLicensesFrom(StreamInput in) throws IOException {
final TrialLicensesBuilder licensesBuilder = TrialLicensesBuilder.trialLicensesBuilder();
boolean exists = in.readBoolean();
if (exists) {
int size = in.readVInt();
for (int i = 0; i < size; i++) {
licensesBuilder.license(trialLicenseFromMap(in.readMap()));
}
return licensesBuilder.build();
}
return null;
}
public static void writeTrialLicensesTo(TrialLicenses trialLicenses, StreamOutput out) throws IOException {
if (trialLicenses == null) {
out.writeBoolean(false);
return;
}
out.writeBoolean(true);
out.writeVInt(trialLicenses.trialLicenses().size());
for (TrialLicenses.TrialLicense trialLicense : trialLicenses) {
out.writeMap(trialLicenseAsMap(trialLicense));
}
}
// TODO: make sure field order is preserved
public static Map<String, Object> trialLicenseAsMap(TrialLicenses.TrialLicense trialLicense) {
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
builder.put(TrialLicenseFields.UID, trialLicense.uid());
builder.put(TrialLicenseFields.TYPE, ESLicenses.Type.TRIAL.string());
builder.put(TrialLicenseFields.SUBSCRIPTION_TYPE, ESLicenses.SubscriptionType.NONE.string());
builder.put(TrialLicenseFields.ISSUE_DATE, trialLicense.issueDate());
builder.put(TrialLicenseFields.FEATURE, trialLicense.feature().string());
builder.put(TrialLicenseFields.EXPIRY_DATE, trialLicense.expiryDate());
builder.put(TrialLicenseFields.MAX_NODES, trialLicense.maxNodes());
builder.put(TrialLicenseFields.ISSUED_TO, trialLicense.issuedTo());
return builder.build();
}
public static TrialLicenses.TrialLicense trialLicenseFromMap(Map<String, Object> map) {
return TrialLicensesBuilder.trialLicenseBuilder()
.uid((String) map.get(TrialLicenseFields.UID))
.issuedTo((String) map.get(TrialLicenseFields.ISSUED_TO))
.maxNodes((int) map.get(TrialLicenseFields.MAX_NODES))
.feature(ESLicenses.FeatureType.fromString((String) map.get(TrialLicenseFields.FEATURE)))
.issueDate((long) map.get(TrialLicenseFields.ISSUE_DATE))
.expiryDate((long) map.get(TrialLicenseFields.EXPIRY_DATE))
.build();
}
final static class TrialLicenseFields {
private final static String UID = "uid";
private final static String TYPE = "type";
private final static String SUBSCRIPTION_TYPE = "subscription_type";
private final static String ISSUE_DATE = "issue_date";
private final static String FEATURE = "feature";
private final static String ISSUED_TO = "issued_to";
private final static String MAX_NODES = "max_nodes";
private final static String EXPIRY_DATE = "expiry_date";
}
}

View File

@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.license.plugin.core;
package org.elasticsearch.license.plugin.core.trial;
import java.util.Collection;

View File

@ -3,18 +3,15 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.license.plugin.core;
package org.elasticsearch.license.plugin.core.trial;
import org.apache.commons.codec.binary.Base64;
import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.license.core.DateUtils;
import org.elasticsearch.license.core.ESLicenses;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.*;
import static org.elasticsearch.license.plugin.core.TrialLicenses.TrialLicense;
import static org.elasticsearch.license.plugin.core.trial.TrialLicenses.TrialLicense;
public class TrialLicensesBuilder {
@ -29,66 +26,6 @@ public class TrialLicensesBuilder {
}
public static TrialLicense fromEncodedTrialLicense(String encodedTrialLicense) {
byte[] encodedBytes = Base64.decodeBase64(encodedTrialLicense);
ByteBuffer byteBuffer = ByteBuffer.wrap(encodedBytes);
int uidLen = byteBuffer.getInt();
byte[] uidBytes = new byte[uidLen];
byteBuffer.get(uidBytes);
String uid = new String(uidBytes, Charset.forName("UTF-8"));
int issuedToLen = byteBuffer.getInt();
byte[] issuedToBytes = new byte[issuedToLen];
byteBuffer.get(issuedToBytes);
String issuedTo = new String(issuedToBytes, Charset.forName("UTF-8"));
int featureLen = byteBuffer.getInt();
byte[] featureBytes = new byte[featureLen];
byteBuffer.get(featureBytes);
String feature = new String(featureBytes, Charset.forName("UTF-8"));
int maxNodes = byteBuffer.getInt();
long issueDate = byteBuffer.getLong();
long expiryDate = byteBuffer.getLong();
return trialLicenseBuilder()
.uid(uid)
.issuedTo(issuedTo)
.feature(ESLicenses.FeatureType.fromString(feature))
.maxNodes(maxNodes)
.issueDate(issueDate)
.expiryDate(expiryDate)
.build();
}
public static String toEncodedTrialLicense(TrialLicense trialLicense) {
byte[] uidBytes = trialLicense.uid().getBytes(Charset.forName("UTF-8"));
String feature = trialLicense.feature().string();
byte[] featureBytes = feature.getBytes(Charset.forName("UTF-8"));
byte[] issuedToBytes = trialLicense.issuedTo().getBytes(Charset.forName("UTF-8"));
// uid len + uid bytes + issuedTo len + issuedTo bytes + feature bytes length + feature bytes + maxNodes + issueDate + expiryDate
int len = 4 + uidBytes.length + issuedToBytes.length + featureBytes.length + 4 + 8 + 8;
final byte[] encodedLicense = new byte[len];
ByteBuffer byteBuffer = ByteBuffer.wrap(encodedLicense);
byteBuffer.putInt(uidBytes.length);
byteBuffer.put(uidBytes);
byteBuffer.putInt(issuedToBytes.length);
byteBuffer.put(issuedToBytes);
byteBuffer.putInt(featureBytes.length);
byteBuffer.put(featureBytes);
byteBuffer.putInt(trialLicense.maxNodes());
byteBuffer.putLong(trialLicense.issueDate());
byteBuffer.putLong(trialLicense.expiryDate());
return Base64.encodeBase64String(encodedLicense);
}
public static TrialLicenses merge(TrialLicenses trialLicenses, TrialLicenses mergeTrialLicenses) {
if (trialLicenses == null && mergeTrialLicenses == null) {
throw new IllegalArgumentException("both licenses can not be null");

View File

@ -13,12 +13,12 @@ import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.plugin.action.get.GetLicenseAction;
import org.elasticsearch.license.plugin.action.get.GetLicenseRequest;
import org.elasticsearch.license.plugin.action.get.GetLicenseResponse;
import org.elasticsearch.license.plugin.core.TrialLicenses;
import org.elasticsearch.license.plugin.core.trial.TrialLicenses;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestBuilderListener;
import static org.elasticsearch.license.plugin.action.Utils.licenseAsMap;
import static org.elasticsearch.license.plugin.action.Utils.trialLicenseAsMap;
import static org.elasticsearch.license.plugin.core.trial.TrialLicenseUtils.trialLicenseAsMap;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.OK;