CEM-9557: Change C2 OperandType to Enum

Signed-off-by: Matthew Burgess <mattyb149@apache.org>

This closes #5649
This commit is contained in:
Csaba Bejan 2022-01-08 21:11:07 +01:00 committed by Matthew Burgess
parent 73de3a0fe7
commit 93b58ccffe
No known key found for this signature in database
GPG Key ID: 05D3DEB8126DAD24
3 changed files with 74 additions and 7 deletions

View File

@ -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<String, String> args;
private Set<String> 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;
}

View File

@ -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<OperandType> fromString(String value) {
return Arrays.stream(values())
.filter(operandType -> operandType.name().equalsIgnoreCase(value))
.findAny();
}
}

View File

@ -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<OperandType> supportedOperands;
OperationType(OperandType... supportedOperands) {
this.supportedOperands = Arrays.stream(supportedOperands).collect(Collectors.toSet());
}
public boolean isSupportedOperand(OperandType operand) {
return supportedOperands.contains(operand);
}
}