0f7c5b88fd04e25bea6113dfc783a05e4e2045f8David Luna/*
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * The contents of this file are subject to the terms of the Common Development and
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * Distribution License (the License). You may not use this file except in compliance with the
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * License.
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott *
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * specific language governing permission and limitations under the License.
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott *
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * When distributing Covered Software, include this CDDL Header Notice in each file and include
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * Header, with the fields enclosed by brackets [] replaced by your own identifying
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * information: "Portions copyright [year] [name of copyright owner]".
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott *
1b5cb8422c825dbdc6d7669e74f285cede6ab71cJames Phillpotts * Copyright 2014-2015 ForgeRock AS.
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott */
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Lunapackage org.forgerock.openam.cts.monitoring.impl.persistence;
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshottimport org.forgerock.openam.cts.CTSPersistentStore;
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshottimport org.forgerock.openam.cts.api.filter.TokenFilter;
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshottimport org.forgerock.openam.cts.api.filter.TokenFilterBuilder;
1b5cb8422c825dbdc6d7669e74f285cede6ab71cJames Phillpottsimport org.forgerock.openam.tokens.TokenType;
1b5cb8422c825dbdc6d7669e74f285cede6ab71cJames Phillpottsimport org.forgerock.openam.tokens.CoreTokenField;
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Lunaimport org.forgerock.openam.cts.exceptions.CoreTokenException;
a093731116a8c24d49b903df7602cf586e499b45Phill Cunningtonimport org.forgerock.openam.sm.datalayer.api.query.PartialToken;
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshottimport org.forgerock.openam.utils.TimeUtils;
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshottimport javax.inject.Inject;
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshottimport java.util.ArrayList;
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshottimport java.util.Calendar;
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshottimport java.util.Collection;
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna/**
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna * Used to query the CTS persistence store and return data to the monitoring service
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna */
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Lunapublic class CtsPersistenceOperationsDelegate {
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott private final CTSPersistentStore store;
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna @Inject
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott public CtsPersistenceOperationsDelegate(CTSPersistentStore store) {
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott this.store = store;
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna }
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna /**
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * Counts the number of tokens in the persistent store that match the requested type.
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna *
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna * @param tokenType The type of token for which we are gathering results
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * @return Zero or positive integer of the number of tokens in the store.
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna * @throws CoreTokenException
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna */
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott public int countTokenEntries(TokenType tokenType) throws CoreTokenException {
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna //create the filter to restrict by token type
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott final TokenFilter tokenFilter = new TokenFilterBuilder()
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott .returnAttribute(CoreTokenField.TOKEN_ID)
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott .and()
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott .withAttribute(CoreTokenField.TOKEN_TYPE, tokenType)
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott .build();
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott return store.attributeQuery(tokenFilter).size();
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna }
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna
0f7c5b88fd04e25bea6113dfc783a05e4e2045f8David Luna /**
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * Counts all Tokens within the CTS Persistent store. All tokens are considered regardless of TokenType.
0f7c5b88fd04e25bea6113dfc783a05e4e2045f8David Luna *
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * @return Zero or positive integer.
0f7c5b88fd04e25bea6113dfc783a05e4e2045f8David Luna * @throws CoreTokenException if there are issues talking with the CTS
0f7c5b88fd04e25bea6113dfc783a05e4e2045f8David Luna */
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott public int countAllTokens() throws CoreTokenException {
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott TokenFilter filter = new TokenFilterBuilder().returnAttribute(CoreTokenField.TOKEN_ID).build();
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott return store.attributeQuery(filter).size();
0f7c5b88fd04e25bea6113dfc783a05e4e2045f8David Luna }
0f7c5b88fd04e25bea6113dfc783a05e4e2045f8David Luna
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna /**
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna * Gathers list of the durations of tokens in epoch'd seconds
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna *
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna * @param tokenType The type of token for which we are gathering results
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna * @return A collection of longs, each of which represents the duration of a token inside the CTS
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna * @throws CoreTokenException
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna */
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna public Collection<Long> listDurationOfTokens(TokenType tokenType) throws CoreTokenException {
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna final Collection<Long> results = new ArrayList<Long>();
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott final long unixTime = TimeUtils.currentUnixTime();
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott final TokenFilter filter = new TokenFilterBuilder()
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott .returnAttribute(CoreTokenField.CREATE_TIMESTAMP)
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott .and()
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott .withAttribute(CoreTokenField.TOKEN_TYPE, tokenType)
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott .build();
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott for (PartialToken token : store.attributeQuery(filter)) {
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott Calendar timestamp = token.getValue(CoreTokenField.CREATE_TIMESTAMP);
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott results.add(unixTime - TimeUtils.toUnixTime(timestamp));
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna }
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna return results;
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna }
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna}