blob: d4b6456ed3e0071b392e118b2bcd8fba1788fda1 [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"
Girish Gowdra50e56422021-06-01 16:46:04 -070029 "github.com/opencord/voltha-lib-go/v5/pkg/log"
Holger Hildebrandtfb402a62021-05-26 14:40:49 +000030 "github.com/opencord/voltha-protos/v4/go/voltha"
31)
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.updateOnuSwImagePersistentData(ctx)
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000107 return &images, nil
108}
109
110func (oo *OnuImageStatus) requestOnuImageAttributes(ctx context.Context, imageID uint16, image *voltha.OnuImage) error {
111 oo.mutexPLastTxMeInstance.Lock()
112 meInstance, err := oo.pDevEntry.PDevOmciCC.sendGetMe(log.WithSpanFromContext(context.TODO(), ctx), me.SoftwareImageClassID,
113 imageID, oo.requestedAttributes, oo.pDevEntry.pOpenOnuAc.omciTimeout, true, oo.respChannel)
114 if err != nil {
115 oo.mutexPLastTxMeInstance.Unlock()
116 logger.Errorw(ctx, "can't send omci request to get data for image id", log.Fields{"image-id": imageID, "device-id": oo.deviceID})
117 return fmt.Errorf("can't-send-omci-request-to-get-data-for-image-id-%d", imageID)
118 }
119 oo.pLastTxMeInstance = meInstance
120 oo.mutexPLastTxMeInstance.Unlock()
121
122 if err = oo.waitForGetOnuImageStatus(ctx, image); err != nil {
123 logger.Errorw(ctx, err.Error(), log.Fields{"device-id": oo.deviceID})
124 return err
125 }
126 return nil
127}
128
129func (oo *OnuImageStatus) waitForGetOnuImageStatus(ctx context.Context, image *voltha.OnuImage) error {
130 oo.setWaitingForResp(true)
131 select {
132 // maybe be also some outside cancel (but no context modeled for the moment ...)
133 case <-ctx.Done():
134 logger.Errorw(ctx, "waitForGetOnuImageStatus context done", log.Fields{"device-id": oo.deviceID})
135 oo.setWaitingForResp(false)
136 return fmt.Errorf("wait-for-image-status-context-done")
137 case <-time.After(oo.pDevEntry.PDevOmciCC.GetMaxOmciTimeoutWithRetries() * time.Second):
138 logger.Errorw(ctx, "waitForGetOnuImageStatus timeout", log.Fields{"device-id": oo.deviceID})
139 oo.setWaitingForResp(false)
140 return fmt.Errorf("wait-for-image-status-timeout")
141 case message, ok := <-oo.respChannel:
142 if !ok {
143 logger.Errorw(ctx, "waitForGetOnuImageStatus response error", log.Fields{"device-id": oo.deviceID})
144 oo.setWaitingForResp(false)
145 return fmt.Errorf("wait-for-image-status-response-error")
146 }
147 switch message.Type {
148 case OMCI:
149 msg, _ := message.Data.(OmciMessage)
150 oo.setWaitingForResp(false)
151 return oo.processGetOnuImageStatusResp(ctx, msg, image)
152 case TestMsg:
153 msg, _ := message.Data.(TestMessage)
154 if msg.TestMessageVal == AbortMessageProcessing {
155 logger.Info(ctx, "waitForGetOnuImageStatus abort msg received", log.Fields{"device-id": oo.deviceID})
156 oo.setWaitingForResp(false)
157 return fmt.Errorf("wait-for-image-status-abort-msg-received")
158 }
159 default:
160 logger.Errorw(ctx, "waitForGetOnuImageStatus wrong msg type received", log.Fields{"msgType": message.Type, "device-id": oo.deviceID})
161 oo.setWaitingForResp(false)
162 return fmt.Errorf("wait-for-image-status-response-error")
163 }
164 }
165 logger.Errorw(ctx, "waitForGetOnuImageStatus processing error", log.Fields{"device-id": oo.deviceID})
166 oo.setWaitingForResp(false)
167 return fmt.Errorf("wait-for-image-status-processing-error")
168
169}
170
171func (oo *OnuImageStatus) processGetOnuImageStatusResp(ctx context.Context, msg OmciMessage, image *voltha.OnuImage) error {
172 if msg.OmciMsg.MessageType != omci.GetResponseType {
173 logger.Errorw(ctx, "processGetOnuImageStatusResp wrong response type received", log.Fields{"respType": msg.OmciMsg.MessageType, "device-id": oo.deviceID})
174 return fmt.Errorf("process-image-status-response-error")
175 }
176 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
177 if msgLayer == nil {
178 logger.Errorw(ctx, "processGetOnuImageStatusResp omci Msg layer not found", log.Fields{"device-id": oo.deviceID})
179 return fmt.Errorf("process-image-status-response-error")
180 }
181 msgObj, msgOk := msgLayer.(*omci.GetResponse)
182 if !msgOk {
183 logger.Errorw(ctx, "processGetOnuImageStatusResp omci msgObj layer could not be found", log.Fields{"device-id": oo.deviceID})
184 return fmt.Errorf("process-image-status-response-error")
185 }
186 oo.mutexPLastTxMeInstance.RLock()
187 if oo.pLastTxMeInstance != nil {
188 if msgObj.EntityClass == oo.pLastTxMeInstance.GetClassID() &&
189 msgObj.EntityInstance == oo.pLastTxMeInstance.GetEntityID() {
190 oo.mutexPLastTxMeInstance.RUnlock()
Holger Hildebrandt05011352021-06-15 09:40:24 +0000191 if err := oo.processAttributesReceived(ctx, msgObj, image); err != nil {
192 logger.Errorw(ctx, err.Error(), log.Fields{"device-id": oo.deviceID})
193 return err
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000194 }
195 return nil
196 }
197 oo.mutexPLastTxMeInstance.RUnlock()
198 logger.Errorw(ctx, "processGetOnuImageStatusResp wrong MeInstance received", log.Fields{"device-id": oo.deviceID})
199 return fmt.Errorf("process-image-status-response-error")
200 }
201 oo.mutexPLastTxMeInstance.RUnlock()
202 logger.Errorw(ctx, "processGetOnuImageStatusResp pLastTxMeInstance is nil", log.Fields{"device-id": oo.deviceID})
203 return fmt.Errorf("process-image-status-response-error")
204}
Holger Hildebrandt05011352021-06-15 09:40:24 +0000205
206func (oo *OnuImageStatus) processAttributesReceived(ctx context.Context, msgObj *omci.GetResponse, image *voltha.OnuImage) error {
207 meAttributes := msgObj.Attributes
208 logger.Debugw(ctx, "processAttributesReceived", log.Fields{"attributes": meAttributes, "device-id": oo.deviceID})
209
Holger Hildebrandt7d869982021-12-20 10:49:54 +0000210 if _, ok := oo.requestedAttributes[cImgVersion]; ok {
211 if msgObj.Result != me.Success {
Holger Hildebrandt05011352021-06-15 09:40:24 +0000212 logger.Errorw(ctx, "processAttributesReceived retrieval of mandatory attributes failed",
213 log.Fields{"device-id": oo.deviceID})
214 return fmt.Errorf("process-image-status-response-error")
215 }
Holger Hildebrandt7d869982021-12-20 10:49:54 +0000216 oo.pDevEntry.handleSwImageIndications(ctx, msgObj.EntityInstance, meAttributes)
217 }
218 for k := range oo.requestedAttributes {
Holger Hildebrandt05011352021-06-15 09:40:24 +0000219
Holger Hildebrandt7d869982021-12-20 10:49:54 +0000220 switch k {
Holger Hildebrandt05011352021-06-15 09:40:24 +0000221 // mandatory attributes
222 case cImgIsCommitted:
223 if meAttributes[cImgIsCommitted].(uint8) == swIsCommitted {
224 image.IsCommited = true
225 } else {
226 image.IsCommited = false
227 }
228 case cImgIsActive:
229 if meAttributes[cImgIsActive].(uint8) == swIsActive {
230 image.IsActive = true
231 } else {
232 image.IsActive = false
233 }
234 case cImgIsValid:
235 if meAttributes[cImgIsValid].(uint8) == swIsValid {
236 image.IsValid = true
237 } else {
238 image.IsValid = false
239 }
240 case cImgVersion:
241 image.Version = TrimStringFromMeOctet(meAttributes[cImgVersion])
242
243 // optional attributes
244 case cImgProductCode:
245 if msgObj.Result == me.Success {
246 image.ProductCode = TrimStringFromMeOctet(meAttributes[cImgProductCode])
247 } else {
248 sResult := msgObj.Result.String()
249 logger.Infow(ctx, "processAttributesReceived - ProductCode",
250 log.Fields{"result": sResult, "unsupported attribute mask": msgObj.UnsupportedAttributeMask, "device-id": oo.deviceID})
251 image.ProductCode = cResponse + sResult
252 }
253 case cImgImageHash:
254 if msgObj.Result == me.Success {
255 bytes, _ := me.InterfaceToOctets(meAttributes[cImgImageHash])
256 image.Hash = hex.EncodeToString(bytes)
257 } else {
258 sResult := msgObj.Result.String()
259 logger.Infow(ctx, "processAttributesReceived - ImageHash",
260 log.Fields{"result": sResult, "unsupported attribute mask": msgObj.UnsupportedAttributeMask, "device-id": oo.deviceID})
261 image.Hash = cResponse + sResult
262 }
263 }
264 }
265 return nil
266}
267
Holger Hildebrandt05011352021-06-15 09:40:24 +0000268func (oo *OnuImageStatus) updateOnuSwImagePersistentData(ctx context.Context) {
269
270 activeImageVersion := oo.pDevEntry.getActiveImageVersion(ctx)
271 oo.pDevEntry.mutexPersOnuConfig.Lock()
272 if oo.pDevEntry.sOnuPersistentData.PersActiveSwVersion != activeImageVersion {
273 logger.Infow(ctx, "Active SW version has been changed at ONU - update persistent data",
274 log.Fields{"old version": oo.pDevEntry.sOnuPersistentData.PersActiveSwVersion,
275 "new version": activeImageVersion, "device-id": oo.deviceID})
276 oo.pDevEntry.sOnuPersistentData.PersActiveSwVersion = activeImageVersion
277 oo.pDevEntry.mutexPersOnuConfig.Unlock()
278 if err := oo.pDevEntry.baseDeviceHandler.storePersistentData(ctx); err != nil {
279 logger.Warnw(ctx, "store persistent data error - continue for now as there will be additional write attempts",
280 log.Fields{"device-id": oo.deviceID, "err": err})
281 }
282 return
283 }
284 oo.pDevEntry.mutexPersOnuConfig.Unlock()
285}
286
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000287func (oo *OnuImageStatus) setWaitingForResp(value bool) {
288 oo.mutexWaitingForResp.Lock()
289 oo.waitingForResp = value
290 oo.mutexWaitingForResp.Unlock()
291}
Holger Hildebrandt05011352021-06-15 09:40:24 +0000292
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000293func (oo *OnuImageStatus) isWaitingForResp() bool {
294 oo.mutexWaitingForResp.RLock()
295 value := oo.waitingForResp
296 oo.mutexWaitingForResp.RUnlock()
297 return value
298}
299
300//CancelProcessing ensures that interrupted processing is canceled while waiting for a response
301func (oo *OnuImageStatus) CancelProcessing(ctx context.Context) {
302 if oo.isWaitingForResp() {
303 abortMsg := Message{
304 Type: TestMsg,
305 Data: TestMessage{
306 TestMessageVal: AbortMessageProcessing,
307 },
308 }
309 oo.respChannel <- abortMsg
310 }
311}