Skip to main content
Jump to: navigation, search

Talk:BaSyx / Documentation / Components / Registry

Authorized Registry Example

This example scenario demonstrating a deployment with two servers with Authorized AAS Registry. Server A is created as an empty server in the cloud. An AAS and a Submodel is pushed to it. Server B is created as a server hosted near a machine. It provides a Submodel containing sensor value. Note : Keycloak instance should be running before working with this scenario. Keycloak instance running on : 127.0.0.1:9006 KEYCLOAK_USER=admin KEYCLOAK_PASSWORD=admin

  1. /*******************************************************************************
  2.  * Copyright (C) 2022 the Eclipse BaSyx Authors
  3.  * 
  4.  * This program and the accompanying materials are made
  5.  * available under the terms of the Eclipse Public License 2.0
  6.  * which is available at https://www.eclipse.org/legal/epl-2.0/
  7.  * 
  8.  * SPDX-License-Identifier: EPL-2.0
  9.  ******************************************************************************/
  10. package org.eclipse.basyx.examples.scenarios.authorization;
  11.  
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import org.eclipse.basyx.aas.manager.ConnectedAssetAdministrationShellManager;
  15. import org.eclipse.basyx.aas.registration.api.IAASRegistry;
  16. import org.eclipse.basyx.components.IComponent;
  17. import org.eclipse.basyx.components.aas.AASServerComponent;
  18. import org.eclipse.basyx.components.aas.configuration.AASServerBackend;
  19. import org.eclipse.basyx.components.aas.configuration.BaSyxAASServerConfiguration;
  20. import org.eclipse.basyx.components.configuration.BaSyxContextConfiguration;
  21. import org.eclipse.basyx.components.registry.RegistryComponent;
  22. import org.eclipse.basyx.components.registry.configuration.BaSyxRegistryConfiguration;
  23. import org.eclipse.basyx.components.registry.configuration.RegistryBackend;
  24. import org.eclipse.basyx.components.servlet.submodel.SubmodelServlet;
  25. import org.eclipse.basyx.extensions.aas.registration.authorization.AuthorizedAASRegistryProxy;
  26. import org.eclipse.basyx.submodel.metamodel.api.identifier.IIdentifier;
  27. import org.eclipse.basyx.submodel.metamodel.map.Submodel;
  28. import org.eclipse.basyx.vab.protocol.http.server.BaSyxHTTPServer;
  29. import org.eclipse.basyx.vab.protocol.api.IConnectorFactory;
  30. import org.eclipse.basyx.vab.protocol.http.connector.HTTPConnectorFactory;
  31. import org.eclipse.basyx.vab.protocol.http.server.BaSyxContext;
  32.  
  33. public class AuthorizedRegistryScenario {
  34. 		private static final String CLOUD_ENDPOINT = "http://localhost:8081/cloud";
  35. 		protected static final String REGISTRY_ENDPOINT = "http://localhost:8080/registry";
  36. 		private static final String AUTHORIZED_REGISTRY_CONTEXT_PATH = "AuthorizedRegistryContext.properties";
  37. 		private static final String CLOUD_EDGE_DEPLOYMENT_SCENARIO_CONTEXT_FILE_PATH = "CloudEdgeDeploymentScenarioAASContext.properties";
  38.  
  39. 		private IAASRegistry registry;
  40.  
  41. 		private static AuthorizationProvider authorizationProvider = new AuthorizationProvider();
  42.  
  43. 		private ConnectedAssetAdministrationShellManager aasManager;
  44.  
  45. 		private static AuthorizedComponentFactory componentFactory = new AuthorizedComponentFactory();
  46.  
  47. 		public static final IIdentifier aasIdentifier = componentFactory.getAAS().getIdentification();
  48.  
  49. 		public static final IIdentifier docuSubmodelIdentifier = componentFactory.getDocuSMDescriptor().getIdentifier();
  50.  
  51. 		public static final IIdentifier edgeSubmodelIdentifier = componentFactory.getEdgeSubmodelDescriptor().getIdentifier();
  52.  
  53. 		private List<IComponent> startedComponents = new ArrayList<>();
  54.  
  55. 		private BaSyxHTTPServer edgeServer;
  56.  
  57. 		public static void main(String[] args) {
  58. 			new AuthorizedRegistryScenario();
  59. 		}
  60.  
  61. 		public AuthorizedRegistryScenario() {
  62. 			startAuthorizedRegistryServer();
  63.  
  64. 			createAuthorizedAASRegistryProxy();
  65.  
  66. 			startAASAndSubmodelServer();
  67.  
  68. 			createAssetAdministrationShellOnCloudServer();
  69.  
  70. 			createSubmodelOnAasCloudServer();
  71.  
  72. 			registerEdgeSubmodelIdentifierIntoAuthorizedRegistry();
  73. 		}
  74.  
  75. 		private void registerEdgeSubmodelIdentifierIntoAuthorizedRegistry() {
  76. 			registry.register(aasIdentifier, componentFactory.getEdgeSubmodelDescriptor());
  77. 		}
  78.  
  79. 		private void createAuthorizedAASRegistryProxy() {
  80. 			registry = new AuthorizedAASRegistryProxy(REGISTRY_ENDPOINT, authorizationProvider.getAuthorizationSupplier());
  81. 		}
  82.  
  83. 		private void createSubmodelOnAasCloudServer() {
  84. 			Submodel docuSubmodel = componentFactory.getDocuSM();
  85.  
  86. 			aasManager.createSubmodel(aasIdentifier, docuSubmodel);
  87. 		}
  88.  
  89. 		private void createAssetAdministrationShellOnCloudServer() {
  90. 			IConnectorFactory connectorFactory = new HTTPConnectorFactory();
  91. 			aasManager = new ConnectedAssetAdministrationShellManager(registry, connectorFactory);
  92.  
  93. 			aasManager.createAAS(componentFactory.getAAS(), CLOUD_ENDPOINT);
  94. 		}
  95.  
  96. 		private void startAASAndSubmodelServer() {
  97. 			startEdgeServer();
  98.  
  99. 			startCloudServer();
  100. 		}
  101.  
  102. 		private void startAuthorizedRegistryServer() {
  103. 			BaSyxContextConfiguration contextConfig = new BaSyxContextConfiguration();
  104.  
  105. 			contextConfig.loadFromResource(AUTHORIZED_REGISTRY_CONTEXT_PATH);
  106.  
  107. 			BaSyxRegistryConfiguration registryConfig = configureAuthorizedBasyxRegistry();
  108.  
  109. 			IComponent component = startRegistryComponent(contextConfig, registryConfig);
  110.  
  111. 			startedComponents.add(component);
  112. 		}
  113.  
  114. 		private IComponent startRegistryComponent(BaSyxContextConfiguration contextConfig,
  115. 				BaSyxRegistryConfiguration registryConfig) {
  116. 			IComponent registryComponent = new RegistryComponent(contextConfig, registryConfig);
  117. 			registryComponent.startComponent();
  118.  
  119. 			return registryComponent;
  120. 		}
  121.  
  122. 		private BaSyxRegistryConfiguration configureAuthorizedBasyxRegistry() {
  123. 			BaSyxRegistryConfiguration registryConfig = new BaSyxRegistryConfiguration(RegistryBackend.INMEMORY);
  124. 			registryConfig.setAuthorizationEnabled(true);
  125.  
  126. 			return registryConfig;
  127. 		}
  128.  
  129. 		private BaSyxContextConfiguration configureBasyxContext(int port, String registryPath) {
  130. 			return new BaSyxContextConfiguration(port, registryPath);
  131. 		}
  132.  
  133. 		private void startEdgeServer() {
  134. 			BaSyxContextConfiguration contextConfig = configureBasyxContext(8082, "");
  135.  
  136. 			BaSyxContext context = contextConfig.createBaSyxContext();
  137.  
  138. 			Submodel edgeSubmodel = componentFactory.createEdgeSubmodel();
  139.  
  140. 			SubmodelServlet smServlet = new SubmodelServlet(edgeSubmodel);
  141.  
  142. 			context.addServletMapping("/oven/" + AuthorizedComponentFactory.EDGESM_ID_SHORT + "/*", smServlet);
  143.  
  144. 			startEdgeServer(context);
  145. 		}
  146.  
  147. 		private void startEdgeServer(BaSyxContext context) {
  148. 			edgeServer = new BaSyxHTTPServer(context);
  149.  
  150. 			edgeServer.start();
  151. 		}
  152.  
  153. 		private void startCloudServer() {
  154. 			BaSyxContextConfiguration contextConfig = new BaSyxContextConfiguration();
  155.  
  156. 			contextConfig.loadFromResource(CLOUD_EDGE_DEPLOYMENT_SCENARIO_CONTEXT_FILE_PATH);
  157.  
  158. 			BaSyxAASServerConfiguration aasServerConfig = new BaSyxAASServerConfiguration(AASServerBackend.INMEMORY, "", REGISTRY_ENDPOINT);
  159.  
  160. 			AASServerComponent cloudServer = new AASServerComponent(contextConfig, aasServerConfig);
  161.  
  162. 			cloudServer.startComponent();
  163.  
  164. 			startedComponents.add(cloudServer);
  165. 		}
  166.  
  167. 		public void stop() {
  168. 			startedComponents.stream().forEach(IComponent::stopComponent);
  169.  
  170. 			edgeServer.shutdown();
  171. 		}
  172. }

Back to the top