Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Talk:BaSyx / Documentation / Components / Registry"

(Authorized Registry Example: new section)
 
Line 1: Line 1:
 
== Authorized Registry Example ==
 
== Authorized Registry Example ==
 
== Example Code ==
 
To do this, we first need to describe the oven in a model and provide this model to the VAB. As [[BaSyx_/_Documentation_/_VAB_Providers | described]], maps can be used for easy definition of models. Thus, an initial model of the oven using Maps is defined. Additionally, the already existing interface to the oven has to be integrated into the map model. To do this, the [[ BaSyx_/_Documentation_/_Lambda_Provider | Lambda Provider]] is used.
 
  
 
<syntaxhighlight lang="java" style="margin-left: 4em" line>
 
<syntaxhighlight lang="java" style="margin-left: 4em" line>

Revision as of 12:35, 5 February 2022

Authorized Registry Example

  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. /**
  34.  * Example scenario demonstrating a deployment with two servers with Authorized AAS Registry
  35.  * 
  36.  * <p>
  37.  * Server A is created as an empty server in the cloud.
  38.  * An AAS and a Submodel is pushed to it.
  39.  * 
  40.  * Server B is created as a server hosted near a machine.
  41.  * It provides a Submodel containing sensor value.
  42.  * </p>
  43.  * 
  44.  * <p>
  45.  * Note :
  46.  * Keycloak instance should be running before working with this scenario.
  47.  * <br/>
  48.  * 
  49.  * Keycloak instance running on : 127.0.0.1:9006 <br/>
  50.  * KEYCLOAK_USER=admin <br/>
  51.  * KEYCLOAK_PASSWORD=admin <br/>
  52.  * </p>
  53.  * @see <a href="https://hub.docker.com/r/jboss/keycloak/">Keycloak Docker Hub</a>
  54.  * @see <a href="https://www.keycloak.org/documentation">Keycloak Documentation</a>
  55.  * 
  56.  * @author danish
  57.  *
  58.  */
  59. public class AuthorizedRegistryScenario {
  60. 		private static final String CLOUD_ENDPOINT = "http://localhost:8081/cloud";
  61. 		protected static final String REGISTRY_ENDPOINT = "http://localhost:8080/registry";
  62. 		private static final String AUTHORIZED_REGISTRY_CONTEXT_PATH = "AuthorizedRegistryContext.properties";
  63. 		private static final String CLOUD_EDGE_DEPLOYMENT_SCENARIO_CONTEXT_FILE_PATH = "CloudEdgeDeploymentScenarioAASContext.properties";
  64.  
  65. 		private IAASRegistry registry;
  66.  
  67. 		private static AuthorizationProvider authorizationProvider = new AuthorizationProvider();
  68.  
  69. 		private ConnectedAssetAdministrationShellManager aasManager;
  70.  
  71. 		private static AuthorizedComponentFactory componentFactory = new AuthorizedComponentFactory();
  72.  
  73. 		public static final IIdentifier aasIdentifier = componentFactory.getAAS().getIdentification();
  74.  
  75. 		public static final IIdentifier docuSubmodelIdentifier = componentFactory.getDocuSMDescriptor().getIdentifier();
  76.  
  77. 		public static final IIdentifier edgeSubmodelIdentifier = componentFactory.getEdgeSubmodelDescriptor().getIdentifier();
  78.  
  79. 		private List<IComponent> startedComponents = new ArrayList<>();
  80.  
  81. 		private BaSyxHTTPServer edgeServer;
  82.  
  83. 		public static void main(String[] args) {
  84. 			new AuthorizedRegistryScenario();
  85. 		}
  86.  
  87. 		public AuthorizedRegistryScenario() {
  88. 			startAuthorizedRegistryServer();
  89.  
  90. 			createAuthorizedAASRegistryProxy();
  91.  
  92. 			startAASAndSubmodelServer();
  93.  
  94. 			createAssetAdministrationShellOnCloudServer();
  95.  
  96. 			createSubmodelOnAasCloudServer();
  97.  
  98. 			registerEdgeSubmodelIdentifierIntoAuthorizedRegistry();
  99. 		}
  100.  
  101. 		private void registerEdgeSubmodelIdentifierIntoAuthorizedRegistry() {
  102. 			registry.register(aasIdentifier, componentFactory.getEdgeSubmodelDescriptor());
  103. 		}
  104.  
  105. 		private void createAuthorizedAASRegistryProxy() {
  106. 			registry = new AuthorizedAASRegistryProxy(REGISTRY_ENDPOINT, authorizationProvider.getAuthorizationSupplier());
  107. 		}
  108.  
  109. 		private void createSubmodelOnAasCloudServer() {
  110. 			Submodel docuSubmodel = componentFactory.getDocuSM();
  111.  
  112. 			aasManager.createSubmodel(aasIdentifier, docuSubmodel);
  113. 		}
  114.  
  115. 		private void createAssetAdministrationShellOnCloudServer() {
  116. 			IConnectorFactory connectorFactory = new HTTPConnectorFactory();
  117. 			aasManager = new ConnectedAssetAdministrationShellManager(registry, connectorFactory);
  118.  
  119. 			aasManager.createAAS(componentFactory.getAAS(), CLOUD_ENDPOINT);
  120. 		}
  121.  
  122. 		private void startAASAndSubmodelServer() {
  123. 			startEdgeServer();
  124.  
  125. 			startCloudServer();
  126. 		}
  127.  
  128. 		private void startAuthorizedRegistryServer() {
  129. 			BaSyxContextConfiguration contextConfig = new BaSyxContextConfiguration();
  130.  
  131. 			contextConfig.loadFromResource(AUTHORIZED_REGISTRY_CONTEXT_PATH);
  132.  
  133. 			BaSyxRegistryConfiguration registryConfig = configureAuthorizedBasyxRegistry();
  134.  
  135. 			IComponent component = startRegistryComponent(contextConfig, registryConfig);
  136.  
  137. 			startedComponents.add(component);
  138. 		}
  139.  
  140. 		private IComponent startRegistryComponent(BaSyxContextConfiguration contextConfig,
  141. 				BaSyxRegistryConfiguration registryConfig) {
  142. 			IComponent registryComponent = new RegistryComponent(contextConfig, registryConfig);
  143. 			registryComponent.startComponent();
  144.  
  145. 			return registryComponent;
  146. 		}
  147.  
  148. 		private BaSyxRegistryConfiguration configureAuthorizedBasyxRegistry() {
  149. 			BaSyxRegistryConfiguration registryConfig = new BaSyxRegistryConfiguration(RegistryBackend.INMEMORY);
  150. 			registryConfig.setAuthorizationEnabled(true);
  151.  
  152. 			return registryConfig;
  153. 		}
  154.  
  155. 		private BaSyxContextConfiguration configureBasyxContext(int port, String registryPath) {
  156. 			return new BaSyxContextConfiguration(port, registryPath);
  157. 		}
  158.  
  159. 		private void startEdgeServer() {
  160. 			BaSyxContextConfiguration contextConfig = configureBasyxContext(8082, "");
  161.  
  162. 			BaSyxContext context = contextConfig.createBaSyxContext();
  163.  
  164. 			Submodel edgeSubmodel = componentFactory.createEdgeSubmodel();
  165.  
  166. 			SubmodelServlet smServlet = new SubmodelServlet(edgeSubmodel);
  167.  
  168. 			context.addServletMapping("/oven/" + AuthorizedComponentFactory.EDGESM_ID_SHORT + "/*", smServlet);
  169.  
  170. 			startEdgeServer(context);
  171. 		}
  172.  
  173. 		private void startEdgeServer(BaSyxContext context) {
  174. 			edgeServer = new BaSyxHTTPServer(context);
  175.  
  176. 			edgeServer.start();
  177. 		}
  178.  
  179. 		private void startCloudServer() {
  180. 			BaSyxContextConfiguration contextConfig = new BaSyxContextConfiguration();
  181.  
  182. 			contextConfig.loadFromResource(CLOUD_EDGE_DEPLOYMENT_SCENARIO_CONTEXT_FILE_PATH);
  183.  
  184. 			BaSyxAASServerConfiguration aasServerConfig = new BaSyxAASServerConfiguration(AASServerBackend.INMEMORY, "", REGISTRY_ENDPOINT);
  185.  
  186. 			AASServerComponent cloudServer = new AASServerComponent(contextConfig, aasServerConfig);
  187.  
  188. 			cloudServer.startComponent();
  189.  
  190. 			startedComponents.add(cloudServer);
  191. 		}
  192.  
  193. 		public void stop() {
  194. 			startedComponents.stream().forEach(IComponent::stopComponent);
  195.  
  196. 			edgeServer.shutdown();
  197. 		}
  198. }

Back to the top