Search in sources :

Example 6 with Subscription

use of org.ligoj.app.model.Subscription in project ligoj-api by ligoj.

the class NodeResource method findSubscriptionsWithParams.

/**
 * Find subscriptions where some parameters are defined.
 *
 * @param id
 *            node identifier
 * @return subscriptions with redefined parameters
 */
protected Map<Subscription, Map<String, String>> findSubscriptionsWithParams(final String id) {
    final Map<Subscription, Map<String, String>> result = new HashMap<>();
    for (final Object[] entityTab : subscriptionRepository.findAllWithValuesByNode(id)) {
        final ParameterValue value = (ParameterValue) entityTab[1];
        result.computeIfAbsent((Subscription) entityTab[0], s -> new HashMap<>()).put(value.getParameter().getId(), value.getData());
    }
    return result;
}
Also used : Parameter(org.ligoj.app.model.Parameter) Produces(javax.ws.rs.Produces) BiFunction(java.util.function.BiFunction) Path(javax.ws.rs.Path) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) SubscriptionMode(org.ligoj.app.api.SubscriptionMode) DataTableAttributes(org.ligoj.bootstrap.core.json.datatable.DataTableAttributes) Map(java.util.Map) ListUtils(org.apache.commons.collections4.ListUtils) DefaultValue(javax.ws.rs.DefaultValue) Subscription(org.ligoj.app.model.Subscription) DELETE(javax.ws.rs.DELETE) Context(javax.ws.rs.core.Context) NodeRepository(org.ligoj.app.dao.NodeRepository) NodeStatus(org.ligoj.app.api.NodeStatus) Transactional(javax.transaction.Transactional) Collection(java.util.Collection) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) OnNullReturn404(org.ligoj.bootstrap.core.resource.OnNullReturn404) Entry(java.util.Map.Entry) Optional(java.util.Optional) UriInfo(javax.ws.rs.core.UriInfo) TableItem(org.ligoj.bootstrap.core.json.TableItem) ParameterRepository(org.ligoj.app.dao.ParameterRepository) LongTaskRunner(org.ligoj.app.resource.plugin.LongTaskRunner) PathParam(javax.ws.rs.PathParam) CacheRemoveAll(javax.cache.annotation.CacheRemoveAll) GET(javax.ws.rs.GET) ToolPlugin(org.ligoj.app.api.ToolPlugin) HashMap(java.util.HashMap) NamedBean(org.ligoj.bootstrap.core.NamedBean) Scheduled(org.springframework.scheduling.annotation.Scheduled) EventRepository(org.ligoj.app.dao.EventRepository) Function(java.util.function.Function) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) SubscriptionRepository(org.ligoj.app.dao.SubscriptionRepository) SecurityHelper(org.ligoj.bootstrap.core.security.SecurityHelper) EnumUtils(org.apache.commons.lang3.EnumUtils) Service(org.springframework.stereotype.Service) ParameterValue(org.ligoj.app.model.ParameterValue) NodeVo(org.ligoj.app.api.NodeVo) PaginationJson(org.ligoj.bootstrap.core.json.PaginationJson) POST(javax.ws.rs.POST) Node(org.ligoj.app.model.Node) SubscriptionStatusWithData(org.ligoj.app.api.SubscriptionStatusWithData) EventType(org.ligoj.app.model.EventType) BusinessException(org.ligoj.bootstrap.core.resource.BusinessException) CacheResult(javax.cache.annotation.CacheResult) ServicePlugin(org.ligoj.app.api.ServicePlugin) PUT(javax.ws.rs.PUT) ParameterValue(org.ligoj.app.model.ParameterValue) HashMap(java.util.HashMap) Subscription(org.ligoj.app.model.Subscription) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with Subscription

use of org.ligoj.app.model.Subscription in project ligoj-api by ligoj.

the class NodeResource method checkSubscriptionStatus.

/**
 * Check status subscription.
 *
 * @param node
 *            node where we must check subscriptions
 * @param status
 *            node status
 */
protected void checkSubscriptionStatus(final Node node, final NodeStatus status) {
    final Map<String, String> nodeParameters = pvResource.getNodeParameters(node.getId());
    // Retrieve subscriptions where parameters are redefined.
    // Other subscriptions get the node's status.
    final Map<Subscription, Map<String, String>> subscriptions = findSubscriptionsWithParams(node.getId());
    NodeStatus newStatus = status;
    if (status == null) {
        // Node status is unknown for now, need a check
        newStatus = NodeStatus.getValue(self.checkNodeStatus(node.getId(), nodeParameters).isUp());
        // Update the node status
        eventResource.registerEvent(node, EventType.STATUS, newStatus.name());
    }
    // Check the subscriptions
    if (newStatus.isUp()) {
        // Check only the subscription in UP nodes
        checkNodeSubscriptions(node, nodeParameters, subscriptions);
    } else {
        // All subscription of this are marked as DOWN
        log.info("Node {} is DOWN, as well for {} related subscriptions", node.getId(), subscriptions.size());
        subscriptions.entrySet().forEach(s -> eventResource.registerEvent(s.getKey(), EventType.STATUS, NodeStatus.DOWN.name()));
    }
}
Also used : Subscription(org.ligoj.app.model.Subscription) Map(java.util.Map) HashMap(java.util.HashMap) NodeStatus(org.ligoj.app.api.NodeStatus)

Example 8 with Subscription

use of org.ligoj.app.model.Subscription in project ligoj-api by ligoj.

the class ToVoConverterTest method apply.

@Test
public void apply() {
    // Sub user repository
    final IamProvider iamProvider = Mockito.mock(IamProvider.class);
    final IUserRepository userRepository = Mockito.mock(IUserRepository.class);
    final IamConfiguration configuration = new IamConfiguration();
    configuration.setUserRepository(userRepository);
    Mockito.when(iamProvider.getConfiguration()).thenReturn(configuration);
    Mockito.when(userRepository.findById(ArgumentMatchers.anyString())).then(invocation -> toUser((String) invocation.getArguments()[0]));
    // Stub subscriptions
    final List<Object[]> subscriptions = new ArrayList<>();
    final Parameter parameter1 = new Parameter();
    parameter1.setId(IdentityResource.PARAMETER_GROUP);
    parameter1.setType(ParameterType.TEXT);
    parameter1.setOwner(new Node());
    final Parameter parameter2 = new Parameter();
    parameter2.setId(IdentityResource.PARAMETER_GROUP);
    parameter2.setType(ParameterType.TEXT);
    parameter2.setOwner(new Node());
    final Parameter parameter3 = new Parameter();
    parameter3.setId("any");
    parameter3.setType(ParameterType.TEXT);
    parameter3.setOwner(new Node());
    final ParameterValue value1 = new ParameterValue();
    value1.setId(1);
    value1.setParameter(parameter1);
    value1.setData("G");
    final ParameterValue value2 = new ParameterValue();
    value2.setId(2);
    value2.setParameter(parameter2);
    value2.setData("any");
    final ParameterValue value3 = new ParameterValue();
    value3.setId(3);
    value3.setParameter(parameter3);
    value3.setData("any");
    final Subscription subscription = new Subscription();
    subscription.setId(1);
    final Node node = new Node();
    node.setId("service:n2");
    subscription.setNode(node);
    subscriptions.add(new Object[] { subscription, value1 });
    subscriptions.add(new Object[] { subscription, value2 });
    subscriptions.add(new Object[] { subscription, value3 });
    // Subscription without status
    final Subscription subscription2 = new Subscription();
    subscription2.setId(-1);
    final Node node2 = new Node();
    node2.setId("service:n1");
    subscription2.setNode(node2);
    subscriptions.add(new Object[] { subscription2, value3 });
    // Stub events
    final Map<Integer, EventVo> events = new HashMap<>();
    final EventVo event = new EventVo();
    event.setSubscription(1);
    event.setNode(new NodeVo());
    event.setValue("UP");
    events.put(1, event);
    // Call
    final ToVoConverter converter = new ToVoConverter(this::toUser, subscriptions, events);
    final Project entity = new Project();
    entity.setId(1);
    entity.setName("N");
    entity.setDescription("D");
    entity.setLastModifiedBy("U1");
    entity.setLastModifiedDate(new Date());
    entity.setCreatedBy("U2");
    entity.setCreatedDate(new Date());
    entity.setPkey("PK");
    entity.setTeamLeader("U3");
    entity.setSubscriptions(Arrays.asList(new Subscription[] { subscription, subscription2 }));
    final ProjectVo vo = converter.apply(entity);
    // Check
    Assertions.assertEquals("N", vo.getName());
    Assertions.assertEquals("D", vo.getDescription());
    Assertions.assertEquals("PK", vo.getPkey());
    Assertions.assertEquals("U2", vo.getCreatedBy().getId());
    Assertions.assertNotNull(vo.getCreatedDate());
    Assertions.assertEquals("U1", vo.getLastModifiedBy().getId());
    Assertions.assertNotNull(vo.getLastModifiedDate());
    Assertions.assertEquals("U3", vo.getTeamLeader().getId());
    Assertions.assertEquals(1, vo.getId().intValue());
    Assertions.assertEquals(2, vo.getSubscriptions().size());
    // Check the statuses and their order by node
    final List<SubscriptionVo> subscriptionsVo = vo.getSubscriptions();
    Assertions.assertNull(subscriptionsVo.get(0).getStatus());
    Assertions.assertEquals("service:n1", subscriptionsVo.get(0).getNode().getId());
    Assertions.assertEquals(NodeStatus.UP, subscriptionsVo.get(1).getStatus());
    Assertions.assertEquals("service:n2", subscriptionsVo.get(1).getNode().getId());
}
Also used : ParameterValue(org.ligoj.app.model.ParameterValue) HashMap(java.util.HashMap) Node(org.ligoj.app.model.Node) ArrayList(java.util.ArrayList) IUserRepository(org.ligoj.app.iam.IUserRepository) IamProvider(org.ligoj.app.iam.IamProvider) Date(java.util.Date) Project(org.ligoj.app.model.Project) Parameter(org.ligoj.app.model.Parameter) IamConfiguration(org.ligoj.app.iam.IamConfiguration) Subscription(org.ligoj.app.model.Subscription) SubscriptionVo(org.ligoj.app.resource.subscription.SubscriptionVo) NodeVo(org.ligoj.app.api.NodeVo) EventVo(org.ligoj.app.resource.node.EventVo) Test(org.junit.jupiter.api.Test)

Example 9 with Subscription

use of org.ligoj.app.model.Subscription in project ligoj-api by ligoj.

the class LongTaskRunnerTest method newTaskSample.

private TaskSampleSubscription newTaskSample() {
    final TaskSampleSubscription taskSample = new TaskSampleSubscription();
    taskSample.setAuthor(DEFAULT_USER);
    taskSample.setData("custom");
    taskSample.setStart(new Date());
    taskSample.setEnd(new Date());
    taskSample.setLocked(em.find(Subscription.class, subscription));
    return taskSample;
}
Also used : TaskSampleSubscription(org.ligoj.app.model.TaskSampleSubscription) Subscription(org.ligoj.app.model.Subscription) TaskSampleSubscription(org.ligoj.app.model.TaskSampleSubscription) Date(java.util.Date)

Example 10 with Subscription

use of org.ligoj.app.model.Subscription in project ligoj-api by ligoj.

the class PluginsClassLoaderTest method toFile.

@Test
public void toFile() throws Exception {
    PluginsClassLoader classLoader = null;
    try {
        System.setProperty("ligoj.home", USER_HOME_DIRECTORY + "/.ligoj");
        final File file = new File(USER_HOME_DIRECTORY, ".ligoj/service-id/ldap/server1/42/foo/bar.log");
        final File subscriptionParent = new File(USER_HOME_DIRECTORY, ".ligoj/service-id");
        FileUtils.deleteQuietly(subscriptionParent);
        Assertions.assertFalse(subscriptionParent.exists());
        Assertions.assertFalse(file.exists());
        classLoader = checkClassLoader();
        final Subscription subscription = newSubscription();
        final File cfile = classLoader.toFile(subscription, "foo", "bar.log");
        Assertions.assertTrue(subscriptionParent.exists());
        Assertions.assertTrue(cfile.getParentFile().exists());
        Assertions.assertTrue(file.getParentFile().exists());
        Assertions.assertFalse(file.exists());
    } finally {
        System.clearProperty("ligoj.home");
        IOUtils.closeQuietly(classLoader);
    }
}
Also used : Subscription(org.ligoj.app.model.Subscription) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

Subscription (org.ligoj.app.model.Subscription)43 Test (org.junit.jupiter.api.Test)22 Path (javax.ws.rs.Path)10 Node (org.ligoj.app.model.Node)10 AbstractServerTest (org.ligoj.app.AbstractServerTest)9 IOException (java.io.IOException)7 GET (javax.ws.rs.GET)7 POST (javax.ws.rs.POST)7 HashMap (java.util.HashMap)6 Transactional (javax.transaction.Transactional)6 Produces (javax.ws.rs.Produces)6 Map (java.util.Map)5 Optional (java.util.Optional)5 PathParam (javax.ws.rs.PathParam)5 MediaType (javax.ws.rs.core.MediaType)5 Slf4j (lombok.extern.slf4j.Slf4j)5 File (java.io.File)4 Date (java.util.Date)4 Function (java.util.function.Function)4 DELETE (javax.ws.rs.DELETE)4