From 93b58ccffebebd7cd26a66a8ba020c1735db9c92 Mon Sep 17 00:00:00 2001 From: Csaba Bejan Date: Sat, 8 Jan 2022 21:11:07 +0100 Subject: [PATCH] CEM-9557: Change C2 OperandType to Enum Signed-off-by: Matthew Burgess This closes #5649 --- .../nifi/c2/protocol/api/C2Operation.java | 16 ++++++-- .../nifi/c2/protocol/api/OperandType.java | 37 +++++++++++++++++++ .../nifi/c2/protocol/api/OperationType.java | 28 ++++++++++++-- 3 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 c2/c2-protocol/c2-protocol-api/src/main/java/org/apache/nifi/c2/protocol/api/OperandType.java diff --git a/c2/c2-protocol/c2-protocol-api/src/main/java/org/apache/nifi/c2/protocol/api/C2Operation.java b/c2/c2-protocol/c2-protocol-api/src/main/java/org/apache/nifi/c2/protocol/api/C2Operation.java index 2a90700793..8828d77fbc 100644 --- a/c2/c2-protocol/c2-protocol-api/src/main/java/org/apache/nifi/c2/protocol/api/C2Operation.java +++ b/c2/c2-protocol/c2-protocol-api/src/main/java/org/apache/nifi/c2/protocol/api/C2Operation.java @@ -17,6 +17,8 @@ package org.apache.nifi.c2.protocol.api; +import static java.lang.String.format; + import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import java.io.Serializable; @@ -29,7 +31,7 @@ public class C2Operation implements Serializable { private String identifier; private OperationType operation; - private String operand; + private OperandType operand; private Map args; private Set dependencies; @@ -48,6 +50,10 @@ public class C2Operation implements Serializable { } public void setOperation(OperationType operation) { + if (operand != null && !operation.isSupportedOperand(operand)) { + throw new IllegalArgumentException(format("%s is not a valid operand for %s", operand, operation)); + } + this.operation = operation; } @@ -58,11 +64,15 @@ public class C2Operation implements Serializable { "If no operand is needed, this field will be absent." + "If one operand is insufficient, the operation will contain an args map" + "with additional keyword parameters and values (see 'args').") - public String getOperand() { + public OperandType getOperand() { return operand; } - public void setOperand(String operand) { + public void setOperand(OperandType operand) { + if (operation != null && !operation.isSupportedOperand(operand)) { + throw new IllegalArgumentException(format("%s is not a valid operand for %s", operand, operation)); + } + this.operand = operand; } diff --git a/c2/c2-protocol/c2-protocol-api/src/main/java/org/apache/nifi/c2/protocol/api/OperandType.java b/c2/c2-protocol/c2-protocol-api/src/main/java/org/apache/nifi/c2/protocol/api/OperandType.java new file mode 100644 index 0000000000..bac9403d76 --- /dev/null +++ b/c2/c2-protocol/c2-protocol-api/src/main/java/org/apache/nifi/c2/protocol/api/OperandType.java @@ -0,0 +1,37 @@ +/* + * 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.protocol.api; + +import java.util.Arrays; +import java.util.Optional; + +public enum OperandType { + + CONFIGURATION, + CONNECTION, + DEBUG, + MANIFEST, + REPOSITORY; + + public static Optional fromString(String value) { + return Arrays.stream(values()) + .filter(operandType -> operandType.name().equalsIgnoreCase(value)) + .findAny(); + } +} + diff --git a/c2/c2-protocol/c2-protocol-api/src/main/java/org/apache/nifi/c2/protocol/api/OperationType.java b/c2/c2-protocol/c2-protocol-api/src/main/java/org/apache/nifi/c2/protocol/api/OperationType.java index 766a13e645..6fe242fe82 100644 --- a/c2/c2-protocol/c2-protocol-api/src/main/java/org/apache/nifi/c2/protocol/api/OperationType.java +++ b/c2/c2-protocol/c2-protocol-api/src/main/java/org/apache/nifi/c2/protocol/api/OperationType.java @@ -17,6 +17,15 @@ package org.apache.nifi.c2.protocol.api; +import static org.apache.nifi.c2.protocol.api.OperandType.CONFIGURATION; +import static org.apache.nifi.c2.protocol.api.OperandType.CONNECTION; +import static org.apache.nifi.c2.protocol.api.OperandType.DEBUG; +import static org.apache.nifi.c2.protocol.api.OperandType.MANIFEST; + +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; + public enum OperationType { // C2 Client Status Updates -> C2 Server @@ -24,14 +33,25 @@ public enum OperationType { HEARTBEAT, // C2 Server -> C2 Client Commands - CLEAR, - DESCRIBE, - UPDATE, + CLEAR(CONNECTION), + DESCRIBE(MANIFEST), + UPDATE(CONFIGURATION), RESTART, START, STOP, PAUSE, REPLICATE, SUBSCRIBE, - TRANSFER + TRANSFER(DEBUG); + + private final Set supportedOperands; + + OperationType(OperandType... supportedOperands) { + this.supportedOperands = Arrays.stream(supportedOperands).collect(Collectors.toSet()); + } + + public boolean isSupportedOperand(OperandType operand) { + return supportedOperands.contains(operand); + } + }