github.com/onosproject/onos-api/go@v0.10.32/onos/uenib/uenib.go (about) 1 // SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org> 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package uenib 6 7 import ( 8 "bytes" 9 "errors" 10 "github.com/gogo/protobuf/jsonpb" 11 "github.com/gogo/protobuf/proto" 12 "github.com/gogo/protobuf/types" 13 "google.golang.org/grpc" 14 ) 15 16 // ID ... 17 type ID string 18 19 // NullID ... 20 const NullID = "" 21 22 // Revision is an object revision 23 type Revision uint64 24 25 // UEServiceClientFactory : Default UEServiceClient creation. 26 var UEServiceClientFactory = func(cc *grpc.ClientConn) UEServiceClient { 27 return NewUEServiceClient(cc) 28 } 29 30 // CreateUEServiceClient creates and returns a new UE service client 31 func CreateUEServiceClient(cc *grpc.ClientConn) UEServiceClient { 32 return UEServiceClientFactory(cc) 33 } 34 35 // GetAspect retrieves the specified aspect value from the given UE. 36 func (ue *UE) GetAspect(destValue proto.Message) error { 37 if ue.Aspects == nil { 38 return errors.New("aspect not found") 39 } 40 aspectType := proto.MessageName(destValue) 41 any := ue.Aspects[aspectType] 42 if any == nil { 43 return errors.New("aspect not found") 44 } 45 if any.TypeUrl != aspectType { 46 return errors.New("unexpected aspect type") 47 } 48 reader := bytes.NewReader(any.Value) 49 err := jsonpb.Unmarshal(reader, destValue) 50 if err != nil { 51 return err 52 } 53 return nil 54 } 55 56 // GetAspectBytes applies the specified aspect as raw JSON bytes to the given UE. 57 func (ue *UE) GetAspectBytes(aspectType string) ([]byte, error) { 58 if ue.Aspects == nil { 59 return nil, errors.New("aspect not found") 60 } 61 any := ue.Aspects[aspectType] 62 if any == nil { 63 return nil, errors.New("aspect not found") 64 } 65 return any.Value, nil 66 } 67 68 // SetAspect applies the specified aspect value to the given ueect. 69 func (ue *UE) SetAspect(value proto.Message) error { 70 jm := jsonpb.Marshaler{} 71 writer := bytes.Buffer{} 72 err := jm.Marshal(&writer, value) 73 if err != nil { 74 return err 75 } 76 if ue.Aspects == nil { 77 ue.Aspects = make(map[string]*types.Any) 78 } 79 ue.Aspects[proto.MessageName(value)] = &types.Any{ 80 TypeUrl: proto.MessageName(value), 81 Value: writer.Bytes(), 82 } 83 return nil 84 } 85 86 // SetAspectBytes applies the specified aspect as raw JSON bytes to the given UE. 87 func (ue *UE) SetAspectBytes(aspectType string, jsonValue []byte) error { 88 any := &types.Any { 89 TypeUrl: aspectType, 90 Value: jsonValue, 91 } 92 if ue.Aspects == nil { 93 ue.Aspects = make(map[string]*types.Any) 94 } 95 ue.Aspects[aspectType] = any 96 return nil 97 }