-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/VSuryaprasad-HCL/sonic-pins …
…into symbolic_import_branch_98
- Loading branch information
Showing
71 changed files
with
4,168 additions
and
1,993 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// 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 | ||
// | ||
// 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 "dvaas/port_id_map.h" | ||
|
||
#include "absl/container/flat_hash_map.h" | ||
#include "absl/container/flat_hash_set.h" | ||
#include "absl/status/status.h" | ||
#include "absl/status/statusor.h" | ||
#include "absl/strings/substitute.h" | ||
#include "gutil/status.h" | ||
#include "lib/p4rt/p4rt_port.h" | ||
|
||
namespace dvaas { | ||
|
||
using pins_test::P4rtPortId; | ||
|
||
absl::StatusOr<MirrorTestbedP4rtPortIdMap> | ||
MirrorTestbedP4rtPortIdMap::CreateFromSutToControlSwitchPortMap( | ||
absl::flat_hash_map<P4rtPortId, pins_test::P4rtPortId> | ||
sut_to_control_port_map) { | ||
absl::flat_hash_map<pins_test::P4rtPortId, pins_test::P4rtPortId> | ||
control_to_sut_port_map; | ||
for (const auto& [sut_port, control_port] : sut_to_control_port_map) { | ||
if (control_to_sut_port_map.contains(control_port)) { | ||
return gutil::InvalidArgumentErrorBuilder() << absl::Substitute( | ||
"Both SUT P4RT port IDs '$0' and '$1' are mapped to control " | ||
"switch P4RT port ID '$2'", | ||
sut_port, control_to_sut_port_map[control_port], control_port); | ||
} | ||
control_to_sut_port_map[control_port] = sut_port; | ||
} | ||
|
||
return MirrorTestbedP4rtPortIdMap(control_to_sut_port_map); | ||
} | ||
|
||
absl::StatusOr<MirrorTestbedP4rtPortIdMap> | ||
MirrorTestbedP4rtPortIdMap::CreateFromControlSwitchToSutPortMap( | ||
absl::flat_hash_map<pins_test::P4rtPortId, pins_test::P4rtPortId> | ||
control_to_sut_port_map) { | ||
absl::flat_hash_map<pins_test::P4rtPortId, pins_test::P4rtPortId> | ||
sut_to_control_port_map; | ||
for (const auto& [control_port, sut_port] : control_to_sut_port_map) { | ||
if (sut_to_control_port_map.contains(sut_port)) { | ||
return gutil::InvalidArgumentErrorBuilder() << absl::Substitute( | ||
"Both control switch P4RT port IDs '$0' and '$1' are mapped " | ||
"to SUT P4RT port ID '$2'", | ||
control_port, sut_to_control_port_map[sut_port], sut_port); | ||
} | ||
sut_to_control_port_map[sut_port] = control_port; | ||
} | ||
|
||
return MirrorTestbedP4rtPortIdMap(control_to_sut_port_map); | ||
} | ||
|
||
absl::StatusOr<pins_test::P4rtPortId> | ||
MirrorTestbedP4rtPortIdMap::GetSutPortConnectedToControlSwitchPort( | ||
const pins_test::P4rtPortId& control_port) const { | ||
// Handle implicit identity map. | ||
if (!control_to_sut_port_map_.has_value()) return control_port; | ||
|
||
// Handle explicit map. | ||
const auto it = control_to_sut_port_map_->find(control_port); | ||
if (it == control_to_sut_port_map_->end()) { | ||
return absl::NotFoundError( | ||
absl::Substitute("Control port '$0' was not found in control switch " | ||
"to SUT P4RT port ID map.", | ||
control_port)); | ||
} else { | ||
return it->second; | ||
} | ||
} | ||
|
||
} // namespace dvaas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// 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 | ||
// | ||
// 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_DVAAS_PORT_ID_MAP_H_ | ||
#define PINS_DVAAS_PORT_ID_MAP_H_ | ||
|
||
#include <optional> | ||
#include <utility> | ||
|
||
#include "absl/container/flat_hash_map.h" | ||
#include "absl/status/status.h" | ||
#include "absl/status/statusor.h" | ||
#include "lib/p4rt/p4rt_port.h" | ||
#include "proto/gnmi/gnmi.grpc.pb.h" | ||
|
||
namespace dvaas { | ||
|
||
// Keeps a map between the P4RT port IDs of connected interfaces between the | ||
// control switch and the SUT in a mirror testbed. | ||
class MirrorTestbedP4rtPortIdMap { | ||
public: | ||
// Creates a port mapping from SUT -> control switch port map. | ||
static absl::StatusOr<MirrorTestbedP4rtPortIdMap> | ||
CreateFromSutToControlSwitchPortMap( | ||
absl::flat_hash_map<pins_test::P4rtPortId, pins_test::P4rtPortId> | ||
sut_to_control_port_map); | ||
|
||
// Creates a port mapping from control switch -> SUT port map. | ||
static absl::StatusOr<MirrorTestbedP4rtPortIdMap> | ||
CreateFromControlSwitchToSutPortMap( | ||
absl::flat_hash_map<pins_test::P4rtPortId, pins_test::P4rtPortId> | ||
control_to_sut_port_map); | ||
|
||
// Creates an implicit map in which any port ID is mapped to itself. | ||
static MirrorTestbedP4rtPortIdMap CreateIdentityMap() { | ||
return MirrorTestbedP4rtPortIdMap(); | ||
} | ||
|
||
// Creates a map in which the P4RT port IDs of interfaces of SUT and control | ||
// switch with the same OpenConfig interface name are mapped to each other. | ||
static absl::StatusOr<MirrorTestbedP4rtPortIdMap> | ||
CreateFromMatchingInterfaceNames(gnmi::gNMI::StubInterface& sut, | ||
gnmi::gNMI::StubInterface& control_switch) { | ||
// TODO: Implement inferring port ID mapping from mirror | ||
// testbed interface names. | ||
return absl::UnimplementedError( | ||
"Inferring port ID mapping from mirror testbed interface names is not " | ||
"supported yet."); | ||
} | ||
|
||
// Returns the P4RT port ID of the SUT interface connected to the interface on | ||
// the control switch with the given P4RT port ID according to the port | ||
// mapping. | ||
absl::StatusOr<pins_test::P4rtPortId> GetSutPortConnectedToControlSwitchPort( | ||
const pins_test::P4rtPortId& control_port) const; | ||
|
||
private: | ||
MirrorTestbedP4rtPortIdMap( | ||
absl::flat_hash_map<pins_test::P4rtPortId, pins_test::P4rtPortId> | ||
control_to_sut_port_map) | ||
: control_to_sut_port_map_(std::move(control_to_sut_port_map)) {} | ||
MirrorTestbedP4rtPortIdMap() = default; | ||
|
||
// If nullopt, an implicit identity map is assumed. | ||
std::optional< | ||
absl::flat_hash_map<pins_test::P4rtPortId, pins_test::P4rtPortId>> | ||
control_to_sut_port_map_; | ||
}; | ||
|
||
} // namespace dvaas | ||
|
||
#endif // PINS_DVAAS_PORT_ID_MAP_H_ |
Oops, something went wrong.