Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
| 22 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 23 | "sync" |
| 24 | ) |
| 25 | |
| 26 | type Store struct { |
| 27 | olts sync.Map |
| 28 | onus sync.Map |
| 29 | bps sync.Map |
| 30 | } |
| 31 | |
| 32 | func NewStore() *Store { |
| 33 | return &Store{ |
| 34 | olts: sync.Map{}, |
| 35 | onus: sync.Map{}, |
| 36 | bps: sync.Map{}, |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func (s *Store) addOlt(ctx context.Context, entry SadisOltEntry) { |
| 41 | logger.Debugw(ctx, "adding-olt", log.Fields{"olt": entry}) |
| 42 | s.olts.Store(entry.ID, entry) |
| 43 | } |
| 44 | |
| 45 | func (s *Store) addOnu(ctx context.Context, entry SadisOnuEntryV2) { |
| 46 | logger.Debugw(ctx, "adding-onu", log.Fields{"onu": entry}) |
| 47 | s.onus.Store(entry.ID, entry) |
| 48 | } |
| 49 | |
| 50 | func (s *Store) addBp(ctx context.Context, entry SadisBWPEntry) { |
| 51 | logger.Debugw(ctx, "adding-bp", log.Fields{"bp": entry}) |
| 52 | s.bps.Store(entry.ID, entry) |
| 53 | } |
| 54 | |
| 55 | func (s *Store) getOlt(ctx context.Context, id string) (*SadisOltEntry, error) { |
| 56 | logger.Debugw(ctx, "getting-olt", log.Fields{"olt": id}) |
| 57 | if entry, ok := s.olts.Load(id); ok { |
| 58 | e := entry.(SadisOltEntry) |
| 59 | return &e, nil |
| 60 | } |
| 61 | return nil, fmt.Errorf("olt-not-found-in-store") |
| 62 | } |
| 63 | |
| 64 | func (s *Store) getOnu(ctx context.Context, id string) (*SadisOnuEntryV2, error) { |
| 65 | logger.Debugw(ctx, "getting-onu", log.Fields{"onu": id}) |
| 66 | if entry, ok := s.onus.Load(id); ok { |
| 67 | e := entry.(SadisOnuEntryV2) |
| 68 | return &e, nil |
| 69 | } |
| 70 | return nil, fmt.Errorf("onu-not-found-in-store") |
| 71 | } |
| 72 | |
| 73 | func (s *Store) getBp(ctx context.Context, id string) (*SadisBWPEntry, error) { |
| 74 | logger.Debugw(ctx, "getting-bp", log.Fields{"bp": id}) |
| 75 | if entry, ok := s.bps.Load(id); ok { |
| 76 | e := entry.(SadisBWPEntry) |
| 77 | return &e, nil |
| 78 | } |
| 79 | return nil, fmt.Errorf("bp-not-found-in-store") |
| 80 | } |