github.com/enbility/spine-go@v0.7.0/model/nodemanagement_additions.go (about) 1 package model 2 3 import ( 4 "reflect" 5 "sync" 6 7 "github.com/enbility/spine-go/util" 8 ) 9 10 var nmMux sync.Mutex 11 12 // NodeManagementDestinationListDataType 13 14 var _ Updater = (*NodeManagementDestinationListDataType)(nil) 15 16 func (r *NodeManagementDestinationListDataType) UpdateList(remoteWrite, persist bool, newList any, filterPartial, filterDelete *FilterType) (any, bool) { 17 var newData []NodeManagementDestinationDataType 18 if newList != nil { 19 newData = newList.(*NodeManagementDestinationListDataType).NodeManagementDestinationData 20 } 21 22 data, success := UpdateList(remoteWrite, r.NodeManagementDestinationData, newData, filterPartial, filterDelete) 23 24 if success && persist { 25 r.NodeManagementDestinationData = data 26 } 27 28 return data, success 29 } 30 31 // NodeManagementUseCaseDataType 32 33 // find the matching UseCaseInformation index for 34 // a given FeatureAddressType, UseCaseActorType and UseCaseNameType 35 // 36 // if UseCaseActorType and UseCaseNameType are empty they are ignored, 37 // and the first matching UseCaseInformation item is returned 38 func (n *NodeManagementUseCaseDataType) useCaseInformationIndex( 39 address FeatureAddressType, 40 actor UseCaseActorType, 41 useCaseName UseCaseNameType, 42 ) (int, bool) { 43 // get the element with the same entity 44 for index, item := range n.UseCaseInformation { 45 if item.Address.Device == nil || 46 item.Address.Entity == nil || 47 !reflect.DeepEqual(item.Address.Device, address.Device) || 48 !reflect.DeepEqual(item.Address.Entity, address.Entity) { 49 continue 50 } 51 52 if len(actor) == 0 && len(useCaseName) == 0 { 53 return index, true 54 } 55 56 if len(actor) > 0 { 57 if item.Actor == nil || *item.Actor != actor { 58 continue 59 } 60 } 61 62 if len(useCaseName) == 0 { 63 return index, true 64 } 65 66 if _, ok := item.useCaseSupportIndex(useCaseName); ok { 67 return index, true 68 } 69 } 70 71 return -1, false 72 } 73 74 // add a new UseCaseSupportType 75 func (n *NodeManagementUseCaseDataType) AddUseCaseSupport( 76 address FeatureAddressType, 77 actor UseCaseActorType, 78 useCaseName UseCaseNameType, 79 useCaseVersion SpecificationVersionType, 80 useCaseDocumemtSubRevision string, 81 useCaseAvailable bool, 82 scenarios []UseCaseScenarioSupportType, 83 ) { 84 nmMux.Lock() 85 defer nmMux.Unlock() 86 87 useCaseSupport := UseCaseSupportType{ 88 UseCaseName: &useCaseName, 89 UseCaseVersion: &useCaseVersion, 90 UseCaseAvailable: &useCaseAvailable, 91 ScenarioSupport: scenarios, 92 UseCaseDocumentSubRevision: &useCaseDocumemtSubRevision, 93 } 94 95 // is there an entry for the entity address and actor 96 usecaseIndex, ok := n.useCaseInformationIndex(address, actor, "") 97 98 if ok { 99 n.UseCaseInformation[usecaseIndex].Add(useCaseSupport) 100 } else { 101 // create a new element for this entity 102 useCaseInformation := UseCaseInformationDataType{ 103 Address: &FeatureAddressType{ 104 Device: address.Device, 105 Entity: address.Entity, 106 }, 107 Actor: &actor, 108 UseCaseSupport: []UseCaseSupportType{useCaseSupport}, 109 } 110 n.UseCaseInformation = append(n.UseCaseInformation, useCaseInformation) 111 } 112 } 113 114 func (n *NodeManagementUseCaseDataType) HasUseCaseSupport( 115 address FeatureAddressType, 116 actor UseCaseActorType, 117 useCaseName UseCaseNameType) bool { 118 nmMux.Lock() 119 defer nmMux.Unlock() 120 121 // is there an entry for the entity address, actor and usecase name 122 _, ok := n.useCaseInformationIndex(address, actor, useCaseName) 123 return ok 124 } 125 126 // Set the availability of a usecase 127 func (n *NodeManagementUseCaseDataType) SetAvailability( 128 address FeatureAddressType, 129 actor UseCaseActorType, 130 useCaseName UseCaseNameType, 131 availability bool, 132 ) { 133 nmMux.Lock() 134 defer nmMux.Unlock() 135 136 // is there an entry for the entity address, actor and usecase name 137 usecaseIndex, ok := n.useCaseInformationIndex(address, actor, useCaseName) 138 if !ok { 139 return 140 } 141 142 useCaseInformation := n.UseCaseInformation[usecaseIndex] 143 for index, item := range useCaseInformation.UseCaseSupport { 144 if item.UseCaseName != nil && *item.UseCaseName == useCaseName { 145 n.UseCaseInformation[usecaseIndex].UseCaseSupport[index].UseCaseAvailable = util.Ptr(availability) 146 147 return 148 } 149 } 150 } 151 152 // Remove a UseCaseSupportType with 153 // a provided FeatureAddressType, UseCaseActorType and UseCaseNameType 154 func (n *NodeManagementUseCaseDataType) RemoveUseCaseSupport( 155 address FeatureAddressType, 156 actor UseCaseActorType, 157 useCaseName UseCaseNameType, 158 ) { 159 nmMux.Lock() 160 defer nmMux.Unlock() 161 162 // is there an entry for the entity address, actor and usecase name 163 usecaseIndex, ok := n.useCaseInformationIndex(address, actor, useCaseName) 164 if !ok { 165 return 166 } 167 168 var usecaseInfo []UseCaseInformationDataType 169 170 for index, item := range n.UseCaseInformation { 171 if index != usecaseIndex { 172 usecaseInfo = append(usecaseInfo, item) 173 continue 174 } 175 176 item.Remove(useCaseName) 177 178 // only add the item if there are any usecases left 179 if len(item.UseCaseSupport) == 0 { 180 continue 181 } 182 183 usecaseInfo = append(usecaseInfo, item) 184 } 185 186 n.UseCaseInformation = usecaseInfo 187 } 188 189 // Remove all data for a given address type 190 func (n *NodeManagementUseCaseDataType) RemoveUseCaseDataForAddress(address FeatureAddressType) { 191 nmMux.Lock() 192 defer nmMux.Unlock() 193 194 var usecaseInfo []UseCaseInformationDataType 195 196 for _, item := range n.UseCaseInformation { 197 if !reflect.DeepEqual(item.Address, &address) { 198 usecaseInfo = append(usecaseInfo, item) 199 continue 200 } 201 } 202 203 n.UseCaseInformation = usecaseInfo 204 }