Skip to content

Commit

Permalink
Fixes #472
Browse files Browse the repository at this point in the history
  • Loading branch information
aedelmann committed Dec 27, 2016
2 parents 5254c44 + eb473ab commit 13c88af
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright (c) 2015-2016 Bosch Software Innovations GmbH and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* The Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Bosch Software Innovations GmbH - Please refer to git log
*/
package org.eclipse.vorto.repository.internal.resolver;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.eclipse.vorto.core.api.model.mapping.MappingModel;
import org.eclipse.vorto.http.model.ModelId;
import org.eclipse.vorto.http.model.ModelResource;
import org.eclipse.vorto.http.model.ModelType;
import org.eclipse.vorto.repository.model.IModelContent;
import org.eclipse.vorto.repository.resolver.IModelIdResolver;
import org.eclipse.vorto.repository.service.IModelRepository;
import org.eclipse.vorto.repository.service.IModelRepository.ContentType;

public abstract class AbstractResolver implements IModelIdResolver {

protected IModelRepository repository;

private String serviceKey;

public AbstractResolver(IModelRepository repository, String serviceKey) {
this.repository = repository;
this.serviceKey = serviceKey;
}

@Override
public ModelId resolve(String id) {
List<ModelResource> mappings = this.repository.search(ModelType.Mapping.name());
Optional<ModelId> foundId = mappings.stream()
.filter(resource -> matchesServiceKey(resource))
.map(r -> doResolve(r,id))
.filter(modelId -> Objects.nonNull(modelId)).findFirst();
return foundId.isPresent() ? foundId.get() : null;
}

private boolean matchesServiceKey(ModelResource resource) {
IModelContent content = this.repository.getModelContent(resource.getId(), ContentType.DSL);
return ((MappingModel)content.getModel()).getTargetPlatform().equals(this.serviceKey);
}

protected abstract ModelId doResolve(ModelResource mappingModelResource, String id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2015-2016 Bosch Software Innovations GmbH and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* The Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Bosch Software Innovations GmbH - Please refer to git log
*/
package org.eclipse.vorto.repository.internal.resolver;

import java.util.Optional;

import org.eclipse.vorto.core.api.model.mapping.Attribute;
import org.eclipse.vorto.core.api.model.mapping.MappingModel;
import org.eclipse.vorto.core.api.model.mapping.MappingRule;
import org.eclipse.vorto.core.api.model.mapping.StereoTypeTarget;
import org.eclipse.vorto.http.model.ModelId;
import org.eclipse.vorto.http.model.ModelResource;
import org.eclipse.vorto.repository.model.IModelContent;
import org.eclipse.vorto.repository.service.IModelRepository;
import org.eclipse.vorto.repository.service.IModelRepository.ContentType;

public class BlueToothUUIDResolver extends AbstractResolver {

public BlueToothUUIDResolver(IModelRepository repository, String serviceKey) {
super(repository,serviceKey);
}

@Override
protected ModelId doResolve(ModelResource mappingModelResource, String id) {
IModelContent content = this.repository.getModelContent(mappingModelResource.getId(), ContentType.DSL);
MappingModel mappingModel = (MappingModel)content.getModel();
Optional<MappingRule> objectRule = mappingModel.getRules().stream().filter(rule -> rule.getTarget() instanceof StereoTypeTarget && ((StereoTypeTarget)rule.getTarget()).getName().equals("DeviceInfoProfile")).findFirst();

if (objectRule.isPresent()) {
Optional<Attribute> objectIdAttribute = ((StereoTypeTarget)objectRule.get().getTarget()).getAttributes().stream().filter(attribute -> attribute.getName().equals("modelNumber")).findFirst();
if (objectIdAttribute.isPresent() && objectIdAttribute.get().getValue().equals(id)) {
return ModelId.fromReference(mappingModel.getReferences().get(0));
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
package org.eclipse.vorto.repository.internal.resolver;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.eclipse.vorto.core.api.model.mapping.Attribute;
Expand All @@ -24,9 +22,7 @@
import org.eclipse.vorto.core.api.model.mapping.StereoTypeTarget;
import org.eclipse.vorto.http.model.ModelId;
import org.eclipse.vorto.http.model.ModelResource;
import org.eclipse.vorto.http.model.ModelType;
import org.eclipse.vorto.repository.model.IModelContent;
import org.eclipse.vorto.repository.resolver.IModelIdResolver;
import org.eclipse.vorto.repository.service.IModelRepository;
import org.eclipse.vorto.repository.service.IModelRepository.ContentType;

Expand All @@ -36,30 +32,14 @@
* @author Alexander Edelmann - Robert Bosch (SEA) Pte. Ltd.
*
*/
public class Lwm2mObjectIdResolver implements IModelIdResolver{

private IModelRepository repository;
public class Lwm2mObjectIdResolver extends AbstractResolver {

public Lwm2mObjectIdResolver(IModelRepository repository) {
this.repository = repository;
public Lwm2mObjectIdResolver(IModelRepository repository, String serviceKey) {
super(repository,serviceKey);
}

@Override
public ModelId resolve(String id) {
List<ModelResource> mappings = this.repository.search(ModelType.Mapping.name());
Optional<ModelId> foundId = mappings.stream()
.filter(resource -> isLwM2MFunctionBlockMapping(resource))
.map(r -> getModelId(r,id))
.filter(modelId -> Objects.nonNull(modelId)).findFirst();
return foundId.isPresent() ? foundId.get() : null;
}

private boolean isLwM2MFunctionBlockMapping(ModelResource resource) {
IModelContent content = this.repository.getModelContent(resource.getId(), ContentType.DSL);
return ((MappingModel)content.getModel()).getTargetPlatform().equals("lwm2m");
}

private ModelId getModelId(ModelResource mappingModelResource, String id) {
protected ModelId doResolve(ModelResource mappingModelResource, String id) {
IModelContent content = this.repository.getModelContent(mappingModelResource.getId(), ContentType.DSL);
MappingModel mappingModel = (MappingModel)content.getModel();
Optional<MappingRule> objectRule = mappingModel.getRules().stream().filter(rule -> rule.getTarget() instanceof StereoTypeTarget && ((StereoTypeTarget)rule.getTarget()).getName().equals("Object")).findFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class ModelIdResolverFactory {
@Autowired
private IModelRepository repository;

private static final String LWM2M_SERVICEKEY = "lwm2m";
private static final String BT_SERVICEKEY = "bluetooth";

private Map<String, IModelIdResolver> resolvers = new HashMap<>();

public IModelIdResolver getResolver(String serviceKey) {
Expand All @@ -41,6 +44,7 @@ public IModelIdResolver getResolver(String serviceKey) {

@PostConstruct
public void initResolvers() {
resolvers.put("lwm2m", new Lwm2mObjectIdResolver(repository));
resolvers.put(LWM2M_SERVICEKEY, new Lwm2mObjectIdResolver(repository,LWM2M_SERVICEKEY));
resolvers.put(BT_SERVICEKEY, new BlueToothUUIDResolver(repository,BT_SERVICEKEY));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2015-2016 Bosch Software Innovations GmbH and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* The Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Bosch Software Innovations GmbH - Please refer to git log
*/
package org.eclipse.vorto.repository.resolver;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.eclipse.vorto.http.model.ModelId;
import org.eclipse.vorto.repository.internal.resolver.BlueToothUUIDResolver;
import org.eclipse.vorto.repository.service.AbstractIntegrationTest;
import org.junit.Test;

public class BlueToothDeviceInfoProfileResolverTest extends AbstractIntegrationTest {

@Test
public void testResolveInfoModelByDeviceInfoProfileSerialNo() {
checkinModel("bluetooth/ColorLight.fbmodel");
checkinModel("bluetooth/ColorLightIM.infomodel");
checkinModel("bluetooth/ColorLight_bluetooth.mapping");

BlueToothUUIDResolver resolver = new BlueToothUUIDResolver(this.modelRepository,"bluetooth");
assertEquals(new ModelId("ColorLightIM", "com.mycompany", "1.0.0"),resolver.resolve("4810"));

assertNotNull(this.modelRepository.getById(resolver.resolve("4810")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void testResolveForExistingObjectId() {
checkinModel("lwm2m/ColorLight.fbmodel");
checkinModel("lwm2m/ColorLight_lwm2m.mapping");

Lwm2mObjectIdResolver lwm2mResolver = new Lwm2mObjectIdResolver(this.modelRepository);
Lwm2mObjectIdResolver lwm2mResolver = new Lwm2mObjectIdResolver(this.modelRepository,"lwm2m");
assertEquals(new ModelId("ColorLight", "com.mycompany.fb", "1.0.0"),lwm2mResolver.resolve("2"));

assertNotNull(this.modelRepository.getById(lwm2mResolver.resolve("2")));
Expand All @@ -41,7 +41,7 @@ public void testResolveResourceId() {
checkinModel("lwm2m/ColorLight.fbmodel");
checkinModel("lwm2m/ColorLight_lwm2m.mapping");

Lwm2mObjectIdResolver lwm2mResolver = new Lwm2mObjectIdResolver(this.modelRepository);
Lwm2mObjectIdResolver lwm2mResolver = new Lwm2mObjectIdResolver(this.modelRepository,"lwm2m");
assertNull(lwm2mResolver.resolve("3"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace com.mycompany.fb
version 1.0.0
displayname "Color Light"
description "Sample Function block model"
category demo
functionblock ColorLight {

configuration {
mandatory isOn as boolean
}

status {
optional consumption as double
mandatory r as int <MIN 0, MAX 255>
mandatory g as int <MIN 0, MAX 255>
mandatory b as int <MIN 0, MAX 255>
}

operations {
on()
off()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace com.mycompany
version 1.0.0
displayname "ColorLight IM"
description "Information model for Color Light IM"
category demo
using com.mycompany.fb.ColorLight ; 1.0.0

infomodel ColorLightIM {

functionblocks {
colorLight as ColorLight
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace examples.mappings.bt
version 1.0.0
displayname "Color Light BT Mapping"
description "Maps ColorLight functionblock to BT GATT"
using com.mycompany.ColorLightIM;1.0.0
infomodelmapping ColorLight_bt {
targetplatform bluetooth

from ColorLightIM to DeviceInfoProfile with {modelNumber:"4810"}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.eclipse.vorto.repository.service.ModelNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -30,7 +29,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/rest/resolve")
@RequestMapping(value = "/rest/resolver")
public class ModelResolveController {

@Autowired
Expand Down

0 comments on commit 13c88af

Please sign in to comment.