Skip to content

Commit

Permalink
KAA-25: Implement server profile update via REST API and Endpoint pro…
Browse files Browse the repository at this point in the history
…file UI.
  • Loading branch information
ikulikov committed Dec 10, 2015
1 parent 46e064a commit 2cf6647
Show file tree
Hide file tree
Showing 63 changed files with 971 additions and 1,033 deletions.
2 changes: 1 addition & 1 deletion common/endpoint-shared/pom.xml
Expand Up @@ -144,10 +144,10 @@
<excludes>
<exclude>**/gen/*.java</exclude>
<exclude>**/*.avsc</exclude>
<exclude>src/main/java/org/kaaproject/kaa/schema/base/Profile.java</exclude>
<exclude>src/main/java/org/kaaproject/kaa/schema/base/Log.java</exclude>
<exclude>src/main/java/org/kaaproject/kaa/schema/base/Notification.java</exclude>
<exclude>src/main/java/org/kaaproject/kaa/schema/base/Configuration.java</exclude>
<exclude>src/main/java/org/kaaproject/kaa/schema/system/EmptyData.java</exclude>
</excludes>
</configuration>
</plugin>
Expand Down
Expand Up @@ -2,5 +2,7 @@
"type": "record",
"name": "EmptyData",
"namespace": "org.kaaproject.kaa.schema.system",
"version": 1,
"dependencies": [],
"fields": []
}
Expand Up @@ -7,7 +7,7 @@
@SuppressWarnings("all")
@org.apache.avro.specific.AvroGenerated
public class EmptyData extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"EmptyData\",\"namespace\":\"org.kaaproject.kaa.schema.system\",\"fields\":[]}");
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"EmptyData\",\"namespace\":\"org.kaaproject.kaa.schema.system\",\"fields\":[],\"version\":1,\"dependencies\":[]}");
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }

public org.apache.avro.Schema getSchema() { return SCHEMA$; }
Expand Down
Expand Up @@ -180,9 +180,15 @@ public void doAppendClosedTest() throws NoSuchFieldException, SecurityException,

logAppender.close();
TestLogDeliveryCallback callback = new TestLogDeliveryCallback();

LogSchemaDto schemaDto = new LogSchemaDto();
schemaDto.setSchema(BasicEndpointProfile.SCHEMA$.toString());
LogSchema schema = new LogSchema(schemaDto);

EndpointProfileDataDto profileDto = new EndpointProfileDataDto("1", ENDPOINT_KEY, 1, "test", 0, null);
BaseLogEventPack logEventPack = new BaseLogEventPack(profileDto, DATE_CREATED, 1, null);

BaseLogEventPack logEventPack = new BaseLogEventPack(profileDto, DATE_CREATED, schema.getVersion(), null);
logEventPack.setLogSchema(schema);

logAppender.doAppend(logEventPack, callback);
Assert.assertTrue(callback.internallError);
}
Expand All @@ -207,8 +213,10 @@ public void doAppendWithCatchIOExceptionTest() throws NoSuchFieldException, Secu
schemaDto.setSchema(BasicEndpointProfile.SCHEMA$.toString());
LogSchema schema = new LogSchema(schemaDto);


EndpointProfileDataDto profileDto = new EndpointProfileDataDto("1", ENDPOINT_KEY, 1, "test", 0, null);
BaseLogEventPack logEventPack = new BaseLogEventPack(profileDto, DATE_CREATED, schema.getVersion(), events);
logEventPack.setLogSchema(schema);

TestLogDeliveryCallback callback = new TestLogDeliveryCallback();
logAppender.doAppend(logEventPack, callback);
Expand Down
Expand Up @@ -138,6 +138,14 @@ public EndpointProfileBodyDto getEndpointProfileBodyByKeyHash(String endpointPro
ResponseEntity<EndpointProfileBodyDto> entity = restTemplate.exchange(url + "endpointProfileBody/" + endpointProfileKeyHash, HttpMethod.GET, null, typeRef);
return entity.getBody();
}

public EndpointProfileDto updateServerProfile(String endpointProfileKey, int version, String serverProfileBody) throws Exception {
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("endpointProfileKey", endpointProfileKey);
params.add("version", version);
params.add("serverProfileBody", serverProfileBody);
return restTemplate.postForObject(url + "updateServerProfile", params, EndpointProfileDto.class);
}

public AuthResultDto checkAuth() throws Exception {
return restTemplate.getForObject(url + "auth/checkAuth", AuthResultDto.class);
Expand Down
Expand Up @@ -84,10 +84,11 @@ public interface ServerProfileService {
/**
* Save server profile data to endpoint profile.
*
* @param keyHash the endpoint key hash identifier.
* @param keyHash the endpoint key hash identifier.
* @param version the server profile schema version
* @param serverProfile server profile data in string representation.
* @return the saved endpoint profile.
*/
EndpointProfileDto saveServerProfile(byte[] keyHash, String serverProfile);
EndpointProfileDto saveServerProfile(byte[] keyHash, int version, String serverProfile);

}
Expand Up @@ -65,16 +65,7 @@ public SdkProfileDto saveSdkProfile(SdkProfileDto sdkProfileDto) {
if (StringUtils.isNotBlank(sdkProfileDto.getId())) {
throw new IncorrectParameterException("Update of existing SDK profile is prohibited.");
} else {
if (StringUtils.isEmpty(sdkProfileDto.getToken())) {
try {
MessageDigest messageDigest = MessageDigest.getInstance(SDK_TOKEN_HASH_ALGORITHM);
messageDigest.update(DtoByteMarshaller.toBytes(sdkProfileDto.toSdkTokenDto()));
String token = Base64.encodeBase64String(messageDigest.digest());
sdkProfileDto.setToken(token);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
SdkTokenGenerator.generateSdkToken(sdkProfileDto);
SdkProfile entity = new SdkProfile(sdkProfileDto);
SdkProfile loaded = sdkProfileDao.findSdkProfileByToken(entity.getToken());
if (loaded == null) {
Expand Down
@@ -0,0 +1,44 @@
/*
* Copyright 2014-2015 CyberVision, Inc.
*
* Licensed 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.kaaproject.kaa.server.common.dao.service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.kaaproject.kaa.common.dto.DtoByteMarshaller;
import org.kaaproject.kaa.common.dto.admin.SdkProfileDto;

public class SdkTokenGenerator {

private static final String SDK_TOKEN_HASH_ALGORITHM = "SHA1";

public static void generateSdkToken(SdkProfileDto sdkProfileDto) {
if (StringUtils.isEmpty(sdkProfileDto.getToken())) {
try {
MessageDigest messageDigest = MessageDigest.getInstance(SDK_TOKEN_HASH_ALGORITHM);
messageDigest.update(DtoByteMarshaller.toBytes(sdkProfileDto.toSdkTokenDto()));
String token = Base64.encodeBase64String(messageDigest.digest());
sdkProfileDto.setToken(token);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}

}
Expand Up @@ -22,13 +22,11 @@
import static org.kaaproject.kaa.server.common.dao.impl.DaoUtil.getDto;
import static org.kaaproject.kaa.server.common.dao.service.Validator.validateId;

import java.nio.ByteBuffer;
import java.util.List;

import org.kaaproject.kaa.common.dto.EndpointProfileDto;
import org.kaaproject.kaa.common.dto.ServerProfileSchemaDto;
import org.kaaproject.kaa.server.common.dao.ServerProfileService;
import org.kaaproject.kaa.server.common.dao.exception.DatabaseProcessingException;
import org.kaaproject.kaa.server.common.dao.exception.IncorrectParameterException;
import org.kaaproject.kaa.server.common.dao.impl.EndpointProfileDao;
import org.kaaproject.kaa.server.common.dao.impl.ServerProfileSchemaDao;
Expand All @@ -41,6 +39,7 @@
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ServerProfileServiceImpl implements ServerProfileService {

private static final Logger LOG = LoggerFactory.getLogger(ServerProfileServiceImpl.class);
Expand All @@ -51,7 +50,6 @@ public class ServerProfileServiceImpl implements ServerProfileService {
private EndpointProfileDao<EndpointProfile> endpointProfileDao;

@Override
@Transactional
public ServerProfileSchemaDto saveServerProfileSchema(ServerProfileSchemaDto dto) {
Validator.validateObject(dto, "Incorrect server profile schema object.");
String appId = dto.getApplicationId();
Expand Down Expand Up @@ -82,21 +80,18 @@ public ServerProfileSchemaDto saveServerProfileSchema(ServerProfileSchemaDto dto
}

@Override
@Transactional
public ServerProfileSchemaDto findLatestServerProfileSchema(String appId) {
Validator.validateId(appId, "Incorrect application id.");
return getDto(serverProfileSchemaDao.findLatestByAppId(appId));
}

@Override
@Transactional
public ServerProfileSchemaDto findServerProfileSchema(String schemaId) {
Validator.validateId(schemaId, "Incorrect server profile schema id.");
return getDto(serverProfileSchemaDao.findById(schemaId));
}

@Override
@Transactional
public List<ServerProfileSchemaDto> findServerProfileSchemasByAppId(String appId) {
Validator.validateId(appId, "Incorrect application id.");
return convertDtoList(serverProfileSchemaDao.findByAppId(appId));
Expand All @@ -109,30 +104,21 @@ public ServerProfileSchemaDto findServerProfileSchemaByAppIdAndVersion(String ap
}

@Override
@Transactional
public void removeServerProfileSchemaById(String profileId) {
Validator.validateId(profileId, "Incorrect server profile schema id.");
serverProfileSchemaDao.removeById(profileId);
}

@Override
@Transactional
public void removeServerProfileSchemaByAppId(String appId) {
Validator.validateId(appId, "Incorrect application id.");
serverProfileSchemaDao.removeByAppId(appId);
}

@Override
public EndpointProfileDto saveServerProfile(byte[] keyHash, String serverProfile) {
public EndpointProfileDto saveServerProfile(byte[] keyHash, int version, String serverProfile) {
Validator.validateHash(keyHash, "Incorrect endpoint key hash.");
EndpointProfile ep = endpointProfileDao.findById(ByteBuffer.wrap(keyHash));
if (ep != null) {
int schemaVersion = ep.getServerProfileVersion();
ep = endpointProfileDao.updateServerProfile(keyHash, schemaVersion, serverProfile);
} else {
throw new DatabaseProcessingException("Can't find endpoint profile by key hash " + keyHash);
}
return ep != null ? ep.toDto() : null;
return getDto(endpointProfileDao.updateServerProfile(keyHash, version, serverProfile));
}

public void setEndpointProfileDao(EndpointProfileDao<EndpointProfile> endpointProfileDao) {
Expand Down
Expand Up @@ -16,12 +16,28 @@

package org.kaaproject.kaa.server.common.dao.impl.sql;

import static org.apache.commons.lang.StringUtils.isBlank;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.junit.Assert;
import org.kaaproject.kaa.common.dto.KaaAuthorityDto;
import org.kaaproject.kaa.common.dto.NotificationTypeDto;
import org.kaaproject.kaa.common.dto.TopicTypeDto;
import org.kaaproject.kaa.common.dto.UpdateStatus;
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaDto;
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaScopeDto;
import org.kaaproject.kaa.common.dto.event.ApplicationEventAction;
import org.kaaproject.kaa.common.dto.event.EventClassType;
import org.kaaproject.kaa.server.common.core.schema.KaaSchemaFactoryImpl;
Expand Down Expand Up @@ -52,21 +68,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import static org.apache.commons.lang.StringUtils.isBlank;

public abstract class HibernateAbstractTest extends AbstractTest {

private static final Logger LOG = LoggerFactory.getLogger(HibernateAbstractTest.class);
Expand Down Expand Up @@ -216,9 +217,7 @@ protected List<ProfileSchema> generateProfSchema(Application app, int count) {
if (app == null) {
app = generateApplication(null);
}

CTLSchemaDto ctlSchemaDto = ctlService.saveCTLSchema(generateCTLSchemaDto(DEFAULT_FQN, app.getTenant().getStringId(), 1, null));
CTLSchema ctlSchema = ctlSchemaDao.findById(ctlSchemaDto.getId());
CTLSchema ctlSchema = generateCTLSchema(DEFAULT_FQN, 1, app.getTenant(), null);
ProfileSchema schemaDto;
schemas = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
Expand All @@ -238,6 +237,28 @@ protected List<ProfileSchema> generateProfSchema(Application app, int count) {
}
return schemas;
}

protected CTLSchema generateCTLSchema(String fqn, int version, Tenant tenant, CTLSchemaScopeDto scope) {
if (scope == null) {
if (tenant == null) {
scope = CTLSchemaScopeDto.SYSTEM;
} else {
scope = CTLSchemaScopeDto.TENANT;
}
}
CTLSchemaMetaInfo metaInfo = new CTLSchemaMetaInfo();
metaInfo.setVersion(version);
metaInfo.setFqn(fqn);
metaInfo.setScope(scope);
metaInfo = ctlSchemaMetaInfoDao.save(metaInfo);
CTLSchema ctlSchema = new CTLSchema();
ctlSchema.setTenant(tenant);
ctlSchema.setBody(UUID.randomUUID().toString());
ctlSchema.setDependencySet(new HashSet<CTLSchema>());
ctlSchema.setMetaInfo(metaInfo);
ctlSchema = ctlSchemaDao.save(ctlSchema);
return ctlSchema;
}

protected List<NotificationSchema> generateNotificationSchema(Application app, int count, NotificationTypeDto type) {
List<NotificationSchema> schemas = Collections.emptyList();
Expand Down Expand Up @@ -487,17 +508,4 @@ protected SdkProfile generateSdkProfile(Application application, String token) {
return sdkProfileDao.save(entity);
}

protected CTLSchema generateCTLSchema(String fqn, Tenant tenant, int version, String body) {
CTLSchema ctlSchema = new CTLSchema();
ctlSchema.setMetaInfo(new CTLSchemaMetaInfo(fqn, version));
if (isBlank(body)) {
body = UUID.randomUUID().toString();
}
ctlSchema.setBody(body);
if (tenant == null) {
tenant = generateTenant();
}
ctlSchema.setTenant(tenant);
return ctlSchema;
}
}
Expand Up @@ -16,12 +16,14 @@

package org.kaaproject.kaa.server.common.dao.impl.sql;

import static org.apache.commons.lang.StringUtils.isBlank;
import static org.kaaproject.kaa.server.common.dao.impl.DaoUtil.convertDtoList;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.junit.Assert;
import org.junit.Before;
Expand All @@ -31,6 +33,7 @@
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaDto;
import org.kaaproject.kaa.server.common.dao.CTLService;
import org.kaaproject.kaa.server.common.dao.model.sql.CTLSchema;
import org.kaaproject.kaa.server.common.dao.model.sql.CTLSchemaMetaInfo;
import org.kaaproject.kaa.server.common.dao.model.sql.Tenant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -91,6 +94,20 @@ public void saveCTLSchemaWithSameFqnAndVersion() {
ctlSchemaDao.save(generateCTLSchema(DEFAULT_FQN, new Tenant(tenant), 11, null));
ctlSchemaDao.save(generateCTLSchema(DEFAULT_FQN, null, 11, null));
}

private CTLSchema generateCTLSchema(String fqn, Tenant tenant, int version, String body) {
CTLSchema ctlSchema = new CTLSchema();
ctlSchema.setMetaInfo(new CTLSchemaMetaInfo(fqn, version));
if (isBlank(body)) {
body = UUID.randomUUID().toString();
}
ctlSchema.setBody(body);
if (tenant == null) {
tenant = generateTenant();
}
ctlSchema.setTenant(tenant);
return ctlSchema;
}

@Test
public void saveCTLSchemaWithDependency() throws InterruptedException {
Expand Down

0 comments on commit 2cf6647

Please sign in to comment.