Skip to content

Commit b53d44b

Browse files
committed
Fixes possible NPE in DiscoveryIntegration
1 parent a84f97b commit b53d44b

2 files changed

Lines changed: 33 additions & 10 deletions

File tree

basyx.aasrepository/basyx.aasrepository-feature-discovery-integration/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/feature/discoveryintegration/DiscoveryIntegrationAasRepository.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
* distribute, sublicense, and/or sell copies of the Software, and to
99
* permit persons to whom the Software is furnished to do so, subject to
1010
* the following conditions:
11-
*
11+
*
1212
* The above copyright notice and this permission notice shall be
1313
* included in all copies or substantial portions of the Software.
14-
*
14+
*
1515
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1616
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1717
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -58,7 +58,7 @@ public class DiscoveryIntegrationAasRepository implements AasRepository {
5858
private AasRepository decorated;
5959

6060
private final AasDiscoveryService discoveryApi;
61-
61+
6262
public DiscoveryIntegrationAasRepository(AasRepository decorated, AasDiscoveryService discoveryApi) {
6363
this.decorated = decorated;
6464
this.discoveryApi = discoveryApi;
@@ -94,6 +94,7 @@ public void deleteAas(String shellId) {
9494
try {
9595
discoveryApi.deleteAllAssetLinksById(shellId);
9696
} catch (Exception e){
97+
logger.error("Failed to unlink asset in discovery service for AAS ID {}", shellId, e);
9798
throw new RepositoryDiscoveryUnlinkException(shellId, e);
9899
}
99100
}
@@ -162,30 +163,52 @@ private void updateAssetLinks(String shellId, AssetAdministrationShell shell) {
162163
}
163164

164165
private static DefaultSpecificAssetId getGlobalAssetIdAsSpecificAssetId(AssetAdministrationShell shell) {
165-
return new DefaultSpecificAssetId.Builder().name("globalAssetId").value(shell.getAssetInformation().getGlobalAssetId()).build();
166+
if (shell.getAssetInformation() == null || shell.getAssetInformation().getGlobalAssetId() == null)
167+
throw new IllegalArgumentException("AssetInformation or GlobalAssetId is null");
168+
169+
return new DefaultSpecificAssetId.Builder()
170+
.name("globalAssetId")
171+
.value(shell.getAssetInformation().getGlobalAssetId())
172+
.build();
166173
}
167174

168175
private void createAssetLinksOnDiscoveryServiceIfNecessary(AssetAdministrationShell shell) {
169-
List<SpecificAssetId> linksToAdd = new ArrayList<>(shell.getAssetInformation().getSpecificAssetIds());
170-
if(shell.getAssetInformation().getGlobalAssetId() != null){
176+
if (shell.getAssetInformation() == null) return;
177+
178+
List<SpecificAssetId> linksToAdd;
179+
if (shell.getAssetInformation().getSpecificAssetIds() != null) {
180+
linksToAdd = new ArrayList<>(shell.getAssetInformation().getSpecificAssetIds());
181+
} else {
182+
linksToAdd = new ArrayList<>();
183+
}
184+
185+
if (shell.getAssetInformation().getGlobalAssetId() != null) {
171186
linksToAdd.add(getGlobalAssetIdAsSpecificAssetId(shell));
172187
}
173-
if(!linksToAdd.isEmpty()){
188+
189+
if(!linksToAdd.isEmpty()) {
174190
try {
175191
discoveryApi.createAllAssetLinksById(shell.getId(), linksToAdd);
176192
} catch (Exception e) {
177193
decorated.deleteAas(shell.getId());
194+
logger.error("Failed to link asset in discovery service for AAS ID {}", shell.getId(), e);
178195
throw new RepositoryDiscoveryLinkException(shell.getId(), e);
179196
}
180197
}
181198
}
182199

183200
private static boolean specificAssetIdsUpdated(AssetAdministrationShell shell, AssetAdministrationShell oldShell) {
184-
return !(oldShell.getAssetInformation().getSpecificAssetIds() != null && oldShell.getAssetInformation().getSpecificAssetIds().equals(shell.getAssetInformation().getSpecificAssetIds()));
201+
return !(oldShell.getAssetInformation() != null &&
202+
oldShell.getAssetInformation().getSpecificAssetIds() != null &&
203+
oldShell.getAssetInformation().getSpecificAssetIds().equals(
204+
shell.getAssetInformation().getSpecificAssetIds()));
185205
}
186206

187207
private static boolean isGlobalAssetIdUpdated(AssetAdministrationShell shell, AssetAdministrationShell oldShell) {
188-
return !(oldShell.getAssetInformation().getGlobalAssetId() != null && oldShell.getAssetInformation().getGlobalAssetId().equals(shell.getAssetInformation().getGlobalAssetId()));
208+
return !(oldShell.getAssetInformation() != null &&
209+
oldShell.getAssetInformation().getGlobalAssetId() != null &&
210+
oldShell.getAssetInformation().getGlobalAssetId().equals(
211+
shell.getAssetInformation().getGlobalAssetId()));
189212
}
190213

191214
}

basyx.aasrepository/basyx.aasrepository-feature-discovery-integration/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/feature/discoveryintegration/AuthorizedAasRepositoryDiscoveryIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void resetRepository() {
6161

6262
@Override
6363
protected AasDiscoveryService getDiscoveryService() {
64-
return new ConnectedAasDiscoveryService("http://localhost:8049");
64+
return new ConnectedAasDiscoveryService("http://localhost:8081");
6565
}
6666

6767
@Override

0 commit comments

Comments
 (0)