Skip to content

Commit eeafe82

Browse files
committed
[apricot] Support for retrieving linkIDs by CRU endpoint and by detector
OCTRL-980 Not yet used by the DCS plugin to send enabled linkIDs for selected detectors, that will come next.
1 parent c71657a commit eeafe82

File tree

10 files changed

+985
-184
lines changed

10 files changed

+985
-184
lines changed

apricot/cacheproxy/service.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ func (s Service) GetEndpointsForCRUCard(hostname, cardSerial string) ([]string,
141141
return s.base.GetEndpointsForCRUCard(hostname, cardSerial)
142142
}
143143

144+
func (s Service) GetLinkIDsForCRUEndpoint(host string, cruId string, endpoint string, onlyEnabled bool) (ids []string, err error) {
145+
return s.base.GetLinkIDsForCRUEndpoint(host, cruId, endpoint, onlyEnabled)
146+
}
147+
148+
func (s Service) GetAliasedLinkIDsForDetector(detector string, onlyEnabled bool) ([]string, error) {
149+
return s.base.GetAliasedLinkIDsForDetector(detector, onlyEnabled)
150+
}
151+
144152
func (s Service) RawGetRecursive(path string) (string, error) {
145153
return s.base.RawGetRecursive(path)
146154
}

apricot/local/service.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"net/url"
4242
"os"
4343
"path/filepath"
44+
"regexp"
4445
"runtime"
4546
"sort"
4647
"strconv"
@@ -51,6 +52,7 @@ import (
5152
var log = logger.New(logrus.StandardLogger(), "confsys")
5253

5354
const inventoryKeyPrefix = "o2/hardware/"
55+
const readoutCardKeyPrefix = "o2/components/readoutcard/"
5456

5557
type Service struct {
5658
src cfgbackend.Source
@@ -185,6 +187,114 @@ func (s *Service) GetHostInventory(detector string) (hosts []string, err error)
185187
return hosts, err
186188
}
187189

190+
func (s *Service) GetLinkIDsForCRUEndpoint(host string, cruId string, endpoint string, onlyEnabled bool) (ids []string, err error) {
191+
192+
cfgPath := readoutCardKeyPrefix + host + "/cru/" + cruId + "/" + endpoint
193+
194+
readoutCardConfig, err := s.src.Get(cfgPath)
195+
if err != nil {
196+
return nil, err
197+
}
198+
199+
var data map[string]map[string]interface{}
200+
if err := json.Unmarshal([]byte(readoutCardConfig), &data); err != nil {
201+
return nil, err
202+
}
203+
204+
linkPattern := regexp.MustCompile(`^link(\d+)$`)
205+
206+
var linkIDs []string
207+
for key, value := range data {
208+
matches := linkPattern.FindStringSubmatch(key)
209+
if matches == nil {
210+
continue
211+
}
212+
linkEnabled, ok := value["enabled"].(string)
213+
if !ok {
214+
// this implies that "enabled" key should be always there, even if we want both enabled and disabled links
215+
continue
216+
}
217+
if linkEnabled == "true" || onlyEnabled == false {
218+
linkIDs = append(linkIDs, matches[1])
219+
}
220+
}
221+
222+
return linkIDs, nil
223+
}
224+
225+
type Aliases struct {
226+
Flp struct {
227+
Alias string `json:"alias"`
228+
} `json:"flp"`
229+
Cards map[string]struct {
230+
Alias string `json:"alias"`
231+
Links map[string]struct {
232+
Alias string `json:"alias"`
233+
} `json:"links"`
234+
} `json:"cards"`
235+
}
236+
237+
func (s *Service) getAliasesForHost(detector, host string) (Aliases, error) {
238+
aliasesPath := inventoryKeyPrefix + "detectors/" + detector + "/flps/" + host + "/aliases"
239+
aliasesFile, err := s.src.Get(aliasesPath)
240+
if err != nil {
241+
return Aliases{}, err
242+
}
243+
244+
var aliases Aliases
245+
if err := json.Unmarshal([]byte(aliasesFile), &aliases); err != nil {
246+
return Aliases{}, err
247+
}
248+
return aliases, nil
249+
}
250+
251+
func (s *Service) GetAliasedLinkIDsForDetector(detector string, onlyEnabled bool) (aliasedLinkIds []string, err error) {
252+
s.logMethod()
253+
254+
hosts, err := s.GetHostInventory(detector)
255+
if err != nil {
256+
return nil, err
257+
}
258+
259+
for _, host := range hosts {
260+
aliases, err := s.getAliasesForHost(detector, host)
261+
if err != nil {
262+
return nil, err
263+
}
264+
265+
crus, err := s.GetCRUCardsForHost(host)
266+
if err != nil {
267+
return nil, err
268+
}
269+
for _, cru := range crus {
270+
endpoints, err := s.GetEndpointsForCRUCard(host, cru)
271+
if err != nil {
272+
return nil, err
273+
}
274+
for _, endpoint := range endpoints {
275+
linkIDs, err := s.GetLinkIDsForCRUEndpoint(host, cru, endpoint, onlyEnabled)
276+
if err != nil {
277+
return nil, err
278+
}
279+
280+
aliasesForCruEndpoint, ok := aliases.Cards[cru+":"+endpoint]
281+
if !ok {
282+
return nil, fmt.Errorf("aliases for cru endpoint %s:%s not found", cru, endpoint)
283+
}
284+
for _, linkID := range linkIDs {
285+
linkIdAlias, ok := aliasesForCruEndpoint.Links[linkID]
286+
if !ok {
287+
return nil, fmt.Errorf("alias for link %s in cru endpoint %s:%s not found", linkID, cru, endpoint)
288+
}
289+
aliasedLinkIds = append(aliasedLinkIds, linkIdAlias.Alias)
290+
}
291+
}
292+
}
293+
}
294+
sort.Strings(aliasedLinkIds)
295+
return aliasedLinkIds, nil
296+
}
297+
188298
func (s *Service) GetDetectorsInventory() (inventory map[string][]string, err error) {
189299
s.logMethod()
190300

apricot/local/service_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,81 @@ var _ = Describe("local service", func() {
482482
})
483483
})
484484
})
485+
Describe("getting link IDs for a CRU card endpoint", func() {
486+
var (
487+
linkIDs []string
488+
err error
489+
)
490+
When("retrieving the link IDs for a CRU card endpoint", func() {
491+
It("should return the correct link IDs", func() {
492+
linkIDs, err = svc.GetLinkIDsForCRUEndpoint("flp001", "0228", "0", false)
493+
Expect(err).NotTo(HaveOccurred())
494+
Expect(linkIDs).To(ContainElements("0", "1", "2", "10"))
495+
})
496+
It("should return the correct enabled link IDs", func() {
497+
linkIDs, err = svc.GetLinkIDsForCRUEndpoint("flp001", "0228", "0", true)
498+
Expect(err).NotTo(HaveOccurred())
499+
Expect(linkIDs).To(ContainElements("0", "2"))
500+
})
501+
})
502+
When("retrieving the link IDs for a non-existing host", func() {
503+
It("should produce an error", func() {
504+
linkIDs, err = svc.GetLinkIDsForCRUEndpoint("NOPE", "0228", "0", true)
505+
Expect(err).To(HaveOccurred())
506+
})
507+
})
508+
When("retrieving the link IDs for a non-existing CRU card", func() {
509+
It("should produce an error", func() {
510+
linkIDs, err = svc.GetLinkIDsForCRUEndpoint("flp001", "NOPE", "0", true)
511+
Expect(err).To(HaveOccurred())
512+
})
513+
})
514+
When("retrieving the link IDs for a non-existing endpoint", func() {
515+
It("should produce an error", func() {
516+
linkIDs, err = svc.GetLinkIDsForCRUEndpoint("flp001", "0228", "NOPE", true)
517+
Expect(err).To(HaveOccurred())
518+
})
519+
})
520+
When("trying to retrieve the link IDs for an FLP belonging to a CRORC detector", func() {
521+
It("should produce an error", func() {
522+
linkIDs, err = svc.GetLinkIDsForCRUEndpoint("flp146", "0110", "0", true)
523+
Expect(err).To(HaveOccurred())
524+
})
525+
})
526+
})
527+
528+
Describe("getting aliased link IDs for a detector", func() {
529+
var (
530+
linkIDs []string
531+
err error
532+
)
533+
When("retrieving the link IDs for a detector", func() {
534+
It("should return the correct link IDs", func() {
535+
linkIDs, err = svc.GetAliasedLinkIDsForDetector("ABC", false)
536+
Expect(err).NotTo(HaveOccurred())
537+
Expect(linkIDs).To(Equal([]string{"01", "123", "400", "58", "59", "600", "62", "63", "a-b_c=d", "string"}))
538+
})
539+
})
540+
When("retrieving the active link IDs for a detector", func() {
541+
It("should return the correct link IDs", func() {
542+
linkIDs, err = svc.GetAliasedLinkIDsForDetector("ABC", true)
543+
Expect(err).NotTo(HaveOccurred())
544+
Expect(linkIDs).To(Equal([]string{"01", "400", "58", "600", "string"}))
545+
})
546+
})
547+
When("retrieving the link IDs for a non-existing detector", func() {
548+
It("should produce an error", func() {
549+
linkIDs, err = svc.GetAliasedLinkIDsForDetector("NOPE", false)
550+
Expect(err).To(HaveOccurred())
551+
})
552+
})
553+
When("retrieving the link IDs for a detector without readoutcard config", func() {
554+
It("should produce an error", func() {
555+
linkIDs, err = svc.GetAliasedLinkIDsForDetector("DEF", false)
556+
Expect(err).To(HaveOccurred())
557+
})
558+
})
559+
})
485560

486561
// TODO:
487562
// GetRuntimeEntry (currently not supporting yaml backend)

apricot/local/service_test.yaml

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,105 @@ o2:
1818
entry12: "hello {% include \"sub/entry12\" %}"
1919
sub:
2020
entry12: "world"
21+
readoutcard:
22+
flp001:
23+
cru:
24+
"0228":
25+
"0": '{
26+
"cru": {
27+
"key" : "value"
28+
},
29+
"links": {
30+
"enabled": "false",
31+
"gbtMux": "TTC",
32+
"feeId": "0x2"
33+
},
34+
"link0": {
35+
"enabled": "true",
36+
"gbtMux": "ttc",
37+
"feeId": "0x680"
38+
},
39+
"link1": {
40+
"enabled": "false",
41+
"gbtMux": "ttc",
42+
"feeId": "0x681"
43+
},
44+
"link2": {
45+
"enabled": "true",
46+
"gbtMux": "ttc",
47+
"feeId": "0x682"
48+
},
49+
"link10": {
50+
"enabled": "false",
51+
"gbtMux": "ttc",
52+
"feeId": "0x683"
53+
}
54+
}'
55+
"1": '{
56+
"cru": {
57+
"key" : "value"
58+
},
59+
"links": {
60+
"enabled": "false",
61+
"gbtMux": "TTC",
62+
"feeId": "0x2"
63+
},
64+
"link0": {
65+
"enabled": "true",
66+
"gbtMux": "ttc",
67+
"feeId": "0x6c0"
68+
},
69+
"link1": {
70+
"enabled": "false",
71+
"gbtMux": "ttc",
72+
"feeId": "0x6c1"
73+
},
74+
"link2": {
75+
"enabled": "false",
76+
"gbtMux": "ttc",
77+
"feeId": "0x6c2"
78+
},
79+
"link3": {
80+
"enabled": "false",
81+
"gbtMux": "ttc",
82+
"feeId": "0x6c3"
83+
}
84+
}'
85+
"0229":
86+
"0": '{
87+
"cru": {
88+
"key" : "value"
89+
},
90+
"links": {
91+
"enabled": "true",
92+
"gbtMux": "TTC",
93+
"feeId": "0x2"
94+
},
95+
"link0": {
96+
"enabled": "true",
97+
"gbtMux": "ttc",
98+
"feeId": "0x680"
99+
}
100+
}'
101+
"1": '{
102+
"cru": {
103+
"key" : "value"
104+
},
105+
"links": {
106+
"enabled": "true",
107+
"gbtMux": "TTC",
108+
"feeId": "0x2"
109+
},
110+
"link0": {
111+
"enabled": "true",
112+
"gbtMux": "ttc",
113+
"feeId": "0x6c0"
114+
}
115+
}'
116+
flp146:
117+
crorc:
118+
"0110":
119+
"0": "{}"
21120
runtime:
22121
aliecs:
23122
defaults:
@@ -30,6 +129,44 @@ o2:
30129
flps:
31130
flp001:
32131
cards: "{ \"key\" : \"value\" }"
132+
aliases: '
133+
{
134+
"flp": {
135+
"alias": "SM 0-1-2-3-4-14-15-16-17"
136+
},
137+
"cards": {
138+
"0228:0": {
139+
"alias": "SM 14-15-16-17 A-Side",
140+
"links": {
141+
"10": {"alias": "a-b_c=d"},
142+
"2": {"alias": "string"},
143+
"1": {"alias": "123"},
144+
"0": {"alias": "01"}
145+
}
146+
},
147+
"0228:1": {
148+
"alias": "SM 14-15-16-17 C-Side",
149+
"links": {
150+
"0": {"alias": "58"},
151+
"1": {"alias": "59"},
152+
"2": {"alias": "62"},
153+
"3": {"alias": "63"}
154+
}
155+
},
156+
"0229:0": {
157+
"alias": "SM 0-1-2-3-4 A-Side",
158+
"links": {
159+
"0": {"alias": "400"}
160+
}
161+
},
162+
"0229:1": {
163+
"alias": "SM 0-1-2-3-4 C-Side",
164+
"links": {
165+
"0": {"alias": "600"}
166+
}
167+
}
168+
}
169+
}'
33170
DEF:
34171
flps:
35172
flp002:

0 commit comments

Comments
 (0)