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 err := d.removeOngoingTransaction(md5sum)
62 xapp.Logger.Error("removeOngoingTransaction() failed:%s", err.Error())
65 prevRestSubsId, exists := d.previousRequestMap[md5sum]
68 if prevRestSubsId != restSubsId {
69 xapp.Logger.Error("Storing md5sum for a processed request for restSubsId %s md5sum %s over a previous restSubsId %s", restSubsId, md5sum, prevRestSubsId)
74 xapp.Logger.Debug("Storing md5sum for a processed request for restSubsId %s md5sum %s", restSubsId, md5sum)
77 d.previousRequestMap[md5sum] = restSubsId
80 func (d *DuplicateCtrl) GetLastKnownRestSubsIdBasedOnMd5sum(md5sum string) (string, bool) {
83 defer d.mutex.Unlock()
89 m, e := d.previousRequestMap[md5sum]
94 func (d *DuplicateCtrl) DeleteLastKnownRestSubsIdBasedOnMd5sum(md5sum string) {
97 defer d.mutex.Unlock()
99 restSubsId, exists := d.previousRequestMap[md5sum]
103 xapp.Logger.Debug("Attempted to delete a cached md5sum, md5sum not set yet")
105 xapp.Logger.Error("Attempted to delete a cached md5sum %s, but the value was not found", md5sum)
108 xapp.Logger.Debug("Deleted a cached md5sum %s for restSubsId %s", md5sum, restSubsId)
109 delete(d.previousRequestMap, md5sum)
113 func CalculateRequestMd5sum(payload interface{}) (string, error) {
114 var data bytes.Buffer
115 enc := gob.NewEncoder(&data)
117 if err := enc.Encode(payload); err != nil {
118 xapp.Logger.Error("%s", err.Error())
122 hash := md5.Sum(data.Bytes())
124 return hex.EncodeToString(hash[:]), nil
127 func (d *DuplicateCtrl) IsDuplicateToOngoingTransaction(restSubsId string, md5sum string) bool {
134 defer d.mutex.Unlock()
136 entry, present := d.ongoingRequestMap[md5sum]
139 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))
144 entry = RetransEntry{restSubsId: restSubsId, startTime: time.Now()}
146 xapp.Logger.Debug("No collision detected against ongoing transaction. Added md5sum %s for restSubsId %s at %s\n", md5sum, entry.restSubsId, entry.startTime)
148 d.ongoingRequestMap[md5sum] = entry
153 func (d *DuplicateCtrl) TransactionComplete(md5sum string) error {
160 defer d.mutex.Unlock()
162 return d.removeOngoingTransaction(md5sum)
165 func (d *DuplicateCtrl) removeOngoingTransaction(md5sum string) error {
167 entry, present := d.ongoingRequestMap[md5sum]
170 xapp.Logger.Error("md5sum : %s NOT found from retransmission table", md5sum)
171 return fmt.Errorf("Retransmission entry not found for md5sum %s", md5sum)
174 xapp.Logger.Debug("Releasing transaction duplicate blocker for %s, md5sum : %s\n", entry.restSubsId, md5sum)
176 delete(d.ongoingRequestMap, md5sum)