blob: d2261342c9d7e466bbb3ee23f4e0dda024234738 [file] [log] [blame]
Holger Hildebrandtfb402a62021-05-26 14:40:49 +00001/*
2 * Copyright 2020-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 adaptercoreonu provides the utility for onu devices, flows and statistics
18package adaptercoreonu
19
20import (
21 "context"
22 "encoding/hex"
23 "fmt"
24 "sync"
25 "time"
26
27 "github.com/opencord/omci-lib-go"
28 me "github.com/opencord/omci-lib-go/generated"
khenaidoo7d3c5582021-08-11 18:09:44 -040029 "github.com/opencord/voltha-lib-go/v7/pkg/log"
30 "github.com/opencord/voltha-protos/v5/go/voltha"
Holger Hildebrandtfb402a62021-05-26 14:40:49 +000031)
32
33//OnuImageStatus implements methods to get status info of onu images
34type OnuImageStatus struct {
35 pDevEntry *OnuDeviceEntry
36 deviceID string
37 requestedAttributes me.AttributeValueMap
38 mutexWaitingForResp sync.RWMutex
39 waitingForResp bool
40 respChannel chan Message
41 mutexPLastTxMeInstance sync.RWMutex
42 pLastTxMeInstance *me.ManagedEntity
43}
44
45const (
46 cImgVersion = "Version"
47 cImgIsCommitted = "IsCommitted"
48 cImgIsActive = "IsActive"
49 cImgIsValid = "IsValid"
50 cImgProductCode = "ProductCode"
51 cImgImageHash = "ImageHash"
52)
Holger Hildebrandt05011352021-06-15 09:40:24 +000053const cResponse = "response: "
Holger Hildebrandtfb402a62021-05-26 14:40:49 +000054
55//NewOnuImageStatus creates a new instance of OnuImageStatus
56func NewOnuImageStatus(pDevEntry *OnuDeviceEntry) *OnuImageStatus {
57 return &OnuImageStatus{
58 pDevEntry: pDevEntry,
59 deviceID: pDevEntry.deviceID,
60 requestedAttributes: make(me.AttributeValueMap),
61 waitingForResp: false,
62 respChannel: make(chan Message),
63 }
64}
Holger Hildebrandt05011352021-06-15 09:40:24 +000065
Holger Hildebrandtfb402a62021-05-26 14:40:49 +000066func (oo *OnuImageStatus) getOnuImageStatus(ctx context.Context) (*voltha.OnuImages, error) {
67
Holger Hildebrandt05011352021-06-15 09:40:24 +000068 if !oo.pDevEntry.baseDeviceHandler.isReadyForOmciConfig() {
69 logger.Errorw(ctx, "command rejected - improper device state", log.Fields{"device-id": oo.deviceID})
70 return nil, fmt.Errorf("command-rejected-improper-device-state")
71 }
Holger Hildebrandtfb402a62021-05-26 14:40:49 +000072 if oo.pDevEntry.PDevOmciCC == nil {
73 logger.Errorw(ctx, "omciCC not ready to receive omci messages", log.Fields{"device-id": oo.deviceID})
74 return nil, fmt.Errorf("omciCC-not-ready-to-receive-omci-messages")
75 }
Holger Hildebrandt05011352021-06-15 09:40:24 +000076 var images voltha.OnuImages
77
Holger Hildebrandtfb402a62021-05-26 14:40:49 +000078 for i := firstSwImageMeID; i <= secondSwImageMeID; i++ {
79 logger.Debugw(ctx, "getOnuImageStatus for image id", log.Fields{"image-id": i, "device-id": oo.deviceID})
80
81 var image voltha.OnuImage
82
83 // TODO: Since the summed length of the attributes exceeds the capacity of a single response,
84 // it is distributed on several requests here. It should be discussed whether, in the course of a refactoring,
85 // a global mechanism should be implemented that automates this distribution - which would entail quite some
86 // changes on the respective receiver sides.
87
88 oo.requestedAttributes = me.AttributeValueMap{cImgVersion: "", cImgIsCommitted: 0, cImgIsActive: 0, cImgIsValid: 0}
89 if err := oo.requestOnuImageAttributes(ctx, uint16(i), &image); err != nil {
90 logger.Errorw(ctx, err.Error(), log.Fields{"requestedAttributes": oo.requestedAttributes, "device-id": oo.deviceID})
91 return nil, err
92 }
93 oo.requestedAttributes = me.AttributeValueMap{cImgProductCode: ""}
94 if err := oo.requestOnuImageAttributes(ctx, uint16(i), &image); err != nil {
95 logger.Errorw(ctx, err.Error(), log.Fields{"requestedAttributes": oo.requestedAttributes, "device-id": oo.deviceID})
96 return nil, err
97 }
98 oo.requestedAttributes = me.AttributeValueMap{cImgImageHash: 0}
99 if err := oo.requestOnuImageAttributes(ctx, uint16(i), &image); err != nil {
100 logger.Errorw(ctx, err.Error(), log.Fields{"requestedAttributes": oo.requestedAttributes, "device-id": oo.deviceID})
101 return nil, err
102 }
103 images.Items = append(images.Items, &image)
104 }
105 logger.Debugw(ctx, "images of the ONU", log.Fields{"images": images})
Holger Hildebrandt05011352021-06-15 09:40:24 +0000106 oo.updateOnuSwImageIndications(ctx, &images)
107 oo.updateOnuSwImagePersistentData(ctx)
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000108 return &images, nil
109}
110
111func (oo *OnuImageStatus) requestOnuImageAttributes(ctx context.Context, imageID uint16, image *voltha.OnuImage) error {
112 oo.mutexPLastTxMeInstance.Lock()
113 meInstance, err := oo.pDevEntry.PDevOmciCC.sendGetMe(log.WithSpanFromContext(context.TODO(), ctx), me.SoftwareImageClassID,
114 imageID, oo.requestedAttributes, oo.pDevEntry.pOpenOnuAc.omciTimeout, true, oo.respChannel)
115 if err != nil {
116 oo.mutexPLastTxMeInstance.Unlock()
117 logger.Errorw(ctx, "can't send omci request to get data for image id", log.Fields{"image-id": imageID, "device-id": oo.deviceID})
118 return fmt.Errorf("can't-send-omci-request-to-get-data-for-image-id-%d", imageID)
119 }
120 oo.pLastTxMeInstance = meInstance
121 oo.mutexPLastTxMeInstance.Unlock()
122
123 if err = oo.waitForGetOnuImageStatus(ctx, image); err != nil {
124 logger.Errorw(ctx, err.Error(), log.Fields{"device-id": oo.deviceID})
125 return err
126 }
127 return nil
128}
129
130func (oo *OnuImageStatus) waitForGetOnuImageStatus(ctx context.Context, image *voltha.OnuImage) error {
131 oo.setWaitingForResp(true)
132 select {
133 // maybe be also some outside cancel (but no context modeled for the moment ...)
134 case <-ctx.Done():
135 logger.Errorw(ctx, "waitForGetOnuImageStatus context done", log.Fields{"device-id": oo.deviceID})
136 oo.setWaitingForResp(false)
137 return fmt.Errorf("wait-for-image-status-context-done")
138 case <-time.After(oo.pDevEntry.PDevOmciCC.GetMaxOmciTimeoutWithRetries() * time.Second):
139 logger.Errorw(ctx, "waitForGetOnuImageStatus timeout", log.Fields{"device-id": oo.deviceID})
140 oo.setWaitingForResp(false)
141 return fmt.Errorf("wait-for-image-status-timeout")
142 case message, ok := <-oo.respChannel:
143 if !ok {
144 logger.Errorw(ctx, "waitForGetOnuImageStatus response error", log.Fields{"device-id": oo.deviceID})
145 oo.setWaitingForResp(false)
146 return fmt.Errorf("wait-for-image-status-response-error")
147 }
148 switch message.Type {
149 case OMCI:
150 msg, _ := message.Data.(OmciMessage)
151 oo.setWaitingForResp(false)
152 return oo.processGetOnuImageStatusResp(ctx, msg, image)
153 case TestMsg:
154 msg, _ := message.Data.(TestMessage)
155 if msg.TestMessageVal == AbortMessageProcessing {
156 logger.Info(ctx, "waitForGetOnuImageStatus abort msg received", log.Fields{"device-id": oo.deviceID})
157 oo.setWaitingForResp(false)
158 return fmt.Errorf("wait-for-image-status-abort-msg-received")
159 }
160 default:
161 logger.Errorw(ctx, "waitForGetOnuImageStatus wrong msg type received", log.Fields{"msgType": message.Type, "device-id": oo.deviceID})
162 oo.setWaitingForResp(false)
163 return fmt.Errorf("wait-for-image-status-response-error")
164 }
165 }
166 logger.Errorw(ctx, "waitForGetOnuImageStatus processing error", log.Fields{"device-id": oo.deviceID})
167 oo.setWaitingForResp(false)
168 return fmt.Errorf("wait-for-image-status-processing-error")
169
170}
171
172func (oo *OnuImageStatus) processGetOnuImageStatusResp(ctx context.Context, msg OmciMessage, image *voltha.OnuImage) error {
173 if msg.OmciMsg.MessageType != omci.GetResponseType {
174 logger.Errorw(ctx, "processGetOnuImageStatusResp wrong response type received", log.Fields{"respType": msg.OmciMsg.MessageType, "device-id": oo.deviceID})
175 return fmt.Errorf("process-image-status-response-error")
176 }
177 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
178 if msgLayer == nil {
179 logger.Errorw(ctx, "processGetOnuImageStatusResp omci Msg layer not found", log.Fields{"device-id": oo.deviceID})
180 return fmt.Errorf("process-image-status-response-error")
181 }
182 msgObj, msgOk := msgLayer.(*omci.GetResponse)
183 if !msgOk {
184 logger.Errorw(ctx, "processGetOnuImageStatusResp omci msgObj layer could not be found", log.Fields{"device-id": oo.deviceID})
185 return fmt.Errorf("process-image-status-response-error")
186 }
187 oo.mutexPLastTxMeInstance.RLock()
188 if oo.pLastTxMeInstance != nil {
189 if msgObj.EntityClass == oo.pLastTxMeInstance.GetClassID() &&
190 msgObj.EntityInstance == oo.pLastTxMeInstance.GetEntityID() {
191 oo.mutexPLastTxMeInstance.RUnlock()
Holger Hildebrandt05011352021-06-15 09:40:24 +0000192 if err := oo.processAttributesReceived(ctx, msgObj, image); err != nil {
193 logger.Errorw(ctx, err.Error(), log.Fields{"device-id": oo.deviceID})
194 return err
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000195 }
196 return nil
197 }
198 oo.mutexPLastTxMeInstance.RUnlock()
199 logger.Errorw(ctx, "processGetOnuImageStatusResp wrong MeInstance received", log.Fields{"device-id": oo.deviceID})
200 return fmt.Errorf("process-image-status-response-error")
201 }
202 oo.mutexPLastTxMeInstance.RUnlock()
203 logger.Errorw(ctx, "processGetOnuImageStatusResp pLastTxMeInstance is nil", log.Fields{"device-id": oo.deviceID})
204 return fmt.Errorf("process-image-status-response-error")
205}
Holger Hildebrandt05011352021-06-15 09:40:24 +0000206
207func (oo *OnuImageStatus) processAttributesReceived(ctx context.Context, msgObj *omci.GetResponse, image *voltha.OnuImage) error {
208 meAttributes := msgObj.Attributes
209 logger.Debugw(ctx, "processAttributesReceived", log.Fields{"attributes": meAttributes, "device-id": oo.deviceID})
210
211 for k := range oo.requestedAttributes {
212
213 if msgObj.Result != me.Success && k != cImgProductCode && k != cImgImageHash {
214 logger.Errorw(ctx, "processAttributesReceived retrieval of mandatory attributes failed",
215 log.Fields{"device-id": oo.deviceID})
216 return fmt.Errorf("process-image-status-response-error")
217 }
218 switch k {
219
220 // mandatory attributes
221 case cImgIsCommitted:
222 if meAttributes[cImgIsCommitted].(uint8) == swIsCommitted {
223 image.IsCommited = true
224 } else {
225 image.IsCommited = false
226 }
227 case cImgIsActive:
228 if meAttributes[cImgIsActive].(uint8) == swIsActive {
229 image.IsActive = true
230 } else {
231 image.IsActive = false
232 }
233 case cImgIsValid:
234 if meAttributes[cImgIsValid].(uint8) == swIsValid {
235 image.IsValid = true
236 } else {
237 image.IsValid = false
238 }
239 case cImgVersion:
240 image.Version = TrimStringFromMeOctet(meAttributes[cImgVersion])
241
242 // optional attributes
243 case cImgProductCode:
244 if msgObj.Result == me.Success {
245 image.ProductCode = TrimStringFromMeOctet(meAttributes[cImgProductCode])
246 } else {
247 sResult := msgObj.Result.String()
248 logger.Infow(ctx, "processAttributesReceived - ProductCode",
249 log.Fields{"result": sResult, "unsupported attribute mask": msgObj.UnsupportedAttributeMask, "device-id": oo.deviceID})
250 image.ProductCode = cResponse + sResult
251 }
252 case cImgImageHash:
253 if msgObj.Result == me.Success {
254 bytes, _ := me.InterfaceToOctets(meAttributes[cImgImageHash])
255 image.Hash = hex.EncodeToString(bytes)
256 } else {
257 sResult := msgObj.Result.String()
258 logger.Infow(ctx, "processAttributesReceived - ImageHash",
259 log.Fields{"result": sResult, "unsupported attribute mask": msgObj.UnsupportedAttributeMask, "device-id": oo.deviceID})
260 image.Hash = cResponse + sResult
261 }
262 }
263 }
264 return nil
265}
266
267func (oo *OnuImageStatus) updateOnuSwImageIndications(ctx context.Context, images *voltha.OnuImages) {
268
269 oo.pDevEntry.mutexOnuSwImageIndications.Lock()
270 validActiveImageFound := false
271 for i := firstSwImageMeID; i <= secondSwImageMeID; i++ {
272 if images.Items[i].IsActive && images.Items[i].IsValid {
273 oo.pDevEntry.onuSwImageIndications.activeEntityEntry.entityID = uint16(i)
274 oo.pDevEntry.onuSwImageIndications.activeEntityEntry.valid = images.Items[i].IsValid
275 oo.pDevEntry.onuSwImageIndications.activeEntityEntry.version = images.Items[i].Version
276 if images.Items[i].IsCommited {
277 oo.pDevEntry.onuSwImageIndications.activeEntityEntry.isCommitted = swIsCommitted
278 } else {
279 oo.pDevEntry.onuSwImageIndications.activeEntityEntry.isCommitted = swIsUncommitted
280 }
281 validActiveImageFound = true
282 break
283 }
284 }
285 if !validActiveImageFound {
286 oo.pDevEntry.onuSwImageIndications.activeEntityEntry.valid = false
287 }
288 validInactiveImageFound := false
289 for i := firstSwImageMeID; i <= secondSwImageMeID; i++ {
290 if !images.Items[i].IsActive && images.Items[i].IsValid {
291 oo.pDevEntry.onuSwImageIndications.inactiveEntityEntry.entityID = uint16(i)
292 oo.pDevEntry.onuSwImageIndications.inactiveEntityEntry.valid = images.Items[i].IsValid
293 oo.pDevEntry.onuSwImageIndications.inactiveEntityEntry.version = images.Items[i].Version
294 if images.Items[i].IsCommited {
295 oo.pDevEntry.onuSwImageIndications.inactiveEntityEntry.isCommitted = swIsCommitted
296 } else {
297 oo.pDevEntry.onuSwImageIndications.inactiveEntityEntry.isCommitted = swIsUncommitted
298 }
299 validInactiveImageFound = true
300 break
301 }
302 }
303 if !validInactiveImageFound {
304 oo.pDevEntry.onuSwImageIndications.inactiveEntityEntry.valid = false
305 }
306 oo.pDevEntry.mutexOnuSwImageIndications.Unlock()
307}
308
309func (oo *OnuImageStatus) updateOnuSwImagePersistentData(ctx context.Context) {
310
311 activeImageVersion := oo.pDevEntry.getActiveImageVersion(ctx)
312 oo.pDevEntry.mutexPersOnuConfig.Lock()
313 if oo.pDevEntry.sOnuPersistentData.PersActiveSwVersion != activeImageVersion {
314 logger.Infow(ctx, "Active SW version has been changed at ONU - update persistent data",
315 log.Fields{"old version": oo.pDevEntry.sOnuPersistentData.PersActiveSwVersion,
316 "new version": activeImageVersion, "device-id": oo.deviceID})
317 oo.pDevEntry.sOnuPersistentData.PersActiveSwVersion = activeImageVersion
318 oo.pDevEntry.mutexPersOnuConfig.Unlock()
319 if err := oo.pDevEntry.baseDeviceHandler.storePersistentData(ctx); err != nil {
320 logger.Warnw(ctx, "store persistent data error - continue for now as there will be additional write attempts",
321 log.Fields{"device-id": oo.deviceID, "err": err})
322 }
323 return
324 }
325 oo.pDevEntry.mutexPersOnuConfig.Unlock()
326}
327
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000328func (oo *OnuImageStatus) setWaitingForResp(value bool) {
329 oo.mutexWaitingForResp.Lock()
330 oo.waitingForResp = value
331 oo.mutexWaitingForResp.Unlock()
332}
Holger Hildebrandt05011352021-06-15 09:40:24 +0000333
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000334func (oo *OnuImageStatus) isWaitingForResp() bool {
335 oo.mutexWaitingForResp.RLock()
336 value := oo.waitingForResp
337 oo.mutexWaitingForResp.RUnlock()
338 return value
339}
340
341//CancelProcessing ensures that interrupted processing is canceled while waiting for a response
342func (oo *OnuImageStatus) CancelProcessing(ctx context.Context) {
343 if oo.isWaitingForResp() {
344 abortMsg := Message{
345 Type: TestMsg,
346 Data: TestMessage{
347 TestMessageVal: AbortMessageProcessing,
348 },
349 }
350 oo.respChannel <- abortMsg
351 }
352}