Skip to content

Commit

Permalink
#21: Checker now takes resource instead of collection of capabilities…
Browse files Browse the repository at this point in the history
…. Implementation of rest api for checker. OSGi service for checker. Checker and restimpl indexer modules added to default modules so that they're included in the build.
  • Loading branch information
Zdenek Vales committed Jan 23, 2020
1 parent be3cbd8 commit 7d6aa5f
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 114 deletions.
5 changes: 5 additions & 0 deletions modules/crce-api-compatibility-checker/osgi.bnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#-----------------------------------------------------------------
# Use this file to add customized Bnd instructions for the bundle
#-----------------------------------------------------------------

Bundle-Activator: ${bundle.namespace}.internal.Activator
2 changes: 2 additions & 0 deletions modules/crce-api-compatibility-checker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<artifactId>crce-api-compatibility-checker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>bundle</packaging>

<name>
Apicomp - API compatibility checker
Expand All @@ -27,6 +28,7 @@
<properties>
<bundle.symbolicName>${namespace}.apicomp</bundle.symbolicName>
<bundle.namespace>${namespace}.apicomp</bundle.namespace>
<bundle.activator>${bundle.namespace}.internal.Activator</bundle.activator>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
package cz.zcu.kiv.crce.apicomp;

import cz.zcu.kiv.crce.apicomp.result.CompatibilityCheckResult;
import cz.zcu.kiv.crce.metadata.Capability;

import java.util.Set;
import cz.zcu.kiv.crce.metadata.Resource;

/**
* Interface for compatibility checkers.
*/
public interface ApiCompatibilityChecker {

/**
* Namespace of the root capability that is expected to hold all metadata relevant to this checker.
*
* @return Capability namespace.
*/
String getRootCapabilityNamespace();

/**
* Verifies that the provided API is supported by this compatibility checker.
* Verification is based on presence of identity capability and it's correct
* namespace and value.
* Verification is based on presence of root capability with namespace equal to
* {@link #getRootCapabilityNamespace()}.
*
* @param apiMetadata Set of capabilities which describes the API.
* @param resource Resource that contains API metadata.
* @return True if the given API is supported.
*/
boolean isApiSupported(Set<Capability> apiMetadata);
boolean isApiSupported(Resource resource);

/**
* Compares two APIs using metadata stored in CRCE and returns result in form of
* a CompatibilityCheckResult object.
*
* @param api1 Set of capabilities containing the metadata for the first API. This might be either single root
* @param api1 Resource with capabilities containing the metadata for the first API. This might be either single root
* capability which contains child capabilities with attributes and/or properties with metadata or set of root
* capabilities with child capabilities with attributes and/or properties.
* @param api2 Set of capabilities containing the metadata for the second API. This might be either single root
* @param api2 Resource with capabilities containing the metadata for the second API. This might be either single root
* capability which contains child capabilities with attributes and/or properties with metadata or set of root
* capabilities with child capabilities with attributes and/or properties.
* @return Result.
*/
CompatibilityCheckResult compareApis(Set<Capability> api1, Set<Capability> api2);
CompatibilityCheckResult compareApis(Resource api1, Resource api2);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package cz.zcu.kiv.crce.apicomp;

import cz.zcu.kiv.crce.apicomp.result.CompatibilityCheckResult;
import cz.zcu.kiv.crce.metadata.Capability;
import cz.zcu.kiv.crce.metadata.Resource;

import java.util.Set;

/**
* A service responsible for calling correct compatibility comparator for
* given metadata.
Expand All @@ -28,12 +25,4 @@ public interface ApiCompatibilityCheckerService {
* @return Checker able to compare this resource.
*/
ApiCompatibilityChecker pickChecker(Resource resource);

/**
* Returns checker compatible for given set of metadata representing API.
*
* @param apiMetadata API metadata.
* @return Checker able to compare this resource.
*/
ApiCompatibilityChecker pickChecker(Set<Capability> apiMetadata);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
import cz.zcu.kiv.crce.apicomp.impl.restimpl.RestApiCompatibilityChecker;
import cz.zcu.kiv.crce.apicomp.impl.webservice.WebservicesCompatibilityChecker;
import cz.zcu.kiv.crce.apicomp.result.CompatibilityCheckResult;
import cz.zcu.kiv.crce.metadata.Capability;
import cz.zcu.kiv.crce.metadata.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* A service responsible for fetching resources' metadata and passing them to
* the correct compatibility checker.
*/
public class ApiCompatibilityCheckerServiceImpl implements ApiCompatibilityCheckerService {

private static final Logger logger = LoggerFactory.getLogger(ApiCompatibilityCheckerServiceImpl.class);

private List<ApiCompatibilityChecker> availableCheckers;

public ApiCompatibilityCheckerServiceImpl() {
Expand All @@ -30,34 +31,21 @@ public ApiCompatibilityCheckerServiceImpl() {
@Override
public CompatibilityCheckResult compareApis(Resource api1, Resource api2) {

// todo: pass only resource to every layer?
Set<Capability> metadata1 = new HashSet<>(api1.getCapabilities());
Set<Capability> metadata2 = new HashSet<>(api2.getCapabilities());

ApiCompatibilityChecker c1 = pickChecker(metadata1),
c2 = pickChecker(metadata2);
ApiCompatibilityChecker c1 = pickChecker(api1),
c2 = pickChecker(api2);

if (c1 == null || c2 == null || !c1.equals(c2)) {
// todo: log error or something
if (c1 == null || !c1.equals(c2)) {
logger.warn("Could not pick right checker or APIs are not comparable.");
return null;
}

return c1.compareApis(metadata1, metadata2);
return c1.compareApis(api1, api2);
}

@Override
public ApiCompatibilityChecker pickChecker(Resource resource) {
Set<Capability> metadata = new HashSet<>(resource.getCapabilities());
return availableCheckers.stream()
.filter(checker -> checker.isApiSupported(metadata))
.findFirst()
.orElse(null);
}

@Override
public ApiCompatibilityChecker pickChecker(Set<Capability> apiMetadata) {
return availableCheckers.stream()
.filter(checker -> checker.isApiSupported(apiMetadata))
.filter(checker -> checker.isApiSupported(resource))
.findFirst()
.orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
import cz.zcu.kiv.crce.compatibility.impl.DefaultDiffImpl;
import cz.zcu.kiv.crce.metadata.Attribute;
import cz.zcu.kiv.crce.metadata.Capability;
import cz.zcu.kiv.crce.metadata.Resource;
import cz.zcu.kiv.crce.metadata.impl.ListAttributeType;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
* Compatibility checker for REST API. Expects metadata structure created by
Expand All @@ -20,37 +24,43 @@
public class RestApiCompatibilityChecker implements ApiCompatibilityChecker {


public RestApiCompatibilityChecker() {
}

@Override
public String getRootCapabilityNamespace() {
return RestimplIndexerConstants.IDENTITY_CAPABILITY_NAMESPACE;
}

@Override
public boolean isApiSupported(Set<Capability> apiMetadata) {
public boolean isApiSupported(Resource resource) {
// API is supported if it contains capability with namespace 'restimpl.identity'
// related constants are in internal package of crce-restimpl-indexer module
return apiMetadata != null &&
apiMetadata
.stream()
.anyMatch(capability -> RestimplIndexerConstants.IDENTITY_CAPABILITY_NAMESPACE.equals(capability.getNamespace()));
return resource != null &&
!resource.getRootCapabilities(getRootCapabilityNamespace()).isEmpty();
}

@Override
public CompatibilityCheckResult compareApis(Set<Capability> api1, Set<Capability> api2) {
CompatibilityCheckResult checkResult = new CompatibilityCheckResult();
public CompatibilityCheckResult compareApis(Resource api1, Resource api2) {
CompatibilityCheckResult checkResult = new CompatibilityCheckResult(api1, api2);

// given the structure of metadata created in crce-restimpl-indexer, both
// capability sets should contain exactly 1 capability
if (api1.size() != 1 || !isApiSupported(api1)) {
if (!isApiSupported(api1)) {
// todo: log error
throw new RuntimeException("API 1 is not supported by this checker!");
}

if (api2.size() != 1 || !isApiSupported(api2)) {
if (!isApiSupported(api2)) {
// todo: log error
throw new RuntimeException("API 2 is not supported by this checker!");
}

// root capabilities are expected to have only one type
// of children - capability which describes the endpoint
// so it can be assumed all children are endpoints
Capability api1Root = api1.iterator().next();
Capability api2Root = api2.iterator().next();
Capability api1Root = api1.getRootCapabilities(getRootCapabilityNamespace()).iterator().next();
Capability api2Root = api2.getRootCapabilities(getRootCapabilityNamespace()).iterator().next();


// compare endpoints and their details
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import cz.zcu.kiv.crce.apicomp.ApiCompatibilityChecker;
import cz.zcu.kiv.crce.apicomp.result.CompatibilityCheckResult;
import cz.zcu.kiv.crce.metadata.Capability;

import java.util.Set;
import cz.zcu.kiv.crce.metadata.Resource;

/**
* Checker for crce-webservices-indexer
Expand All @@ -13,12 +11,17 @@
public class WebservicesCompatibilityChecker implements ApiCompatibilityChecker {

@Override
public boolean isApiSupported(Set<Capability> apiMetadata) {
public String getRootCapabilityNamespace() {
return null;
}

@Override
public boolean isApiSupported(Resource resource) {
return false;
}

@Override
public CompatibilityCheckResult compareApis(Set<Capability> api1, Set<Capability> api2) {
public CompatibilityCheckResult compareApis(Resource api1, Resource api2) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@
import org.apache.felix.dm.DependencyManager;
import org.osgi.framework.BundleContext;

import java.util.Hashtable;

/**
* Apicomp bundle activator.
*/
public class Activator extends DependencyActivatorBase {

@Override
public void start(BundleContext context) throws Exception {
super.start(context);

System.out.println("Start the Apicomp bundle.");
}

@Override
public void init(BundleContext context, DependencyManager manager) throws Exception {
System.out.println("Starting the Apicomp bundle.");
System.out.println("Init the Apicomp bundle.");


manager.add(createComponent()
.setInterface(ApiCompatibilityCheckerService.class.getName(), null)
.setImplementation(ApiCompatibilityCheckerServiceImpl.class)
);
context.registerService(ApiCompatibilityCheckerService.class, new ApiCompatibilityCheckerServiceImpl(), new Hashtable<>());
// manager.add(createComponent()
// .setInterface(ApiCompatibilityCheckerService.class.getName(), null)
// .setImplementation(ApiCompatibilityCheckerServiceImpl.class)
// );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cz.zcu.kiv.crce.compatibility.Contract;
import cz.zcu.kiv.crce.compatibility.Diff;
import cz.zcu.kiv.crce.compatibility.Difference;
import cz.zcu.kiv.crce.metadata.Resource;
import cz.zcu.kiv.crce.metadata.type.Version;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -51,6 +52,18 @@ public CompatibilityCheckResult() {
diffDetails = new ArrayList<>();
}

/**
* Diff object of two resources.
*
* @param baseResource Base resource.
* @param resource Resource that is being compared to the base one.
*/
public CompatibilityCheckResult(Resource baseResource, Resource resource) {
this();
baseResourceName = baseResource.getId();
resourceName = resource.getId();
}

@Override
public String getId() {
return null;
Expand Down
Loading

0 comments on commit 7d6aa5f

Please sign in to comment.