Seed code
[nonrtric/rapp/orufhrecovery.git] / goversion / internal / repository / lookupservice.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package repository
22
23 import (
24         "fmt"
25 )
26
27 type IdNotMappedError struct {
28         Id string
29 }
30
31 func (inme IdNotMappedError) Error() string {
32         return fmt.Sprintf("O-RU-ID: %v not mapped.", inme.Id)
33 }
34
35 type LookupService interface {
36         Init() error
37         GetODuID(oRuId string) (string, error)
38 }
39
40 type LookupServiceImpl struct {
41         csvFileHelper CsvFileHelper
42         csvFileName   string
43
44         oRuIdToODuIdMap map[string]string
45 }
46
47 func NewLookupServiceImpl(fileHelper CsvFileHelper, fileName string) *LookupServiceImpl {
48         s := LookupServiceImpl{
49                 csvFileHelper: fileHelper,
50                 csvFileName:   fileName,
51         }
52         s.oRuIdToODuIdMap = make(map[string]string)
53         return &s
54 }
55
56 func (s LookupServiceImpl) Init() error {
57         if csvData, err := s.csvFileHelper.GetCsvFromFile(s.csvFileName); err == nil {
58                 for _, each := range csvData {
59                         s.oRuIdToODuIdMap[each[0]] = each[1]
60                 }
61                 return nil
62         } else {
63                 return err
64         }
65 }
66
67 func (s LookupServiceImpl) GetODuID(oRuId string) (string, error) {
68         if oDuId, ok := s.oRuIdToODuIdMap[oRuId]; ok {
69                 return oDuId, nil
70         } else {
71                 return "", IdNotMappedError{Id: oRuId}
72         }
73 }