Search in sources :

Example 21 with Pair

use of org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair in project cas by apereo.

the class RegisteredServiceMutantRegexAttributeFilter method filter.

@Override
public Map<String, Object> filter(final Map<String, Object> givenAttributes) {
    final Map<String, Object> attributesToRelease = new HashMap<>();
    givenAttributes.entrySet().stream().filter(filterProvidedGivenAttributes()).forEach(entry -> {
        final String attributeName = entry.getKey();
        if (getPatterns().containsKey(attributeName)) {
            final Set<Object> attributeValues = CollectionUtils.toCollection(entry.getValue());
            LOGGER.debug("Found attribute [{}] in pattern definitions with value(s) [{}]", attributeName, attributeValues);
            final Collection<Pair<Pattern, String>> patterns = createPatternsAndReturnValue(attributeName);
            final List<Object> finalValues = patterns.stream().map(patternDefn -> {
                final Pattern pattern = patternDefn.getLeft();
                LOGGER.debug("Found attribute [{}] in the pattern definitions. Processing pattern [{}]", attributeName, pattern.pattern());
                final List<Object> filteredValues = filterAndMapAttributeValuesByPattern(attributeValues, pattern, patternDefn.getValue());
                LOGGER.debug("Filtered attribute values for [{}] are [{}]", attributeName, filteredValues);
                return filteredValues;
            }).flatMap(Collection::stream).collect(Collectors.toList());
            if (finalValues.isEmpty()) {
                LOGGER.debug("Attribute [{}] has no values remaining and shall be excluded", attributeName);
            } else {
                collectAttributeWithFilteredValues(attributesToRelease, attributeName, finalValues);
            }
        } else {
            handleUnmappedAttribute(attributesToRelease, entry.getKey(), entry.getValue());
        }
    });
    LOGGER.debug("Received [{}] attributes. Filtered and released [{}]", givenAttributes.size(), attributesToRelease.size());
    return attributesToRelease;
}
Also used : Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) Pair(org.apache.commons.lang3.tuple.Pair)

Example 22 with Pair

use of org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair in project rskj by rsksmart.

the class BridgeSupport method processFundsMigration.

private void processFundsMigration() throws IOException {
    Wallet retiringFederationWallet = getRetiringFederationWallet();
    List<UTXO> availableUTXOs = getRetiringFederationBtcUTXOs();
    ReleaseTransactionSet releaseTransactionSet = provider.getReleaseTransactionSet();
    Federation activeFederation = getActiveFederation();
    if (federationIsInMigrationAge(activeFederation) && hasMinimumFundsToMigrate(retiringFederationWallet)) {
        logger.info("Active federation (age={}) is in migration age and retiring federation has funds to migrate: {}.", rskExecutionBlock.getNumber() - activeFederation.getCreationBlockNumber(), retiringFederationWallet.getBalance().toFriendlyString());
        Pair<BtcTransaction, List<UTXO>> createResult = createMigrationTransaction(retiringFederationWallet, activeFederation.getAddress());
        BtcTransaction btcTx = createResult.getLeft();
        List<UTXO> selectedUTXOs = createResult.getRight();
        // Add the TX to the release set
        releaseTransactionSet.add(btcTx, rskExecutionBlock.getNumber());
        // Mark UTXOs as spent
        availableUTXOs.removeIf(utxo -> selectedUTXOs.stream().anyMatch(selectedUtxo -> utxo.getHash().equals(selectedUtxo.getHash()) && utxo.getIndex() == selectedUtxo.getIndex()));
    }
    if (retiringFederationWallet != null && federationIsPastMigrationAge(activeFederation)) {
        if (retiringFederationWallet.getBalance().isGreaterThan(Coin.ZERO)) {
            logger.info("Federation is past migration age and will try to migrate remaining balance: {}.", retiringFederationWallet.getBalance().toFriendlyString());
            try {
                Pair<BtcTransaction, List<UTXO>> createResult = createMigrationTransaction(retiringFederationWallet, activeFederation.getAddress());
                BtcTransaction btcTx = createResult.getLeft();
                List<UTXO> selectedUTXOs = createResult.getRight();
                // Add the TX to the release set
                releaseTransactionSet.add(btcTx, rskExecutionBlock.getNumber());
                // Mark UTXOs as spent
                availableUTXOs.removeIf(utxo -> selectedUTXOs.stream().anyMatch(selectedUtxo -> utxo.getHash().equals(selectedUtxo.getHash()) && utxo.getIndex() == selectedUtxo.getIndex()));
            } catch (Exception e) {
                logger.error("Unable to complete retiring federation migration. Balance left: {} in {}", retiringFederationWallet.getBalance().toFriendlyString(), getRetiringFederationAddress());
                panicProcessor.panic("updateCollection", "Unable to complete retiring federation migration.");
            }
        }
        logger.info("Retiring federation migration finished. Available UTXOs left: {}.", availableUTXOs.size());
        provider.setOldFederation(null);
    }
}
Also used : java.util(java.util) Hex(org.spongycastle.util.encoders.Hex) LoggerFactory(org.slf4j.LoggerFactory) RskAddress(co.rsk.core.RskAddress) Keccak256(co.rsk.crypto.Keccak256) Block(org.ethereum.core.Block) TransactionSignature(co.rsk.bitcoinj.crypto.TransactionSignature) Pair(org.apache.commons.lang3.tuple.Pair) BridgeConstants(co.rsk.config.BridgeConstants) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) BigInteger(java.math.BigInteger) Nullable(javax.annotation.Nullable) Wallet(co.rsk.bitcoinj.wallet.Wallet) BtcBlockStore(co.rsk.bitcoinj.store.BtcBlockStore) PanicProcessor(co.rsk.panic.PanicProcessor) ScriptChunk(co.rsk.bitcoinj.script.ScriptChunk) Logger(org.slf4j.Logger) IOException(java.io.IOException) Instant(java.time.Instant) Repository(org.ethereum.core.Repository) Collectors(java.util.stream.Collectors) SendRequest(co.rsk.bitcoinj.wallet.SendRequest) Program(org.ethereum.vm.program.Program) ScriptBuilder(co.rsk.bitcoinj.script.ScriptBuilder) Script(co.rsk.bitcoinj.script.Script) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) RskSystemProperties(co.rsk.config.RskSystemProperties) BridgeEventLogger(co.rsk.peg.utils.BridgeEventLogger) Transaction(org.ethereum.core.Transaction) InputStream(java.io.InputStream) Wallet(co.rsk.bitcoinj.wallet.Wallet) IOException(java.io.IOException) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException)

Example 23 with Pair

use of org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair in project ddf by codice.

the class TestWorkspaceQueryService method testRun.

@SuppressWarnings("unchecked")
@Test
public void testRun() throws SchedulerException, UnsupportedQueryException, SourceUnavailableException, FederationException {
    String workspaceId = "3";
    QueryUpdateSubscriber queryUpdateSubscriber = mock(QueryUpdateSubscriber.class);
    WorkspaceService workspaceService = mock(WorkspaceService.class);
    CatalogFramework catalogFramework = mock(CatalogFramework.class);
    FilterBuilder filterBuilder = mock(FilterBuilder.class);
    Scheduler scheduler = mock(Scheduler.class);
    when(scheduler.getContext()).thenReturn(mock(SchedulerContext.class));
    Supplier<Optional<Scheduler>> schedulerSupplier = () -> Optional.of(scheduler);
    SecurityService securityService = new SecurityService() {

        @Override
        public Subject getSystemSubject() {
            return mock(Subject.class);
        }

        @Override
        public Map<String, Serializable> addSystemSubject(Map<String, Serializable> properties) {
            return properties;
        }
    };
    FilterService filterService = mock(FilterService.class);
    when(filterService.getModifiedDateFilter(any())).thenReturn(mock(Filter.class));
    when(filterBuilder.anyOf(Mockito.any(Filter.class))).thenReturn(mock(Or.class));
    when(filterBuilder.allOf(Mockito.<Filter>anyVararg())).thenReturn(mock(And.class));
    WorkspaceQueryServiceImpl workspaceQueryServiceImpl = new WorkspaceQueryServiceImpl(queryUpdateSubscriber, workspaceService, catalogFramework, filterBuilder, schedulerSupplier, securityService, filterService);
    workspaceQueryServiceImpl.setQueryTimeInterval(60);
    String ecql = "area( Polygon((10 10, 20 10, 20 20, 10 10)) ) BETWEEN 10000 AND 30000";
    WorkspaceMetacardImpl workspaceMetacard = mock(WorkspaceMetacardImpl.class);
    when(workspaceMetacard.getId()).thenReturn(workspaceId);
    QueryMetacardImpl queryMetacardWithSource = mock(QueryMetacardImpl.class);
    when(queryMetacardWithSource.getSources()).thenReturn(Collections.singletonList("SomeSource"));
    when(queryMetacardWithSource.getCql()).thenReturn(ecql);
    Attribute id1 = mock(Attribute.class);
    when(id1.getValue()).thenReturn("1");
    when(queryMetacardWithSource.getAttribute(Metacard.ID)).thenReturn(id1);
    QueryMetacardImpl queryMetacardWithoutSource = mock(QueryMetacardImpl.class);
    when(queryMetacardWithoutSource.getSources()).thenReturn(Collections.emptyList());
    when(queryMetacardWithoutSource.getCql()).thenReturn(ecql);
    Attribute id2 = mock(Attribute.class);
    when(id2.getValue()).thenReturn("2");
    when(queryMetacardWithoutSource.getAttribute(Metacard.ID)).thenReturn(id2);
    Map<String, Pair<WorkspaceMetacardImpl, List<QueryMetacardImpl>>> queryMetacards = Collections.singletonMap(id2.getValue().toString(), new ImmutablePair<>(workspaceMetacard, Arrays.asList(queryMetacardWithSource, queryMetacardWithoutSource)));
    when(workspaceService.getQueryMetacards()).thenReturn(queryMetacards);
    long hitCount1 = 10;
    long hitCount2 = 20;
    QueryResponse queryResponse = mock(QueryResponse.class);
    when(queryResponse.getHits()).thenReturn(hitCount1).thenReturn(hitCount2);
    when(catalogFramework.query(any())).thenReturn(queryResponse);
    workspaceQueryServiceImpl.setSubject(new Subject() {

        @Override
        public boolean isGuest() {
            return false;
        }

        @Override
        public Object getPrincipal() {
            return null;
        }

        @Override
        public PrincipalCollection getPrincipals() {
            return null;
        }

        @Override
        public boolean isPermitted(String s) {
            return false;
        }

        @Override
        public boolean isPermitted(Permission permission) {
            return false;
        }

        @Override
        public boolean[] isPermitted(String... strings) {
            return new boolean[0];
        }

        @Override
        public boolean[] isPermitted(List<Permission> list) {
            return new boolean[0];
        }

        @Override
        public boolean isPermittedAll(String... strings) {
            return false;
        }

        @Override
        public boolean isPermittedAll(Collection<Permission> collection) {
            return false;
        }

        @Override
        public void checkPermission(String s) throws AuthorizationException {
        }

        @Override
        public void checkPermission(Permission permission) throws AuthorizationException {
        }

        @Override
        public void checkPermissions(String... strings) throws AuthorizationException {
        }

        @Override
        public void checkPermissions(Collection<Permission> collection) throws AuthorizationException {
        }

        @Override
        public boolean hasRole(String s) {
            return false;
        }

        @Override
        public boolean[] hasRoles(List<String> list) {
            return new boolean[0];
        }

        @Override
        public boolean hasAllRoles(Collection<String> collection) {
            return false;
        }

        @Override
        public void checkRole(String s) throws AuthorizationException {
        }

        @Override
        public void checkRoles(Collection<String> collection) throws AuthorizationException {
        }

        @Override
        public void checkRoles(String... strings) throws AuthorizationException {
        }

        @Override
        public void login(AuthenticationToken authenticationToken) throws AuthenticationException {
        }

        @Override
        public boolean isAuthenticated() {
            return false;
        }

        @Override
        public boolean isRemembered() {
            return false;
        }

        @Override
        public Session getSession() {
            return null;
        }

        @Override
        public Session getSession(boolean b) {
            return null;
        }

        @Override
        public void logout() {
        }

        @Override
        public <V> V execute(Callable<V> callable) throws ExecutionException {
            try {
                return callable.call();
            } catch (Exception e) {
                throw new ExecutionException(e);
            }
        }

        @Override
        public void execute(Runnable runnable) {
        }

        @Override
        public <V> Callable<V> associateWith(Callable<V> callable) {
            return null;
        }

        @Override
        public Runnable associateWith(Runnable runnable) {
            return null;
        }

        @Override
        public void runAs(PrincipalCollection principalCollection) throws NullPointerException, IllegalStateException {
        }

        @Override
        public boolean isRunAs() {
            return false;
        }

        @Override
        public PrincipalCollection getPreviousPrincipals() {
            return null;
        }

        @Override
        public PrincipalCollection releaseRunAs() {
            return null;
        }
    });
    workspaceQueryServiceImpl.setCronString("0 0 0 * * ?");
    workspaceQueryServiceImpl.setQueryTimeoutMinutes(5L);
    workspaceQueryServiceImpl.run();
    ArgumentCaptor<Map> argumentCaptor = ArgumentCaptor.forClass(Map.class);
    verify(queryUpdateSubscriber).notify(argumentCaptor.capture());
    Map queryUpdateSubscriberArgumentRaw = argumentCaptor.getValue();
    Map<String, Pair<WorkspaceMetacardImpl, Long>> queryUpdateSubscriberArgument = (Map<String, Pair<WorkspaceMetacardImpl, Long>>) queryUpdateSubscriberArgumentRaw;
    assertThat(queryUpdateSubscriberArgument.get(workspaceId).getRight(), is(hitCount1 + hitCount2));
}
Also used : Serializable(java.io.Serializable) Or(org.opengis.filter.Or) AuthenticationToken(org.apache.shiro.authc.AuthenticationToken) AuthorizationException(org.apache.shiro.authz.AuthorizationException) AuthenticationException(org.apache.shiro.authc.AuthenticationException) QueryMetacardImpl(org.codice.ddf.catalog.ui.metacard.workspace.QueryMetacardImpl) CatalogFramework(ddf.catalog.CatalogFramework) SchedulerContext(org.quartz.SchedulerContext) Permission(org.apache.shiro.authz.Permission) Optional(java.util.Optional) WorkspaceService(org.codice.ddf.catalog.ui.query.monitor.api.WorkspaceService) And(org.opengis.filter.And) QueryUpdateSubscriber(org.codice.ddf.catalog.ui.query.monitor.api.QueryUpdateSubscriber) Map(java.util.Map) Attribute(ddf.catalog.data.Attribute) Scheduler(org.quartz.Scheduler) FilterService(org.codice.ddf.catalog.ui.query.monitor.api.FilterService) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Callable(java.util.concurrent.Callable) FilterBuilder(ddf.catalog.filter.FilterBuilder) SecurityService(org.codice.ddf.catalog.ui.query.monitor.api.SecurityService) ExecutionException(org.apache.shiro.subject.ExecutionException) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Subject(ddf.security.Subject) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) SchedulerException(org.quartz.SchedulerException) FederationException(ddf.catalog.federation.FederationException) AuthorizationException(org.apache.shiro.authz.AuthorizationException) ExecutionException(org.apache.shiro.subject.ExecutionException) AuthenticationException(org.apache.shiro.authc.AuthenticationException) Filter(org.opengis.filter.Filter) QueryResponse(ddf.catalog.operation.QueryResponse) WorkspaceMetacardImpl(org.codice.ddf.catalog.ui.metacard.workspace.WorkspaceMetacardImpl) Session(org.apache.shiro.session.Session) Test(org.junit.Test)

Example 24 with Pair

use of org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair in project ddf by codice.

the class WorkspaceQueryServiceImpl method run.

/**
     * Main entry point, should be called by a scheduler.
     */
public void run() {
    SECURITY.runAsAdmin(() -> {
        Subject runSubject = subject != null ? subject : SECURITY.getSystemSubject();
        return runSubject.execute(() -> {
            LOGGER.trace("running workspace query service");
            Map<String, Pair<WorkspaceMetacardImpl, List<QueryMetacardImpl>>> queryMetacards = workspaceService.getQueryMetacards();
            LOGGER.debug("queryMetacards: size={}", queryMetacards.size());
            List<WorkspaceTask> workspaceTasks = createWorkspaceTasks(queryMetacards);
            LOGGER.debug("workspaceTasks: size={}", workspaceTasks.size());
            Map<String, Pair<WorkspaceMetacardImpl, Long>> results = executeWorkspaceTasks(workspaceTasks, queryTimeoutMinutes, TimeUnit.MINUTES);
            LOGGER.debug("results: {}", results);
            queryUpdateSubscriber.notify(results);
            return null;
        });
    });
}
Also used : QueryMetacardImpl(org.codice.ddf.catalog.ui.metacard.workspace.QueryMetacardImpl) Subject(ddf.security.Subject) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Example 25 with Pair

use of org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair in project ddf by codice.

the class TestQueryUpdateSubscriberList method testNotify.

@Test
public void testNotify() {
    QueryUpdateSubscriber childSubscriber = mock(QueryUpdateSubscriber.class);
    QueryUpdateSubscriberList queryUpdateSubscriberList = new QueryUpdateSubscriberList(Collections.singletonList(childSubscriber));
    Map<String, Pair<WorkspaceMetacardImpl, Long>> workspaceMetacardMap = Collections.emptyMap();
    queryUpdateSubscriberList.notify(workspaceMetacardMap);
    verify(childSubscriber).notify(workspaceMetacardMap);
}
Also used : QueryUpdateSubscriber(org.codice.ddf.catalog.ui.query.monitor.api.QueryUpdateSubscriber) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Aggregations

Pair (org.apache.commons.lang3.tuple.Pair)685 ArrayList (java.util.ArrayList)209 List (java.util.List)154 Test (org.junit.Test)150 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)142 HashMap (java.util.HashMap)123 Collectors (java.util.stream.Collectors)123 Map (java.util.Map)112 Message (com.microsoft.azure.sdk.iot.device.Message)71 IOException (java.io.IOException)70 MutablePair (org.apache.commons.lang3.tuple.MutablePair)64 java.util (java.util)55 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)52 Set (java.util.Set)49 StringUtils (org.apache.commons.lang3.StringUtils)48 File (java.io.File)46 Optional (java.util.Optional)45 Arrays (java.util.Arrays)44 HashSet (java.util.HashSet)40 Test (org.junit.jupiter.api.Test)39