You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@nifi.apache.org by pvillard31 <gi...@git.apache.org> on 2016/03/06 16:43:11 UTC

[GitHub] nifi pull request: NIFI-1537 Added SNMP processors

GitHub user pvillard31 opened a pull request:

    https://github.com/apache/nifi/pull/257

    NIFI-1537 Added SNMP processors

    Processors for SNMP set/get/walk operations over SNMP v1, v2c & v3 using SNMP4J library.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/pvillard31/nifi NIFI-1537

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/nifi/pull/257.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #257
    
----
commit 4ae03bb7ed6b2739351bcda58528d16b497821ff
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-02-16T10:02:10Z

    Merge remote-tracking branch 'refs/remotes/apache/master'

commit e3c6b5741b22d22a4c9a3476c843be5390171353
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-02-17T10:39:01Z

    Merge remote-tracking branch 'refs/remotes/apache/master'

commit 40ab6c3d51d68b56fd11eb28510d4e3029dc2a73
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-02-18T12:24:14Z

    Merge remote-tracking branch 'refs/remotes/apache/master'

commit 7be76f11bac3d37b293a64e0ff6394ec32f05bb4
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-02-19T09:19:54Z

    Merge remote-tracking branch 'refs/remotes/apache/master'

commit 7663d1ffbb9ac997cc9463878c04dbd3c20e9ee2
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-02-19T22:22:03Z

    Merge remote-tracking branch 'refs/remotes/apache/master'

commit 899376100ad6bb603969a06378bac28c5219a8f3
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-02-22T15:24:05Z

    Merge remote-tracking branch 'refs/remotes/apache/master' into NIFI-1537

commit 8d4a62f7de98b402d76277dd219e82f66fafbbc9
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-02-23T08:00:55Z

    Merge remote-tracking branch 'refs/remotes/apache/master' into NIFI-1537

commit 258bb3f8f239c582879d310197188b9dbbae923c
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-02-28T08:08:56Z

    Merge remote-tracking branch 'refs/remotes/apache/master' into NIFI-1537

commit 01796bc659ba6efd04695cd0dcc12ae61dc9d79b
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-02-28T08:15:20Z

    Merge remote-tracking branch 'refs/remotes/apache/master'

commit 67d257ced8de89ad15be9edb024efdc0782a883f
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-02-28T09:12:02Z

    NIFI-1537 Added SNMP processors (get/set)

commit ca320c4e3056eef412ef68286b7b11ed916dfbae
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-02-28T09:12:02Z

    NIFI-1537 Added SNMP processors (get/set)

commit 7dfc98eff5c1b95dc35995015baa21fcdf46bc98
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-02-28T15:40:52Z

    NIFI-1537 Handling when SNMP agent stopped

commit 33bb01d8631ed523da2093becf8e7d7042eb6668
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-02-28T18:59:27Z

    Merge branch 'NIFI-1537' of https://github.com/pvillard31/nifi into NIFI-1537

commit a8a829970b955fd70656605e4f850a76ea415359
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-03-05T18:50:10Z

    Merge remote-tracking branch 'refs/remotes/apache/master' into NIFI-1537

commit a13be73dd8aa3a0100ca478a3171045f6730a3ae
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-03-06T13:15:19Z

    NIFI-1537 checkstyle + workaround bridge issue

commit 726ada409d92a7a59a42af0536e9fc47618853f9
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-02-28T09:12:02Z

    NIFI-1537 Added SNMP processors

commit 05833d935e60d6133ea11cf1e629189586c91ca7
Author: Pierre Villard <pi...@gmail.com>
Date:   2016-03-06T15:30:42Z

    Merge branch 'NIFI-1537' of https://github.com/pvillard31/nifi into NIFI-1537

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request #257: NIFI-1537 Added SNMP processors

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/nifi/pull/257


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request: NIFI-1537 Added SNMP processors

Posted by pvillard31 <gi...@git.apache.org>.
Github user pvillard31 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/257#discussion_r55185061
  
    --- Diff: nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/processors/SNMPUtils.java ---
    @@ -0,0 +1,268 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.nifi.snmp.processors;
    +
    +import java.lang.reflect.Method;
    +import java.lang.reflect.Modifier;
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Vector;
    +import java.util.regex.Pattern;
    +
    +import org.apache.nifi.components.ValidationContext;
    +import org.apache.nifi.components.ValidationResult;
    +import org.apache.nifi.components.Validator;
    +import org.apache.nifi.flowfile.FlowFile;
    +import org.apache.nifi.processor.ProcessSession;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.snmp4j.PDU;
    +import org.snmp4j.security.AuthMD5;
    +import org.snmp4j.security.AuthSHA;
    +import org.snmp4j.security.Priv3DES;
    +import org.snmp4j.security.PrivAES128;
    +import org.snmp4j.security.PrivAES192;
    +import org.snmp4j.security.PrivAES256;
    +import org.snmp4j.security.PrivDES;
    +import org.snmp4j.security.SecurityLevel;
    +import org.snmp4j.smi.OID;
    +import org.snmp4j.smi.VariableBinding;
    +import org.snmp4j.util.TreeEvent;
    +
    +/**
    + * Utility helper class that simplifies interactions with target SNMP API and NIFI API.
    + */
    +abstract class SNMPUtils {
    +
    +    /** logger */
    +    private final static Logger logger = LoggerFactory.getLogger(SNMPUtils.class);
    +
    +    /** OID Pattern */
    +    public final static Pattern OID_PATTERN = Pattern.compile("[[0-9]+\\.]*");
    +
    +    /** delimiter for properties name */
    +    public final static String SNMP_PROP_DELIMITER = "$";
    +
    +    /** prefix for SNMP properties in flow file */
    +    public final static String SNMP_PROP_PREFIX = "snmp" + SNMP_PROP_DELIMITER;
    +
    +    /** list of properties name when performing simple get */
    +    private final static List<String> propertyNames = Arrays.asList("snmp$errorIndex", "snmp$errorStatus", "snmp$errorStatusText",
    +            "snmp$nonRepeaters", "snmp$requestID", "snmp$type", "snmp$variableBindings");
    +
    +    /** used to validate OID syntax */
    +    public static final Validator SNMP_OID_VALIDATOR = new Validator() {
    +        @Override
    +        public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
    +            final ValidationResult.Builder builder = new ValidationResult.Builder();
    +            builder.subject(subject).input(input);
    +            if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
    +                return builder.valid(true).explanation("Contains Expression Language").build();
    +            }
    +            try {
    +                if (OID_PATTERN.matcher(input).matches()) {
    +                    builder.valid(true);
    +                } else {
    +                    builder.valid(false).explanation(input + "is not a valid OID");
    +                }
    +            } catch (final IllegalArgumentException e) {
    +                builder.valid(false).explanation(e.getMessage());
    +            }
    +            return builder.build();
    +        }
    +    };
    +
    +    /**
    +     * Updates {@link FlowFile} with attributes representing PDU properties
    +     * @param response PDU retried from SNMP Agent
    +     * @param flowFile instance of target {@link FlowFile}
    +     * @param processSession instance of {@link ProcessSession}
    +     * @return updated {@link FlowFile}
    +     */
    +    public static FlowFile updateFlowFileAttributesWithPduProperties(PDU response, FlowFile flowFile, ProcessSession processSession) {
    +        if (response != null) {
    +            try {
    +                Method[] methods = PDU.class.getDeclaredMethods();
    +                Map<String, String> attributes = new HashMap<String, String>();
    +                for (Method method : methods) {
    +                    if (Modifier.isPublic(method.getModifiers()) && (method.getParameterCount() == 0) && method.getName().startsWith("get")) {
    --- End diff --
    
    Thanks Oleg, I have fixed it.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request: NIFI-1537 Added SNMP processors

Posted by pvillard31 <gi...@git.apache.org>.
Github user pvillard31 commented on the pull request:

    https://github.com/apache/nifi/pull/257#issuecomment-193330177
  
    Pre-requisite: NIFI-1595 has to be merged in before


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request: NIFI-1537 Added SNMP processors

Posted by olegz <gi...@git.apache.org>.
Github user olegz commented on the pull request:

    https://github.com/apache/nifi/pull/257#issuecomment-200559586
  
    Pierre
    
    You may want to rebase (merge conflicts) and push again since it's out of sink.
    Also, aside from unit tests would you mind giving some pointers on how to test it outside of unit tests? A link on how to setup an environment or a short writeup. I am not that familiar with SNMP


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi issue #257: NIFI-1537 Added SNMP processors

Posted by olegz <gi...@git.apache.org>.
Github user olegz commented on the issue:

    https://github.com/apache/nifi/pull/257
  
    Ok, finally I am in this PR and no distractions ;)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request: NIFI-1537 Added SNMP processors

Posted by pvillard31 <gi...@git.apache.org>.
Github user pvillard31 commented on the pull request:

    https://github.com/apache/nifi/pull/257#issuecomment-193298687
  
    Oleg, yes sure, will do!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi issue #257: NIFI-1537 Added SNMP processors

Posted by olegz <gi...@git.apache.org>.
Github user olegz commented on the issue:

    https://github.com/apache/nifi/pull/257
  
    . . . and you almost scored ;) but. . . anyway, will try to wrap it up tomorrow


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi issue #257: NIFI-1537 Added SNMP processors

Posted by olegz <gi...@git.apache.org>.
Github user olegz commented on the issue:

    https://github.com/apache/nifi/pull/257
  
    @pvillard31  Had a merge conflict again, but fixed it myself. Nothing to do, just making comment for traceability.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request: NIFI-1537 Added SNMP processors

Posted by olegz <gi...@git.apache.org>.
Github user olegz commented on the pull request:

    https://github.com/apache/nifi/pull/257#issuecomment-193297602
  
    Pierre, so the ReflectionUtils fix is in https://github.com/apache/nifi/pull/260. So watch the PR and once it is merged, rebase, update (remove that temp fix code) and test and let me know. I've already tested it with your branch but still extra validation would help.



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request: NIFI-1537 Added SNMP processors

Posted by olegz <gi...@git.apache.org>.
Github user olegz commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/257#discussion_r55369456
  
    --- Diff: nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/processors/AbstractSNMPProcessor.java ---
    @@ -0,0 +1,414 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.nifi.snmp.processors;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.Collection;
    +import java.util.List;
    +
    +import org.apache.nifi.annotation.lifecycle.OnStopped;
    +import org.apache.nifi.components.PropertyDescriptor;
    +import org.apache.nifi.components.ValidationContext;
    +import org.apache.nifi.components.ValidationResult;
    +import org.apache.nifi.processor.AbstractProcessor;
    +import org.apache.nifi.processor.ProcessContext;
    +import org.apache.nifi.processor.ProcessSession;
    +import org.apache.nifi.processor.Processor;
    +import org.apache.nifi.processor.exception.ProcessException;
    +import org.apache.nifi.processor.util.StandardValidators;
    +import org.snmp4j.AbstractTarget;
    +import org.snmp4j.CommunityTarget;
    +import org.snmp4j.Snmp;
    +import org.snmp4j.TransportMapping;
    +import org.snmp4j.UserTarget;
    +import org.snmp4j.mp.MPv3;
    +import org.snmp4j.mp.SnmpConstants;
    +import org.snmp4j.security.SecurityModels;
    +import org.snmp4j.security.SecurityProtocols;
    +import org.snmp4j.security.USM;
    +import org.snmp4j.security.UsmUser;
    +import org.snmp4j.smi.OctetString;
    +import org.snmp4j.smi.UdpAddress;
    +import org.snmp4j.transport.DefaultUdpTransportMapping;
    +
    +/**
    + * Base processor that uses SNMP4J client API
    + * (http://www.snmp4j.org/)
    + *
    + * @param <T> the type of {@link SNMPWorker}. Please see {@link SNMPSetter}
    + *            and {@link SNMPGetter}
    + */
    +abstract class AbstractSNMPProcessor<T extends SNMPWorker> extends AbstractProcessor {
    +
    +    /** property to define host of the SNMP agent */
    +    public static final PropertyDescriptor HOST = new PropertyDescriptor.Builder()
    +            .name("Host Name")
    +            .description("Network address of SNMP Agent (e.g., localhost)")
    +            .required(true)
    +            .defaultValue("localhost")
    +            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    +            .build();
    +
    +    /** property to define port of the SNMP agent */
    +    public static final PropertyDescriptor PORT = new PropertyDescriptor.Builder()
    +            .name("Port")
    +            .description("Numeric value identifying Port of SNMP Agent (e.g., 161)")
    +            .required(true)
    +            .defaultValue("161")
    +            .addValidator(StandardValidators.PORT_VALIDATOR)
    +            .build();
    +
    +    /** property to define SNMP version to use */
    +    public static final PropertyDescriptor SNMP_VERSION = new PropertyDescriptor.Builder()
    +            .name("SNMP Version")
    +            .description("SNMP Version to use")
    +            .required(true)
    +            .allowableValues("SNMPv1", "SNMPv2c", "SNMPv3")
    +            .defaultValue("SNMPv1")
    +            .build();
    +
    +    /** property to define SNMP community to use */
    +    public static final PropertyDescriptor SNMP_COMMUNITY = new PropertyDescriptor.Builder()
    +            .name("SNMP Community (v1 & v2c)")
    +            .description("SNMP Community to use (e.g., public)")
    +            .required(false)
    +            .defaultValue("public")
    +            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    +            .build();
    +
    +    /** property to define SNMP security level to use */
    +    public static final PropertyDescriptor SNMP_SECURITY_LEVEL = new PropertyDescriptor.Builder()
    +            .name("SNMP Security Level")
    +            .description("SNMP Security Level to use")
    +            .required(true)
    +            .allowableValues("noAuthNoPriv", "authNoPriv", "authPriv")
    +            .defaultValue("authPriv")
    +            .build();
    +
    +    /** property to define SNMP security name to use */
    +    public static final PropertyDescriptor SNMP_SECURITY_NAME = new PropertyDescriptor.Builder()
    +            .name("SNMP Security name / user name")
    +            .description("Security name used for SNMP exchanges")
    +            .required(false)
    +            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    +            .build();
    +
    +    /** property to define SNMP authentication protocol to use */
    +    public static final PropertyDescriptor SNMP_AUTH_PROTOCOL = new PropertyDescriptor.Builder()
    +            .name("SNMP Authentication Protocol")
    +            .description("SNMP Authentication Protocol to use")
    +            .required(true)
    +            .allowableValues("MD5", "SHA", "")
    +            .defaultValue("")
    +            .build();
    +
    +    /** property to define SNMP authentication password to use */
    +    public static final PropertyDescriptor SNMP_AUTH_PASSWORD = new PropertyDescriptor.Builder()
    +            .name("SNMP Authentication pass phrase")
    +            .description("Pass phrase used for SNMP authentication protocol")
    +            .required(false)
    +            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    +            .sensitive(true)
    +            .build();
    +
    +    /** property to define SNMP private protocol to use */
    +    public static final PropertyDescriptor SNMP_PRIV_PROTOCOL = new PropertyDescriptor.Builder()
    +            .name("SNMP Private Protocol")
    +            .description("SNMP Private Protocol to use")
    +            .required(true)
    +            .allowableValues("DES", "3DES", "AES128", "AES192", "AES256", "")
    +            .defaultValue("")
    +            .build();
    +
    +    /** property to define SNMP private password to use */
    +    public static final PropertyDescriptor SNMP_PRIV_PASSWORD = new PropertyDescriptor.Builder()
    +            .name("SNMP Private protocol pass phrase")
    +            .description("Pass phrase used for SNMP private protocol")
    +            .required(false)
    +            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    +            .sensitive(true)
    +            .build();
    +
    +    /** property to define the number of SNMP retries when requesting the SNMP Agent */
    +    public static final PropertyDescriptor SNMP_RETRIES = new PropertyDescriptor.Builder()
    +            .name("Number of retries")
    +            .description("Set the number of retries when requesting the SNMP Agent")
    +            .required(true)
    +            .defaultValue("0")
    +            .addValidator(StandardValidators.INTEGER_VALIDATOR)
    +            .build();
    +
    +    /** property to define the timeout when requesting the SNMP Agent */
    +    public static final PropertyDescriptor SNMP_TIMEOUT = new PropertyDescriptor.Builder()
    +            .name("Timeout (ms)")
    +            .description("Set the timeout (in milliseconds) when requesting the SNMP Agent")
    +            .required(true)
    +            .defaultValue("5000")
    +            .addValidator(StandardValidators.INTEGER_VALIDATOR)
    +            .build();
    +
    +    /** list of property descriptors */
    +    static List<PropertyDescriptor> descriptors = new ArrayList<>();
    +
    +    /*
    +     * Will ensure that list of PropertyDescriptors is build only once, since
    +     * all other life cycle methods are invoked multiple times.
    +     */
    +    static {
    +        descriptors.add(HOST);
    +        descriptors.add(PORT);
    +        descriptors.add(SNMP_VERSION);
    +        descriptors.add(SNMP_COMMUNITY);
    +        descriptors.add(SNMP_SECURITY_LEVEL);
    +        descriptors.add(SNMP_SECURITY_NAME);
    +        descriptors.add(SNMP_AUTH_PROTOCOL);
    +        descriptors.add(SNMP_AUTH_PASSWORD);
    +        descriptors.add(SNMP_PRIV_PROTOCOL);
    +        descriptors.add(SNMP_PRIV_PASSWORD);
    +        descriptors.add(SNMP_RETRIES);
    +        descriptors.add(SNMP_TIMEOUT);
    +    }
    +
    +    /** SNMP target */
    +    protected volatile AbstractTarget snmpTarget;
    +
    +    /** transport mapping */
    +    protected volatile TransportMapping transportMapping;
    +
    +    /** SNMP */
    +    protected volatile Snmp snmp;
    +
    +    /** target resource */
    +    protected volatile T targetResource;
    +
    +    /**
    +     * Will builds target resource upon first invocation and will delegate to the
    +     * implementation of {@link #onTriggerSnmp(ProcessContext, ProcessSession)} method for
    +     * further processing.
    +     */
    +    @Override
    +    public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    +        synchronized (this) {
    +            this.buildTargetResource(context);
    +        }
    +        this.onTriggerSnmp(context, session);
    +    }
    +
    +    /**
    +     * Will close current SNMP mapping.
    +     */
    +    @OnStopped
    +    public void close() {
    +        try {
    +            if (this.targetResource != null) {
    +                this.targetResource.close();
    +            }
    +        } catch (Exception e) {
    +            this.getLogger().warn("Failure while closing target resource " + this.targetResource, e);
    +        }
    +        this.targetResource = null;
    +
    +        try {
    +            if (this.transportMapping != null) {
    +                this.transportMapping.close();
    +            }
    +        } catch (IOException e) {
    +            this.getLogger().warn("Failure while closing UDP transport mapping", e);
    +        }
    +        this.transportMapping = null;
    +
    +        try {
    +            if (this.snmp != null) {
    +                this.snmp.close();
    +            }
    +        } catch (IOException e) {
    +            this.getLogger().warn("Failure while closing UDP transport mapping", e);
    +        }
    +        this.snmp = null;
    +    }
    +
    +    /**
    +     * @see org.apache.nifi.components.AbstractConfigurableComponent#customValidate(org.apache.nifi.components.ValidationContext)
    +     */
    +    @Override
    +    protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
    +        final List<ValidationResult> problems = new ArrayList<>(super.customValidate(validationContext));
    +
    +        final boolean isVersion3 = "SNMPv3".equals(validationContext.getProperty(SNMP_VERSION).getValue());
    +
    +        if(isVersion3) {
    +            final boolean isSecurityNameSet = validationContext.getProperty(SNMP_SECURITY_NAME).isSet();
    +            if(!isSecurityNameSet) {
    +                problems.add(new ValidationResult.Builder()
    +                        .input("SNMP Security Name")
    +                        .valid(false)
    +                        .explanation("SNMP Security Name must be set with SNMPv3.")
    +                        .build());
    +            }
    +
    +            final boolean isAuthProtOK = !"".equals(validationContext.getProperty(SNMP_AUTH_PROTOCOL).getValue());
    +            final boolean isAuthPwdSet = validationContext.getProperty(SNMP_AUTH_PASSWORD).isSet();
    +            final boolean isPrivProtOK = !"".equals(validationContext.getProperty(SNMP_PRIV_PROTOCOL).getValue());
    +            final boolean isPrivPwdSet = validationContext.getProperty(SNMP_PRIV_PASSWORD).isSet();
    +
    +            switch(validationContext.getProperty(SNMP_SECURITY_LEVEL).getValue()) {
    +            case "authNoPriv":
    +                if(!isAuthProtOK || !isAuthPwdSet) {
    +                    problems.add(new ValidationResult.Builder()
    +                            .input("SNMP Security Level")
    +                            .valid(false)
    +                            .explanation("Authentication protocol and password must be set when using authNoPriv security level.")
    +                            .build());
    +                }
    +                break;
    +            case "authPriv":
    +                if(!isAuthProtOK || !isAuthPwdSet || !isPrivProtOK || !isPrivPwdSet) {
    +                    problems.add(new ValidationResult.Builder()
    +                            .input("SNMP Security Level")
    +                            .valid(false)
    +                            .explanation("All protocols and passwords must be set when using authPriv security level.")
    +                            .build());
    +                }
    +                break;
    +            case "noAuthNoPriv":
    +            default:
    +                break;
    +            }
    +        } else {
    +            final boolean isCommunitySet = validationContext.getProperty(SNMP_COMMUNITY).isSet();
    +            if(!isCommunitySet) {
    +                problems.add(new ValidationResult.Builder()
    +                        .input("SNMP Community")
    +                        .valid(false)
    +                        .explanation("SNMP Community must be set with SNMPv1 and SNMPv2c.")
    +                        .build());
    +            }
    +        }
    +
    +        return problems;
    +    }
    +
    +    /**
    +     * Delegate method to supplement
    +     * {@link #onTrigger(ProcessContext, ProcessSession)}. It is implemented by
    +     * sub-classes to perform {@link Processor} specific functionality.
    +     *
    +     * @param context
    +     *            instance of {@link ProcessContext}
    +     * @param session
    +     *            instance of {@link ProcessSession}
    +     * @throws ProcessException Process exception
    +     */
    +    protected abstract void onTriggerSnmp(ProcessContext context, ProcessSession session) throws ProcessException;
    +
    +    /**
    +     * Delegate method to supplement building of target {@link SNMPWorker} (see
    +     * {@link SNMPSetter} or {@link SNMPGetter}) and is implemented by
    +     * sub-classes.
    +     *
    +     * @param context
    +     *            instance of {@link ProcessContext}
    +     * @return new instance of {@link SNMPWorker}
    +     */
    +    protected abstract T finishBuildingTargetResource(ProcessContext context);
    +
    +    /**
    +     * Builds target resource.
    +     * @param context Process context
    +     */
    +    private void buildTargetResource(ProcessContext context) {
    +        if((this.transportMapping == null) || !this.transportMapping.isListening() || (this.snmp == null)) {
    +            try {
    +                this.transportMapping = new DefaultUdpTransportMapping();
    +                this.snmp = new Snmp(this.transportMapping);
    +
    +                if("SNMPv3".equals(context.getProperty(SNMP_VERSION).getValue())) {
    +                    USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
    +                    SecurityModels.getInstance().addSecurityModel(usm);
    --- End diff --
    
    I just initiated a DISCUSS thread on the dev list. Basically while there is not much we can do at the moment, the above code will set the security model that will be applicable across all instances of SNMP processors within a single instance of NiFi.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request: NIFI-1537 Added SNMP processors

Posted by michalklempa <gi...@git.apache.org>.
Github user michalklempa commented on the pull request:

    https://github.com/apache/nifi/pull/257#issuecomment-200691342
  
    @olegz there is http://snmpsim.sourceforge.net/ project, which can be used for testing purposes. Some simulation files are available with distribution, so basic testing may be done out-of-the-box


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request: NIFI-1537 Added SNMP processors

Posted by olegz <gi...@git.apache.org>.
Github user olegz commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/257#discussion_r55371374
  
    --- Diff: nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SetSNMPTest.java ---
    @@ -0,0 +1,411 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.nifi.snmp.processors;
    +
    +import static org.junit.Assert.assertEquals;
    +import static org.junit.Assert.assertNotNull;
    +import static org.junit.Assert.assertTrue;
    +
    +import java.util.HashMap;
    +import java.util.Map;
    +
    +import org.apache.nifi.util.MockFlowFile;
    +import org.apache.nifi.util.TestRunner;
    +import org.apache.nifi.util.TestRunners;
    +import org.junit.AfterClass;
    +import org.junit.BeforeClass;
    +import org.junit.Test;
    +import org.snmp4j.AbstractTarget;
    +import org.snmp4j.CommunityTarget;
    +import org.snmp4j.Snmp;
    +import org.snmp4j.agent.mo.DefaultMOFactory;
    +import org.snmp4j.agent.mo.MOAccessImpl;
    +import org.snmp4j.mp.SnmpConstants;
    +import org.snmp4j.smi.Integer32;
    +import org.snmp4j.smi.OID;
    +import org.snmp4j.smi.OctetString;
    +
    +/**
    + * Class to test SNMP Get processor
    + */
    +public class SetSNMPTest {
    +
    +    /** agent for version v1 */
    +    private static TestSnmpAgentV1 agentv1 = null;
    +    /** agent for version v2c */
    +    private static TestSnmpAgentV2c agentv2c = null;
    +    /** OID for system description */
    +    private static final OID sysDescr = new OID("1.3.6.1.2.1.1.1.0");
    +    /** value we set for system description at set-up */
    +    private static final String value = "MySystemDescr";
    +    /** OID for read only access */
    +    private static final OID readOnlyOID = new OID("1.3.6.1.2.1.1.3.0");
    +    /** value we set for read only at set-up */
    +    private static final int readOnlyValue = 1;
    +    /** OID for write only access */
    +    private static final OID writeOnlyOID = new OID("1.3.6.1.2.1.1.3.0");
    +    /** value we set for write only at set-up */
    +    private static final int writeOnlyValue = 1;
    +
    +    /**
    +     * Method to set up different SNMP agents
    +     * @throws Exception Exception
    +     */
    +    @BeforeClass
    +    public static void setUp() throws Exception {
    +        agentv2c = new TestSnmpAgentV2c("0.0.0.0/2001");
    +        agentv2c.start();
    +        agentv2c.unregisterManagedObject(agentv2c.getSnmpv2MIB());
    +        agentv2c.registerManagedObject(
    +                DefaultMOFactory.getInstance().createScalar(sysDescr,
    +                        MOAccessImpl.ACCESS_READ_WRITE,
    +                        new OctetString(value)));
    +        agentv2c.registerManagedObject(
    +                DefaultMOFactory.getInstance().createScalar(readOnlyOID,
    +                        MOAccessImpl.ACCESS_READ_ONLY,
    +                        new Integer32(readOnlyValue)));
    +
    +        agentv1 = new TestSnmpAgentV1("0.0.0.0/2002");
    +        agentv1.start();
    +        agentv1.unregisterManagedObject(agentv1.getSnmpv2MIB());
    +        agentv1.registerManagedObject(
    +                DefaultMOFactory.getInstance().createScalar(sysDescr,
    +                        MOAccessImpl.ACCESS_READ_WRITE,
    +                        new OctetString(value)));
    +        agentv1.registerManagedObject(
    +                DefaultMOFactory.getInstance().createScalar(writeOnlyOID,
    +                        MOAccessImpl.ACCESS_WRITE_ONLY,
    +                        new Integer32(writeOnlyValue)));
    +    }
    +
    +    /**
    +     * Method to close SNMP Agent once the tests are completed
    +     * @throws Exception Exception
    +     */
    +    @AfterClass
    +    public static void tearDown() throws Exception {
    +        agentv1.stop();
    +        agentv2c.stop();
    +    }
    +
    +    //    /**
    +    //     * Test to check FlowFile handling when performing a SNMP Get.
    +    //     * First we set a new value for the OID we want to request, then we
    +    //     * request this OID and we check that the returned value if the one
    +    //     * we set just before.
    +    //     * @throws Exception Exception
    +    //     */
    +    //    @Test
    +    //    public void validateSuccessfullSnmpSetv2c() throws Exception {
    +    //        Snmp snmp = SNMPUtilsTest.createSnmp();
    +    //        CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2001", SnmpConstants.version2c);
    +    //
    +    //        try (SNMPSetter setter = new SNMPSetter(snmp, target)) {
    +    //            PDU pdu = new PDU();
    +    //            pdu.add(new VariableBinding(new OID(sysDescr), new OctetString("test")));
    +    //            pdu.setType(PDU.SET);
    +    //            ResponseEvent response = setter.set(pdu);
    +    //            if(response.getResponse().getErrorStatus() != PDU.noError ) {
    +    //                fail();
    +    //            }
    +    //            Thread.sleep(200);
    +    //
    +    //            SetSNMP pubProc = new LocalSetSnmp(snmp, target);
    +    //            TestRunner runner = TestRunners.newTestRunner(pubProc);
    +    //
    +    //            runner.setProperty(SetSNMP.OID, sysDescr.toString());
    +    //            runner.setProperty(SetSNMP.HOST, "127.0.0.1");
    +    //            runner.setProperty(SetSNMP.PORT, "2002");
    +    //            runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public");
    +    //            runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv1");
    +    //
    +    //            runner.run();
    +    //            Thread.sleep(200);
    +    //            final MockFlowFile successFF = runner.getFlowFilesForRelationship(SetSNMP.REL_SUCCESS).get(0);
    +    //            assertNotNull(successFF);
    +    //            assertEquals("test", successFF.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + sysDescr.toString()));
    +    //        }
    +    //    }
    +    //
    +    //    /**
    +    //     * Test to check FlowFile handling when performing a SNMP Get.
    +    //     * First we set a new value for the OID we want to request, then we
    +    //     * request this OID and we check that the returned value if the one
    +    //     * we set just before.
    +    //     * @throws Exception Exception
    +    //     */
    +    //    @Test
    +    //    public void validateSuccessfullSnmpSetv1() throws Exception {
    +    //        Snmp snmp = SNMPUtilsTest.createSnmp();
    +    //        CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2002", SnmpConstants.version1);
    +    //
    +    //        try (SNMPSetter setter = new SNMPSetter(snmp, target)) {
    +    //            PDU pdu = new PDU();
    +    //            pdu.add(new VariableBinding(new OID(sysDescr), new OctetString("test")));
    +    //            pdu.setType(PDU.SET);
    +    //            ResponseEvent response = setter.set(pdu);
    +    //            if(response.getResponse().getErrorStatus() != PDU.noError ) {
    +    //                fail();
    +    //            }
    +    //            Thread.sleep(200);
    +    //
    +    //            SetSNMP pubProc = new LocalSetSnmp(snmp, target);
    +    //            TestRunner runner = TestRunners.newTestRunner(pubProc);
    +    //
    +    //            runner.setProperty(SetSNMP.OID, sysDescr.toString());
    +    //            runner.setProperty(SetSNMP.HOST, "127.0.0.1");
    +    //            runner.setProperty(SetSNMP.PORT, "2002");
    +    //            runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public");
    +    //            runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv1");
    +    //
    +    //            runner.run();
    +    //            Thread.sleep(200);
    +    //            final MockFlowFile successFF = runner.getFlowFilesForRelationship(SetSNMP.REL_SUCCESS).get(0);
    +    //            assertNotNull(successFF);
    +    //            assertEquals("test", successFF.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + sysDescr.toString()));
    +    //        }
    +    //    }
    +
    --- End diff --
    
    Pierre, did you intent to keep commented code above?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request: NIFI-1537 Added SNMP processors

Posted by pvillard31 <gi...@git.apache.org>.
Github user pvillard31 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/257#discussion_r55373363
  
    --- Diff: nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/test/java/org/apache/nifi/snmp/processors/SetSNMPTest.java ---
    @@ -0,0 +1,411 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.nifi.snmp.processors;
    +
    +import static org.junit.Assert.assertEquals;
    +import static org.junit.Assert.assertNotNull;
    +import static org.junit.Assert.assertTrue;
    +
    +import java.util.HashMap;
    +import java.util.Map;
    +
    +import org.apache.nifi.util.MockFlowFile;
    +import org.apache.nifi.util.TestRunner;
    +import org.apache.nifi.util.TestRunners;
    +import org.junit.AfterClass;
    +import org.junit.BeforeClass;
    +import org.junit.Test;
    +import org.snmp4j.AbstractTarget;
    +import org.snmp4j.CommunityTarget;
    +import org.snmp4j.Snmp;
    +import org.snmp4j.agent.mo.DefaultMOFactory;
    +import org.snmp4j.agent.mo.MOAccessImpl;
    +import org.snmp4j.mp.SnmpConstants;
    +import org.snmp4j.smi.Integer32;
    +import org.snmp4j.smi.OID;
    +import org.snmp4j.smi.OctetString;
    +
    +/**
    + * Class to test SNMP Get processor
    + */
    +public class SetSNMPTest {
    +
    +    /** agent for version v1 */
    +    private static TestSnmpAgentV1 agentv1 = null;
    +    /** agent for version v2c */
    +    private static TestSnmpAgentV2c agentv2c = null;
    +    /** OID for system description */
    +    private static final OID sysDescr = new OID("1.3.6.1.2.1.1.1.0");
    +    /** value we set for system description at set-up */
    +    private static final String value = "MySystemDescr";
    +    /** OID for read only access */
    +    private static final OID readOnlyOID = new OID("1.3.6.1.2.1.1.3.0");
    +    /** value we set for read only at set-up */
    +    private static final int readOnlyValue = 1;
    +    /** OID for write only access */
    +    private static final OID writeOnlyOID = new OID("1.3.6.1.2.1.1.3.0");
    +    /** value we set for write only at set-up */
    +    private static final int writeOnlyValue = 1;
    +
    +    /**
    +     * Method to set up different SNMP agents
    +     * @throws Exception Exception
    +     */
    +    @BeforeClass
    +    public static void setUp() throws Exception {
    +        agentv2c = new TestSnmpAgentV2c("0.0.0.0/2001");
    +        agentv2c.start();
    +        agentv2c.unregisterManagedObject(agentv2c.getSnmpv2MIB());
    +        agentv2c.registerManagedObject(
    +                DefaultMOFactory.getInstance().createScalar(sysDescr,
    +                        MOAccessImpl.ACCESS_READ_WRITE,
    +                        new OctetString(value)));
    +        agentv2c.registerManagedObject(
    +                DefaultMOFactory.getInstance().createScalar(readOnlyOID,
    +                        MOAccessImpl.ACCESS_READ_ONLY,
    +                        new Integer32(readOnlyValue)));
    +
    +        agentv1 = new TestSnmpAgentV1("0.0.0.0/2002");
    +        agentv1.start();
    +        agentv1.unregisterManagedObject(agentv1.getSnmpv2MIB());
    +        agentv1.registerManagedObject(
    +                DefaultMOFactory.getInstance().createScalar(sysDescr,
    +                        MOAccessImpl.ACCESS_READ_WRITE,
    +                        new OctetString(value)));
    +        agentv1.registerManagedObject(
    +                DefaultMOFactory.getInstance().createScalar(writeOnlyOID,
    +                        MOAccessImpl.ACCESS_WRITE_ONLY,
    +                        new Integer32(writeOnlyValue)));
    +    }
    +
    +    /**
    +     * Method to close SNMP Agent once the tests are completed
    +     * @throws Exception Exception
    +     */
    +    @AfterClass
    +    public static void tearDown() throws Exception {
    +        agentv1.stop();
    +        agentv2c.stop();
    +    }
    +
    +    //    /**
    +    //     * Test to check FlowFile handling when performing a SNMP Get.
    +    //     * First we set a new value for the OID we want to request, then we
    +    //     * request this OID and we check that the returned value if the one
    +    //     * we set just before.
    +    //     * @throws Exception Exception
    +    //     */
    +    //    @Test
    +    //    public void validateSuccessfullSnmpSetv2c() throws Exception {
    +    //        Snmp snmp = SNMPUtilsTest.createSnmp();
    +    //        CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2001", SnmpConstants.version2c);
    +    //
    +    //        try (SNMPSetter setter = new SNMPSetter(snmp, target)) {
    +    //            PDU pdu = new PDU();
    +    //            pdu.add(new VariableBinding(new OID(sysDescr), new OctetString("test")));
    +    //            pdu.setType(PDU.SET);
    +    //            ResponseEvent response = setter.set(pdu);
    +    //            if(response.getResponse().getErrorStatus() != PDU.noError ) {
    +    //                fail();
    +    //            }
    +    //            Thread.sleep(200);
    +    //
    +    //            SetSNMP pubProc = new LocalSetSnmp(snmp, target);
    +    //            TestRunner runner = TestRunners.newTestRunner(pubProc);
    +    //
    +    //            runner.setProperty(SetSNMP.OID, sysDescr.toString());
    +    //            runner.setProperty(SetSNMP.HOST, "127.0.0.1");
    +    //            runner.setProperty(SetSNMP.PORT, "2002");
    +    //            runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public");
    +    //            runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv1");
    +    //
    +    //            runner.run();
    +    //            Thread.sleep(200);
    +    //            final MockFlowFile successFF = runner.getFlowFilesForRelationship(SetSNMP.REL_SUCCESS).get(0);
    +    //            assertNotNull(successFF);
    +    //            assertEquals("test", successFF.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + sysDescr.toString()));
    +    //        }
    +    //    }
    +    //
    +    //    /**
    +    //     * Test to check FlowFile handling when performing a SNMP Get.
    +    //     * First we set a new value for the OID we want to request, then we
    +    //     * request this OID and we check that the returned value if the one
    +    //     * we set just before.
    +    //     * @throws Exception Exception
    +    //     */
    +    //    @Test
    +    //    public void validateSuccessfullSnmpSetv1() throws Exception {
    +    //        Snmp snmp = SNMPUtilsTest.createSnmp();
    +    //        CommunityTarget target = SNMPUtilsTest.createCommTarget("public", "127.0.0.1/2002", SnmpConstants.version1);
    +    //
    +    //        try (SNMPSetter setter = new SNMPSetter(snmp, target)) {
    +    //            PDU pdu = new PDU();
    +    //            pdu.add(new VariableBinding(new OID(sysDescr), new OctetString("test")));
    +    //            pdu.setType(PDU.SET);
    +    //            ResponseEvent response = setter.set(pdu);
    +    //            if(response.getResponse().getErrorStatus() != PDU.noError ) {
    +    //                fail();
    +    //            }
    +    //            Thread.sleep(200);
    +    //
    +    //            SetSNMP pubProc = new LocalSetSnmp(snmp, target);
    +    //            TestRunner runner = TestRunners.newTestRunner(pubProc);
    +    //
    +    //            runner.setProperty(SetSNMP.OID, sysDescr.toString());
    +    //            runner.setProperty(SetSNMP.HOST, "127.0.0.1");
    +    //            runner.setProperty(SetSNMP.PORT, "2002");
    +    //            runner.setProperty(SetSNMP.SNMP_COMMUNITY, "public");
    +    //            runner.setProperty(SetSNMP.SNMP_VERSION, "SNMPv1");
    +    //
    +    //            runner.run();
    +    //            Thread.sleep(200);
    +    //            final MockFlowFile successFF = runner.getFlowFilesForRelationship(SetSNMP.REL_SUCCESS).get(0);
    +    //            assertNotNull(successFF);
    +    //            assertEquals("test", successFF.getAttributes().get(SNMPUtils.SNMP_PROP_PREFIX + sysDescr.toString()));
    +    //        }
    +    //    }
    +
    --- End diff --
    
    Definitely not, my bad. Going to fix it.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request: NIFI-1537 Added SNMP processors

Posted by olegz <gi...@git.apache.org>.
Github user olegz commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/257#discussion_r55147036
  
    --- Diff: nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/processors/SNMPUtils.java ---
    @@ -0,0 +1,268 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.nifi.snmp.processors;
    +
    +import java.lang.reflect.Method;
    +import java.lang.reflect.Modifier;
    +import java.util.Arrays;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Vector;
    +import java.util.regex.Pattern;
    +
    +import org.apache.nifi.components.ValidationContext;
    +import org.apache.nifi.components.ValidationResult;
    +import org.apache.nifi.components.Validator;
    +import org.apache.nifi.flowfile.FlowFile;
    +import org.apache.nifi.processor.ProcessSession;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.snmp4j.PDU;
    +import org.snmp4j.security.AuthMD5;
    +import org.snmp4j.security.AuthSHA;
    +import org.snmp4j.security.Priv3DES;
    +import org.snmp4j.security.PrivAES128;
    +import org.snmp4j.security.PrivAES192;
    +import org.snmp4j.security.PrivAES256;
    +import org.snmp4j.security.PrivDES;
    +import org.snmp4j.security.SecurityLevel;
    +import org.snmp4j.smi.OID;
    +import org.snmp4j.smi.VariableBinding;
    +import org.snmp4j.util.TreeEvent;
    +
    +/**
    + * Utility helper class that simplifies interactions with target SNMP API and NIFI API.
    + */
    +abstract class SNMPUtils {
    +
    +    /** logger */
    +    private final static Logger logger = LoggerFactory.getLogger(SNMPUtils.class);
    +
    +    /** OID Pattern */
    +    public final static Pattern OID_PATTERN = Pattern.compile("[[0-9]+\\.]*");
    +
    +    /** delimiter for properties name */
    +    public final static String SNMP_PROP_DELIMITER = "$";
    +
    +    /** prefix for SNMP properties in flow file */
    +    public final static String SNMP_PROP_PREFIX = "snmp" + SNMP_PROP_DELIMITER;
    +
    +    /** list of properties name when performing simple get */
    +    private final static List<String> propertyNames = Arrays.asList("snmp$errorIndex", "snmp$errorStatus", "snmp$errorStatusText",
    +            "snmp$nonRepeaters", "snmp$requestID", "snmp$type", "snmp$variableBindings");
    +
    +    /** used to validate OID syntax */
    +    public static final Validator SNMP_OID_VALIDATOR = new Validator() {
    +        @Override
    +        public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
    +            final ValidationResult.Builder builder = new ValidationResult.Builder();
    +            builder.subject(subject).input(input);
    +            if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
    +                return builder.valid(true).explanation("Contains Expression Language").build();
    +            }
    +            try {
    +                if (OID_PATTERN.matcher(input).matches()) {
    +                    builder.valid(true);
    +                } else {
    +                    builder.valid(false).explanation(input + "is not a valid OID");
    +                }
    +            } catch (final IllegalArgumentException e) {
    +                builder.valid(false).explanation(e.getMessage());
    +            }
    +            return builder.build();
    +        }
    +    };
    +
    +    /**
    +     * Updates {@link FlowFile} with attributes representing PDU properties
    +     * @param response PDU retried from SNMP Agent
    +     * @param flowFile instance of target {@link FlowFile}
    +     * @param processSession instance of {@link ProcessSession}
    +     * @return updated {@link FlowFile}
    +     */
    +    public static FlowFile updateFlowFileAttributesWithPduProperties(PDU response, FlowFile flowFile, ProcessSession processSession) {
    +        if (response != null) {
    +            try {
    +                Method[] methods = PDU.class.getDeclaredMethods();
    +                Map<String, String> attributes = new HashMap<String, String>();
    +                for (Method method : methods) {
    +                    if (Modifier.isPublic(method.getModifiers()) && (method.getParameterCount() == 0) && method.getName().startsWith("get")) {
    --- End diff --
    
    Pierre, I noticed this when I was researching for your OnStopped issue. I believe this method 'getParameterCount' was introduced in Java 8 while we still have to be compliant with Java 7. So consider refactoring.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request: NIFI-1537 Added SNMP processors

Posted by olegz <gi...@git.apache.org>.
Github user olegz commented on the pull request:

    https://github.com/apache/nifi/pull/257#issuecomment-200797003
  
    @michalklempa @pvillard31 thank you both for the input. Will be reviewing and testing. 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi issue #257: NIFI-1537 Added SNMP processors

Posted by trixpan <gi...@git.apache.org>.
Github user trixpan commented on the issue:

    https://github.com/apache/nifi/pull/257
  
    @olegz  any luck with this review? Cheers


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi issue #257: NIFI-1537 Added SNMP processors

Posted by pvillard31 <gi...@git.apache.org>.
Github user pvillard31 commented on the issue:

    https://github.com/apache/nifi/pull/257
  
    @trixpan @olegz @michalklempa 
    I rebased the PR against master


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request: NIFI-1537 Added SNMP processors

Posted by pvillard31 <gi...@git.apache.org>.
Github user pvillard31 commented on the pull request:

    https://github.com/apache/nifi/pull/257#issuecomment-200748951
  
    @olegz I just rebased the PR.
    
    As @michalklempa mentioned, you can use online SNMP service provided by snmpsim (http://snmpsim.sourceforge.net/public-snmp-simulator.html), otherwise it depends of your OS. Personally, I tried the processor by requesting basic SNMP data (such as uptime for example) on a CentOS machine with net-snmp agent running with various configurations in SNMP v1, v2c and v3 (MD5/DES). 
    
    As a side note, at this moment, the processor is expecting an OID in numerical format. If I try the following command on my computer:
    
    `snmpwalk -v2c -c public demo.snmplabs.com IF-MIB::interfaces`
    
    I can get numerical OID by adding the option -On:
    
    `snmpwalk -v2c -c public demo.snmplabs.com IF-MIB::interfaces -On`
    
    This way I can see that IF-MIB::interfaces is equivalent to .1.3.6.1.2.1.2.2.1
    
    I will try to write something to help setting an environment. On a Linux-like environment, a quick setup can be:
    yum install net-snmp net-snmp-utils
    service snmpd start
    
    It will allow access to basic SNMP information in v1 and v2c with 'public' as community. For SNMP v3 setup, it is possible to add users using the command net-snmp-create-v3-user.
    
    Hope it helps, if not, let me know ;)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi issue #257: NIFI-1537 Added SNMP processors

Posted by pvillard31 <gi...@git.apache.org>.
Github user pvillard31 commented on the issue:

    https://github.com/apache/nifi/pull/257
  
    @olegz half-time ;) done!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi issue #257: NIFI-1537 Added SNMP processors

Posted by olegz <gi...@git.apache.org>.
Github user olegz commented on the issue:

    https://github.com/apache/nifi/pull/257
  
    @pvillard31 could you please rebase and fix merge conflicts one last time. I am now committed to review/test it so we can have it in 0.7.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---