/
session.go
70 lines (62 loc) · 1.25 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) 2019, AT&T Intellectual Property. All rights reserved.
//
// Copyright (c) 2015-2017 by Brocade Communications Systems, Inc.
// All rights reserved.
//
// SPDX-License-Identifier: GPL-2.0-only
package ifmgrd
import (
"sync"
"github.com/danos/config/data"
"github.com/danos/config/schema"
"github.com/danos/mgmterror"
)
type Session struct {
candidate *data.Node
running *data.Node
schema schema.Node
}
type Sessions struct {
sync.RWMutex
sessions map[string]*Session
}
func NewSessionMap() *Sessions {
return &Sessions{
sessions: make(map[string]*Session),
}
}
func (s *Sessions) New(
sid string,
candidate, running *data.Node,
schema schema.Node,
) (*Session, error) {
s.Lock()
defer s.Unlock()
sess, ok := s.sessions[sid]
if ok {
err := mgmterror.NewOperationFailedApplicationError()
err.Message = "session exists"
return nil, err
}
sess = &Session{
candidate: candidate,
running: running,
schema: schema,
}
s.sessions[sid] = sess
return sess, nil
}
func (s *Sessions) Delete(sid string) {
s.Lock()
defer s.Unlock()
_, exists := s.sessions[sid]
if !exists {
return
}
delete(s.sessions, sid)
}
func (s *Sessions) Get(sid string) *Session {
s.RLock()
defer s.RUnlock()
return s.sessions[sid]
}