Github user mcgilman commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2510#discussion_r177536829
  
    --- Diff: 
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-site-to-site/src/main/java/org/apache/nifi/remote/PeerDescriptionModifier.java
 ---
    @@ -0,0 +1,160 @@
    +/*
    + * 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.remote;
    +
    +import org.apache.nifi.attribute.expression.language.PreparedQuery;
    +import org.apache.nifi.attribute.expression.language.Query;
    +import org.apache.nifi.remote.protocol.SiteToSiteTransportProtocol;
    +import org.apache.nifi.util.NiFiProperties;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.util.List;
    +import java.util.Map;
    +import java.util.stream.Collectors;
    +
    +import static java.lang.String.format;
    +import static org.apache.commons.lang3.StringUtils.isBlank;
    +
    +public class PeerDescriptionModifier {
    +
    +    private static final Logger logger = 
LoggerFactory.getLogger(PeerDescriptionModifier.class);
    +
    +    public enum RequestType {
    +        SiteToSiteDetail,
    +        Peers
    +    }
    +
    +    private static class Route {
    +        private String name;
    +        private SiteToSiteTransportProtocol protocol;
    +        private PreparedQuery predicate;
    +        private PreparedQuery hostname;
    +        private PreparedQuery port;
    +        private PreparedQuery secure;
    +
    +        private boolean isValid() {
    +            if (hostname == null) {
    +                logger.warn("Ignore invalid route definition {} because 
'hostname' is not specified.", name);return false;
    +            }
    +            if (port == null) {
    +                logger.warn("Ignore invalid route definition {} because 
'port' is not specified.", name);
    +                return false;
    +            }
    +            return true;
    +        }
    +
    +        private PeerDescription getTarget(final Map<String, String> 
variables) {
    +            final String targetHostName = 
hostname.evaluateExpressions(variables, null);
    +            if (isBlank(targetHostName)) {
    +                throw new IllegalStateException("Target hostname was not 
resolved for the route definition " + name);
    +            }
    +
    +            final String targetPortStr = 
port.evaluateExpressions(variables, null);
    +            if (isBlank(targetPortStr)) {
    +                throw new IllegalStateException("Target port was not 
resolved for the route definition " + name);
    +            }
    +
    +            String targetIsSecure = secure == null ? null : 
secure.evaluateExpressions(variables, null);
    +            if (isBlank(targetIsSecure)) {
    +                targetIsSecure = "false";
    +            }
    +            return new PeerDescription(targetHostName, 
Integer.valueOf(targetPortStr), Boolean.valueOf(targetIsSecure));
    +        }
    +    }
    +
    +    private Map<SiteToSiteTransportProtocol, List<Route>> routes;
    +
    +
    +    private static final String PROPERTY_PREFIX = "nifi.remote.route.";
    +
    +    public PeerDescriptionModifier(final NiFiProperties properties) {
    +        final Map<String, List<String>> routeDefinitions = 
properties.getPropertyKeys().stream()
    +                .filter(k -> k.startsWith(PROPERTY_PREFIX))
    +                .collect(Collectors.groupingBy(k -> 
k.substring(PROPERTY_PREFIX.length(), k.lastIndexOf('.'))));
    +
    +        routes = routeDefinitions.entrySet().stream().map(r -> {
    +            final Route route = new Route();
    +            final String[] key = r.getKey().split("\\.");
    +            route.protocol = 
SiteToSiteTransportProtocol.valueOf(key[0].toUpperCase());
    +            route.name = key[1];
    +            r.getValue().forEach(k -> {
    +                final String name = k.substring(k.lastIndexOf('.') + 1);
    +                final String value = properties.getProperty(k);
    +                switch (name) {
    +                    case "when":
    +                        route.predicate = Query.prepare(value);
    +                        break;
    +                    case "hostname":
    +                        route.hostname = Query.prepare(value);
    +                        break;
    +                    case "port":
    +                        route.port = Query.prepare(value);
    +                        break;
    +                    case "secure":
    +                        route.secure = Query.prepare(value);
    +                        break;
    +                }
    +            });
    +            return route;
    +        }).filter(Route::isValid).collect(Collectors.groupingBy(r -> 
r.protocol));
    +
    +    }
    +
    +    private void addVariables(Map<String, String> map, String prefix, 
PeerDescription peer) {
    +        map.put(format("%s.hostname", prefix), peer.getHostname());
    +        map.put(format("%s.port", prefix), String.valueOf(peer.getPort()));
    +        map.put(format("%s.secure", prefix), 
String.valueOf(peer.isSecure()));
    +    }
    +
    +    public boolean isModificationNeeded(final SiteToSiteTransportProtocol 
protocol) {
    --- End diff --
    
    Does it make sense to make this method private and invoke it from within 
the modify method? It seems like the general usage is to check if modification 
is needed and then invoke modify. Alternatively, we could invoke modify 
unconditionally and check if modification is needed within. If modification is 
not needed, then target could just be returned.


---

Reply via email to