@@ -79,7 +79,7 @@ public AssetAdministrationShell getAas(String shellId) throws ElementDoesNotExis
7979
8080 @ Override
8181 public void createAas (AssetAdministrationShell shell ) throws CollidingIdentifierException {
82- AssetAdministrationShellDescriptor descriptor = new AasDescriptorFactory ( aasRepositoryRegistryLink . getAasRepositoryBaseURLs (), attributeMapper ). create (shell );
82+ AssetAdministrationShellDescriptor descriptor = createDescriptor (shell );
8383
8484 decorated .createAas (shell );
8585
@@ -96,7 +96,16 @@ public void createAas(AssetAdministrationShell shell) throws CollidingIdentifier
9696
9797 @ Override
9898 public void updateAas (String shellId , AssetAdministrationShell shell ) {
99+ AssetAdministrationShell previousShell = decorated .getAas (shellId );
100+
99101 decorated .updateAas (shellId , shell );
102+
103+ try {
104+ updateDescriptor (shellId , shell );
105+ } catch (RuntimeException e ) {
106+ rollbackAasUpdate (shellId , previousShell , e );
107+ throw e ;
108+ }
100109 }
101110
102111 @ Override
@@ -128,7 +137,16 @@ public void removeSubmodelReference(String shellId, String submodelId) {
128137
129138 @ Override
130139 public void setAssetInformation (String shellId , AssetInformation shellInfo ) throws ElementDoesNotExistException {
140+ AssetInformation previousAssetInformation = decorated .getAssetInformation (shellId );
141+
131142 decorated .setAssetInformation (shellId , shellInfo );
143+
144+ try {
145+ updateDescriptor (shellId , decorated .getAas (shellId ));
146+ } catch (RuntimeException e ) {
147+ rollbackAssetInformationUpdate (shellId , previousAssetInformation , e );
148+ throw e ;
149+ }
132150 }
133151
134152 @ Override
@@ -144,7 +162,61 @@ private void registerAas(AssetAdministrationShellDescriptor descriptor) {
144162
145163 logger .info ("Shell '{}' has been automatically linked with the Registry" , descriptor .getId ());
146164 } catch (ApiException e ) {
147- throw new RepositoryRegistryLinkException (descriptor .getId (), e );
165+ throw createRegistryLinkException (descriptor .getId (), "creation" , e );
166+ }
167+ }
168+
169+ private void updateDescriptor (String shellId , AssetAdministrationShell shell ) {
170+ RegistryAndDiscoveryInterfaceApi registryApi = aasRepositoryRegistryLink .getRegistryApi ();
171+
172+ try {
173+ AssetAdministrationShellDescriptor existingDescriptor = registryApi .getAssetAdministrationShellDescriptorById (shellId );
174+ AssetAdministrationShellDescriptor updatedDescriptor = createDescriptor (shell );
175+ updatedDescriptor .setSubmodelDescriptors (existingDescriptor .getSubmodelDescriptors ());
176+
177+ registryApi .putAssetAdministrationShellDescriptorById (shellId , updatedDescriptor );
178+
179+ logger .info ("Shell descriptor '{}' has been automatically updated in the Registry" , shellId );
180+ } catch (ApiException e ) {
181+ throw createRegistryLinkException (shellId , "update" , e );
182+ }
183+ }
184+
185+ private AssetAdministrationShellDescriptor createDescriptor (AssetAdministrationShell shell ) {
186+ return new AasDescriptorFactory (aasRepositoryRegistryLink .getAasRepositoryBaseURLs (), attributeMapper ).create (shell );
187+ }
188+
189+ private RepositoryRegistryLinkException createRegistryLinkException (String shellId , String operation , ApiException cause ) {
190+ StringBuilder details = new StringBuilder ("Registry descriptor " ).append (operation ).append (" failed" );
191+
192+ if (cause .getCode () > 0 )
193+ details .append (" with HTTP status " ).append (cause .getCode ());
194+
195+ String responseDetails = cause .getResponseBody ();
196+ if (responseDetails == null || responseDetails .isBlank ())
197+ responseDetails = cause .getMessage ();
198+
199+ if (responseDetails != null && !responseDetails .isBlank ())
200+ details .append (": " ).append (responseDetails );
201+
202+ return new RepositoryRegistryLinkException (shellId , details .toString (), cause );
203+ }
204+
205+ private void rollbackAasUpdate (String shellId , AssetAdministrationShell previousShell , RuntimeException updateException ) {
206+ try {
207+ decorated .updateAas (shellId , previousShell );
208+ } catch (RuntimeException rollbackException ) {
209+ updateException .addSuppressed (rollbackException );
210+ logger .error ("Unable to restore AAS '{}' after Registry synchronization failed." , shellId , rollbackException );
211+ }
212+ }
213+
214+ private void rollbackAssetInformationUpdate (String shellId , AssetInformation previousAssetInformation , RuntimeException updateException ) {
215+ try {
216+ decorated .setAssetInformation (shellId , previousAssetInformation );
217+ } catch (RuntimeException rollbackException ) {
218+ updateException .addSuppressed (rollbackException );
219+ logger .error ("Unable to restore Asset Information for AAS '{}' after Registry synchronization failed." , shellId , rollbackException );
148220 }
149221 }
150222
0 commit comments