Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCTRL-980 Send enabled links to DCS for selected detectors #656

Merged
merged 2 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apricot/cacheproxy/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ func (s Service) GetEndpointsForCRUCard(hostname, cardSerial string) ([]string,
return s.base.GetEndpointsForCRUCard(hostname, cardSerial)
}

func (s Service) GetLinkIDsForCRUEndpoint(host string, cruId string, endpoint string, onlyEnabled bool) (ids []string, err error) {
return s.base.GetLinkIDsForCRUEndpoint(host, cruId, endpoint, onlyEnabled)
}

func (s Service) GetAliasedLinkIDsForDetector(detector string, onlyEnabled bool) ([]string, error) {
return s.base.GetAliasedLinkIDsForDetector(detector, onlyEnabled)
}

func (s Service) RawGetRecursive(path string) (string, error) {
return s.base.RawGetRecursive(path)
}
Expand Down
110 changes: 110 additions & 0 deletions apricot/local/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"runtime"
"sort"
"strconv"
Expand All @@ -51,6 +52,7 @@ import (
var log = logger.New(logrus.StandardLogger(), "confsys")

const inventoryKeyPrefix = "o2/hardware/"
const readoutCardKeyPrefix = "o2/components/readoutcard/"

type Service struct {
src cfgbackend.Source
Expand Down Expand Up @@ -185,6 +187,114 @@ func (s *Service) GetHostInventory(detector string) (hosts []string, err error)
return hosts, err
}

func (s *Service) GetLinkIDsForCRUEndpoint(host string, cruId string, endpoint string, onlyEnabled bool) (ids []string, err error) {

cfgPath := readoutCardKeyPrefix + host + "/cru/" + cruId + "/" + endpoint

readoutCardConfig, err := s.src.Get(cfgPath)
if err != nil {
return nil, err
}

var data map[string]map[string]interface{}
if err := json.Unmarshal([]byte(readoutCardConfig), &data); err != nil {
return nil, err
}

linkPattern := regexp.MustCompile(`^link(\d+)$`)

var linkIDs []string
for key, value := range data {
matches := linkPattern.FindStringSubmatch(key)
if matches == nil {
continue
}
linkEnabled, ok := value["enabled"].(string)
if !ok {
// this implies that "enabled" key should be always there, even if we want both enabled and disabled links
continue
}
if linkEnabled == "true" || onlyEnabled == false {
linkIDs = append(linkIDs, matches[1])
}
}

return linkIDs, nil
}

type Aliases struct {
Flp struct {
Alias string `json:"alias"`
} `json:"flp"`
Cards map[string]struct {
Alias string `json:"alias"`
Links map[string]struct {
Alias string `json:"alias"`
} `json:"links"`
} `json:"cards"`
}

func (s *Service) getAliasesForHost(detector, host string) (Aliases, error) {
aliasesPath := inventoryKeyPrefix + "detectors/" + detector + "/flps/" + host + "/aliases"
aliasesFile, err := s.src.Get(aliasesPath)
if err != nil {
return Aliases{}, err
}

var aliases Aliases
if err := json.Unmarshal([]byte(aliasesFile), &aliases); err != nil {
return Aliases{}, err
}
return aliases, nil
}

func (s *Service) GetAliasedLinkIDsForDetector(detector string, onlyEnabled bool) (aliasedLinkIds []string, err error) {
s.logMethod()

hosts, err := s.GetHostInventory(detector)
if err != nil {
return nil, err
}

for _, host := range hosts {
aliases, err := s.getAliasesForHost(detector, host)
if err != nil {
return nil, err
}

crus, err := s.GetCRUCardsForHost(host)
if err != nil {
return nil, err
}
for _, cru := range crus {
endpoints, err := s.GetEndpointsForCRUCard(host, cru)
if err != nil {
return nil, err
}
for _, endpoint := range endpoints {
linkIDs, err := s.GetLinkIDsForCRUEndpoint(host, cru, endpoint, onlyEnabled)
if err != nil {
return nil, err
}

aliasesForCruEndpoint, ok := aliases.Cards[cru+":"+endpoint]
if !ok {
return nil, fmt.Errorf("aliases for cru endpoint %s:%s not found", cru, endpoint)
}
for _, linkID := range linkIDs {
linkIdAlias, ok := aliasesForCruEndpoint.Links[linkID]
if !ok {
return nil, fmt.Errorf("alias for link %s in cru endpoint %s:%s not found", linkID, cru, endpoint)
}
aliasedLinkIds = append(aliasedLinkIds, linkIdAlias.Alias)
}
}
}
}
sort.Strings(aliasedLinkIds)
return aliasedLinkIds, nil
}

func (s *Service) GetDetectorsInventory() (inventory map[string][]string, err error) {
s.logMethod()

Expand Down
75 changes: 75 additions & 0 deletions apricot/local/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,81 @@ var _ = Describe("local service", func() {
})
})
})
Describe("getting link IDs for a CRU card endpoint", func() {
var (
linkIDs []string
err error
)
When("retrieving the link IDs for a CRU card endpoint", func() {
It("should return the correct link IDs", func() {
linkIDs, err = svc.GetLinkIDsForCRUEndpoint("flp001", "0228", "0", false)
Expect(err).NotTo(HaveOccurred())
Expect(linkIDs).To(ContainElements("0", "1", "2", "10"))
})
It("should return the correct enabled link IDs", func() {
linkIDs, err = svc.GetLinkIDsForCRUEndpoint("flp001", "0228", "0", true)
Expect(err).NotTo(HaveOccurred())
Expect(linkIDs).To(ContainElements("0", "2"))
})
})
When("retrieving the link IDs for a non-existing host", func() {
It("should produce an error", func() {
linkIDs, err = svc.GetLinkIDsForCRUEndpoint("NOPE", "0228", "0", true)
Expect(err).To(HaveOccurred())
})
})
When("retrieving the link IDs for a non-existing CRU card", func() {
It("should produce an error", func() {
linkIDs, err = svc.GetLinkIDsForCRUEndpoint("flp001", "NOPE", "0", true)
Expect(err).To(HaveOccurred())
})
})
When("retrieving the link IDs for a non-existing endpoint", func() {
It("should produce an error", func() {
linkIDs, err = svc.GetLinkIDsForCRUEndpoint("flp001", "0228", "NOPE", true)
Expect(err).To(HaveOccurred())
})
})
When("trying to retrieve the link IDs for an FLP belonging to a CRORC detector", func() {
It("should produce an error", func() {
linkIDs, err = svc.GetLinkIDsForCRUEndpoint("flp146", "0110", "0", true)
Expect(err).To(HaveOccurred())
})
})
})

Describe("getting aliased link IDs for a detector", func() {
var (
linkIDs []string
err error
)
When("retrieving the link IDs for a detector", func() {
It("should return the correct link IDs", func() {
linkIDs, err = svc.GetAliasedLinkIDsForDetector("ABC", false)
Expect(err).NotTo(HaveOccurred())
Expect(linkIDs).To(Equal([]string{"01", "123", "400", "58", "59", "600", "62", "63", "a-b_c=d", "string"}))
})
})
When("retrieving the active link IDs for a detector", func() {
It("should return the correct link IDs", func() {
linkIDs, err = svc.GetAliasedLinkIDsForDetector("ABC", true)
Expect(err).NotTo(HaveOccurred())
Expect(linkIDs).To(Equal([]string{"01", "400", "58", "600", "string"}))
})
})
When("retrieving the link IDs for a non-existing detector", func() {
It("should produce an error", func() {
linkIDs, err = svc.GetAliasedLinkIDsForDetector("NOPE", false)
Expect(err).To(HaveOccurred())
})
})
When("retrieving the link IDs for a detector without readoutcard config", func() {
It("should produce an error", func() {
linkIDs, err = svc.GetAliasedLinkIDsForDetector("DEF", false)
Expect(err).To(HaveOccurred())
})
})
})

// TODO:
// GetRuntimeEntry (currently not supporting yaml backend)
Expand Down
137 changes: 137 additions & 0 deletions apricot/local/service_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,105 @@ o2:
entry12: "hello {% include \"sub/entry12\" %}"
sub:
entry12: "world"
readoutcard:
flp001:
cru:
"0228":
"0": '{
"cru": {
"key" : "value"
},
"links": {
"enabled": "false",
"gbtMux": "TTC",
"feeId": "0x2"
},
"link0": {
"enabled": "true",
"gbtMux": "ttc",
"feeId": "0x680"
},
"link1": {
"enabled": "false",
"gbtMux": "ttc",
"feeId": "0x681"
},
"link2": {
"enabled": "true",
"gbtMux": "ttc",
"feeId": "0x682"
},
"link10": {
"enabled": "false",
"gbtMux": "ttc",
"feeId": "0x683"
}
}'
"1": '{
"cru": {
"key" : "value"
},
"links": {
"enabled": "false",
"gbtMux": "TTC",
"feeId": "0x2"
},
"link0": {
"enabled": "true",
"gbtMux": "ttc",
"feeId": "0x6c0"
},
"link1": {
"enabled": "false",
"gbtMux": "ttc",
"feeId": "0x6c1"
},
"link2": {
"enabled": "false",
"gbtMux": "ttc",
"feeId": "0x6c2"
},
"link3": {
"enabled": "false",
"gbtMux": "ttc",
"feeId": "0x6c3"
}
}'
"0229":
"0": '{
"cru": {
"key" : "value"
},
"links": {
"enabled": "true",
"gbtMux": "TTC",
"feeId": "0x2"
},
"link0": {
"enabled": "true",
"gbtMux": "ttc",
"feeId": "0x680"
}
}'
"1": '{
"cru": {
"key" : "value"
},
"links": {
"enabled": "true",
"gbtMux": "TTC",
"feeId": "0x2"
},
"link0": {
"enabled": "true",
"gbtMux": "ttc",
"feeId": "0x6c0"
}
}'
flp146:
crorc:
"0110":
"0": "{}"
runtime:
aliecs:
defaults:
Expand All @@ -30,6 +129,44 @@ o2:
flps:
flp001:
cards: "{ \"key\" : \"value\" }"
aliases: '
{
"flp": {
"alias": "SM 0-1-2-3-4-14-15-16-17"
},
"cards": {
"0228:0": {
"alias": "SM 14-15-16-17 A-Side",
"links": {
"10": {"alias": "a-b_c=d"},
"2": {"alias": "string"},
"1": {"alias": "123"},
"0": {"alias": "01"}
}
},
"0228:1": {
"alias": "SM 14-15-16-17 C-Side",
"links": {
"0": {"alias": "58"},
"1": {"alias": "59"},
"2": {"alias": "62"},
"3": {"alias": "63"}
}
},
"0229:0": {
"alias": "SM 0-1-2-3-4 A-Side",
"links": {
"0": {"alias": "400"}
}
},
"0229:1": {
"alias": "SM 0-1-2-3-4 C-Side",
"links": {
"0": {"alias": "600"}
}
}
}
}'
DEF:
flps:
flp002:
Expand Down
Loading
Loading