# Code coverage report for lib/services/client/objectRegistry.js

## Statements: 92% (69 / 75) Branches: 76% (19 / 25) Functions: 100% (15 / 15) Lines: 92% (69 / 75)

All files » lib/services/client/ » objectRegistry.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217

    1

    1

    1

    1
    226
    225

    225

    1

    1
    159
    159

    159
    81

    78

    1
    33
    33
    1

    32
    32

    32

    1
    34
    34

    34
    32
    32
    32

    2

    1
    27

    27
    12

    27

    1
    41
    41
    2

    39
    39
    39

    1
    17
    17

    17
    17

    1
    25
    25

    25

    25
    25

    1
    5

    5
    5

    1
    18
    18

    1
    1
    1
    1
    1
    1
    1
    1
    1
    1/*
     * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
     *
     * This file is part of iotagent-lwm2m-lib
     *
     * iotagent-lwm2m-lib is free software: you can redistribute it and/or
     * modify it under the terms of the GNU Affero General Public License as
     * published by the Free Software Foundation, either version 3 of the License,
     * or (at your option) any later version.
     *
     * iotagent-lwm2m-lib is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     * See the GNU Affero General Public License for more details.
     *
     * You should have received a copy of the GNU Affero General Public
     * License along with iotagent-lwm2m-lib.
     * If not, seehttp://www.gnu.org/licenses/.
     *
     * For those usages not covered by the GNU Affero General Public License
     * please contact with::[contacto@tid.es]
     */

    'use strict';

    var errors = require('../../errors'),
        registryBus  = new (require('events')).EventEmitter(),
        registry = {},
        attributesRegistry = {},
        OBJECT_URI_REGEX = /^\/\d+\/\d+$/;

    registryBus.setMaxListeners(0);

    /**
     * Parse a string containing an Object URI. If the URI is not well-formed or if it contains anything beyond the Object
     * ID, an error is raised.
     *
     * @param {String} objectUri        String representation of an OMA LWTM2M Object URI (with object Type and Id)
     */
    function parseUri(objectUri, callback) {
        if (objectUri && objectUri.match && objectUri.match(OBJECT_URI_REGEX)) {
            var parsedUri = objectUri.split('/'),
                object = {
                    objectType: parsedUri[1],
                    objectId: parsedUri[2],
                    objectUri: objectUri
                };

            callback(null, object);
        } else {
            callback(new errors.WrongObjectUri(objectUri));
        }
    }

    /**
     * Get the object of the registry represented by the given String URI.
     *
     * @param {String} objectUri        String representation of an OMA LWTM2M Object URI (with object Type and Id)
     */
    function getObject(objectUri, callback) {
        parseUri(objectUri, function(error, parsedObject) {
            Iif (error) {
                callback(error);
            } else if (registry[objectUri]) {
                callback(null, registry[objectUri]);
            } else {
                callback(new errors.ObjectNotFound(objectUri));
            }
        });
    }

    /**
     * Create a new object in the registry with the given URI. If there already is an object in that URI, it is overwritten.
     *
     * @param {String} objectUri        String representation of an OMA LWTM2M Object URI (with object Type and Id)
     */
    function create(objectUri, callback) {
        parseUri(objectUri, function(error, parsedObject) {
            if (error) {
                callback(error);
            } else {
                registry[objectUri] = parsedObject;
                registry[objectUri].attributes = {};

                callback(null, parsedObject);
            }
        });
    }

    /**
     * Removes the object identified by the URI from the registry.
     *
     * @param {String} objectUri        String representation of an OMA LWTM2M Object URI (with object Type and Id)
     */
    function remove(objectUri, callback) {
        parseUri(objectUri, function(error, parsedObject){
            Iif (error) {
                callback(error);
            } else if (registry[objectUri]) {
                var removedObj = registry[objectUri];
                delete registry[objectUri];
                callback(null, removedObj);
            } else {
                callback(new errors.ObjectNotFound(objectUri));
            }
        });
    }

    /**
     * List all the objects in the object registry.
     */
    function list(callback) {
        var keyList = Object.keys(registry),
            result = [];

        for (var i=0; i < keyList.length; i++) {
            result.push(registry[keyList[i]]);
        }

        callback(null, result);
    }

    /**
     * Modify the object represented by the given URI, by setting the selected resource to the given value. If the resource
     * doesn't exist it is created. If it does exist, its value is overwritten.
     *
     * @param {String} objectUri                         String representation of an OMA LWTM2M Object URI
     *                                                  (with object Type and Id)
     * @param {Integer} resourceId                      Id of the resource to set
     * @param {String} resourceValue                    New value of the resource
     */
    function setResource(objectUri, resourceId, resourceValue, callback) {
        getObject(objectUri, function(error, retrievedObject) {
            if (error) {
                callback(error);
            } else {
                registryBus.emit(objectUri, 'setResource', resourceId, resourceValue);
                retrievedObject.attributes[resourceId] = resourceValue;
                callback(null, retrievedObject);
            }
        });
    }

    /**
     * Modify the object represented by the given URI, by removing the selected resource.
     *
     * @param objectUri                         String representation of an OMA LWTM2M Object URI (with object Type and Id)
     * @param {Integer} resourceId              Id of the resource to remove
     */
    function unsetResource(objectUri, resourceId, callback) {
        getObject(objectUri, function(error, retrievedObject) {
            Iif (error) {
                callback(error);
            } else {
                delete retrievedObject.attributes[resourceId];
                callback(null, retrievedObject);
            }
        });
    }

    /**
     * Removes all information an handlers from the registry, setting it back to its original state.
     */
    function resetRegistry(callback) {
        registryBus.removeAllListeners();
        registry = {};

        attributesRegistry = {};

        Eif (callback) {
            callback();
        }  else {
            return;
        }
    }

    /**
     * Set the attribute set passed as a parameter as the attributes for the object type, instance or resource indicated.
     * The set of attributes is completely overwritten by the new one.
     *
     * @param {String} uri          URI of an object type, instance or resource.
     * @param {String} attributes   Object containing the new set of attributes for the selected entity.
     */
    function setAttributes(uri, attributes, callback) {
        attributesRegistry[uri] = attributes;

        Eif (callback) {
            callback();
        }  else {
            return;
        }
    }

    /**
     * Get the current set of attributes for the given entity (object type, instance or resource).
     *
     * @param {String} uri          URI of an object type, instance or resource.
     * @returns {Object}            An object containing the current attributes or undefined if there are no attributes.
     */
    function getAttributes(uri, callback) {
        Eif (callback) {
            callback(null, attributesRegistry[uri]);
        }  else {
            return attributesRegistry[uri];
        }
    }

    exports.create = create;
    exports.remove = remove;
    exports.get = getObject;
    exports.setResource = setResource;
    exports.unsetResource = unsetResource;
    exports.setAttributes = setAttributes;
    exports.getAttributes = getAttributes;
    exports.list = list;
    exports.bus = registryBus;
    exports.reset = resetRegistry;