2 ==================================================================================
3 Copyright (c) 2021 Nokia
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================================
30 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
33 type RetransEntry struct {
38 type DuplicateCtrl struct {
40 ongoingRequestMap map[string]RetransEntry
41 previousRequestMap map[string]string
45 func (d *DuplicateCtrl) Init() {
46 d.ongoingRequestMap = make(map[string]RetransEntry)
47 d.previousRequestMap = make(map[string]string)
50 func (d *DuplicateCtrl) SetMd5sumFromLastOkRequest(restSubsId string, md5sum string) {
53 defer d.mutex.Unlock()
56 xapp.Logger.Error("Attempt to store empty md5sum for restubsId %s retransmission map skipped", restSubsId)
60 d.removeOngoingTransaction(md5sum)
62 prevRestSubsId, exists := d.previousRequestMap[md5sum]
65 if prevRestSubsId != restSubsId {
66 xapp.Logger.Error("Storing md5sum for a processed request for restSubsId %s md5sum %s over a previous restSubsId %s", restSubsId, md5sum, prevRestSubsId)
71 xapp.Logger.Debug("Storing md5sum for a processed request for restSubsId %s md5sum %s", restSubsId, md5sum)
74 d.previousRequestMap[md5sum] = restSubsId
77 func (d *DuplicateCtrl) GetLastKnownRestSubsIdBasedOnMd5sum(md5sum string) (string, bool) {
80 defer d.mutex.Unlock()
86 m, e := d.previousRequestMap[md5sum]
91 func (d *DuplicateCtrl) DeleteLastKnownRestSubsIdBasedOnMd5sum(md5sum string) {
94 defer d.mutex.Unlock()
96 restSubsId, exists := d.previousRequestMap[md5sum]
100 xapp.Logger.Debug("Attempted to delete a cached md5sum, md5sum not set yet")
102 xapp.Logger.Error("Attempted to delete a cached md5sum %s, but the value was not found", md5sum)
105 xapp.Logger.Debug("Deleted a cached md5sum %s for restSubsId %s", md5sum, restSubsId)
106 delete(d.previousRequestMap, md5sum)
110 func CalculateRequestMd5sum(payload interface{}) (string, error) {
111 var data bytes.Buffer
112 enc := gob.NewEncoder(&data)
114 if err := enc.Encode(payload); err != nil {
115 xapp.Logger.Error("%s", err.Error())
119 hash := md5.Sum(data.Bytes())
121 return hex.EncodeToString(hash[:]), nil
124 func (d *DuplicateCtrl) IsDuplicateToOngoingTransaction(restSubsId string, md5sum string) bool {
131 defer d.mutex.Unlock()
133 entry, present := d.ongoingRequestMap[md5sum]
136 xapp.Logger.Debug("Collision detected. REST subs ID %s has ongoing transaction with md5sum : %s started at %s\n", entry.restSubsId, md5sum, entry.startTime.Format(time.ANSIC))
141 entry = RetransEntry{restSubsId: restSubsId, startTime: time.Now()}
143 xapp.Logger.Debug("No collision detected against ongoing transaction. Added md5sum %s for restSubsId %s at %s\n", md5sum, entry.restSubsId, entry.startTime)
145 d.ongoingRequestMap[md5sum] = entry
150 func (d *DuplicateCtrl) TransactionComplete(md5sum string) error {
157 defer d.mutex.Unlock()
159 return d.removeOngoingTransaction(md5sum)
162 func (d *DuplicateCtrl) removeOngoingTransaction(md5sum string) error {
164 entry, present := d.ongoingRequestMap[md5sum]
167 xapp.Logger.Error("md5sum : %s NOT found from retransmission table", md5sum)
168 return fmt.Errorf("Retransmission entry not found for md5sum %s", md5sum)
171 xapp.Logger.Debug("Releasing transaction duplicate blocker for %s, md5sum : %s\n", entry.restSubsId, md5sum)
173 delete(d.ongoingRequestMap, md5sum)