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