Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIFI-12898 Allow uploading asset and referencing via Parameter #9138

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions nifi-api/src/main/java/org/apache/nifi/asset/Asset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.asset;

import java.io.File;
import java.util.Optional;

/**
* An Asset is a representation of some resource that is necessary in order to run a dataflow.
* An Asset is always accessed as a local file.
*/
public interface Asset {

/**
* Returns a unique identifier for the Asset
*/
String getIdentifier();

/**
* Returns the identifier of the parameter context the Asset belongs to
*/
String getParameterContextIdentifier();

/**
* Returns the name of the Asset
*/
String getName();

/**
* Returns the local file that the Asset is associated with
*/
File getFile();

/**
* Returns the digest of the contents of the local file that the Asset is associated with.
* The digest will not be present when the asset is considered missing and the local file does not exist.
*/
Optional<String> getDigest();
}
44 changes: 44 additions & 0 deletions nifi-api/src/main/java/org/apache/nifi/flow/VersionedAsset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.flow;

import io.swagger.v3.oas.annotations.media.Schema;

public class VersionedAsset {
private String identifier;
private String name;

@Schema(description = "The identifier of the asset")
public String getIdentifier() {
return identifier;
}

public void setIdentifier(final String identifier) {
this.identifier = identifier;
}

@Schema(description = "The name of the asset")
public String getName() {
return name;
}

public void setName(final String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.swagger.v3.oas.annotations.media.Schema;

import java.util.List;
import java.util.Objects;

public class VersionedParameter {
Expand All @@ -27,6 +28,7 @@ public class VersionedParameter {
private boolean sensitive;
private boolean provided;
private String value;
private List<VersionedAsset> referencedAssets;

@Schema(description = "The name of the parameter")
public String getName() {
Expand Down Expand Up @@ -73,6 +75,15 @@ public void setValue(String value) {
this.value = value;
}

@Schema(description = "The assets that are referenced by this parameter")
public List<VersionedAsset> getReferencedAssets() {
return referencedAssets;
}

public void setReferencedAssets(final List<VersionedAsset> referencedAssets) {
this.referencedAssets = referencedAssets;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
123 changes: 111 additions & 12 deletions nifi-api/src/main/java/org/apache/nifi/parameter/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,39 @@
*/
package org.apache.nifi.parameter;

import org.apache.nifi.asset.Asset;

import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class Parameter {
private final ParameterDescriptor descriptor;
private final String value;
private final String parameterContextId;
private final boolean provided;
private final List<Asset> referencedAssets;

public Parameter(final ParameterDescriptor descriptor, final String value, final String parameterContextId, final Boolean provided) {
this.descriptor = descriptor;
this.value = value;
this.parameterContextId = parameterContextId;
this.provided = provided == null ? false : provided.booleanValue();
}
private Parameter(final Builder builder) {
this.descriptor = new ParameterDescriptor.Builder()
.name(builder.name)
.description(builder.description)
.sensitive(builder.sensitive)
.build();

public Parameter(final Parameter parameter, final String parameterContextId) {
this(parameter.getDescriptor(), parameter.getValue(), parameterContextId, parameter.isProvided());
}
this.parameterContextId = builder.parameterContextId;
this.provided = builder.provided;

public Parameter(final ParameterDescriptor descriptor, final String value) {
this(descriptor, value, null, false);
this.referencedAssets = builder.referencedAssets;
if (this.referencedAssets == null || this.referencedAssets.isEmpty()) {
this.value = builder.value;
} else {
this.value = referencedAssets.stream()
.map(Asset::getFile)
.map(File::getAbsolutePath)
.collect(Collectors.joining(","));
}
}

public ParameterDescriptor getDescriptor() {
Expand All @@ -47,6 +59,10 @@ public String getValue() {
return value;
}

public List<Asset> getReferencedAssets() {
return referencedAssets;
}

public String getParameterContextId() {
return parameterContextId;
}
Expand All @@ -62,7 +78,10 @@ public boolean equals(final Object o) {
}

final Parameter parameter = (Parameter) o;
return Objects.equals(descriptor, parameter.descriptor) && Objects.equals(value, parameter.value);
return Objects.equals(descriptor, parameter.descriptor)
&& Objects.equals(value, parameter.value)
&& Objects.equals(parameterContextId, parameter.parameterContextId)
&& Objects.equals(referencedAssets, parameter.referencedAssets);
}

@Override
Expand All @@ -77,4 +96,84 @@ public int hashCode() {
public boolean isProvided() {
return provided;
}

public static class Builder {
private String name;
private String description;
private boolean sensitive;
private String value;
private String parameterContextId;
private boolean provided;
private List<Asset> referencedAssets = List.of();

public Builder fromParameter(final Parameter parameter) {
descriptor(parameter.getDescriptor());
this.parameterContextId = parameter.getParameterContextId();
this.provided = parameter.isProvided();
this.referencedAssets = parameter.getReferencedAssets() == null ? List.of() : parameter.getReferencedAssets();
if (this.referencedAssets.isEmpty()) {
this.value = parameter.getValue();
}

return this;
}

public Builder descriptor(final ParameterDescriptor descriptor) {
this.name = descriptor.getName();
this.description = descriptor.getDescription();
this.sensitive = descriptor.isSensitive();
return this;
}

public Builder name(final String name) {
this.name = name;
return this;
}

public Builder description(final String description) {
this.description = description;
return this;
}

public Builder sensitive(final boolean sensitive) {
this.sensitive = sensitive;
return this;
}

public Builder value(final String value) {
this.value = value;
this.referencedAssets = List.of();
return this;
}

public Builder parameterContextId(final String parameterContextId) {
this.parameterContextId = parameterContextId;
return this;
}

public Builder provided(final Boolean provided) {
this.provided = provided != null && provided;
return this;
}

public Builder referencedAssets(final List<Asset> referencedAssets) {
this.referencedAssets = referencedAssets == null ? List.of() : referencedAssets;
if (!this.referencedAssets.isEmpty()) {
this.value = null;
}

return this;
}

public Parameter build() {
if (name == null) {
throw new IllegalStateException("Name or Descriptor is required");
}
if (value != null && referencedAssets != null && !referencedAssets.isEmpty()) {
throw new IllegalStateException("A Parameter's value or referenced assets may be set but not both");
}

return new Parameter(this);
}
}
}
Loading