blob: e766b71f53953abc58abca0d3fd2be5ebee7f47a [file] [log] [blame]
Stephane Barbariea75791c2019-01-24 10:58:06 -05001/*
2 * Copyright 2019-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 */
16package core
17
18import (
19 "context"
20 "github.com/opencord/voltha-go/common/log"
21 "github.com/opencord/voltha-go/db/model"
William Kurkiandaa6bb22019-03-07 12:26:28 -050022 "github.com/opencord/voltha-protos/go/voltha"
Stephane Barbariea75791c2019-01-24 10:58:06 -050023 "google.golang.org/grpc/codes"
24 "google.golang.org/grpc/status"
25)
26
27// Enumerated type to keep track of miscellaneous data path agents
28type DataModelType int
29
30// Enumerated list of data path agents
31const (
32 Adapters DataModelType = 1 + iota
33 AlarmFilters
34 CoreInstances
35 DeviceTypes
36 DeviceGroups
37 Voltha
38)
39
40// String equivalent for data path agents
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040041var commonTypes = []string{
Stephane Barbariea75791c2019-01-24 10:58:06 -050042 "Adapters",
43 "AlarmFilters",
44 "CoreInstances",
45 "DeviceTypes",
46 "DeviceGroups",
47 "Voltha",
48}
49
50// String converts the enumerated data path agent value to its string equivalent
51func (t DataModelType) String() string {
52 return commonTypes[t-1]
53}
54
55// ModelProxyManager controls requests made to the miscellaneous data path agents
56type ModelProxyManager struct {
57 modelProxy map[string]*ModelProxy
58 clusterDataProxy *model.Proxy
59}
60
61func newModelProxyManager(cdProxy *model.Proxy) *ModelProxyManager {
62 var mgr ModelProxyManager
63 mgr.modelProxy = make(map[string]*ModelProxy)
64 mgr.clusterDataProxy = cdProxy
65 return &mgr
66}
67
68// GetDeviceType returns the device type associated to the provided id
69func (mpMgr *ModelProxyManager) GetVoltha(ctx context.Context) (*voltha.Voltha, error) {
70 log.Debug("GetVoltha")
71
72 var agent *ModelProxy
73 var exists bool
74
75 if agent, exists = mpMgr.modelProxy[Voltha.String()]; !exists {
76 agent = newModelProxy("", mpMgr.clusterDataProxy)
77 mpMgr.modelProxy[Voltha.String()] = agent
78 }
79
80 if instance, _ := agent.Get(); instance != nil {
81 return instance.(*voltha.Voltha), nil
82 }
83
84 return &voltha.Voltha{}, status.Errorf(codes.NotFound, "no-voltha-instance")
85}
86
87// ListCoreInstances returns all the core instances known to the system
88func (mpMgr *ModelProxyManager) ListCoreInstances(ctx context.Context) (*voltha.CoreInstances, error) {
89 log.Debug("ListCoreInstances")
90
91 // TODO: Need to retrieve the list of registered cores
92
93 return &voltha.CoreInstances{}, status.Errorf(codes.NotFound, "no-core-instances")
94}
95
96// GetCoreInstance returns the core instance associated to the provided id
97func (mpMgr *ModelProxyManager) GetCoreInstance(ctx context.Context, id string) (*voltha.CoreInstance, error) {
98 log.Debugw("GetCoreInstance", log.Fields{"id": id})
99
100 // TODO: Need to retrieve the list of registered cores
101
102 return &voltha.CoreInstance{}, status.Errorf(codes.NotFound, "core-instance-%s", id)
103}
104
105// ListAdapters returns all the device types known to the system
106func (mpMgr *ModelProxyManager) ListAdapters(ctx context.Context) (*voltha.Adapters, error) {
107 log.Debug("ListAdapters")
108
109 var agent *ModelProxy
110 var exists bool
111
112 if agent, exists = mpMgr.modelProxy[Adapters.String()]; !exists {
113 agent = newModelProxy("adapters", mpMgr.clusterDataProxy)
114 mpMgr.modelProxy[Adapters.String()] = agent
115 }
116
117 adapters := &voltha.Adapters{}
118 if items, _ := agent.Get(); items != nil {
119 for _, item := range items.([]interface{}) {
120 adapters.Items = append(adapters.Items, item.(*voltha.Adapter))
121 }
122 log.Debugw("retrieved-adapters", log.Fields{"adapters": adapters})
123 return adapters, nil
124 }
125
126 return adapters, status.Errorf(codes.NotFound, "no-adapters")
127}
128
129// ListDeviceTypes returns all the device types known to the system
130func (mpMgr *ModelProxyManager) ListDeviceTypes(ctx context.Context) (*voltha.DeviceTypes, error) {
131 log.Debug("ListDeviceTypes")
132
133 var agent *ModelProxy
134 var exists bool
135
136 if agent, exists = mpMgr.modelProxy[DeviceTypes.String()]; !exists {
137 agent = newModelProxy("device_types", mpMgr.clusterDataProxy)
138 mpMgr.modelProxy[DeviceTypes.String()] = agent
139 }
140
141 deviceTypes := &voltha.DeviceTypes{}
142 if items, _ := agent.Get(); items != nil {
143 for _, item := range items.([]interface{}) {
144 deviceTypes.Items = append(deviceTypes.Items, item.(*voltha.DeviceType))
145 }
146 return deviceTypes, nil
147 }
148
149 return deviceTypes, status.Errorf(codes.NotFound, "no-device-types")
150}
151
152// GetDeviceType returns the device type associated to the provided id
153func (mpMgr *ModelProxyManager) GetDeviceType(ctx context.Context, id string) (*voltha.DeviceType, error) {
154 log.Debugw("GetDeviceType", log.Fields{"id": id})
155
156 var agent *ModelProxy
157 var exists bool
158
159 if agent, exists = mpMgr.modelProxy[DeviceTypes.String()]; !exists {
160 agent = newModelProxy("device_types", mpMgr.clusterDataProxy)
161 mpMgr.modelProxy[DeviceTypes.String()] = agent
162 }
163
164 if deviceType, _ := agent.Get(id); deviceType != nil {
165 return deviceType.(*voltha.DeviceType), nil
166 }
167
168 return &voltha.DeviceType{}, status.Errorf(codes.NotFound, "device-type-%s", id)
169}
170
171// ListDeviceGroups returns all the device groups known to the system
172func (mpMgr *ModelProxyManager) ListDeviceGroups(ctx context.Context) (*voltha.DeviceGroups, error) {
173 log.Debug("ListDeviceGroups")
174
175 var agent *ModelProxy
176 var exists bool
177
178 if agent, exists = mpMgr.modelProxy[DeviceGroups.String()]; !exists {
179 agent = newModelProxy("device_groups", mpMgr.clusterDataProxy)
180 mpMgr.modelProxy[DeviceGroups.String()] = agent
181 }
182
183 deviceGroups := &voltha.DeviceGroups{}
184 if items, _ := agent.Get(); items != nil {
185 for _, item := range items.([]interface{}) {
186 deviceGroups.Items = append(deviceGroups.Items, item.(*voltha.DeviceGroup))
187 }
188 return deviceGroups, nil
189 }
190
191 return deviceGroups, status.Errorf(codes.NotFound, "no-device-groups")
192}
193
194// GetDeviceGroup returns the device group associated to the provided id
195func (mpMgr *ModelProxyManager) GetDeviceGroup(ctx context.Context, id string) (*voltha.DeviceGroup, error) {
196 log.Debugw("GetDeviceGroup", log.Fields{"id": id})
197
198 var agent *ModelProxy
199 var exists bool
200
201 if agent, exists = mpMgr.modelProxy[DeviceGroups.String()]; !exists {
202 agent = newModelProxy("device_groups", mpMgr.clusterDataProxy)
203 mpMgr.modelProxy[DeviceGroups.String()] = agent
204 }
205
206 if deviceGroup, _ := agent.Get(id); deviceGroup != nil {
207 return deviceGroup.(*voltha.DeviceGroup), nil
208 }
209
210 return &voltha.DeviceGroup{}, status.Errorf(codes.NotFound, "device-group-%s", id)
211}
212
213// ListAlarmFilters returns all the alarm filters known to the system
214func (mpMgr *ModelProxyManager) ListAlarmFilters(ctx context.Context) (*voltha.AlarmFilters, error) {
215 log.Debug("ListAlarmFilters")
216
217 var agent *ModelProxy
218 var exists bool
219
220 if agent, exists = mpMgr.modelProxy[AlarmFilters.String()]; !exists {
221 agent = newModelProxy("alarm_filters", mpMgr.clusterDataProxy)
222 mpMgr.modelProxy[AlarmFilters.String()] = agent
223 }
224
225 alarmFilters := &voltha.AlarmFilters{}
226 if items, _ := agent.Get(); items != nil {
227 for _, item := range items.([]interface{}) {
228 alarmFilters.Filters = append(alarmFilters.Filters, item.(*voltha.AlarmFilter))
229 }
230 return alarmFilters, nil
231 }
232
233 return alarmFilters, status.Errorf(codes.NotFound, "no-alarm-filters")
234}
235
236// GetAlarmFilter returns the alarm filter associated to the provided id
237func (mpMgr *ModelProxyManager) GetAlarmFilter(ctx context.Context, id string) (*voltha.AlarmFilter, error) {
238 log.Debugw("GetAlarmFilter", log.Fields{"id": id})
239
240 var agent *ModelProxy
241 var exists bool
242
243 if agent, exists = mpMgr.modelProxy[AlarmFilters.String()]; !exists {
244 agent = newModelProxy("alarm_filters", mpMgr.clusterDataProxy)
245 mpMgr.modelProxy[AlarmFilters.String()] = agent
246 }
247
248 if alarmFilter, _ := agent.Get(id); alarmFilter != nil {
249 return alarmFilter.(*voltha.AlarmFilter), nil
250 }
251 return &voltha.AlarmFilter{}, status.Errorf(codes.NotFound, "alarm-filter-%s", id)
252}