Skip to content

[CPO] Add library function to parse cpo.json - #28077

Merged
yxieca merged 6 commits into
sonic-net:masterfrom
nexthop-ai:optical-devices-json
Aug 4, 2026
Merged

[CPO] Add library function to parse cpo.json#28077
yxieca merged 6 commits into
sonic-net:masterfrom
nexthop-ai:optical-devices-json

Conversation

@bgallagher-nexthop

@bgallagher-nexthop bgallagher-nexthop commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Why I did it

The "Port Mapping for CPO" HLD introduces a new file: cpo.json. It also stipulates that a library function will be introduced to parse this JSON file for vendors to use in their platform code.

This PR adds that function to parse the cpo.json file and return a dictionary that vendors can use in platform code.

How I did it

A new function is added called get_cpo_data(). It does the following:

  • performs a two-stage lookup, preferring a file located in the hwsku directory over the platform directory
  • loads the JSON data and performs a minimal amount of normalization

How to verify it

A unit-test has been added.

Which release branch to backport (provide reason below if selected)

  • 202305
  • 202311
  • 202405
  • 202411
  • 202505
  • 202511
  • 202605

Tested branch (Please provide the tested image version)

Description for the changelog

[CPO] Add library function to parse cpo.json

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

abelamit
abelamit previously approved these changes Jul 1, 2026
Comment thread src/sonic-py-common/sonic_py_common/device_info.py Outdated
Comment thread src/sonic-py-common/sonic_py_common/device_info.py Outdated
Comment thread src/sonic-py-common/sonic_py_common/device_info.py Outdated
Comment thread src/sonic-py-common/tests/device_info_test.py Outdated
Comment thread src/sonic-py-common/sonic_py_common/device_info.py Outdated
Comment thread src/sonic-py-common/sonic_py_common/device_info.py Outdated
Comment thread src/sonic-py-common/sonic_py_common/device_info.py Outdated
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@bgallagher-nexthop bgallagher-nexthop changed the title [CPO] Add library function to parse optical_devices.json [CPO] Add library function to parse cpo.json Jul 13, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Brian Gallagher <bgallagher@nexthop.ai>
Signed-off-by: Brian Gallagher <bgallagher@nexthop.ai>
Signed-off-by: Brian Gallagher <bgallagher@nexthop.ai>
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@aditya-nexthop aditya-nexthop left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, left a few nits.

Comment thread src/sonic-py-common/sonic_py_common/device_info.py
Comment thread src/sonic-py-common/tests/device_info_test.py Outdated
Comment thread src/sonic-py-common/tests/device_info_test.py Outdated
Signed-off-by: Brian Gallagher <bgallagher@nexthop.ai>
@prgeor

prgeor commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new sonic_py_common.device_info.get_cpo_data() helper to locate and parse the new cpo.json configuration (preferring HwSKU over platform scope) and adds a unit test validating lookup precedence and lane-field normalization.

Changes:

  • Added get_cpo_data() plus helper functions to locate cpo.json and normalize lane-related fields into List[int].
  • Added unit tests covering happy-path parsing/normalization and file lookup precedence/fallback behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/sonic-py-common/sonic_py_common/device_info.py Adds get_cpo_data() and helper routines for locating and normalizing cpo.json.
src/sonic-py-common/tests/device_info_test.py Adds unit tests for get_cpo_data() behavior, normalization, and path precedence.

Comment thread src/sonic-py-common/sonic_py_common/device_info.py Outdated
Comment thread src/sonic-py-common/sonic_py_common/device_info.py
Signed-off-by: Brian Gallagher <bgallagher@nexthop.ai>
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@micas-net micas-net left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

}
}
"""
for device in cpo_data.get('devices', {}).values():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bgallagher-nexthop how is the information in this file tied back to the cpo object? For eg, the i2c path is not being considered for eeprom path which say will be used by cpoutil to do hexdump?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have elsfp and oe object for each logical port, shouldn't these parsed information reflect in those? i.e this file is the source of truth for the platform from where these objects derive the mapping information per logical port.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise platform will hardcode thse mapping in their own .py file which defeats the purpose.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is only for normalization purposes, just to make the data easier to use -- strings like "1,2,3" just get normalized into an actual array [1,2,3]so clients don't have to do this normalization themselves

The info in this file is intended to be used by vendors in their platform code:

  • when creating CPO objects, this file is the source-of-truth for which ports should have CpoBase objects created for them (see [CPO] Extend ChassisBase to support CPO ports sonic-platform-common#700 for where it is used)
  • it can contain platform specific information like i2c path information if the vendor needs it. not all vendors will use this though -- PDDF users will not do so for instance
  • xcvrd will use this file to map logical interfaces and physical ports to CPO devices (i.e "Ethernet0 and Ethernet8 are both using OE1")

Signed-off-by: aditya-nexthop <aditya@nexthop.ai>
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@yxieca
yxieca merged commit b4e3ebb into sonic-net:master Aug 4, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants