use of io.gravitee.repository.management.model.Event in project gravitee-management-rest-api by gravitee-io.
the class EventServiceImpl method convert.
private Event convert(NewEventEntity newEventEntity) {
Event event = new Event();
event.setType(io.gravitee.repository.management.model.EventType.valueOf(newEventEntity.getType().toString()));
event.setPayload(newEventEntity.getPayload());
event.setParentId(newEventEntity.getParentId());
event.setProperties(newEventEntity.getProperties());
return event;
}
use of io.gravitee.repository.management.model.Event in project gravitee-gateway by gravitee-io.
the class SyncManager method getLastEvent.
private Event getLastEvent(final String api, long nextLastRefreshAt) {
final EventCriteria.Builder eventCriteriaBuilder;
if (lastRefreshAt == -1) {
eventCriteriaBuilder = new EventCriteria.Builder().property(Event.EventProperties.API_ID.getValue(), api);
} else {
eventCriteriaBuilder = new EventCriteria.Builder().property(Event.EventProperties.API_ID.getValue(), api).from(lastRefreshAt - TIMEFRAME_BEFORE_DELAY).to(nextLastRefreshAt + TIMEFRAME_AFTER_DELAY);
}
List<Event> events = eventRepository.search(eventCriteriaBuilder.types(EventType.PUBLISH_API, EventType.UNPUBLISH_API, EventType.START_API, EventType.STOP_API).build(), new PageableBuilder().pageNumber(0).pageSize(1).build()).getContent();
return (!events.isEmpty()) ? events.get(0) : null;
}
use of io.gravitee.repository.management.model.Event in project gravitee-gateway by gravitee-io.
the class SyncManager method refresh.
public void refresh() {
logger.debug("Synchronization #{} started at {}", counter.incrementAndGet(), Instant.now().toString());
logger.debug("Refreshing gateway state...");
try {
// Extract all registered APIs
Map<String, io.gravitee.repository.management.model.Api> apis = apiRepository.findAll().stream().collect(Collectors.toMap(io.gravitee.repository.management.model.Api::getId, api -> api));
long nextLastRefreshAt = System.currentTimeMillis();
// Get last event by API
Map<String, Event> events = new HashMap<>();
apis.forEach((id, api) -> {
Event event = getLastEvent(id, nextLastRefreshAt);
if (event != null) {
events.put(id, event);
}
});
// Extract definition for each event
Map<String, Api> definitions = new HashMap<>();
events.values().forEach(event -> {
try {
// Read API definition from event
io.gravitee.repository.management.model.Api eventPayload = objectMapper.readValue(event.getPayload(), io.gravitee.repository.management.model.Api.class);
Api definition = objectMapper.readValue(eventPayload.getDefinition(), Api.class);
io.gravitee.repository.management.model.Api api = apis.get(definition.getId());
// Update definition with required information for deployment phase
definition.setEnabled(api.getLifecycleState() == LifecycleState.STARTED);
definition.setDeployedAt(eventPayload.getDeployedAt());
definitions.put(definition.getId(), definition);
} catch (IOException ioe) {
logger.error("Error while determining deployed APIs store into events payload", ioe);
}
});
// Determine APIs to undeploy because of deployment tags
Set<String> apisToRemove = definitions.values().stream().filter(api -> !hasMatchingTags(api)).map(Api::getId).collect(Collectors.toSet());
// Undeploy APIs not relative to this gateway instance (different deployment tags)
apisToRemove.forEach(apiId -> {
apiManager.undeploy(apiId);
apis.remove(apiId);
events.remove(apiId);
definitions.remove(apiId);
});
// Determine API which must be stopped and stop them
events.entrySet().stream().filter(apiEvent -> {
Event event = apiEvent.getValue();
return event.getType() == EventType.STOP_API || event.getType() == EventType.UNPUBLISH_API;
}).forEach(apiEvent -> apiManager.undeploy(apiEvent.getKey()));
// Determine API which must be deployed
events.entrySet().stream().filter(apiEvent -> {
Event event = apiEvent.getValue();
return event.getType() == EventType.START_API || event.getType() == EventType.PUBLISH_API;
}).forEach(apiEvent -> {
// Read API definition from event
Api definition = definitions.get(apiEvent.getKey());
// API to deploy
enhanceWithData(definition);
if (definition != null) {
// Get deployed API
Api deployedApi = apiManager.get(definition.getId());
// API is not yet deployed, so let's do it !
if (deployedApi == null) {
apiManager.deploy(definition);
} else if (deployedApi.getDeployedAt().before(definition.getDeployedAt())) {
apiManager.update(definition);
}
}
});
lastRefreshAt = nextLastRefreshAt;
} catch (TechnicalException te) {
logger.error("Unable to sync instance", te);
}
logger.debug("Synchronization #{} ended at {}", counter.get(), Instant.now().toString());
}
use of io.gravitee.repository.management.model.Event in project gravitee-gateway by gravitee-io.
the class SyncManagerTest method test_shouldUndeployIfLastEventIsUnpublishAPI.
@Test
public void test_shouldUndeployIfLastEventIsUnpublishAPI() throws Exception {
io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").build();
final Api mockApi = mockApi(api);
final Event mockEvent = mockEvent(api, EventType.PUBLISH_API);
final Event mockEvent2 = mockEvent(api, EventType.UNPUBLISH_API);
when(eventRepository.search(any(EventCriteria.class), any(Pageable.class))).thenReturn(new Page<>(Collections.singletonList(mockEvent), 0, 0, 1), new Page<>(Collections.singletonList(mockEvent2), 0, 0, 1));
when(apiRepository.findAll()).thenReturn(Collections.singleton(api));
syncManager.refresh();
when(apiManager.apis()).thenReturn(Collections.singleton(mockApi));
when(apiRepository.findAll()).thenReturn(Collections.singleton(api));
syncManager.refresh();
verify(apiManager).deploy(mockApi);
verify(apiManager, never()).update(any(Api.class));
verify(apiManager).undeploy(mockApi.getId());
}
use of io.gravitee.repository.management.model.Event in project gravitee-gateway by gravitee-io.
the class SyncManagerTest method test_shouldUpdateIfLastEventIsStopAPI.
@Test
public void test_shouldUpdateIfLastEventIsStopAPI() throws Exception {
io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").lifecycleState(LifecycleState.STARTED).build();
Instant updateDateInst = api.getUpdatedAt().toInstant().plus(Duration.ofHours(1));
io.gravitee.repository.management.model.Api api2 = new RepositoryApiBuilder().id("api-test").updatedAt(Date.from(updateDateInst)).definition("test").lifecycleState(LifecycleState.STOPPED).build();
final Api mockApi = mockApi(api);
mockApi(api2);
final Event mockEvent = mockEvent(api, EventType.PUBLISH_API);
final Event mockEvent2 = mockEvent(api2, EventType.STOP_API);
List<Event> events = new ArrayList<Event>();
events.add(mockEvent);
events.add(mockEvent2);
when(eventRepository.search(any(EventCriteria.class), any(Pageable.class))).thenReturn(new Page<>(Collections.singletonList(mockEvent), 0, 0, 1));
when(apiRepository.findAll()).thenReturn(Collections.singleton(api));
when(apiManager.apis()).thenReturn(Collections.singleton(mockApi));
syncManager.refresh();
when(eventRepository.search(any(EventCriteria.class), any(Pageable.class))).thenReturn(new Page<>(events, 0, 0, 2));
when(apiRepository.findAll()).thenReturn(Collections.singleton(api2));
when(apiManager.get(api.getId())).thenReturn(mockApi);
syncManager.refresh();
verify(apiManager).deploy(mockApi);
verify(apiManager).update(mockApi);
verify(apiManager, never()).undeploy(any(String.class));
}
Aggregations