Skip to content

Commit

Permalink
Remove uuid from mergedConfig schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill380 committed Jul 6, 2016
1 parent fa01bb2 commit 63a3155
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
Expand Up @@ -16,10 +16,6 @@

package org.kaaproject.kaa.server.common.core.algorithms;

import static org.kaaproject.kaa.server.common.core.algorithms.CommonConstants.KAA_NAMESPACE;
import static org.kaaproject.kaa.server.common.core.algorithms.CommonConstants.UUID_SIZE;
import static org.kaaproject.kaa.server.common.core.algorithms.CommonConstants.UUID_TYPE;

import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
Expand All @@ -30,6 +26,10 @@
import org.apache.avro.Schema.Type;
import org.apache.avro.generic.GenericData;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;

import static org.kaaproject.kaa.server.common.core.algorithms.CommonConstants.*;

public class AvroUtils {

Expand Down Expand Up @@ -92,4 +92,38 @@ public static void copyJsonProperties(JsonProperties src, JsonProperties dst) {
}
}


public static JsonNode injectUuids(JsonNode json) {
boolean containerWithoutId = json.isContainerNode() && !json.has(UUID_FIELD);
boolean notArray = !(json instanceof ArrayNode);
boolean childIsNotArray = !(json.size() == 1 && json.getElements().next() instanceof ArrayNode);

if (containerWithoutId && notArray && childIsNotArray) {
((ObjectNode)json).put(UUID_FIELD, (Integer)null);
}

for (JsonNode node : json) {
if (node.isContainerNode())
injectUuids(node);
}

return json;
}

public static JsonNode removeUuids(JsonNode json) {
boolean containerWithId = json.isContainerNode() && json.has(UUID_FIELD);
boolean isArray = json.isArray();
boolean childIsNotArray = !(json.size() == 1 && json.getElements().next().isArray());

if (containerWithId && !isArray && childIsNotArray) {
((ObjectNode)json).remove(UUID_FIELD);
}

for (JsonNode node : json) {
if (node.isContainerNode())
injectUuids(node);
}

return json;
}
}
Expand Up @@ -129,6 +129,7 @@
import org.kaaproject.kaa.server.admin.shared.services.KaaAdminService;
import org.kaaproject.kaa.server.admin.shared.services.KaaAdminServiceException;
import org.kaaproject.kaa.server.admin.shared.services.ServiceErrorCode;
import org.kaaproject.kaa.server.common.core.algorithms.AvroUtils;
import org.kaaproject.kaa.server.common.core.schema.KaaSchemaFactoryImpl;
import org.kaaproject.kaa.server.common.dao.exception.NotFoundException;
import org.kaaproject.kaa.server.common.plugin.KaaPluginConfig;
Expand Down Expand Up @@ -2568,7 +2569,7 @@ public RecordField getConfigurationRecordDataFromFile(String schema, String file
byte[] body = getFileContent(fileItemName);

JsonNode json = new ObjectMapper().readTree(body);
json = injectUuids(json);
json = AvroUtils.injectUuids(json);
body = json.toString().getBytes();

GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(schema);
Expand All @@ -2580,22 +2581,7 @@ public RecordField getConfigurationRecordDataFromFile(String schema, String file
}
}

private JsonNode injectUuids(JsonNode json) {
boolean containerWithoutId = json.isContainerNode() && !json.has("__uuid");
boolean notArray = !(json instanceof ArrayNode);
boolean childIsNotArray = !(json.size() == 1 && json.getElements().next() instanceof ArrayNode);

if (containerWithoutId && notArray && childIsNotArray) {
((ObjectNode)json).put("__uuid", (Integer)null);
}

for (JsonNode node : json) {
if (node.isContainerNode())
injectUuids(node);
}

return json;
}

private void checkExpiredDate(NotificationDto notification) throws KaaAdminServiceException {
if (null != notification.getExpiredAt() && notification.getExpiredAt().before(new Date())) {
Expand Down
Expand Up @@ -24,6 +24,8 @@
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.kaaproject.kaa.common.avro.GenericAvroConverter;
import org.kaaproject.kaa.common.dto.ConfigurationDto;
import org.kaaproject.kaa.common.dto.ConfigurationSchemaDto;
Expand All @@ -36,6 +38,7 @@
import org.kaaproject.kaa.common.endpoint.security.MessageEncoderDecoder;
import org.kaaproject.kaa.common.hash.EndpointObjectHash;
import org.kaaproject.kaa.server.common.Base64Util;
import org.kaaproject.kaa.server.common.core.algorithms.AvroUtils;
import org.kaaproject.kaa.server.common.core.algorithms.delta.BaseBinaryDelta;
import org.kaaproject.kaa.server.common.core.algorithms.override.OverrideAlgorithm;
import org.kaaproject.kaa.server.common.core.algorithms.override.OverrideAlgorithmFactory;
Expand Down Expand Up @@ -230,7 +233,7 @@ public ConfigurationCacheEntry compute(DeltaCacheKey deltaKey) {
LOG.debug("[{}] Configuration hash for {} is {}", endpointId, deltaKey,
MessageEncoderDecoder.bytesToHex(deltaCache.getHash().getData()));
return deltaCache;
} catch (GetDeltaException e) {
} catch (GetDeltaException | IOException e) {
throw new RuntimeException(e); // NOSONAR
}
}
Expand Down Expand Up @@ -356,9 +359,10 @@ public BaseData compute(List<EndpointGroupStateDto> key) {
return mergedConfiguration;
}

private ConfigurationCacheEntry buildBaseResyncDelta(String endpointId, RawData mergedConfiguration, EndpointObjectHash userConfigurationHash) {
byte[] configuration = GenericAvroConverter.toRawData(mergedConfiguration.getRawData(), mergedConfiguration.getSchema()
.getRawSchema());
private ConfigurationCacheEntry buildBaseResyncDelta(String endpointId, RawData rawMergedConf, EndpointObjectHash userConfigurationHash) throws IOException {
JsonNode json = new ObjectMapper().readTree(rawMergedConf.getRawData());
json = AvroUtils.removeUuids(json);
byte[] configuration = GenericAvroConverter.toRawData(json.toString(), rawMergedConf.getSchema().getRawSchema());
return new ConfigurationCacheEntry(configuration, new BaseBinaryDelta(configuration), EndpointObjectHash.fromSHA1(configuration),
userConfigurationHash);
}
Expand Down

0 comments on commit 63a3155

Please sign in to comment.