How to use the opennms.API.Restriction function in opennms

To help you get started, we’ve selected a few opennms examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github OpenNMS / opennms-helm / src / spec / entity_ds_datasource_spec.js View on Github external
it('should map from api to ui filter with nested restrictions when serialized and deserialized again', () => {
                // Create the filter
                const apiFilter = new API.Filter()
                    .withClause(new API.Clause(new API.Restriction("alarmAckUser", API.Comparators.EQ, "Administrator"), API.Operators.AND))
                    .withClause(new API.Clause(
                        new API.NestedRestriction()
                            .withClause(new API.Clause(new API.Restriction("severity", API.Comparators.GE, "WARNING"), API.Operators.AND)),
                        API.Operators.AND));

                // Simulate persisting and reloading
                const serialized = JSON.stringify(apiFilter);
                const deserialized = JSON.parse(serialized);
                const cloned = API.Filter.fromJson(deserialized);

                // Now try to map it to an ui filter
                const uiFilter = mapping.getUiFilter(cloned);
                expect(uiFilter.getQueryString()).to.eql("select all alarms where alarmAckUser = 'Administrator' and (severity >= 'WARNING')");
            });
        });
github OpenNMS / opennms-helm / src / spec / entity_ds_datasource_spec.js View on Github external
it('should map from api to ui filter with nested restrictions when serialized and deserialized again', () => {
                // Create the filter
                const apiFilter = new API.Filter()
                    .withClause(new API.Clause(new API.Restriction("alarmAckUser", API.Comparators.EQ, "Administrator"), API.Operators.AND))
                    .withClause(new API.Clause(
                        new API.NestedRestriction()
                            .withClause(new API.Clause(new API.Restriction("severity", API.Comparators.GE, "WARNING"), API.Operators.AND)),
                        API.Operators.AND));

                // Simulate persisting and reloading
                const serialized = JSON.stringify(apiFilter);
                const deserialized = JSON.parse(serialized);
                const cloned = API.Filter.fromJson(deserialized);

                // Now try to map it to an ui filter
                const uiFilter = mapping.getUiFilter(cloned);
                expect(uiFilter.getQueryString()).to.eql("select all alarms where alarmAckUser = 'Administrator' and (severity >= 'WARNING')");
            });
        });
github OpenNMS / opennms-helm / src / spec / entity_ds_datasource_spec.js View on Github external
it('should substitute scoped variables', () => {
                // The filter with variables
                const filter = new API.Filter()
                    .withClause(new API.Clause(new API.Restriction("key", API.Comparators.EQ, "$variable1"), API.Operators.AND))
                    .withClause(new API.Clause(new API.Restriction("key2", API.Comparators.EQ, "Hello this is my [[variable1]]"), API.Operators.AND))
                    .withClause(new API.Clause(new API.Restriction("key3", API.Comparators.EQ, "value3"), API.Operators.AND));

                // The scoped variables
                const options = {
                    scopedVars: {
                        "variable1": {value: "dummy-value"}
                    }
                };

                const substitutedFilter = ctx.datasource.buildQuery(filter, options);

                // Verify
                expect(substitutedFilter.clauses[0].restriction.value).to.equal("dummy-value");
                expect(substitutedFilter.clauses[1].restriction.value).to.equal("Hello this is my dummy-value");
                expect(substitutedFilter.clauses[2].restriction.value).to.equal("value3");
            });
github OpenNMS / opennms-helm / src / spec / entity_ds_datasource_spec.js View on Github external
it('should substitude $range_from and $range_to accordingly', () => {
                // The input filter
                const filter = new API.Filter()
                    .withClause(new API.Clause(new API.Restriction("key", API.Comparators.EQ, "$range_from"), API.Operators.AND))
                    .withClause(new API.Clause(new API.Restriction("key2", API.Comparators.EQ, "$range_to"), API.Operators.AND))
                    .withClause(new API.Clause(new API.Restriction("key3", API.Comparators.EQ, "[[range_from]]"), API.Operators.AND))
                    .withClause(new API.Clause(new API.Restriction("key4", API.Comparators.EQ, "[[range_to]]"), API.Operators.AND));

                // Options
                const options = {
                    targets: [filter],
                    range: {
                        from: ctx.range_from,
                        to: ctx.range_to,
                    },
                    scopedVars: {}
                };

                // Build query and verify
                const substitutedFilter = ctx.datasource.buildQuery(filter, options);
                expect(substitutedFilter.clauses[0].restriction.value).to.equal(ctx.range_from);
github OpenNMS / opennms-helm / src / datasources / entity-ds / datasource.js View on Github external
buildQuery(filter, options) {
      var clonedFilter = API.Filter.fromJson(filter);

      // Before replacing any variables, add a global time range restriction (which is hidden to the user)
      if (options && options.enforceTimeRange) {
          if (!options.entity || options.entity.type === 'alarm') {
            clonedFilter.withAndRestriction(
                new API.NestedRestriction()
                    .withAndRestriction(new API.Restriction("lastEventTime", API.Comparators.GE, "$range_from"))
                    .withAndRestriction(new API.Restriction("lastEventTime", API.Comparators.LE, "$range_to")));
          }
      }

      // Substitute $ or [[variable]] in the restriction value
      this.substitute(clonedFilter.clauses, options);
      return clonedFilter;
  }
github OpenNMS / opennms-helm / src / datasources / entity-ds / Examples.js View on Github external
import {API, Model} from 'opennms';

export class Example {
    constructor(description, apiFilter) {
        this.apiFilter = apiFilter;
        this.description = description;
    }
}

// Some examples to use
export const Examples = [
    new Example("Show all alarms", new API.Filter()),
    new Example("Show all alarms for location 'Default'",
        new API.Filter().withAndRestriction(new API.Restriction("node.location.locationName", API.Comparators.EQ, 'Default'))),
    new Example("Show all alarms with a severity between 'Warning' and 'Major'",
        new API.Filter()
            .withAndRestriction(new API.Restriction("alarm.severity", API.Comparators.GE, Model.Severities.WARNING.label))
            .withAndRestriction(new API.Restriction("alarm.severity", API.Comparators.LE, Model.Severities.MAJOR.label))),
    new Example("Show all alarms for nodes in category 'Servers'",
        new API.Filter().withAndRestriction(new API.Restriction("category.name", API.Comparators.EQ, 'Severs'))),
    new Example("Show all unacknowledged alarms",
        new API.Filter().withAndRestriction(new API.Restriction("alarmAckTime", API.Comparators.EQ, 'null'))),
    new Example("Show all acknowledged alarms",
        new API.Filter().withAndRestriction(new API.Restriction("alarmAckTime", API.Comparators.NE, 'null'))),

];
github OpenNMS / opennms-helm / src / datasources / entity-ds / datasource.js View on Github external
// *either* be an array with 1 entry, or just the raw value >:|
                        // so we normalize it back to just the raw value here if necessary
                        if (_.isArray(templateVariable.current.value) && templateVariable.current.value.length === 1) {
                            templateVariable.current.value = templateVariable.current.value[0];
                        }

                        // now if it's *still* an array, we chop it up into nested restrictions
                        if (_.isArray(templateVariable.current.value)) {
                            const replacement = new API.NestedRestriction();
                            let values = templateVariable.current.value;
                            if (!_.isArray(values)) {
                                values = [values];
                            }
                            for (const value of values) {
                                if (restriction.comparator.id === API.Comparators.EQ.id) {
                                    replacement.withOrRestriction(new API.Restriction(restriction.attribute, restriction.comparator, value));
                                } else if (restriction.comparator.id === API.Comparators.NE.id) {
                                    replacement.withAndRestriction(new API.Restriction(restriction.attribute, restriction.comparator, value));
                                } else {
                                    throw new Error('Unable to query "' + restriction.attribute + '": multi-select values with variable substitution must be either "=" or "!="');
                                }
                            }

                            // we've turned a single restriction into a nested one, so re-process it as a
                            // collection and skip the simple replacement below
                            clause.restriction = replacement;
                            self.substitute(clause.restriction.clauses, options);
                            return;
                        }
                    }
                }
github OpenNMS / opennms-helm / src / spec / entity_ds_datasource_spec.js View on Github external
it ('should turn a node criteria fs:fid restriction into 2 separate clauses', () => {
                const filter = new API.Filter()
                    .withClause(new API.Clause(new API.Restriction('node', API.Comparators.EQ, 'FS:FID'), API.Operators.AND));

                const actualFilter = ctx.datasource.buildQuery(filter, {});
                expect(filter).not.to.equal(actualFilter);
                expect(actualFilter.clauses.length).to.equal(1);
                expect(actualFilter.clauses[0].restriction.clauses[0].restriction.value).to.equal('FS');
                expect(actualFilter.clauses[0].restriction.clauses[1].restriction.value).to.equal('FID');
            });
github OpenNMS / opennms-helm / src / datasources / entity-ds / AlarmEntity.js View on Github external
const getValueRestriction = val => {
          if (!self.datasource.templateSrv.isAllValue(val) && !_.isNil(val)) {
            if ((selected.resource === 'categories' || selected.resource === 'category.name')) {
              return new API.Restriction('category.name', comparator, val);
            } else if (selected.inputType === 'text') {
              if (val.length === 0) {
                  return undefined;
              }
              if (!val.startsWith('*') && !val.endsWith('*')) {
                  return new API.Restriction(key, comparator, '*' + val + '*');
              }
            }
            return new API.Restriction(key, comparator, val);
          }
          return undefined;
        };
        if (selected.value) {