Search in sources :

Example 6 with Application

use of org.codice.ddf.admin.application.service.Application in project ddf by codice.

the class ApplicationServiceImpl method traverseDependencies.

/**
     * Finds a parent and children dependencies for each app.  Needs to be run twice
     * in order to get full dependency correlations.
     *
     * @param appMap               Application Map containing all the application nodes.
     * @param filteredApplications Set containing all the application nodes minus those in the ignored list
     * @param reportDebug          Boolean that allows debug statements to be output or not.  Only reason
     *                             why this exists is because this function will be called twice and only
     *                             the second set of statements will be relevant
     */
private void traverseDependencies(Map<Application, ApplicationNodeImpl> appMap, Set<Application> filteredApplications, boolean reportDebug) {
    // find dependencies in each app and add them into correct node
    for (Entry<Application, ApplicationNodeImpl> curAppNode : appMap.entrySet()) {
        try {
            // main feature will contain dependencies
            Feature mainFeature = curAppNode.getKey().getMainFeature();
            if (null == mainFeature) {
                if (reportDebug) {
                    LOGGER.debug("Application \"{}\" does not contain a main feature", curAppNode.getKey().getName());
                }
                continue;
            }
            // eliminate duplications with a set
            Set<Dependency> dependencies = new HashSet<>(mainFeature.getDependencies());
            // remove any features that are local to the application
            dependencies.removeAll(curAppNode.getKey().getFeatures());
            // loop through all of the features that are left to determine
            // where they are from
            Set<Application> depAppSet = new HashSet<>();
            for (Dependency curDepFeature : dependencies) {
                Application dependencyApp = findFeature(featuresService.getFeature(curDepFeature.getName()), filteredApplications);
                if (dependencyApp != null) {
                    if (dependencyApp.equals(curAppNode.getKey())) {
                        if (reportDebug) {
                            LOGGER.debug("Self-dependency");
                        }
                        continue;
                    } else {
                        if (reportDebug) {
                            LOGGER.debug("Application {} depends on the feature {} which is located in application {}.", curAppNode.getKey().getName(), curDepFeature.getName(), dependencyApp.getName());
                        }
                        depAppSet.add(dependencyApp);
                    }
                }
            }
            if (!depAppSet.isEmpty()) {
                Application parentApp;
                if (depAppSet.size() > 1) {
                    parentApp = findCommonParent(depAppSet, appMap);
                    if (parentApp == null) {
                        if (reportDebug) {
                            LOGGER.debug("Found more than 1 application dependency for application {}. Could not determine which one is the correct parent. Application will be sent back as root application.", curAppNode.getKey().getName());
                        }
                        continue;
                    }
                } else {
                    parentApp = depAppSet.iterator().next();
                }
                // update the dependency app with a new child
                ApplicationNode parentAppNode = appMap.get(parentApp);
                parentAppNode.getChildren().add(curAppNode.getValue());
                curAppNode.getValue().setParent(parentAppNode);
            } else {
                if (reportDebug) {
                    LOGGER.debug("No dependency applications found for {}. This will be sent back as a root application.", curAppNode.getKey().getName());
                }
            }
        // ApplicationServiceException from DDF and Exception from Karaf
        // (FeaturesService)
        } catch (Exception e) {
            if (reportDebug) {
                LOGGER.warn("Encountered error while determining dependencies for \"{}\". This may cause an incomplete application hierarchy to be created.", curAppNode.getKey().getName(), e);
            }
        }
    }
}
Also used : ApplicationNode(org.codice.ddf.admin.application.service.ApplicationNode) Dependency(org.apache.karaf.features.Dependency) Application(org.codice.ddf.admin.application.service.Application) Feature(org.apache.karaf.features.Feature) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityServiceException(ddf.security.service.SecurityServiceException) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) HashSet(java.util.HashSet)

Example 7 with Application

use of org.codice.ddf.admin.application.service.Application in project ddf by codice.

the class ApplicationServiceImpl method getApplications.

@Override
public Set<Application> getApplications() {
    LOGGER.trace("Getting all applications.");
    Repository[] repos = {};
    try {
        repos = featuresService.listRepositories();
        LOGGER.debug("Found {} applications from feature service.", repos.length);
        if (LOGGER.isDebugEnabled()) {
            for (int ii = 0; ii < repos.length; ++ii) {
                LOGGER.debug("Repo/App {}: {}", ii, repos[ii].getName());
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Unable to get list of Repositories.", e);
    }
    Set<Application> applications = new HashSet<Application>(repos.length);
    for (int i = 0; i < repos.length; i++) {
        Application newApp = new ApplicationImpl(repos[i]);
        try {
            if (!ignoredApplicationNames.contains(newApp.getName()) && newApp.getFeatures().size() > 0 && isPermittedToViewFeature(newApp.getName())) {
                applications.add(newApp);
            }
        } catch (ApplicationServiceException ase) {
            LOGGER.warn("Exception while trying to find information for application named {}. " + "It will be excluded from the application list.", newApp.getName(), ase);
        }
    }
    return new TreeSet<>(applications);
}
Also used : ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) Repository(org.apache.karaf.features.Repository) TreeSet(java.util.TreeSet) Application(org.codice.ddf.admin.application.service.Application) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityServiceException(ddf.security.service.SecurityServiceException) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) HashSet(java.util.HashSet)

Example 8 with Application

use of org.codice.ddf.admin.application.service.Application in project ddf by codice.

the class ApplicationUploadEndpoint method update.

@POST
@Path("/update")
@Produces("application/json")
public Response update(MultipartBody multipartBody, @Context UriInfo requestUriInfo) {
    LOGGER.trace("ENTERING: update");
    Response response;
    List<Attachment> attachmentList = multipartBody.getAllAttachments();
    File newFile = null;
    for (Attachment attachment : attachmentList) {
        newFile = createFileFromAttachement(attachment);
    }
    try {
        if (newFile != null) {
            ZipFileApplicationDetails appDetails = ApplicationFileInstaller.getAppDetails(newFile);
            if (appDetails != null) {
                // lets get the existing app if it exists.
                Application existingApplication = appService.getApplication(appDetails.getName());
                // assume false until proved
                boolean wasExistingAppStarted = false;
                // otherwise.
                if (existingApplication != null) {
                    wasExistingAppStarted = appService.isApplicationStarted(existingApplication);
                    appService.removeApplication(existingApplication);
                }
                appService.addApplication(newFile.toURI());
                // if application was started before it was removed, lets try and start it.
                if (wasExistingAppStarted) {
                    appService.startApplication(appDetails.getName());
                }
            } else {
                throw new ApplicationServiceException("No Application details could be extracted from the provided file.");
            }
        } else {
            throw new ApplicationServiceException("No file attachment provided.");
        }
        // we need to output valid JSON to the client so fileupload can correctly call
        // done/fail callbacks correctly.
        Response.ResponseBuilder responseBuilder = Response.ok("{\"status\":\"success\"}").type("application/json");
        response = responseBuilder.build();
    } catch (ApplicationServiceException e) {
        LOGGER.warn("Unable to update an application on the server: {}", newFile, e);
        Response.ResponseBuilder responseBuilder = Response.serverError();
        response = responseBuilder.build();
    }
    LOGGER.trace("EXITING: update");
    return response;
}
Also used : Response(javax.ws.rs.core.Response) ZipFileApplicationDetails(org.codice.ddf.admin.application.service.impl.ZipFileApplicationDetails) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) File(java.io.File) Application(org.codice.ddf.admin.application.service.Application) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 9 with Application

use of org.codice.ddf.admin.application.service.Application in project ddf by codice.

the class TestApplicationService method bTestAppStatus.

@Test
public void bTestAppStatus() throws ApplicationServiceException, InterruptedException {
    systemSubject.execute(() -> {
        // Test AppService
        ApplicationService applicationService = getServiceManager().getService(ApplicationService.class);
        Set<Application> apps = applicationService.getApplications();
        List<Application> catalogList = apps.stream().filter(a -> CATALOG_APP.equals(a.getName())).collect(Collectors.toList());
        if (catalogList.size() != 1) {
            fail("Expected to find 1 " + CATALOG_APP + " in Application list.");
        }
        Application catalog = catalogList.get(0);
        try {
            applicationService.startApplication(catalog);
            getServiceManager().waitForAllBundles();
        } catch (ApplicationServiceException e) {
            LOGGER.error("Failed to start the {}: {}", CATALOG_APP, e.getMessage());
            fail();
        } catch (InterruptedException e) {
            LOGGER.error("Failed to start start all bundles {}", e.getMessage());
            fail();
        }
        assertNotNull("Application [" + CATALOG_APP + "] must not be null", catalog);
        ApplicationStatus status = applicationService.getApplicationStatus(catalog);
        assertThat("Application [" + CATALOG_APP + "] should be ACTIVE", status.getState(), is(ACTIVE));
        List<Application> registryList = apps.stream().filter(a -> REGISTRY_APP.equals(a.getName())).collect(Collectors.toList());
        if (catalogList.size() != 1) {
            fail("Expected to find 1 " + REGISTRY_APP + " in Application list.");
        }
        Application registry = registryList.get(0);
        assertNotNull("Application [" + REGISTRY_APP + "] must not be null", registry);
        status = applicationService.getApplicationStatus(registry);
        assertThat("Application [" + REGISTRY_APP + "] should be INACTIVE", status.getState(), is(INACTIVE));
        // Test Commands
        String response = console.runCommand(STATUS_COMMAND + CATALOG_APP);
        assertThat(CATALOG_APP + " should be ACTIVE", response, containsString(ACTIVE_APP));
        response = console.runCommand(STATUS_COMMAND + REGISTRY_APP);
        assertThat(REGISTRY_APP + " should be INACTIVE", response, containsString(INACTIVE_APP));
    });
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) MethodSorters(org.junit.runners.MethodSorters) PaxExam(org.ops4j.pax.exam.junit.PaxExam) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) INACTIVE(org.codice.ddf.admin.application.service.ApplicationStatus.ApplicationState.INACTIVE) CoreMatchers.not(org.hamcrest.CoreMatchers.not) RunWith(org.junit.runner.RunWith) LoggerFactory(org.slf4j.LoggerFactory) Application(org.codice.ddf.admin.application.service.Application) Assert.assertThat(org.junit.Assert.assertThat) Subject(org.apache.shiro.subject.Subject) BeforeExam(org.codice.ddf.itests.common.annotations.BeforeExam) AbstractIntegrationTest(org.codice.ddf.itests.common.AbstractIntegrationTest) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) ApplicationStatus(org.codice.ddf.admin.application.service.ApplicationStatus) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) LoggingUtils(org.codice.ddf.itests.common.utils.LoggingUtils) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Logger(org.slf4j.Logger) Security(org.codice.ddf.security.common.Security) Assert.assertNotNull(org.junit.Assert.assertNotNull) Set(java.util.Set) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SkipUnstableTest(org.codice.ddf.itests.common.annotations.SkipUnstableTest) ExamReactorStrategy(org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy) PerSuite(org.ops4j.pax.exam.spi.reactors.PerSuite) List(java.util.List) Rule(org.junit.Rule) ACTIVE(org.codice.ddf.admin.application.service.ApplicationStatus.ApplicationState.ACTIVE) FixMethodOrder(org.junit.FixMethodOrder) ConditionalIgnoreRule(org.codice.ddf.itests.common.annotations.ConditionalIgnoreRule) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) ApplicationStatus(org.codice.ddf.admin.application.service.ApplicationStatus) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Application(org.codice.ddf.admin.application.service.Application) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) AbstractIntegrationTest(org.codice.ddf.itests.common.AbstractIntegrationTest) Test(org.junit.Test) SkipUnstableTest(org.codice.ddf.itests.common.annotations.SkipUnstableTest)

Example 10 with Application

use of org.codice.ddf.admin.application.service.Application in project ddf by codice.

the class TestApplicationService method cTestAppStartStop.

@Test
// DDF-2954
@ConditionalIgnoreRule.ConditionalIgnore(condition = SkipUnstableTest.class)
public void cTestAppStartStop() throws ApplicationServiceException {
    systemSubject.execute(() -> {
        // Test AppService
        ApplicationService applicationService = getServiceManager().getService(ApplicationService.class);
        Application registry = applicationService.getApplication(REGISTRY_APP);
        assertNotNull("Application [" + REGISTRY_APP + "] must not be null", registry);
        ApplicationStatus status = applicationService.getApplicationStatus(registry);
        assertThat(REGISTRY_APP + " should be INACTIVE", status.getState(), is(INACTIVE));
        try {
            applicationService.startApplication(registry);
        } catch (ApplicationServiceException e) {
            LOGGER.error("Failed to start the {}: {}", REGISTRY_APP, e.getMessage());
            fail();
        }
        status = applicationService.getApplicationStatus(registry);
        assertThat(REGISTRY_APP + " should be ACTIVE after start, but was [" + status.getState() + "]", status.getState(), is(ACTIVE));
        try {
            applicationService.stopApplication(registry);
        } catch (ApplicationServiceException e) {
            LOGGER.error("Failed to stop the {}: {}", REGISTRY_APP, e.getMessage());
            fail();
        }
        status = applicationService.getApplicationStatus(registry);
        assertThat(REGISTRY_APP + " should be INACTIVE after stop", status.getState(), is(INACTIVE));
        // Test Commands
        String response = console.runCommand(STATUS_COMMAND + REGISTRY_APP);
        assertThat(REGISTRY_APP + " should be INACTIVE", response, containsString(INACTIVE_APP));
        response = console.runCommand(START_COMMAND + REGISTRY_APP);
        assertThat(REGISTRY_APP + " should be empty response after " + START_COMMAND, response, isEmptyString());
        response = console.runCommand(STATUS_COMMAND + REGISTRY_APP);
        assertThat(REGISTRY_APP + " should be ACTIVE after " + START_COMMAND, response, containsString(ACTIVE_APP));
        response = console.runCommand(STOP_COMMAND + REGISTRY_APP);
        assertThat(REGISTRY_APP + " should be empty response after " + START_COMMAND, response, isEmptyString());
        response = console.runCommand(STATUS_COMMAND + REGISTRY_APP);
        assertThat(REGISTRY_APP + " should be INACTIVE after " + STOP_COMMAND, response, containsString(INACTIVE_APP));
    });
}
Also used : ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) ApplicationStatus(org.codice.ddf.admin.application.service.ApplicationStatus) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Application(org.codice.ddf.admin.application.service.Application) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) AbstractIntegrationTest(org.codice.ddf.itests.common.AbstractIntegrationTest) Test(org.junit.Test) SkipUnstableTest(org.codice.ddf.itests.common.annotations.SkipUnstableTest)

Aggregations

Application (org.codice.ddf.admin.application.service.Application)55 Test (org.junit.Test)43 HashSet (java.util.HashSet)28 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)26 Repository (org.apache.karaf.features.Repository)21 FeaturesService (org.apache.karaf.features.FeaturesService)19 Feature (org.apache.karaf.features.Feature)14 ApplicationServiceException (org.codice.ddf.admin.application.service.ApplicationServiceException)13 ApplicationStatus (org.codice.ddf.admin.application.service.ApplicationStatus)13 ApplicationNode (org.codice.ddf.admin.application.service.ApplicationNode)11 ArrayList (java.util.ArrayList)9 RepositoryImpl (org.apache.karaf.features.internal.service.RepositoryImpl)7 HashMap (java.util.HashMap)5 Logger (org.slf4j.Logger)5 Appender (ch.qos.logback.core.Appender)4 URI (java.net.URI)4 TreeSet (java.util.TreeSet)4 ArgumentMatcher (org.mockito.ArgumentMatcher)4 Mockito.anyString (org.mockito.Mockito.anyString)4 SecurityServiceException (ddf.security.service.SecurityServiceException)3