# SNMP JavaScript API

## Connector functions SNMP JS API guide

This JavaScript code provides predefined functions to execute SNMP requests from the connector function. They are explained below.

### JS SNMP API

For REQUEST Connector Functions we will use the functions `get` and `set` described below. You have an object, named `snmp`, with those functions described. You must use `snmp.get` or `snmp.set`.

If you want to collect data after executing any of these function you can call *collectCF* and you can set various oids in the URL provided as you can see in the next example:

```json
collectCF(result.data, "snmps://<oidValue>");
```

#### snmp.addOid()

Adds an OID to the list of OIDs to be retrieved/setted.

| Param            | Type    | Description         |
|------------------|---------|---------------------|
| oid              | string  | OID to add.         |
| type             | string  | Type of the value.  |
| value            | string  | Value to set.       | 

Example of use:

```javascript
// Example for get
snmp.addOid('1.3.6.1.4.1.2007.4.1.2.2.2.18.3.2.1.10.3');
snmp.get()

//Example for set
snmp.addOid('1.0.1.4.5.123456.1.6', 'INTEGER', '3');
snmp.set()
```

#### snmp.get()

Executes a multi SNMP get attribute request with specified payload. You must have previously given value to the properties `snmp.ip`, `snmp.port` (161 by default), `snmp.community` (in case of snmp version 1), `snmp.version` (3 by default), `snmp.retries` (3 by default), `snmp.timeout` (5000 by default) and security info in case of snmpv3 version (`snmp.securityName`, `snmp.authentication`, `snmp.privacy`, `snmp.authPassphrase` and `snmp.privPassphrase`).
In addition, you have to add the oids you want to get using the `snmp.addOid` function with the oid string as many times as you want.

**Kind**: global function  
**Returns**: String - A JSON with response. This JSON will have two fields: 'result' of the get operation and 'data' with response of the device.  

| SNMP Field     | Type    | Description                                                                                                                                                                 |
|----------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ip             | string  | IP address of the device you want to connect to.                                                                                                                            |
| port           | number  | Port of the device you want to connect to. By default is 161.                                                                                                               |
| oids           | Array   | Array of strings with the list of wanted oids. To add items to the list, you must call the function 'snmp.addOid' with the oid as parameter as many times as you want oids. |
| community       | string  | The community octet string. This is a convenience method to set the security name for community based SNMP (v1 and v2c).                                                     |
| securityName   | string  | The security name of the user (typically the user name).                                                                                                                    |
| authentication | string  | The authentication protocol ID to be associated with this user. If set to null, this user only supports unauthenticated messages.                                           |
| authPassphrase | string  | The authentication passphrase. If not null, authentication must also be not null.                                                                                           |
| privacy        | string  | The privacy protocol ID to be associated with this user. If set to null, this user only supports unencrypted messages.                                                      |
| privPassphrase | string  | The privacy passphrase. If not null, privacy must also be not null.                                                                                                         |
| version        | number  | Version number of the SNMP. By default is 3.                                                                                                                                |
| retries        | number  | Number of retries for the request. By default is 3.                                                                                                                         |
| timeout        | number  | Timeout of the request in millis. By default is 5000.                                                                                                                       |

Here is an example of use for the `snmp.addOid` function for `snmp.get`:

```javascript
snmp.addOid('1.3.6.1.4.1.2007.4.1.2.2.2.18.3.2.1.10.3');
snmp.addOid('1.3.6.1.4.1.2007.4.1.2.2.2.18.3.2.1.11.3');
snmp.get();
```

#### snmp.set()

Executes a multi SNMP set attribute request with specified payload. You must have previously given value to the properties `snmp.ip`, `snmp.port` (161 by default), `snmp.community` (in case of snmp version 1), `snmp.version` (3 by default), `snmp.retries` (3 by default), `snmp.timeout` (5000 by default) and security info in case of snmpv3 version (`snmp.securityName`, `snmp.authentication`, `snmp.privacy`, `snmp.authPassphrase` and `snmp.privPassphrase`).
In addition, you have to add the oids you want to set using the `snmp.addOid` function with the oid string, the type of the value and the value you want to set to as many times as you want.

**Kind**: global function  
**Returns**: String - A JSON with response. This JSON will have two fields: 'result' of the get operation and 'data' with response of the device. This 'data' contains pairs of (oid, value). 

| SNMP Field     | Type    | Description                                                                                                                                                                                                                                                           |
|----------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ip             | string  | IP address of the device you want to connect to.                                                                                                                                                                                                                      |
| port           | number  | Port of the device you want to connect to. By default is 161.                                                                                                                                                                                                         |
| oids           | Array   | Array of items with the list of oids you want to set. To add items to the list, you must call the function 'snmp.addOid' with the oid you want to set, the type of the value for that oid and the value you want to set as parameters as many times as you want oids. |
| community       | string  | The community octet string. This is a convenience method to set the security name for community based SNMP (v1 and v2c).                                                                                                                                               |
| securityName   | string  | The security name of the user (typically the user name).                                                                                                                                                                                                              |
| authentication | string  | The authentication protocol ID to be associated with this user. If set to null, this user only supports unauthenticated messages.                                                                                                                                     |
| authPassphrase | string  | The authentication passphrase. If not null, authentication must also be not null.                                                                                                                                                                                     |
| privacy        | string  | The privacy protocol ID to be associated with this user. If set to null, this user only supports unencrypted messages.                                                                                                                                                |
| privPassphrase | string  | The privacy passphrase. If not null, privacy must also be not null.                                                                                                                                                                                                   |
| version        | number  | Version number of the SNMP. By default is 3.                                                                                                                                                                                                                          |
| retries        | number  | Number of retries for the request. By default is 3.                                                                                                                                                                                                                   |
| timeout        | number  | Timeout of the request in millis. By default is 5000.                                                                                                                                                                                                                 |

Here is an example of use for the `snmp.addOid` function for `snmp.set`:

```javascript
snmp.addOid('1.0.1.4.5.123456.1.5', 'OCTET_STRING', params.variableList[0].value);
snmp.addOid('1.0.1.4.5.123456.1.6', 'INTEGER', '3');
snmp.set();
```
The allowed values for `type` are: `OBJECT_IDENTIFIER`, `INTEGER`, `BIT_STRING`, `OCTET_STRING`, `GAUGE32`, `COUNTER32`, `COUNTER64`, `TIMETICKS`, `OPAQUE` and `IPADDRESS`.

Here is a successful response example:

```json
{
  "result": {
    "code": "SUCCESSFUL",
    "description": "Operation executed successfully"
  },
  "data": {
    "oid":"value",
    "oid1":"value1",
    ....
    "oidN":"valueN"
  }
}
```

And here an error response example:

```json
{
  "result": {
    "code": "ERROR_PROCESSING",
    "description": "Error getting data"
  },
  "data": {}
}
```
