Search in sources :

Example 1 with Service

use of org.codice.ddf.admin.core.api.Service in project ddf by codice.

the class AdminPollerTest method testAllSourceInfo.

@Test
public void testAllSourceInfo() {
    Action operatorActionOne = getTestAction("operationIdOne", "operationTitle1", "operationDescription1", "https://localhost:8993/provider/someAction");
    Action operatorActionTwo = getTestAction("operationIdTwo", "operationTitleTwo", "operationDescriptionTwo", "https://localhost:8993/provider/someAction");
    ImmutableList<MultiActionProvider> operationActions = ImmutableList.of(getHandleableTestActionProvider(operatorActionOne), getNotHandleableTestActionProvider(), getHandleableTestActionProvider(operatorActionTwo));
    poller.setOperationActionProviders(operationActions);
    Action reportActionOne = getTestAction("reportId", "reportTitle", "reportDescription", "https://localhost:8993/provider/someAction");
    ImmutableList<MultiActionProvider> reportActions = ImmutableList.of(getHandleableTestActionProvider(reportActionOne), getNotHandleableTestActionProvider(), getNotHandleableTestActionProvider());
    poller.setReportActionProviders(reportActions);
    List<Service> sources = poller.allSourceInfo();
    assertThat(sources, notNullValue());
    assertThat(sources.size(), is(2));
    assertThat(sources.get(0), not(hasKey("configurations")));
    assertThat(sources.get(1), hasKey("configurations"));
    List<Map<String, Object>> configurations = (List) sources.get(1).get("configurations");
    assertThat(configurations, hasSize(2));
    Map<String, Object> configurationMap = configurations.get(0);
    assertThat(configurationMap, hasKey("operation_actions"));
    assertThat(configurationMap, hasKey("report_actions"));
    List<Map<String, Object>> operationActionsList = (List) configurationMap.get("operation_actions");
    Map<String, String> operatorActionOneMap = getMapFromAction(operatorActionOne);
    Map<String, String> operatorActionTwoMap = getMapFromAction(operatorActionTwo);
    assertThat(operationActionsList, contains(operatorActionOneMap, operatorActionTwoMap));
    List<Map<String, Object>> reportActionsList = (List) configurationMap.get("report_actions");
    Map<String, String> reportActionMap = getMapFromAction(reportActionOne);
    assertThat(reportActionsList, contains(reportActionMap));
}
Also used : Action(ddf.action.Action) MultiActionProvider(ddf.action.MultiActionProvider) ConfiguredService(ddf.catalog.service.ConfiguredService) Service(org.codice.ddf.admin.core.api.Service) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with Service

use of org.codice.ddf.admin.core.api.Service in project ddf by codice.

the class AdminPollerServiceBean method allSourceInfo.

@Override
public List<Service> allSourceInfo() {
    // Get list of metatypes
    List<Service> metatypes = helper.getMetatypes();
    // Loop through each metatype and find its configurations
    for (Service metatype : metatypes) {
        try {
            List<Configuration> configs = helper.getConfigurations(metatype);
            List<ConfigurationDetails> configurations = new ArrayList<>();
            if (configs != null) {
                for (Configuration config : configs) {
                    ConfigurationDetails source = new ConfigurationDetailsImpl();
                    String pid = config.getPid();
                    String fPid = config.getFactoryPid();
                    boolean disabled = pid.endsWith(ConfigurationStatus.DISABLED_EXTENSION);
                    if (fPid != null) {
                        disabled = disabled || fPid.endsWith(ConfigurationStatus.DISABLED_EXTENSION);
                    }
                    source.setId(pid);
                    source.setEnabled(!disabled);
                    source.setFactoryPid(fPid);
                    if (!disabled) {
                        source.setName(helper.getName(config));
                        source.setBundleName(helper.getBundleName(config));
                        source.setBundleLocation(config.getBundleLocation());
                        source.setBundle(helper.getBundleId(config));
                    } else {
                        source.setName(config.getPid());
                    }
                    Dictionary<String, Object> properties = config.getProperties();
                    ConfigurationProperties plist = new ConfigurationPropertiesImpl();
                    for (String key : Collections.list(properties.keys())) {
                        plist.put(key, properties.get(key));
                    }
                    source.setConfigurationProperties(plist);
                    source.put(MAP_ENTRY_REPORT_ACTIONS, getActions(config, reportActionProviders));
                    source.put(MAP_ENTRY_OPERATION_ACTIONS, getActions(config, operationActionProviders));
                    configurations.add(source);
                }
                metatype.setConfigurations(configurations);
            }
        } catch (Exception e) {
            LOGGER.debug("Error getting source info: {}", e.getMessage());
        }
    }
    metatypes.sort(Comparator.comparing(Service::getId, String.CASE_INSENSITIVE_ORDER));
    return metatypes;
}
Also used : Configuration(org.osgi.service.cm.Configuration) ArrayList(java.util.ArrayList) ConfiguredService(ddf.catalog.service.ConfiguredService) Service(org.codice.ddf.admin.core.api.Service) ConfigurationProperties(org.codice.ddf.admin.core.api.ConfigurationProperties) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) MalformedObjectNameException(javax.management.MalformedObjectNameException) ConfigurationDetails(org.codice.ddf.admin.core.api.ConfigurationDetails)

Example 3 with Service

use of org.codice.ddf.admin.core.api.Service in project ddf by codice.

the class ApplicationServiceBeanTest method testGetServicesASE.

/**
 * Tests the {@link ApplicationServiceBean#getServices(String)} method for the case where an
 * ApplicationServiceException is thrown
 *
 * @throws Exception
 */
// TODO RAP 29 Aug 16: DDF-2443 - Fix test to not depend on specific log output
@Test
public void testGetServicesASE() throws Exception {
    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    final Appender mockAppender = mock(Appender.class);
    when(mockAppender.getName()).thenReturn("MOCK");
    root.addAppender(mockAppender);
    root.setLevel(Level.ALL);
    ApplicationServiceBean serviceBean = newApplicationServiceBean();
    Bundle testBundle = mock(Bundle.class);
    Bundle[] bundles = { testBundle };
    when(bundleContext.getBundles()).thenReturn(bundles);
    List<Service> services = new ArrayList<>();
    Service testService1 = new ServiceImpl();
    services.add(testService1);
    when(testAppService.getApplication(TEST_APP_NAME)).thenReturn(testApp);
    when(testConfigAdminExt.listServices(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(services);
    serviceBean.getServices(TEST_APP_NAME);
}
Also used : Appender(ch.qos.logback.core.Appender) Bundle(org.osgi.framework.Bundle) ServiceImpl(org.codice.ddf.admin.core.impl.ServiceImpl) ArrayList(java.util.ArrayList) SystemService(org.apache.karaf.system.SystemService) Service(org.codice.ddf.admin.core.api.Service) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) MetaTypeService(org.osgi.service.metatype.MetaTypeService) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Logger(org.slf4j.Logger) SecurityLogger(ddf.security.audit.SecurityLogger) Test(org.junit.Test)

Example 4 with Service

use of org.codice.ddf.admin.core.api.Service in project ddf by codice.

the class ApplicationServiceBean method getServices.

/**
 * {@inheritDoc}.
 */
@Override
public List<Map<String, Object>> getServices(String applicationID) {
    try {
        List<Service> services = configAdmin.listServices(getDefaultFactoryLdapFilter(), getDefaultLdapFilter());
        if (services.isEmpty()) {
            return Collections.emptyList();
        }
        Set<BundleInfo> bundleInfos = getBundleInfosForApplication(applicationID);
        if (bundleInfos == null) {
            return Collections.emptyList();
        }
        Set<String> bundleLocations = bundleInfos.stream().map(BundleInfo::getLocation).collect(Collectors.toSet());
        MetaTypeService metatypeService = getMetaTypeService();
        Set<MetaTypeInformation> metatypeInformation = (metatypeService == null) ? Collections.emptySet() : Arrays.stream(getContext().getBundles()).filter(b -> bundleLocations.contains(computeLocation(b))).map(metatypeService::getMetaTypeInformation).collect(Collectors.toSet());
        return services.stream().filter(service -> hasBundleLocation(service, bundleLocations) || hasMetatypesForService(service, metatypeInformation)).collect(Collectors.toList());
    } catch (VirtualMachineError e) {
        throw e;
    } catch (Throwable e) {
        LOGGER.info("Could not retrieve services", e);
        throw new ApplicationServiceBeanException("Could not retrieve services");
    }
}
Also used : Arrays(java.util.Arrays) ConfigurationAdmin(org.codice.ddf.admin.core.api.ConfigurationAdmin) Constants(org.osgi.framework.Constants) LoggerFactory(org.slf4j.LoggerFactory) Application(org.codice.ddf.admin.application.service.Application) FeaturesService(org.apache.karaf.features.FeaturesService) SynchronizedInstaller(org.codice.ddf.sync.installer.api.SynchronizedInstaller) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) Map(java.util.Map) Bundle(org.osgi.framework.Bundle) EnumSet(java.util.EnumSet) ReflectionException(javax.management.ReflectionException) BundleInfo(org.apache.karaf.features.BundleInfo) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) SystemService(org.apache.karaf.system.SystemService) Service(org.codice.ddf.admin.core.api.Service) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Feature(org.apache.karaf.features.Feature) FeatureDetails(org.codice.ddf.admin.application.rest.model.FeatureDetails) Set(java.util.Set) ObjectName(javax.management.ObjectName) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) Collectors(java.util.stream.Collectors) BundleContext(org.osgi.framework.BundleContext) Objects(java.util.Objects) MalformedObjectNameException(javax.management.MalformedObjectNameException) List(java.util.List) Stream(java.util.stream.Stream) AccessController(java.security.AccessController) ApplicationPlugin(org.codice.ddf.admin.application.plugin.ApplicationPlugin) HashMap(java.util.HashMap) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) ArrayList(java.util.ArrayList) SERVICE_FACTORYPID(org.osgi.service.cm.ConfigurationAdmin.SERVICE_FACTORYPID) CollectionUtils(org.apache.commons.collections.CollectionUtils) MBeanServer(javax.management.MBeanServer) MBeanRegistrationException(javax.management.MBeanRegistrationException) ManagementFactory(java.lang.management.ManagementFactory) InstanceNotFoundException(javax.management.InstanceNotFoundException) Nullable(javax.annotation.Nullable) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) PrivilegedActionException(java.security.PrivilegedActionException) Logger(org.slf4j.Logger) SecurityLogger(ddf.security.audit.SecurityLogger) FileUtils(org.apache.commons.io.FileUtils) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) MetaTypeService(org.osgi.service.metatype.MetaTypeService) MBeanException(javax.management.MBeanException) Paths(java.nio.file.Paths) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Collections(java.util.Collections) FrameworkUtil(org.osgi.framework.FrameworkUtil) MetaTypeService(org.osgi.service.metatype.MetaTypeService) FeaturesService(org.apache.karaf.features.FeaturesService) SystemService(org.apache.karaf.system.SystemService) Service(org.codice.ddf.admin.core.api.Service) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) MetaTypeService(org.osgi.service.metatype.MetaTypeService) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) BundleInfo(org.apache.karaf.features.BundleInfo)

Example 5 with Service

use of org.codice.ddf.admin.core.api.Service in project ddf by codice.

the class ApplicationServiceBeanTest method testGetServicesMetatypeInfo.

/**
 * Tests the {@link ApplicationServiceBean#getServices(String)} method for the case where the
 * services do not have the "configurations" key and there is MetatypeInformation present for each
 * service.
 *
 * <p>This test mostly just checks that
 *
 * @throws Exception
 */
@Test
public void testGetServicesMetatypeInfo() throws Exception {
    ApplicationServiceBean serviceBean = newApplicationServiceBean();
    ServiceTracker testServiceTracker = mock(ServiceTracker.class);
    serviceBean.setServiceTracker(testServiceTracker);
    MetaTypeService testMTS = mock(MetaTypeService.class);
    MetaTypeInformation testMTI = mock(MetaTypeInformation.class);
    when(testServiceTracker.getService()).thenReturn(testMTS);
    when(testMTS.getMetaTypeInformation(any(Bundle.class))).thenReturn(testMTI);
    when(testMTI.getPids()).thenReturn(new String[] { "001", "002" });
    when(testMTI.getFactoryPids()).thenReturn(new String[] { "001", "002" });
    Bundle testBundle = mock(Bundle.class);
    Bundle[] bundles = { testBundle };
    when(bundleContext.getBundles()).thenReturn(bundles);
    when(testBundle.getLocation()).thenReturn(TEST_LOCATION);
    List<Service> services = new ArrayList<>();
    Service testService2 = mock(Service.class);
    Service testService1 = mock(Service.class);
    services.add(testService1);
    services.add(testService2);
    List<Map<String, Object>> testService1Configs = new ArrayList<>();
    Map<String, Object> testConfig1 = new HashMap<>();
    testConfig1.put("bundle_location", TEST_LOCATION);
    testService1Configs.add(testConfig1);
    List<Map<String, Object>> testService2Configs = new ArrayList<>();
    Map<String, Object> testConfig2 = new HashMap<>();
    testConfig2.put("bundle_location", TEST_LOCATION);
    testService1Configs.add(testConfig2);
    when(testService1.get("factory")).thenReturn(true);
    when(testService2.get("factory")).thenReturn(false);
    when(testService1.get("configurations")).thenReturn(testService1Configs);
    when(testService2.get("configurations")).thenReturn(testService2Configs);
    when(testService1.get("id")).thenReturn("001");
    when(testService2.get("id")).thenReturn("002");
    BundleInfo testBundle1 = mock(BundleInfo.class);
    Set<BundleInfo> testBundles = new HashSet<>();
    testBundles.add(testBundle1);
    when(testApp.getBundles()).thenReturn(testBundles);
    when(testBundle1.getLocation()).thenReturn(TEST_LOCATION);
    when(testAppService.getApplication(TEST_APP_NAME)).thenReturn(testApp);
    when(testConfigAdminExt.listServices(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(services);
    assertThat("Should find the given services.", serviceBean.getServices(TEST_APP_NAME).get(0), is(testService1));
}
Also used : MetaTypeService(org.osgi.service.metatype.MetaTypeService) ServiceTracker(org.osgi.util.tracker.ServiceTracker) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) SystemService(org.apache.karaf.system.SystemService) Service(org.codice.ddf.admin.core.api.Service) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) MetaTypeService(org.osgi.service.metatype.MetaTypeService) MetaTypeInformation(org.osgi.service.metatype.MetaTypeInformation) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BundleInfo(org.apache.karaf.features.BundleInfo) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Service (org.codice.ddf.admin.core.api.Service)18 ArrayList (java.util.ArrayList)14 MetaTypeService (org.osgi.service.metatype.MetaTypeService)14 Test (org.junit.Test)11 HashMap (java.util.HashMap)10 Map (java.util.Map)10 Bundle (org.osgi.framework.Bundle)8 IOException (java.io.IOException)6 List (java.util.List)6 ManagedService (org.osgi.service.cm.ManagedService)6 Collections (java.util.Collections)5 Hashtable (java.util.Hashtable)5 Collectors (java.util.stream.Collectors)5 Arrays (java.util.Arrays)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 SystemService (org.apache.karaf.system.SystemService)4 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)4 ConfigurationAdminPlugin (org.codice.ddf.ui.admin.api.plugin.ConfigurationAdminPlugin)4 BundleContext (org.osgi.framework.BundleContext)4 Filter (org.osgi.framework.Filter)4