nifi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From olegz <...@git.apache.org>
Subject [GitHub] nifi pull request: NIFI-1537 Added SNMP processors
Date Sun, 06 Mar 2016 19:59:03 GMT
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.
---

Mime
View raw message