Search in sources :

Example 1 with InvalidDDFFileException

use of org.eclipse.leshan.core.model.InvalidDDFFileException in project thingsboard by thingsboard.

the class DefaultTbResourceService method saveResource.

@Override
public TbResource saveResource(TbResource resource) throws ThingsboardException {
    log.trace("Executing saveResource [{}]", resource);
    if (StringUtils.isEmpty(resource.getData())) {
        throw new DataValidationException("Resource data should be specified!");
    }
    if (ResourceType.LWM2M_MODEL.equals(resource.getResourceType())) {
        try {
            List<ObjectModel> objectModels = ddfFileParser.parse(new ByteArrayInputStream(Base64.getDecoder().decode(resource.getData())), resource.getSearchText());
            if (!objectModels.isEmpty()) {
                ObjectModel objectModel = objectModels.get(0);
                String resourceKey = objectModel.id + LWM2M_SEPARATOR_KEY + objectModel.version;
                String name = objectModel.name;
                resource.setResourceKey(resourceKey);
                if (resource.getId() == null) {
                    resource.setTitle(name + " id=" + objectModel.id + " v" + objectModel.version);
                }
                resource.setSearchText(resourceKey + LWM2M_SEPARATOR_SEARCH_TEXT + name);
            } else {
                throw new DataValidationException(String.format("Could not parse the XML of objectModel with name %s", resource.getSearchText()));
            }
        } catch (InvalidDDFFileException e) {
            log.error("Failed to parse file {}", resource.getFileName(), e);
            throw new DataValidationException("Failed to parse file " + resource.getFileName());
        } catch (IOException e) {
            throw new ThingsboardException(e, ThingsboardErrorCode.GENERAL);
        }
        if (resource.getResourceType().equals(ResourceType.LWM2M_MODEL) && toLwM2mObject(resource, true) == null) {
            throw new DataValidationException(String.format("Could not parse the XML of objectModel with name %s", resource.getSearchText()));
        }
    } else {
        resource.setResourceKey(resource.getFileName());
    }
    return resourceService.saveResource(resource);
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ObjectModel(org.eclipse.leshan.core.model.ObjectModel) ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidDDFFileException(org.eclipse.leshan.core.model.InvalidDDFFileException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) IOException(java.io.IOException)

Example 2 with InvalidDDFFileException

use of org.eclipse.leshan.core.model.InvalidDDFFileException in project thingsboard by thingsboard.

the class DefaultTbResourceService method toLwM2mObject.

private LwM2mObject toLwM2mObject(TbResource resource, boolean isSave) {
    try {
        DDFFileParser ddfFileParser = new DDFFileParser(new DefaultDDFFileValidator());
        List<ObjectModel> objectModels = ddfFileParser.parse(new ByteArrayInputStream(Base64.getDecoder().decode(resource.getData())), resource.getSearchText());
        if (objectModels.size() == 0) {
            return null;
        } else {
            ObjectModel obj = objectModels.get(0);
            LwM2mObject lwM2mObject = new LwM2mObject();
            lwM2mObject.setId(obj.id);
            lwM2mObject.setKeyId(resource.getResourceKey());
            lwM2mObject.setName(obj.name);
            lwM2mObject.setMultiple(obj.multiple);
            lwM2mObject.setMandatory(obj.mandatory);
            LwM2mInstance instance = new LwM2mInstance();
            instance.setId(0);
            List<LwM2mResourceObserve> resources = new ArrayList<>();
            obj.resources.forEach((k, v) -> {
                if (isSave) {
                    LwM2mResourceObserve lwM2MResourceObserve = new LwM2mResourceObserve(k, v.name, false, false, false);
                    resources.add(lwM2MResourceObserve);
                } else if (v.operations.isReadable()) {
                    LwM2mResourceObserve lwM2MResourceObserve = new LwM2mResourceObserve(k, v.name, false, false, false);
                    resources.add(lwM2MResourceObserve);
                }
            });
            if (isSave || resources.size() > 0) {
                instance.setResources(resources.toArray(LwM2mResourceObserve[]::new));
                lwM2mObject.setInstances(new LwM2mInstance[] { instance });
                return lwM2mObject;
            } else {
                return null;
            }
        }
    } catch (IOException | InvalidDDFFileException e) {
        log.error("Could not parse the XML of objectModel with name [{}]", resource.getSearchText(), e);
        return null;
    }
}
Also used : ObjectModel(org.eclipse.leshan.core.model.ObjectModel) DefaultDDFFileValidator(org.eclipse.leshan.core.model.DefaultDDFFileValidator) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DDFFileParser(org.eclipse.leshan.core.model.DDFFileParser) ByteArrayInputStream(java.io.ByteArrayInputStream) LwM2mResourceObserve(org.thingsboard.server.common.data.lwm2m.LwM2mResourceObserve) InvalidDDFFileException(org.eclipse.leshan.core.model.InvalidDDFFileException) LwM2mObject(org.thingsboard.server.common.data.lwm2m.LwM2mObject) LwM2mInstance(org.thingsboard.server.common.data.lwm2m.LwM2mInstance)

Example 3 with InvalidDDFFileException

use of org.eclipse.leshan.core.model.InvalidDDFFileException in project thingsboard by thingsboard.

the class LwM2MTestClient method init.

public void init(Security security, Configuration coapConfig, int port, boolean isRpc, boolean isBootstrap, int shortServerId, int shortServerIdBs, Security securityBs, DefaultLwM2mUplinkMsgHandler defaultLwM2mUplinkMsgHandler, LwM2mClientContext clientContext) throws InvalidDDFFileException, IOException {
    Assert.assertNull("client already initialized", leshanClient);
    this.defaultLwM2mUplinkMsgHandlerTest = defaultLwM2mUplinkMsgHandler;
    this.clientContext = clientContext;
    List<ObjectModel> models = new ArrayList<>();
    for (String resourceName : resources) {
        models.addAll(ObjectLoader.loadDdfFile(LwM2MTestClient.class.getClassLoader().getResourceAsStream("lwm2m/" + resourceName), resourceName));
    }
    LwM2mModel model = new StaticModel(models);
    ObjectsInitializer initializer = new ObjectsInitializer(model);
    if (securityBs == null) {
        initializer.setInstancesForObject(SECURITY, this.lwm2mSecurity = security);
    } else {
        securityBs.setId(0);
        security.setId(1);
        LwM2mInstanceEnabler[] instances = new LwM2mInstanceEnabler[] { this.lwm2mSecurityBs = securityBs, this.lwm2mSecurity = security };
        initializer.setClassForObject(SECURITY, Security.class);
        initializer.setInstancesForObject(SECURITY, instances);
    }
    if (isBootstrap) {
        initializer.setInstancesForObject(SERVER, lwm2mServerBs = new Lwm2mServer(shortServerIdBs, 300));
    } else {
        if (securityBs == null) {
            initializer.setInstancesForObject(SERVER, lwm2mServer = new Lwm2mServer(shortServerId, 300));
        } else {
            lwm2mServerBs = new Lwm2mServer(shortServerIdBs, 300);
            lwm2mServerBs.setId(0);
            lwm2mServer = new Lwm2mServer(shortServerId, 300);
            lwm2mServer.setId(1);
            LwM2mInstanceEnabler[] instances = new LwM2mInstanceEnabler[] { lwm2mServerBs, lwm2mServer };
            initializer.setClassForObject(SERVER, Server.class);
            initializer.setInstancesForObject(SERVER, instances);
        }
    }
    initializer.setInstancesForObject(DEVICE, lwM2MDevice = new SimpleLwM2MDevice(executor));
    initializer.setInstancesForObject(FIRMWARE, fwLwM2MDevice = new FwLwM2MDevice());
    initializer.setInstancesForObject(SOFTWARE_MANAGEMENT, swLwM2MDevice = new SwLwM2MDevice());
    initializer.setClassForObject(ACCESS_CONTROL, DummyInstanceEnabler.class);
    initializer.setInstancesForObject(BINARY_APP_DATA_CONTAINER, lwM2MBinaryAppDataContainer = new LwM2mBinaryAppDataContainer(executor, OBJECT_INSTANCE_ID_0), new LwM2mBinaryAppDataContainer(executor, OBJECT_INSTANCE_ID_1));
    locationParams = new LwM2MLocationParams();
    locationParams.getPos();
    initializer.setInstancesForObject(LOCATION, new LwM2mLocation(locationParams.getLatitude(), locationParams.getLongitude(), locationParams.getScaleFactor(), executor, OBJECT_INSTANCE_ID_0));
    initializer.setInstancesForObject(TEMPERATURE_SENSOR, lwM2MTemperatureSensor = new LwM2mTemperatureSensor(executor, OBJECT_INSTANCE_ID_0), new LwM2mTemperatureSensor(executor, OBJECT_INSTANCE_ID_12));
    DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder(coapConfig);
    dtlsConfig.set(DTLS_RECOMMENDED_CIPHER_SUITES_ONLY, true);
    DefaultRegistrationEngineFactory engineFactory = new DefaultRegistrationEngineFactory();
    engineFactory.setReconnectOnUpdate(false);
    engineFactory.setResumeOnConnect(true);
    LeshanClientBuilder builder = new LeshanClientBuilder(endpoint);
    builder.setLocalAddress("0.0.0.0", port);
    builder.setObjects(initializer.createAll());
    builder.setCoapConfig(coapConfig);
    builder.setDtlsConfig(dtlsConfig);
    builder.setRegistrationEngineFactory(engineFactory);
    builder.setSharedExecutor(executor);
    builder.setDecoder(new DefaultLwM2mDecoder(false));
    builder.setEncoder(new DefaultLwM2mEncoder(new LwM2mValueConverterImpl(), false));
    clientState = ON_INIT;
    clientStates = new HashSet<>();
    clientStates.add(clientState);
    leshanClient = builder.build();
    LwM2mClientObserver observer = new LwM2mClientObserver() {

        @Override
        public void onBootstrapStarted(ServerIdentity bsserver, BootstrapRequest request) {
            clientState = ON_BOOTSTRAP_STARTED;
            clientStates.add(clientState);
        }

        @Override
        public void onBootstrapSuccess(ServerIdentity bsserver, BootstrapRequest request) {
            clientState = ON_BOOTSTRAP_SUCCESS;
            clientStates.add(clientState);
        }

        @Override
        public void onBootstrapFailure(ServerIdentity bsserver, BootstrapRequest request, ResponseCode responseCode, String errorMessage, Exception cause) {
            clientState = ON_BOOTSTRAP_FAILURE;
            clientStates.add(clientState);
        }

        @Override
        public void onBootstrapTimeout(ServerIdentity bsserver, BootstrapRequest request) {
            clientState = ON_BOOTSTRAP_TIMEOUT;
            clientStates.add(clientState);
        }

        @Override
        public void onRegistrationStarted(ServerIdentity server, RegisterRequest request) {
            clientState = ON_REGISTRATION_STARTED;
            clientStates.add(clientState);
        }

        @Override
        public void onRegistrationSuccess(ServerIdentity server, RegisterRequest request, String registrationID) {
            clientState = ON_REGISTRATION_SUCCESS;
            clientStates.add(clientState);
        }

        @Override
        public void onRegistrationFailure(ServerIdentity server, RegisterRequest request, ResponseCode responseCode, String errorMessage, Exception cause) {
            clientState = ON_REGISTRATION_FAILURE;
            clientStates.add(clientState);
        }

        @Override
        public void onRegistrationTimeout(ServerIdentity server, RegisterRequest request) {
            clientState = ON_REGISTRATION_TIMEOUT;
            clientStates.add(clientState);
        }

        @Override
        public void onUpdateStarted(ServerIdentity server, UpdateRequest request) {
            clientState = ON_UPDATE_STARTED;
            clientStates.add(clientState);
        }

        @Override
        public void onUpdateSuccess(ServerIdentity server, UpdateRequest request) {
            clientState = ON_UPDATE_SUCCESS;
            clientStates.add(clientState);
        }

        @Override
        public void onUpdateFailure(ServerIdentity server, UpdateRequest request, ResponseCode responseCode, String errorMessage, Exception cause) {
            clientState = ON_UPDATE_FAILURE;
            clientStates.add(clientState);
        }

        @Override
        public void onUpdateTimeout(ServerIdentity server, UpdateRequest request) {
            clientState = ON_UPDATE_TIMEOUT;
            clientStates.add(clientState);
        }

        @Override
        public void onDeregistrationStarted(ServerIdentity server, DeregisterRequest request) {
            clientState = ON_DEREGISTRATION_STARTED;
            clientStates.add(clientState);
        }

        @Override
        public void onDeregistrationSuccess(ServerIdentity server, DeregisterRequest request) {
            clientState = ON_DEREGISTRATION_SUCCESS;
            clientStates.add(clientState);
        }

        @Override
        public void onDeregistrationFailure(ServerIdentity server, DeregisterRequest request, ResponseCode responseCode, String errorMessage, Exception cause) {
            clientState = ON_DEREGISTRATION_FAILURE;
            clientStates.add(clientState);
        }

        @Override
        public void onDeregistrationTimeout(ServerIdentity server, DeregisterRequest request) {
            clientState = ON_DEREGISTRATION_TIMEOUT;
            clientStates.add(clientState);
        }

        @Override
        public void onUnexpectedError(Throwable unexpectedError) {
            clientState = ON_EXPECTED_ERROR;
            clientStates.add(clientState);
        }
    };
    this.leshanClient.addObserver(observer);
    if (!isRpc) {
        this.start(true);
    }
}
Also used : BootstrapRequest(org.eclipse.leshan.core.request.BootstrapRequest) ObjectModel(org.eclipse.leshan.core.model.ObjectModel) LwM2mValueConverterImpl(org.thingsboard.server.transport.lwm2m.utils.LwM2mValueConverterImpl) DefaultLwM2mEncoder(org.eclipse.leshan.core.node.codec.DefaultLwM2mEncoder) LeshanClientBuilder(org.eclipse.leshan.client.californium.LeshanClientBuilder) ArrayList(java.util.ArrayList) StaticModel(org.eclipse.leshan.core.model.StaticModel) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) LwM2mInstanceEnabler(org.eclipse.leshan.client.resource.LwM2mInstanceEnabler) ServerIdentity(org.eclipse.leshan.client.servers.ServerIdentity) LwM2mClientObserver(org.eclipse.leshan.client.observer.LwM2mClientObserver) RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) DefaultRegistrationEngineFactory(org.eclipse.leshan.client.engine.DefaultRegistrationEngineFactory) ResponseCode(org.eclipse.leshan.core.ResponseCode) ObjectsInitializer(org.eclipse.leshan.client.resource.ObjectsInitializer) UpdateRequest(org.eclipse.leshan.core.request.UpdateRequest) LeshanClientBuilder(org.eclipse.leshan.client.californium.LeshanClientBuilder) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) DefaultLwM2mDecoder(org.eclipse.leshan.core.node.codec.DefaultLwM2mDecoder) DeregisterRequest(org.eclipse.leshan.core.request.DeregisterRequest) InvalidDDFFileException(org.eclipse.leshan.core.model.InvalidDDFFileException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)3 InvalidDDFFileException (org.eclipse.leshan.core.model.InvalidDDFFileException)3 ObjectModel (org.eclipse.leshan.core.model.ObjectModel)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 DtlsConnectorConfig (org.eclipse.californium.scandium.config.DtlsConnectorConfig)1 LeshanClientBuilder (org.eclipse.leshan.client.californium.LeshanClientBuilder)1 DefaultRegistrationEngineFactory (org.eclipse.leshan.client.engine.DefaultRegistrationEngineFactory)1 LwM2mClientObserver (org.eclipse.leshan.client.observer.LwM2mClientObserver)1 LwM2mInstanceEnabler (org.eclipse.leshan.client.resource.LwM2mInstanceEnabler)1 ObjectsInitializer (org.eclipse.leshan.client.resource.ObjectsInitializer)1 ServerIdentity (org.eclipse.leshan.client.servers.ServerIdentity)1 ResponseCode (org.eclipse.leshan.core.ResponseCode)1 DDFFileParser (org.eclipse.leshan.core.model.DDFFileParser)1 DefaultDDFFileValidator (org.eclipse.leshan.core.model.DefaultDDFFileValidator)1 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)1 StaticModel (org.eclipse.leshan.core.model.StaticModel)1 DefaultLwM2mDecoder (org.eclipse.leshan.core.node.codec.DefaultLwM2mDecoder)1 DefaultLwM2mEncoder (org.eclipse.leshan.core.node.codec.DefaultLwM2mEncoder)1 BootstrapRequest (org.eclipse.leshan.core.request.BootstrapRequest)1