From eb473ab93f4fdee296985247274e43e7001ec604 Mon Sep 17 00:00:00 2001 From: aedelmann Date: Tue, 27 Dec 2016 10:59:38 +0800 Subject: [PATCH] Added resolver for bluetooth device info profile --- .../internal/resolver/AbstractResolver.java | 58 +++++++++++++++++++ .../resolver/BlueToothUUIDResolver.java | 49 ++++++++++++++++ .../resolver/Lwm2mObjectIdResolver.java | 30 ++-------- .../resolver/ModelIdResolverFactory.java | 6 +- ...lueToothDeviceInfoProfileResolverTest.java | 39 +++++++++++++ .../resolver/Lwm2mObjectIdResolverTest.java | 4 +- .../bluetooth/ColorLight.fbmodel | 23 ++++++++ .../bluetooth/ColorLightIM.infomodel | 13 +++++ .../bluetooth/ColorLight_bluetooth.mapping | 10 ++++ .../web/ModelGenerationController.java | 1 - .../web/ModelResolveController.java | 3 +- 11 files changed, 205 insertions(+), 31 deletions(-) create mode 100644 server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/AbstractResolver.java create mode 100644 server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/BlueToothUUIDResolver.java create mode 100644 server/repo/repo-core/src/test/java/org/eclipse/vorto/repository/resolver/BlueToothDeviceInfoProfileResolverTest.java create mode 100644 server/repo/repo-core/src/test/resources/sample_models/bluetooth/ColorLight.fbmodel create mode 100644 server/repo/repo-core/src/test/resources/sample_models/bluetooth/ColorLightIM.infomodel create mode 100644 server/repo/repo-core/src/test/resources/sample_models/bluetooth/ColorLight_bluetooth.mapping diff --git a/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/AbstractResolver.java b/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/AbstractResolver.java new file mode 100644 index 0000000000..24e24d8e3d --- /dev/null +++ b/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/AbstractResolver.java @@ -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 mappings = this.repository.search(ModelType.Mapping.name()); + Optional 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); + +} diff --git a/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/BlueToothUUIDResolver.java b/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/BlueToothUUIDResolver.java new file mode 100644 index 0000000000..f84514e191 --- /dev/null +++ b/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/BlueToothUUIDResolver.java @@ -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 objectRule = mappingModel.getRules().stream().filter(rule -> rule.getTarget() instanceof StereoTypeTarget && ((StereoTypeTarget)rule.getTarget()).getName().equals("DeviceInfoProfile")).findFirst(); + + if (objectRule.isPresent()) { + Optional 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; + } +} diff --git a/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/Lwm2mObjectIdResolver.java b/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/Lwm2mObjectIdResolver.java index 441fb76533..9b35322fd7 100644 --- a/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/Lwm2mObjectIdResolver.java +++ b/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/Lwm2mObjectIdResolver.java @@ -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; @@ -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; @@ -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 mappings = this.repository.search(ModelType.Mapping.name()); - Optional 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 objectRule = mappingModel.getRules().stream().filter(rule -> rule.getTarget() instanceof StereoTypeTarget && ((StereoTypeTarget)rule.getTarget()).getName().equals("Object")).findFirst(); diff --git a/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/ModelIdResolverFactory.java b/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/ModelIdResolverFactory.java index 52bf86ec70..a5c0d4b080 100644 --- a/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/ModelIdResolverFactory.java +++ b/server/repo/repo-core/src/main/java/org/eclipse/vorto/repository/internal/resolver/ModelIdResolverFactory.java @@ -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 resolvers = new HashMap<>(); public IModelIdResolver getResolver(String serviceKey) { @@ -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)); } } diff --git a/server/repo/repo-core/src/test/java/org/eclipse/vorto/repository/resolver/BlueToothDeviceInfoProfileResolverTest.java b/server/repo/repo-core/src/test/java/org/eclipse/vorto/repository/resolver/BlueToothDeviceInfoProfileResolverTest.java new file mode 100644 index 0000000000..35c8aa7384 --- /dev/null +++ b/server/repo/repo-core/src/test/java/org/eclipse/vorto/repository/resolver/BlueToothDeviceInfoProfileResolverTest.java @@ -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"))); + } + +} diff --git a/server/repo/repo-core/src/test/java/org/eclipse/vorto/repository/resolver/Lwm2mObjectIdResolverTest.java b/server/repo/repo-core/src/test/java/org/eclipse/vorto/repository/resolver/Lwm2mObjectIdResolverTest.java index 4bdce5e9bf..49dd8f6a69 100644 --- a/server/repo/repo-core/src/test/java/org/eclipse/vorto/repository/resolver/Lwm2mObjectIdResolverTest.java +++ b/server/repo/repo-core/src/test/java/org/eclipse/vorto/repository/resolver/Lwm2mObjectIdResolverTest.java @@ -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"))); @@ -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")); } } diff --git a/server/repo/repo-core/src/test/resources/sample_models/bluetooth/ColorLight.fbmodel b/server/repo/repo-core/src/test/resources/sample_models/bluetooth/ColorLight.fbmodel new file mode 100644 index 0000000000..b049c2627d --- /dev/null +++ b/server/repo/repo-core/src/test/resources/sample_models/bluetooth/ColorLight.fbmodel @@ -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 + mandatory g as int + mandatory b as int + } + + operations { + on() + off() + } +} diff --git a/server/repo/repo-core/src/test/resources/sample_models/bluetooth/ColorLightIM.infomodel b/server/repo/repo-core/src/test/resources/sample_models/bluetooth/ColorLightIM.infomodel new file mode 100644 index 0000000000..5f0bd6b1c8 --- /dev/null +++ b/server/repo/repo-core/src/test/resources/sample_models/bluetooth/ColorLightIM.infomodel @@ -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 + } +} \ No newline at end of file diff --git a/server/repo/repo-core/src/test/resources/sample_models/bluetooth/ColorLight_bluetooth.mapping b/server/repo/repo-core/src/test/resources/sample_models/bluetooth/ColorLight_bluetooth.mapping new file mode 100644 index 0000000000..7305afc55f --- /dev/null +++ b/server/repo/repo-core/src/test/resources/sample_models/bluetooth/ColorLight_bluetooth.mapping @@ -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"} +} diff --git a/server/repo/repo-ui/src/main/java/org/eclipse/vorto/repository/web/ModelGenerationController.java b/server/repo/repo-ui/src/main/java/org/eclipse/vorto/repository/web/ModelGenerationController.java index d51d39b911..722a15be8e 100644 --- a/server/repo/repo-ui/src/main/java/org/eclipse/vorto/repository/web/ModelGenerationController.java +++ b/server/repo/repo-ui/src/main/java/org/eclipse/vorto/repository/web/ModelGenerationController.java @@ -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; diff --git a/server/repo/repo-ui/src/main/java/org/eclipse/vorto/repository/web/ModelResolveController.java b/server/repo/repo-ui/src/main/java/org/eclipse/vorto/repository/web/ModelResolveController.java index 5338765e45..9dbc9918f0 100644 --- a/server/repo/repo-ui/src/main/java/org/eclipse/vorto/repository/web/ModelResolveController.java +++ b/server/repo/repo-ui/src/main/java/org/eclipse/vorto/repository/web/ModelResolveController.java @@ -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; @@ -30,7 +29,7 @@ import org.springframework.web.bind.annotation.RestController; @RestController -@RequestMapping(value = "/rest/resolve") +@RequestMapping(value = "/rest/resolver") public class ModelResolveController { @Autowired