khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | */ |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 16 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "crypto/md5" |
| 22 | "encoding/json" |
| 23 | "fmt" |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 24 | "reflect" |
| 25 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 26 | "github.com/golang/protobuf/proto" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 27 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 28 | ) |
| 29 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 30 | // DataRevision stores the data associated to a revision along with its calculated checksum hash value |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 31 | type DataRevision struct { |
| 32 | Data interface{} |
| 33 | Hash string |
| 34 | } |
| 35 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 36 | // NewDataRevision creates a new instance of a DataRevision structure |
| 37 | func NewDataRevision(root *root, data interface{}) *DataRevision { |
| 38 | dr := DataRevision{} |
| 39 | dr.Data = data |
| 40 | dr.Hash = dr.hashData(root, data) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 41 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 42 | return &dr |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 43 | } |
| 44 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 45 | func (dr *DataRevision) hashData(root *root, data interface{}) string { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 46 | var buffer bytes.Buffer |
| 47 | |
| 48 | if IsProtoMessage(data) { |
| 49 | if pbdata, err := proto.Marshal(data.(proto.Message)); err != nil { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 50 | log.Debugf("problem to marshal protobuf data --> err: %s", err.Error()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 51 | } else { |
| 52 | buffer.Write(pbdata) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 53 | // To ensure uniqueness in case data is nil, also include data type |
| 54 | buffer.Write([]byte(reflect.TypeOf(data).String())) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | } else if reflect.ValueOf(data).IsValid() { |
| 58 | dataObj := reflect.New(reflect.TypeOf(data).Elem()) |
| 59 | if json, err := json.Marshal(dataObj.Interface()); err != nil { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 60 | log.Debugf("problem to marshal data --> err: %s", err.Error()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 61 | } else { |
| 62 | buffer.Write(json) |
| 63 | } |
| 64 | } else { |
| 65 | dataObj := reflect.New(reflect.TypeOf(data).Elem()) |
| 66 | buffer.Write(dataObj.Bytes()) |
| 67 | } |
| 68 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 69 | // Add the root pointer that owns the current data for extra uniqueness |
| 70 | rootPtr := fmt.Sprintf("%p", root) |
| 71 | buffer.Write([]byte(rootPtr)) |
| 72 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 73 | return fmt.Sprintf("%x", md5.Sum(buffer.Bytes()))[:12] |
| 74 | } |