Skip to content

Commit

Permalink
Adding basic_switch.h to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
kishanps authored and bibhuprasad-hcl committed Jul 9, 2024
1 parent 6ed239d commit d2d061d
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2024 Google LLC
#
# 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
#
# https://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(
default_visibility = ["//visibility:public"],
licenses = ["notice"],
)

cc_library(
name = "basic_switch",
hdrs = ["basic_switch.h"],
deps = [
"//p4_pdpi:p4_runtime_session",
"//thinkit:switch",
"@com_github_gnmi//proto/gnmi:gnmi_cc_grpc_proto",
"@com_github_gnoi//cert:cert_cc_grpc_proto",
"@com_github_gnoi//diag:diag_cc_grpc_proto",
"@com_github_gnoi//factory_reset:factory_reset_cc_grpc_proto",
"@com_github_gnoi//os:os_cc_grpc_proto",
"@com_github_gnoi//system:system_cc_grpc_proto",
"@com_github_grpc_grpc//:grpc++_public_hdrs",
"@com_github_grpc_grpc//:grpc_security_base",
"@com_github_p4lang_p4runtime//:p4runtime_cc_grpc",
"@com_google_absl//absl/status:statusor",
],
)

171 changes: 171 additions & 0 deletions lib/basic_switch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Copyright (c) 2024, Google Inc.
//
// 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.

#ifndef PINS_LIB_BASIC_SWITCH_H_
#define PINS_LIB_BASIC_SWITCH_H_

#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <utility>

#include "absl/status/statusor.h"
#include "cert/cert.grpc.pb.h"
#include "diag/diag.grpc.pb.h"
#include "factory_reset/factory_reset.grpc.pb.h"
#include "grpc/grpc_security_constants.h"
#include "grpcpp/security/credentials.h"
#include "grpcpp/support/channel_arguments.h"
#include "grpcpp/support/stub_options.h"
#include "os/os.grpc.pb.h"
#include "p4/v1/p4runtime.grpc.pb.h"
#include "p4_pdpi/p4_runtime_session.h"
#include "proto/gnmi/gnmi.grpc.pb.h"
#include "system/system.grpc.pb.h"
#include "thinkit/switch.h"

namespace pins_test {

// SwitchServices contains the addresses of all of the gRPC services that
// interact with the switch interfaces.
struct SwitchServices {
std::string p4runtime_address;
std::string gnmi_address;
std::string gnoi_address;
};

class LocalTcp {
public:
std::shared_ptr<grpc::ChannelCredentials> Credentials() {
return grpc::experimental::LocalCredentials(LOCAL_TCP);
}
};

// A simple stub creation policy that creates a custom channel to the address
// using the `CredentialsPolicy` passed in. This policy has one function
// `std::shared_ptr<grpc::ChannelCredentials> Credentials()`.
template <class CredentialsPolicy>
class CreateGrpcStub : private CredentialsPolicy {
public:
template <class... Args>
explicit CreateGrpcStub(Args&&... args)
: CredentialsPolicy(std::forward<Args>(args)...) {}

template <class NewStubFunction>
auto Create(NewStubFunction&& new_stub, const std::string& address,
std::string_view /*chassis*/, std::string_view /*stub_type*/,
grpc::ChannelArguments channel_arguments = {}) {
return new_stub(
grpc::CreateCustomChannel(address, CredentialsPolicy::Credentials(),
channel_arguments),
grpc::StubOptions());
}
};

// BasicSwitch implements ThinKit's Switch interface by creating stubs to
// addresses of the various gRPC services that connect to the switch. The
// template parameter allows the user to provide a policy class to create new
// gRPC stubs for every gRPC service. This policy class should have a function
// called `Create` that uses the first parameter, which will be passed the
// `NewStub` function of a given gRPC service, to create a stub with the desired
// channel/credentials. Any arguments after `SwitchServices services` will be
// forwarded to this policy class.
//
// e.g.
// class CreateMyGrpcStub {
// public:
// template <class NewStubFunction>
// auto Create(NewStubFunction&& new_stub, const std::string& address,
// std::string_view /*chassis*/, std::string_view /*stub_type*/,
// grpc::ChannelArguments channel_arguments = {}) {
// return new_stub(grpc::CreateCustomChannel(...), ...);
// }
// };
// BasicSwitch<CreateMyGrpcStub> my_switch(...);
// absl::make_unique<BasicSwitch<CreateGrpcStub<LocalTcp>>>(...);
template <class CreateStubPolicy>
class BasicSwitch : public thinkit::Switch, private CreateStubPolicy {
public:
template <class... Args>
BasicSwitch(std::string chassis_name, uint32_t device_id,
SwitchServices services, Args&&... args)
: CreateStubPolicy(std::forward<Args>(args)...),
chassis_name_(std::move(chassis_name)),
device_id_(device_id),
services_(std::move(services)) {}

const std::string& ChassisName() override { return chassis_name_; };

uint32_t DeviceId() override { return device_id_; }

absl::StatusOr<std::unique_ptr<p4::v1::P4Runtime::StubInterface>>
CreateP4RuntimeStub() override {
return CreateStubPolicy::Create(
p4::v1::P4Runtime::NewStub, services_.p4runtime_address, chassis_name_,
"P4 Runtime", pdpi::GrpcChannelArgumentsForP4rt());
}

absl::StatusOr<std::unique_ptr<gnmi::gNMI::StubInterface>> CreateGnmiStub()
override {
return CreateStubPolicy::Create(gnmi::gNMI::NewStub, services_.gnmi_address,
chassis_name_, "gNMI");
}

absl::StatusOr<
std::unique_ptr<::gnoi::factory_reset::FactoryReset::StubInterface>>
CreateGnoiFactoryResetStub() override {
return CreateStubPolicy::Create(
::gnoi::factory_reset::FactoryReset::NewStub, services_.gnoi_address,
chassis_name_, "gNOI Factory Reset");
}

absl::StatusOr<std::unique_ptr<::gnoi::system::System::StubInterface>>
CreateGnoiSystemStub() override {
return CreateStubPolicy::Create(::gnoi::system::System::NewStub,
services_.gnoi_address, chassis_name_,
"gNOI System");
}

absl::StatusOr<std::unique_ptr<::gnoi::diag::Diag::StubInterface>>
CreateGnoiDiagStub() override {
return CreateStubPolicy::Create(::gnoi::diag::Diag::NewStub,
services_.gnoi_address, chassis_name_,
"gNOI Diag");
}

absl::StatusOr<std::unique_ptr<
::gnoi::certificate::CertificateManagement::StubInterface>>
CreateGnoiCertificateStub() override {
return CreateStubPolicy::Create(
::gnoi::certificate::CertificateManagement::NewStub,
services_.gnoi_address, chassis_name_, "gNOI Certificate");
}

absl::StatusOr<std::unique_ptr<::gnoi::os::OS::StubInterface>>
CreateGnoiOsStub() override {
return CreateStubPolicy::Create(::gnoi::os::OS::NewStub,
services_.gnoi_address, chassis_name_,
"gNOI OS");
}

private:
std::string chassis_name_;
uint32_t device_id_;
SwitchServices services_;
};

} // namespace pins_test

#endif // PINS_LIB_BASIC_SWITCH_H_
13 changes: 13 additions & 0 deletions pins_infra_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ def pins_infra_deps():
sha256 = "34af2f15cf7367513b352bdcd2493ab14ce43692d2dcd9dfc499492966c64dcf",
)

if not native.existing_rule("com_github_gnmi"):
http_archive(
name = "com_github_gnmi",
# v0.10.0 release; commit-hash:5473f2ef722ee45c3f26eee3f4a44a7d827e3575.
url = "https://github.com/openconfig/gnmi/archive/refs/tags/v0.10.0.zip",
strip_prefix = "gnmi-0.10.0",
patch_args = ["-p1"],
patches = [
"@com_github_google_pins_infra//:bazel/patches/gnmi-001-fix_virtual_proto_import.patch",
],
sha256 = "2231e1cc398a523fa840810fa6fdb8960639f7b91b57bb8f12ed8681e0142a67",
)

if not native.existing_rule("com_github_gnoi"):
http_archive(
name = "com_github_gnoi",
Expand Down

0 comments on commit d2d061d

Please sign in to comment.