Skip to content

Commit

Permalink
Includes AAS Registry
Browse files Browse the repository at this point in the history
Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>
  • Loading branch information
mdanish98 committed Aug 29, 2024
1 parent c1f8fef commit 7ae9a6e
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,9 @@
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.authorization.rules.rbac.backend.inmemory</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.authorization.rules.rbac.backend.submodel</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
package org.eclipse.digitaltwin.basyx.aasregistry.feature.authorization;

import org.eclipse.digitaltwin.basyx.aasregistry.feature.authorization.rbac.AasRegistryTargetPermissionVerifier;
import org.eclipse.digitaltwin.basyx.aasregistry.feature.authorization.rbac.backend.submodel.AasRegistryTargetInformationAdapter;
import org.eclipse.digitaltwin.basyx.authorization.CommonAuthorizationProperties;
import org.eclipse.digitaltwin.basyx.authorization.rbac.RbacPermissionResolver;
import org.eclipse.digitaltwin.basyx.authorization.rbac.RbacStorage;
import org.eclipse.digitaltwin.basyx.authorization.rbac.RoleProvider;
import org.eclipse.digitaltwin.basyx.authorization.rbac.SimpleRbacPermissionResolver;
import org.eclipse.digitaltwin.basyx.authorization.rbac.TargetPermissionVerifier;
import org.eclipse.digitaltwin.basyx.authorization.rules.rbac.backend.submodel.TargetInformationAdapter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -54,5 +56,11 @@ public TargetPermissionVerifier<AasRegistryTargetInformation> getAasTargetPermis
public RbacPermissionResolver<AasRegistryTargetInformation> getAasPermissionResolver(RbacStorage rbacStorage, RoleProvider roleProvider, TargetPermissionVerifier<AasRegistryTargetInformation> targetPermissionVerifier) {
return new SimpleRbacPermissionResolver<>(rbacStorage, roleProvider, targetPermissionVerifier);
}

@Bean
public TargetInformationAdapter getAasRegistryTargetInformationAdapter() {

return new AasRegistryTargetInformationAdapter();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*******************************************************************************
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* SPDX-License-Identifier: MIT
******************************************************************************/

package org.eclipse.digitaltwin.basyx.aasregistry.feature.authorization.rbac.backend.submodel;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.digitaltwin.aas4j.v3.model.Property;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementList;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultProperty;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementCollection;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementList;
import org.eclipse.digitaltwin.basyx.aasregistry.feature.authorization.AasRegistryTargetInformation;
import org.eclipse.digitaltwin.basyx.authorization.rbac.TargetInformation;
import org.eclipse.digitaltwin.basyx.authorization.rules.rbac.backend.submodel.TargetInformationAdapter;
import org.eclipse.digitaltwin.basyx.core.exceptions.InvalidTargetInformationException;

/**
* An implementation of the {@link TargetInformationAdapter} to adapt with Aas
* {@link TargetInformation}
*
* @author danish
*/
public class AasRegistryTargetInformationAdapter implements TargetInformationAdapter {

@Override
public SubmodelElementCollection adapt(TargetInformation targetInformation) {

SubmodelElementCollection targetInformationSMC = new DefaultSubmodelElementCollection.Builder().idShort("targetInformation").build();

SubmodelElementList aasId = new DefaultSubmodelElementList.Builder().idShort("aasIds").build();

List<SubmodelElement> aasIds = ((AasRegistryTargetInformation) targetInformation).getAasIds().stream().map(this::transform).collect(Collectors.toList());
aasId.setValue(aasIds);

targetInformationSMC.setValue(Arrays.asList(aasId));

return targetInformationSMC;
}

@Override
public TargetInformation adapt(SubmodelElementCollection targetInformation) {

SubmodelElement aasIdSubmodelElement = targetInformation.getValue().stream().filter(sme -> sme.getIdShort().equals("aasIds")).findAny().orElseThrow(
() -> new InvalidTargetInformationException("The TargetInformation defined in the SubmodelElementCollection Rule with id: " + targetInformation.getIdShort() + " is not compatible with the " + getClass().getName()));

if (!(aasIdSubmodelElement instanceof SubmodelElementList))
throw new InvalidTargetInformationException("The TargetInformation defined in the SubmodelElementCollection Rule with id: " + targetInformation.getIdShort() + " is not compatible with the " + getClass().getName());

SubmodelElementList aasIdList = (SubmodelElementList) aasIdSubmodelElement;

List<String> aasIds = aasIdList.getValue().stream().map(Property.class::cast).map(Property::getValue).collect(Collectors.toList());

return new AasRegistryTargetInformation(aasIds);
}

private Property transform(String aasId) {
return new DefaultProperty.Builder().value(aasId).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*******************************************************************************
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* SPDX-License-Identifier: MIT
******************************************************************************/

package org.eclipse.digitaltwin.basyx.aasregistry.regression.feature.authorization;

import static org.junit.Assert.*;
import org.eclipse.digitaltwin.aas4j.v3.model.Property;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementList;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultProperty;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementCollection;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementList;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.digitaltwin.basyx.aasregistry.feature.authorization.AasRegistryTargetInformation;
import org.eclipse.digitaltwin.basyx.aasregistry.feature.authorization.rbac.backend.submodel.AasRegistryTargetInformationAdapter;
import org.eclipse.digitaltwin.basyx.authorization.rbac.TargetInformation;
import org.eclipse.digitaltwin.basyx.core.exceptions.InvalidTargetInformationException;

/**
* Tests {@link AasRegistryTargetInformationAdapter}
*
* @author danish
*/
public class AasRegistryTargetInformationAdapterTest {

private AasRegistryTargetInformationAdapter aasRegistryTargetInformationAdapter;

@Before
public void setUp() {
aasRegistryTargetInformationAdapter = new AasRegistryTargetInformationAdapter();
}

@Test
public void testAdaptTargetInformationToSubmodelElementCollection() {

List<String> aasIds = Arrays.asList("aasId1", "aasId2");
TargetInformation targetInformation = new AasRegistryTargetInformation(aasIds);

SubmodelElementCollection result = aasRegistryTargetInformationAdapter.adapt(targetInformation);

assertEquals("targetInformation", result.getIdShort());

List<SubmodelElement> elements = result.getValue();
assertEquals(1, elements.size());

SubmodelElementList aasIdList = (SubmodelElementList) elements.get(0);
assertEquals("aasIds", aasIdList.getIdShort());

List<String> actualAasIds = aasIdList.getValue().stream().map(Property.class::cast).map(Property::getValue).map(String::valueOf).collect(Collectors.toList());
assertEquals(aasIds, actualAasIds);
}

@Test
public void testAdaptSubmodelElementCollectionToTargetInformation() {

List<String> expectedAasIds = Arrays.asList("aasId1", "aasId2");
List<SubmodelElement> aasIdProperties = expectedAasIds.stream().map(aasId -> new DefaultProperty.Builder().value(aasId).build()).collect(Collectors.toList());

SubmodelElementList aasIdList = new DefaultSubmodelElementList.Builder().idShort("aasIds").value(aasIdProperties).build();

SubmodelElementCollection targetInformationSMC = new DefaultSubmodelElementCollection.Builder().idShort("targetInformation").value(Collections.singletonList(aasIdList)).build();

TargetInformation result = aasRegistryTargetInformationAdapter.adapt(targetInformationSMC);

assertTrue(result instanceof AasRegistryTargetInformation);
assertEquals(expectedAasIds, ((AasRegistryTargetInformation) result).getAasIds());
}

@Test
public void testAdaptTargetInformationWithEmptyAasIds() {

List<String> aasIds = Collections.emptyList();
TargetInformation targetInformation = new AasRegistryTargetInformation(aasIds);

SubmodelElementCollection result = aasRegistryTargetInformationAdapter.adapt(targetInformation);

assertEquals("targetInformation", result.getIdShort());

List<SubmodelElement> elements = result.getValue();
assertEquals(1, elements.size());

SubmodelElementList aasIdList = (SubmodelElementList) elements.get(0);
assertEquals("aasIds", aasIdList.getIdShort());

List<String> actualAasIds = aasIdList.getValue().stream()
.map(Property.class::cast)
.map(Property::getValue)
.map(String::valueOf)
.collect(Collectors.toList());
assertTrue(actualAasIds.isEmpty());
}

@Test(expected = InvalidTargetInformationException.class)
public void testAdaptSubmodelElementCollectionWithInvalidStructure() {

SubmodelElementCollection targetInformationSMC = new DefaultSubmodelElementCollection.Builder().idShort("targetInformation")
.value(Collections.singletonList(new DefaultProperty.Builder().idShort("invalidElement").value("value").build()))
.build();

aasRegistryTargetInformationAdapter.adapt(targetInformationSMC);
}

@Test(expected = InvalidTargetInformationException.class)
public void testAdaptSubmodelElementCollectionWithoutAasIds() {

SubmodelElementCollection targetInformationSMC = new DefaultSubmodelElementCollection.Builder().idShort("targetInformation")
.value(Collections.emptyList())
.build();

aasRegistryTargetInformationAdapter.adapt(targetInformationSMC);
}

}

0 comments on commit 7ae9a6e

Please sign in to comment.