|
| 1 | +/* |
| 2 | + * Copyright DataStax, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.datastax.oss.driver.internal.core.addresstranslation; |
| 17 | + |
| 18 | +import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator; |
| 19 | +import com.datastax.oss.driver.api.core.config.DriverOption; |
| 20 | +import com.datastax.oss.driver.api.core.context.DriverContext; |
| 21 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 22 | +import java.net.InetSocketAddress; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +/** |
| 27 | + * This translator always returns same hostname, no matter what IP address a node has but still |
| 28 | + * using its native transport port. |
| 29 | + * |
| 30 | + * <p>The translator can be used for scenarios when all nodes are behind some kind of proxy, and it |
| 31 | + * is not tailored for one concrete use case. One can use this, for example, for AWS PrivateLink as |
| 32 | + * all nodes would be exposed to consumer - behind one hostname pointing to AWS Endpoint. |
| 33 | + */ |
| 34 | +public class FixedHostNameAddressTranslator implements AddressTranslator { |
| 35 | + |
| 36 | + private static final Logger LOG = LoggerFactory.getLogger(FixedHostNameAddressTranslator.class); |
| 37 | + |
| 38 | + public static final String ADDRESS_TRANSLATOR_ADVERTISED_HOSTNAME = |
| 39 | + "advanced.address-translator.advertised-hostname"; |
| 40 | + |
| 41 | + public static DriverOption ADDRESS_TRANSLATOR_ADVERTISED_HOSTNAME_OPTION = |
| 42 | + new DriverOption() { |
| 43 | + @NonNull |
| 44 | + @Override |
| 45 | + public String getPath() { |
| 46 | + return ADDRESS_TRANSLATOR_ADVERTISED_HOSTNAME; |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + private final String advertisedHostname; |
| 51 | + private final String logPrefix; |
| 52 | + |
| 53 | + public FixedHostNameAddressTranslator(@NonNull DriverContext context) { |
| 54 | + logPrefix = context.getSessionName(); |
| 55 | + advertisedHostname = |
| 56 | + context |
| 57 | + .getConfig() |
| 58 | + .getDefaultProfile() |
| 59 | + .getString(ADDRESS_TRANSLATOR_ADVERTISED_HOSTNAME_OPTION); |
| 60 | + } |
| 61 | + |
| 62 | + @NonNull |
| 63 | + @Override |
| 64 | + public InetSocketAddress translate(@NonNull InetSocketAddress address) { |
| 65 | + final int port = address.getPort(); |
| 66 | + LOG.debug("[{}] Resolved {}:{} to {}:{}", logPrefix, address, port, advertisedHostname, port); |
| 67 | + return new InetSocketAddress(advertisedHostname, port); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void close() {} |
| 72 | +} |
0 commit comments