summaryrefslogtreecommitdiffstats
path: root/infra/translate-utils/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'infra/translate-utils/src/main/java')
-rw-r--r--infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizer.java32
1 files changed, 23 insertions, 9 deletions
diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizer.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizer.java
index 62dbcd6c9..b6430a355 100644
--- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizer.java
+++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/ReflexiveListReaderCustomizer.java
@@ -16,31 +16,39 @@
package io.fd.honeycomb.translate.util.read;
-import static com.google.common.base.Preconditions.checkArgument;
-
import com.google.common.base.Optional;
+import io.fd.honeycomb.translate.read.ReadContext;
+import io.fd.honeycomb.translate.read.ReadFailedException;
import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
import io.fd.honeycomb.translate.util.ReflectionUtils;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Collections;
-import java.util.List;
-import javax.annotation.Nonnull;
import org.opendaylight.yangtools.concepts.Builder;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.Identifiable;
import org.opendaylight.yangtools.yang.binding.Identifier;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+import javax.annotation.Nonnull;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.*;
/**
* Might be slow.
*/
-public abstract class ReflexiveListReaderCustomizer<C extends DataObject & Identifiable<K>, K extends Identifier<C>, B extends Builder<C>>
+public class ReflexiveListReaderCustomizer<C extends DataObject & Identifiable<K>, K extends Identifier<C>, B extends Builder<C>>
extends ReflexiveReaderCustomizer<C, B>
implements ListReaderCustomizer<C, K, B> {
+ private final List<K> staticKeys;
- public ReflexiveListReaderCustomizer(final Class<C> typeClass, final Class<B> builderClass) {
+ public ReflexiveListReaderCustomizer(@Nonnull final Class<C> typeClass, @Nonnull final Class<B> builderClass,
+ @Nonnull final List<K> staticKeys) {
super(typeClass, builderClass);
+ this.staticKeys = checkNotNull(staticKeys, "Static keys cannot be null");
+ checkState(!this.staticKeys.isEmpty(), "No static keys provided");
}
@Override
@@ -62,4 +70,10 @@ public abstract class ReflexiveListReaderCustomizer<C extends DataObject & Ident
throw new IllegalArgumentException("Unable to set " + readData + " to " + parentBuilder, e);
}
}
+
+ @Nonnull
+ @Override
+ public List<K> getAllIds(@Nonnull InstanceIdentifier<C> id, @Nonnull ReadContext context) throws ReadFailedException {
+ return staticKeys;
+ }
}
.highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #75715e } /* Generic.Subheading */ .highlight .kc { color: #66d9ef } /* Keyword.Constant */ .highlight .kd { color: #66d9ef } /* Keyword.Declaration */ .highlight .kn { color: #f92672 } /* Keyword.Namespace */ .highlight .kp { color: #66d9ef } /* Keyword.Pseudo */ .highlight .kr { color: #66d9ef } /* Keyword.Reserved */ .highlight .kt { color: #66d9ef } /* Keyword.Type */ .highlight .ld { color: #e6db74 } /* Literal.Date */ .highlight .m { color: #ae81ff } /* Literal.Number */ .highlight .s { color: #e6db74 } /* Literal.String */ .highlight .na { color: #a6e22e } /* Name.Attribute */ .highlight .nb { color: #f8f8f2 } /* Name.Builtin */ .highlight .nc { color: #a6e22e } /* Name.Class */ .highlight .no { color: #66d9ef } /* Name.Constant */ .highlight .nd { color: #a6e22e } /* Name.Decorator */ .highlight .ni { color: #f8f8f2 } /* Name.Entity */ .highlight .ne { color: #a6e22e } /* Name.Exception */ .highlight .nf { color: #a6e22e } /* Name.Function */ .highlight .nl { color: #f8f8f2 } /* Name.Label */ .highlight .nn { color: #f8f8f2 } /* Name.Namespace */ .highlight .nx { color: #a6e22e } /* Name.Other */ .highlight .py { color: #f8f8f2 } /* Name.Property */ .highlight .nt { color: #f92672 } /* Name.Tag */ .highlight .nv { color: #f8f8f2 } /* Name.Variable */ .highlight .ow { color: #f92672 } /* Operator.Word */ .highlight .w { color: #f8f8f2 } /* Text.Whitespace */ .highlight .mb { color: #ae81ff } /* Literal.Number.Bin */ .highlight .mf { color: #ae81ff } /* Literal.Number.Float */ .highlight .mh { color: #ae81ff } /* Literal.Number.Hex */ .highlight .mi { color: #ae81ff } /* Literal.Number.Integer */ .highlight .mo { color: #ae81ff } /* Literal.Number.Oct */ .highlight .sa { color: #e6db74 } /* Literal.String.Affix */ .highlight .sb { color: #e6db74 } /* Literal.String.Backtick */ .highlight .sc { color: #e6db74 } /* Literal.String.Char */ .highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */ .highlight .sd { color: #e6db74 } /* Literal.String.Doc */ .highlight .s2 { color: #e6db74 } /* Literal.String.Double */ .highlight .se { color: #ae81ff } /* Literal.String.Escape */ .highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */ .highlight .si { color: #e6db74 } /* Literal.String.Interpol */ .highlight .sx { color: #e6db74 } /* Literal.String.Other */ .highlight .sr { color: #e6db74 } /* Literal.String.Regex */ .highlight .s1 { color: #e6db74 } /* Literal.String.Single */ .highlight .ss { color: #e6db74 } /* Literal.String.Symbol */ .highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #a6e22e } /* Name.Function.Magic */ .highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */ .highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */ .highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */ .highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */ .highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */ } @media (prefers-color-scheme: light) { .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
/*
 * Copyright (c) 2021 Cisco and/or its affiliates.
 * Licensed 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.
 */

#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>

extern "C" {
#define WITH_TESTS
#include <hicn/base/loop.h>
#include <hicn/config/configuration.h>
#include <hicn/core/forwarder.h>
#include <hicn/core/listener.h>
#include <hicn/core/address_pair.h>
#include <hicn/core/address.h>
#include <hicn/core/strategy.h>
#include <hicn/strategies/local_remote.h>
}

class StrategyLocalRemoteTest : public ::testing::Test {
 protected:
  StrategyLocalRemoteTest() {
    conf_ = configuration_create();
    MAIN_LOOP = loop_create();
    fwd_ = forwarder_create(conf_);

    /* Strategy and strategy entry */
    entry_ = {
        .type = STRATEGY_TYPE_LOCAL_REMOTE,
        .options =
            {
                .random = {},
            },
        .state = {.random = {}},
    };

    strategy_initialize(&entry_, fwd_);
  }

  virtual ~StrategyLocalRemoteTest() {
    INFO("loop stopped");
    forwarder_free(fwd_);
    loop_free(MAIN_LOOP);
    MAIN_LOOP = NULL;
    strategy_finalize(&entry_);
  }

  strategy_entry_t entry_;
  nexthops_t available_nexthops_ = NEXTHOPS_EMPTY;
  configuration_t* conf_;
  forwarder_t* fwd_;
  msgbuf_t msgbuf_;
};

TEST_F(StrategyLocalRemoteTest, InputLocalOutputLocal) {
  address_t prod_addr = ADDRESS4_LOCALHOST(12345);
  address_t cons_addr = ADDRESS4_LOCALHOST(12346);
  address_t listener_addr = ADDRESS4_LOCALHOST(9596);

  listener_t* listener = listener_create(FACE_TYPE_UDP_LISTENER, &listener_addr,
                                         "lo", "lo_udp4", fwd_);

  address_pair_t pair_conn_prod = {
      .local = listener_addr,
      .remote = prod_addr,
  };

  address_pair_t pair_conn_cons = {
      .local = listener_addr,
      .remote = cons_addr,
  };

  unsigned prod_conn_id =
      listener_create_connection(listener, "conp", &pair_conn_prod);
  unsigned cons_conn_id =
      listener_create_connection(listener, "conc", &pair_conn_cons);

  msgbuf_.connection_id = cons_conn_id;

  nexthops_add(&available_nexthops_, prod_conn_id);
  nexthops_t* nexthops;
  nexthops = strategy_lookup_nexthops(&entry_, &available_nexthops_, &msgbuf_);

  EXPECT_EQ(nexthops_get_curlen(nexthops), (size_t)0);
}

TEST_F(StrategyLocalRemoteTest, InputRemoteOutputRemote) {
  address_t prod_addr = ADDRESS4_LOCALHOST(12345);
  address_t cons_addr = ADDRESS4_LOCALHOST(12346);
  address_t listener_addr = ADDRESS4_LOCALHOST(9596);

  listener_t* listener = listener_create(FACE_TYPE_UDP_LISTENER, &listener_addr,
                                         "lo", "lo_udp4", fwd_);

  address_pair_t pair_conn_prod = {
      .local = listener_addr,
      .remote = prod_addr,
  };

  address_pair_t pair_conn_cons = {
      .local = listener_addr,
      .remote = cons_addr,
  };

  connection_t* conn;
  unsigned prod_conn_id =
      listener_create_connection(listener, "conp", &pair_conn_prod);
  unsigned cons_conn_id =
      listener_create_connection(listener, "conc", &pair_conn_cons);

  // fake two remote connections
  conn = connection_table_get_by_id(forwarder_get_connection_table(fwd_),
                                    prod_conn_id);
  conn->local = false;
  conn = connection_table_get_by_id(forwarder_get_connection_table(fwd_),
                                    cons_conn_id);
  conn->local = false;

  msgbuf_.connection_id = cons_conn_id;

  nexthops_add(&available_nexthops_, prod_conn_id);
  nexthops_t* nexthops;
  nexthops = strategy_lookup_nexthops(&entry_, &available_nexthops_, &msgbuf_);

  EXPECT_EQ(nexthops_get_curlen(nexthops), (size_t)0);
}

TEST_F(StrategyLocalRemoteTest, InputLocalOutputRemote) {
  address_t prod_addr = ADDRESS4_LOCALHOST(12345);
  address_t cons_addr = ADDRESS4_LOCALHOST(12346);
  address_t listener_addr = ADDRESS4_LOCALHOST(9596);

  listener_t* listener = listener_create(FACE_TYPE_UDP_LISTENER, &listener_addr,
                                         "lo", "lo_udp4", fwd_);

  address_pair_t pair_conn_prod = {
      .local = listener_addr,
      .remote = prod_addr,
  };

  address_pair_t pair_conn_cons = {
      .local = listener_addr,
      .remote = cons_addr,
  };

  connection_t* conn;
  unsigned prod_conn_id =
      listener_create_connection(listener, "conp", &pair_conn_prod);
  unsigned cons_conn_id =
      listener_create_connection(listener, "conc", &pair_conn_cons);

  conn = connection_table_get_by_id(forwarder_get_connection_table(fwd_),
                                    prod_conn_id);
  conn->local = false;
  conn = connection_table_get_by_id(forwarder_get_connection_table(fwd_),
                                    cons_conn_id);
  conn->local = true;

  msgbuf_.connection_id = cons_conn_id;

  nexthops_add(&available_nexthops_, prod_conn_id);
  nexthops_t* nexthops;
  nexthops = strategy_lookup_nexthops(&entry_, &available_nexthops_, &msgbuf_);

  EXPECT_EQ(nexthops_get_curlen(nexthops), (size_t)1);
}

TEST_F(StrategyLocalRemoteTest, InputRemoteOutputLocal) {
  address_t cons_addr = ADDRESS4_LOCALHOST(12345);
  address_t prod_addr = ADDRESS4_LOCALHOST(12346);
  address_t listener_addr = ADDRESS4_LOCALHOST(9695);

  listener_t* listener = listener_create(FACE_TYPE_UDP_LISTENER, &listener_addr,
                                         "lo", "lo_udp4", fwd_);

  address_pair_t pair_conn_prod = {
      .local = listener_addr,
      .remote = prod_addr,
  };

  address_pair_t pair_conn_cons = {
      .local = listener_addr,
      .remote = cons_addr,
  };

  connection_t* conn;
  unsigned prod_conn_id =
      listener_create_connection(listener, "conp", &pair_conn_prod);
  unsigned cons_conn_id =
      listener_create_connection(listener, "conc", &pair_conn_cons);

  conn = connection_table_get_by_id(forwarder_get_connection_table(fwd_),
                                    prod_conn_id);
  ASSERT_TRUE(conn != NULL);
  conn->local = true;
  conn = connection_table_get_by_id(forwarder_get_connection_table(fwd_),
                                    cons_conn_id);
  ASSERT_TRUE(conn != NULL);
  conn->local = false;

  msgbuf_.connection_id = cons_conn_id;

  nexthops_add(&available_nexthops_, prod_conn_id);
  nexthops_t* nexthops;
  nexthops = strategy_lookup_nexthops(&entry_, &available_nexthops_, &msgbuf_);

  EXPECT_EQ(nexthops_get_curlen(nexthops), (size_t)1);
}