briansolo1985 commented on code in PR #8898:
URL: https://github.com/apache/nifi/pull/8898#discussion_r1629272952


##########
c2/c2-client-bundle/c2-client-service/src/main/java/org/apache/nifi/c2/client/service/operation/SyncResourceOperationHandler.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nifi.c2.client.service.operation;
+
+import static java.util.Optional.ofNullable;
+import static org.apache.commons.lang3.StringUtils.EMPTY;
+import static 
org.apache.nifi.c2.protocol.api.C2OperationState.OperationState.NOT_APPLIED;
+import static org.apache.nifi.c2.protocol.api.OperandType.RESOURCE;
+import static org.apache.nifi.c2.protocol.api.OperationType.SYNC;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import java.util.List;
+import java.util.Map;
+import org.apache.nifi.c2.client.api.C2Client;
+import org.apache.nifi.c2.protocol.api.C2Operation;
+import org.apache.nifi.c2.protocol.api.C2OperationAck;
+import org.apache.nifi.c2.protocol.api.C2OperationState;
+import org.apache.nifi.c2.protocol.api.C2OperationState.OperationState;
+import org.apache.nifi.c2.protocol.api.OperandType;
+import org.apache.nifi.c2.protocol.api.OperationType;
+import org.apache.nifi.c2.protocol.api.ResourceItem;
+import org.apache.nifi.c2.protocol.api.ResourcesGlobalHash;
+import org.apache.nifi.c2.serializer.C2Serializer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SyncResourceOperationHandler implements C2OperationHandler {
+
+    static final String GLOBAL_HASH_FIELD = "globalHash";
+    static final String RESOURCE_LIST_FIELD = "resourceList";
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(SyncResourceOperationHandler.class);
+
+    private final C2Client c2Client;
+    private final OperandPropertiesProvider operandPropertiesProvider;
+    private final SyncResourceStrategy syncResourceStrategy;
+    private final C2Serializer c2Serializer;
+
+    public SyncResourceOperationHandler(C2Client c2Client, 
OperandPropertiesProvider operandPropertiesProvider, SyncResourceStrategy 
syncResourceStrategy,
+                                        C2Serializer c2Serializer) {
+        this.c2Client = c2Client;
+        this.operandPropertiesProvider = operandPropertiesProvider;
+        this.syncResourceStrategy = syncResourceStrategy;
+        this.c2Serializer = c2Serializer;
+    }
+
+    public static SyncResourceOperationHandler create(C2Client c2Client, 
OperandPropertiesProvider operandPropertiesProvider, SyncResourceStrategy 
syncResourceStrategy,
+                                                      C2Serializer 
c2Serializer) {
+        if (c2Client == null) {
+            throw new IllegalArgumentException("C2Client should not be null");
+        }
+        if (operandPropertiesProvider == null) {
+            throw new IllegalArgumentException("OperandPropertiesProvider 
should not be not null");
+        }
+        if (syncResourceStrategy == null) {
+            throw new IllegalArgumentException("Sync resource strategy should 
not be null");
+        }
+        if (c2Serializer == null) {
+            throw new IllegalArgumentException("C2 serializer should not be 
null");
+        }
+        return new SyncResourceOperationHandler(c2Client, 
operandPropertiesProvider, syncResourceStrategy, c2Serializer);
+    }
+
+    @Override
+    public OperationType getOperationType() {
+        return SYNC;
+    }
+
+    @Override
+    public OperandType getOperandType() {
+        return RESOURCE;
+    }
+
+    @Override
+    public Map<String, Object> getProperties() {
+        return operandPropertiesProvider.getProperties();
+    }
+
+    @Override
+    public C2OperationAck handle(C2Operation operation) {
+        String operationId = 
ofNullable(operation.getIdentifier()).orElse(EMPTY);
+
+        ResourcesGlobalHash resourcesGlobalHash = getOperationArg(operation, 
GLOBAL_HASH_FIELD, new TypeReference<>() {
+        });
+        if (resourcesGlobalHash == null) {
+            LOG.error("Resources global hash could not be constructed from C2 
request");
+            return operationAck(operationId, operationState(NOT_APPLIED, 
"Resources global hash element was not found"));
+        }
+        List<ResourceItem> resourceItems = getOperationArg(operation, 
RESOURCE_LIST_FIELD, new TypeReference<>() {
+        });
+        if (resourceItems == null) {
+            LOG.error("Resource item list could not be constructed from C2 
request");
+            return operationAck(operationId, operationState(NOT_APPLIED, 
"Resource item list element was not found"));
+        }
+
+        OperationState operationState = 
syncResourceStrategy.synchronizeResourceRepository(resourcesGlobalHash, 
resourceItems, c2Client::retrieveResourceItem,
+            relativeUrl -> c2Client.getCallbackUrl(null, relativeUrl));
+        C2OperationState resultState = operationState(
+            operationState,
+            switch (operationState) {
+                case NOT_APPLIED -> "No resource items were retrieved, please 
check the log for errors";
+                case PARTIALLY_APPLIED -> "Resource repository is partially 
synced, retrieving some items failed. Pleas check log for errors";
+                case FULLY_APPLIED -> "Agent Resource repository is in sync 
with the C2 server";
+                default -> "Unexpected status, please check the log for 
errors";
+            }
+        );
+
+        return operationAck(operationId, resultState);
+    }
+
+    private <T> T getOperationArg(C2Operation operation, String key, 
TypeReference<T> type) {

Review Comment:
   Refactored, not sure if it became clearer



##########
minifi/minifi-nar-bundles/minifi-framework-bundle/minifi-framework/minifi-framework-core/src/main/java/org/apache/nifi/minifi/c2/command/syncresource/DefaultSyncResourceStrategy.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nifi.minifi.c2.command.syncresource;
+
+import static java.nio.file.Files.copy;
+import static java.nio.file.Files.createTempFile;
+import static java.nio.file.Files.deleteIfExists;
+import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
+import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
+import static java.util.Map.entry;
+import static java.util.Optional.empty;
+import static java.util.Optional.ofNullable;
+import static java.util.UUID.randomUUID;
+import static java.util.function.Predicate.not;
+import static java.util.stream.Collectors.toSet;
+import static org.apache.commons.io.file.PathUtils.createParentDirectories;
+import static 
org.apache.nifi.c2.protocol.api.C2OperationState.OperationState.FULLY_APPLIED;
+import static 
org.apache.nifi.c2.protocol.api.C2OperationState.OperationState.NOT_APPLIED;
+import static 
org.apache.nifi.c2.protocol.api.C2OperationState.OperationState.NO_OPERATION;
+import static 
org.apache.nifi.c2.protocol.api.C2OperationState.OperationState.PARTIALLY_APPLIED;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Map.Entry;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+import org.apache.nifi.c2.client.service.operation.SyncResourceStrategy;
+import org.apache.nifi.c2.protocol.api.C2OperationState.OperationState;
+import org.apache.nifi.c2.protocol.api.ResourceItem;
+import org.apache.nifi.c2.protocol.api.ResourcesGlobalHash;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DefaultSyncResourceStrategy implements SyncResourceStrategy {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(DefaultSyncResourceStrategy.class);
+
+    private static final Set<Entry<OperationState, OperationState>> 
SUCCESS_RESULT_PAIRS = Set.of(
+        entry(NO_OPERATION, NO_OPERATION),
+        entry(NO_OPERATION, FULLY_APPLIED),
+        entry(FULLY_APPLIED, NO_OPERATION),
+        entry(FULLY_APPLIED, FULLY_APPLIED));
+
+    private static final Set<Entry<OperationState, OperationState>> 
FAILED_RESULT_PAIRS = Set.of(
+        entry(NO_OPERATION, NOT_APPLIED),
+        entry(NOT_APPLIED, NO_OPERATION),
+        entry(NOT_APPLIED, NOT_APPLIED));
+
+    private final ResourceRepository resourceRepository;
+    private final Path assetDirectory;
+    private final Path extensionDirectory;
+
+    public DefaultSyncResourceStrategy(ResourceRepository resourceRepository, 
Path assetDirectory, Path extensionDirectory) {
+        this.resourceRepository = resourceRepository;
+        this.assetDirectory = assetDirectory;
+        this.extensionDirectory = extensionDirectory;
+    }
+
+    @Override
+    public OperationState synchronizeResourceRepository(ResourcesGlobalHash 
c2GlobalHash, List<ResourceItem> c2ServerItems,
+                                                        BiFunction<String, 
Function<InputStream, Optional<Path>>, Optional<Path>> resourceDownloadFunction,
+                                                        Function<String, 
Optional<String>> urlEnrichFunction) {
+        Set<ResourceItem> c2Items = c2ServerItems.stream().collect(toSet());

Review Comment:
   Refactored



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to