private Map<String, Object> marshalChannelInfo(ChannelInfo channelInfo) throws ParseException { Map<String, Object> unmarshalledChannelInfo = new HashMap<>(); unmarshalledChannelInfo.put(ChannelInfoSchema.CHANNEL_NAME, channelInfo.getName()); unmarshalledChannelInfo.put(ChannelInfoSchema.CHANNEL_TIMESTAMP, KapuaDateUtils.formatDate(channelInfo.getFirstMessageOn())); unmarshalledChannelInfo.put(ChannelInfoSchema.CHANNEL_CLIENT_ID, channelInfo.getClientId()); unmarshalledChannelInfo.put(ChannelInfoSchema.CHANNEL_SCOPE_ID, channelInfo.getScopeId().toStringId()); unmarshalledChannelInfo.put(ChannelInfoSchema.CHANNEL_MESSAGE_ID, channelInfo.getFirstMessageId().toString()); return unmarshalledChannelInfo; }
private ChannelInfo unmarshalChannelInfo(Map<String, Object> channelInfoMap) throws DatamodelMappingException, JsonParseException, JsonMappingException, IOException, ParseException { KapuaId scopeId = new KapuaEid(new BigInteger((String) channelInfoMap.get(ChannelInfoSchema.CHANNEL_SCOPE_ID))); ChannelInfo channelInfo = new ChannelInfoImpl(scopeId); String id = (String) channelInfoMap.get(ModelContext.DATASTORE_ID_KEY); channelInfo.setId(new StorableIdImpl(id)); channelInfo.setClientId((String) channelInfoMap.get(ChannelInfoSchema.CHANNEL_CLIENT_ID)); channelInfo.setName((String) channelInfoMap.get(ChannelInfoSchema.CHANNEL_NAME)); channelInfo.setFirstMessageId(new StorableIdImpl((String) channelInfoMap.get(ChannelInfoSchema.CHANNEL_MESSAGE_ID))); channelInfo.setFirstMessageOn(KapuaDateUtils.parseDate((String) channelInfoMap.get(ChannelInfoSchema.CHANNEL_TIMESTAMP))); return channelInfo; }
/** * Get the channel identifier getting parameters from the channelInfo. Then it calls {@link ChannelInfoField#getOrDeriveId(StorableId id, KapuaId scopeId, String clientId, String channel)} * * @param id * @param channelInfo * @return */ public static String getOrDeriveId(StorableId id, ChannelInfo channelInfo) { return getOrDeriveId(id, channelInfo.getScopeId(), channelInfo.getClientId(), channelInfo.getName()); }
sort.add(SortField.descending(MessageSchema.MESSAGE_TIMESTAMP)); MessageQuery messageQuery = new MessageQueryImpl(channelInfo.getScopeId()); messageQuery.setAskTotalCount(true); messageQuery.setFetchStyle(StorableFetchStyle.FIELDS); messageQuery.setSortFields(sort); RangePredicate messageIdPredicate = new RangePredicateImpl(new StorableFieldImpl(ChannelInfoSchema.CHANNEL_TIMESTAMP), channelInfo.getFirstMessageOn(), null); TermPredicate clientIdPredicate = storablePredicateFactory.newTermPredicate(MessageField.CLIENT_ID, channelInfo.getClientId()); TermPredicate channelPredicate = storablePredicateFactory.newTermPredicate(MessageField.CHANNEL, channelInfo.getName()); } else if (messageList.isEmpty()) { LOG.warn("Cannot find last timestamp for the specified client id '{}' - account '{}'", channelInfo.getScopeId(), channelInfo.getClientId()); } else { LOG.error("Cannot find last timestamp for the specified client id '{}' - account '{}'. More than one result returned by the query!", channelInfo.getScopeId(), channelInfo.getClientId()); channelInfo.setLastMessageId(lastPublishedMessageId); channelInfo.setLastMessageOn(lastPublishedMessageTimestamp);
ConfigurationException, ClientException { ArgumentValidator.notNull(channelInfo, "channelInfo"); ArgumentValidator.notNull(channelInfo.getScopeId(), "channelInfo.scopeId"); ArgumentValidator.notNull(channelInfo.getName(), "channelInfo.name"); ArgumentValidator.notNull(channelInfo.getFirstMessageId(), "channelInfo.messageId"); ArgumentValidator.notNull(channelInfo.getFirstMessageOn(), "channelInfo.messageTimestamp"); String channelInfoId = ChannelInfoField.getOrDeriveId(channelInfo.getId(), channelInfo); StorableId storableId = new StorableIdImpl(channelInfoId); if (!DatastoreCacheManager.getInstance().getChannelsCache().get(channelInfoId)) { ChannelInfo storedField = find(channelInfo.getScopeId(), storableId); if (storedField == null) { Metadata metadata = mediator.getMetadata(channelInfo.getScopeId(), channelInfo.getFirstMessageOn().getTime()); String registryIndexName = metadata.getRegistryIndexName(); UpdateRequest request = new UpdateRequest(channelInfo.getId().toString(), new TypeDescriptor(metadata.getRegistryIndexName(), ChannelInfoSchema.CHANNEL_TYPE_NAME), channelInfo); response = client.upsert(request);
@Then("^Client \"(.+)\" first published on a channel in the list \"(.+)\" on \"(.+)\"$") public void checkFirstPublishDateOnChannel(String clientId, String lstKey, String date) throws Exception { ChannelInfoListResult chnList = (ChannelInfoListResult) stepData.get(lstKey); Date tmpCaptured = KapuaDateUtils.parseDate(date); for (ChannelInfo tmpInfo : chnList.getItems()) { if (tmpInfo.getClientId().equals(clientId)) { assertEquals(tmpCaptured, tmpInfo.getFirstMessageOn()); return; } } fail(String.format("No channel matches the client id [%s]", clientId)); }
private void checkChannelInfoAgainstPreparedMessages(ChannelInfoListResult chnInfo, List<KapuaDataMessage> msgLst) { Set<String> msgTopics = new HashSet<>(); Set<String> msgClients = new HashSet<>(); Set<String> infoTopics = new HashSet<>(); Set<String> infoClients = new HashSet<>(); assertNotNull("No channel info data!", chnInfo); assertNotNull("No messages to compare to!", msgLst); for (KapuaDataMessage tmpMsg : msgLst) { msgClients.add(tmpMsg.getClientId()); msgTopics.add(tmpMsg.getChannel().toString()); } for (ChannelInfo tmpInfo : chnInfo.getItems()) { infoClients.add(tmpInfo.getClientId()); infoTopics.add(tmpInfo.getName()); } assertEquals("The number of clients does not match!", msgClients.size(), infoClients.size()); assertEquals("The number of topics does not match!", msgTopics.size(), infoTopics.size()); for (String tmpTopic : msgTopics) { assertTrue(String.format("The topic [%s] is not found in the info list!", tmpTopic), infoTopics.contains(tmpTopic)); } for (String tmpClient : msgClients) { assertTrue(String.format("The client id [%s] is not found in the info list!", tmpClient), infoClients.contains(tmpClient)); } }
private void isChannelForFirstMessageInStoreOK(StorableId msgId, Date storedOn) throws KapuaException { KapuaId tmpAccId = ((Account) stepData.get("LastAccount")).getId(); String tmpClId = ((Device) stepData.get("LastDevice")).getClientId(); AndPredicate andPredicate = new AndPredicateImpl(); andPredicate.getPredicates().add(new TermPredicateImpl(ChannelInfoField.CLIENT_ID, tmpClId)); ChannelInfoQuery channelInfoQuery = DatastoreQueryFactory.createBaseChannelInfoQuery(tmpAccId, 100); channelInfoQuery.setPredicate(andPredicate); channelInfoQuery.addFetchAttributes(ChannelInfoField.TIMESTAMP.field()); ChannelInfoListResult channelInfoList = channelInfoRegistryService.query(channelInfoQuery); assertNotNull("Cannot find the channel info registry!", channelInfoList); assertNotEquals("Cannot find the channel info registry!", channelInfoList.getSize(), 0); assertNotNull("Cannot find the channel info registry!", channelInfoList.getFirstItem()); assertEquals("Wrong channel info message id!", channelInfoList.getFirstItem().getFirstMessageId(), msgId); assertEquals("Wrong channel info message on!", channelInfoList.getFirstItem().getFirstMessageOn(), storedOn); }
@Then("^Client \"(.+)\" last published on a channel in the list \"(.+)\" on \"(.+)\"$") public void checkLastPublishDateOnChannel(String clientId, String lstKey, String date) throws Exception { ChannelInfoListResult chnList = (ChannelInfoListResult) stepData.get(lstKey); Date tmpCaptured = KapuaDateUtils.parseDate(date); for (ChannelInfo tmpInfo : chnList.getItems()) { if (tmpInfo.getClientId().equals(clientId)) { assertEquals(tmpCaptured, tmpInfo.getLastMessageOn()); return; } } fail(String.format("No channel matches the client id [%s]", clientId)); }
@When("^I pick the ID of the channel number (\\d+) in the list \"(.*)\" and remember it as \"(.*)\"$") public void pickAChannelIdFromAList(int index, String lstKey, String idKey) { ChannelInfoListResult tmpLst = (ChannelInfoListResult) stepData.get(lstKey); StorableId tmpId = tmpLst.getItem(index).getId(); stepData.put(idKey, tmpId); }
@Then("^The channel list \"(.+)\" contains the following topics$") public void checkThattheChannelInfoContainsTheSpecifiedTopics(String lstKey, List<TestTopic> topics) { ChannelInfoListResult tmpList = (ChannelInfoListResult) stepData.get(lstKey); Set<String> infoTopics = new HashSet<>(); assertEquals("Wrong number of topics found!", tmpList.getSize(), topics.size()); for (ChannelInfo tmpInfo : tmpList.getItems()) { infoTopics.add(tmpInfo.getName()); } for (TestTopic tmpTop : topics) { assertTrue(String.format("The topic [%s] was not found!", tmpTop.getTopic()), infoTopics.contains(tmpTop.getTopic())); } }
@When("^I delete all channels from the list \"(.*)\"$") public void deleteAllChannelsFromList(String lstKey) throws KapuaException { Account account = (Account) stepData.get("LastAccount"); ChannelInfoListResult tmpList = (ChannelInfoListResult) stepData.get(lstKey); for (ChannelInfo tmpItem : tmpList.getItems()) { channelInfoRegistryServiceProxy.delete(account.getId(), tmpItem.getId()); } }
String id = channels.getResult().get(0).getId().toString(); if (channelsCache.get(id)) { channelsCache.remove(id);