Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* A post processor which uses the channel property *archive* to add pv's to the archiver appliance The archive
Expand Down Expand Up @@ -48,6 +49,8 @@ public class AAChannelProcessor implements ChannelProcessor {
private String archivePropertyName;
@Value("${aa.archiver_property_name:archiver}")
private String archiverPropertyName;
@Value("${aa.archive_extra_fields_property_name:archive_extra_fields}")
private String archiveExtraFieldsPropertyName;
@Value("${aa.auto_pause:}")
private List<String> autoPauseOptions;

Expand All @@ -63,7 +66,8 @@ public String processorInfo() {
Map<String, String> processorProperties = Map.of("archiveProperty", archivePropertyName,
"archiverProperty", archiverPropertyName,
"Archivers", aaURLs.keySet().toString(),
"AutoPauseOn", autoPauseOptions.toString()
"AutoPauseOn", autoPauseOptions.toString(),
"archiveExtraFieldsProperty", archiveExtraFieldsPropertyName
);
return "AAChannelProcessor: ProcessProperties " + processorProperties;
}
Expand Down Expand Up @@ -104,31 +108,21 @@ public long process(List<Channel> channels) throws JsonProcessingException {
channel.getProperties().stream()
.filter(xmlProperty -> archiverPropertyName.equalsIgnoreCase(xmlProperty.getName()))
.findFirst()
.map(xmlProperty -> {
String archiverValue = xmlProperty.getValue();
// archiver property can be comma separated list of archivers
if (archiverValue != null && !archiverValue.isEmpty()) {
return Arrays.stream(archiverValue.split(","))
.map(String::trim)
.filter(s -> !s.isEmpty());
} else {
return defaultArchivers.stream();
}
})
.map(this::getArchiversStrings)
.orElse(defaultArchivers.stream()) // Use defaultArchivers list if no matching property found
.forEach(archiverAlias -> {
try {
addChannelChange(channel, archiverAliasToArchivePVOptions, archiversInfo, archiveProperty, archiverAlias);
} catch (Exception e) {
logger.log(Level.WARNING, String.format("Failed to process %s", channel), e);
}
});
.forEach(archiverAlias -> tryAddChannelChange(channel, archiverAlias, archiverAliasToArchivePVOptions, archiversInfo, archiveProperty));
} else if (autoPauseOptions.contains(archivePropertyName)) {
aaURLs.keySet().forEach(archiverAlias -> archiverAliasToArchivePVOptions
.get(archiverAlias)
.add(createArchivePV(List.of(), channel, "", PV_STATUS_INACTIVE)));
.add(createArchivePV(List.of(), channel.getName(), "", PV_STATUS_INACTIVE)));
}
});
long finalCount = processArchiversAndOptions(archiverAliasToArchivePVOptions, archiversInfo);
logger.log(Level.INFO, () -> String.format("Configured %s channels.", finalCount));
return finalCount;
}

private long processArchiversAndOptions(Map<String, List<ArchivePVOptions>> archiverAliasToArchivePVOptions, Map<String, ArchiverInfo> archiversInfo) throws JsonProcessingException {
long count = 0;
for (Map.Entry<String, List<ArchivePVOptions>> e : archiverAliasToArchivePVOptions.entrySet()) {
ArchiverInfo archiverInfo = archiversInfo.get(e.getKey());
Expand All @@ -142,9 +136,27 @@ public long process(List<Channel> channels) throws JsonProcessingException {
getArchiveActions(archivePVSList, archiverInfo);
count += archiverClient.configureAA(archiveActionArchivePVMap, archiverInfo.url());
}
long finalCount = count;
logger.log(Level.INFO, () -> String.format("Configured %s channels.", finalCount));
return finalCount;
return count;
}

private void tryAddChannelChange(Channel channel, String archiverAlias, Map<String, List<ArchivePVOptions>> archiverAliasToArchivePVOptions, Map<String, ArchiverInfo> archiversInfo, Optional<Property> archiveProperty) {
try {
addChannelChange(channel, archiverAliasToArchivePVOptions, archiversInfo, archiveProperty, archiverAlias);
} catch (Exception e) {
logger.log(Level.WARNING, String.format("Failed to process %s", channel), e);
}
}

private Stream<String> getArchiversStrings(Property xmlProperty) {
String archiverValue = xmlProperty.getValue();
// archiver property can be comma separated list of archivers
if (archiverValue != null && !archiverValue.isEmpty()) {
return Arrays.stream(archiverValue.split(","))
.map(String::trim)
.filter(s -> !s.isEmpty());
} else {
return defaultArchivers.stream();
}
}

private void addChannelChange(Channel channel,
Expand All @@ -157,16 +169,34 @@ private void addChannelChange(Channel channel,
.findFirst()
.map(Property::getValue)
.orElse(PV_STATUS_INACTIVE);
String pvStatusAutoPause = autoPauseOptions.contains(PV_STATUS_PROPERTY_NAME) ? pvStatus : PV_STATUS_ACTIVE;
List<String> extraFields = channel.getProperties().stream()
.filter(property -> archiveExtraFieldsPropertyName.equalsIgnoreCase(property.getName()))
.findFirst()
.map(Property::getValue)
.map(AAChannelProcessor::parseExtraFieldsProperty)
.orElse(List.of());
if (aaArchivePVS.containsKey(archiverAlias) && archiveProperty.isPresent()) {
ArchivePVOptions newArchiverPV = createArchivePV(
archiversInfo.get(archiverAlias).policies(),
channel,
channel.getName(),
archiveProperty.get().getValue(),
autoPauseOptions.contains(PV_STATUS_PROPERTY_NAME) ? pvStatus : PV_STATUS_ACTIVE);
pvStatusAutoPause);
List<ArchivePVOptions> extraFieldsPVs = extraFields.stream().map(f -> createArchivePV(
archiversInfo.get(archiverAlias).policies(),
String.format("%s.%s", channel.getName(), f),
archiveProperty.get().getValue(),
pvStatusAutoPause)
).toList();
aaArchivePVS.get(archiverAlias).add(newArchiverPV);
aaArchivePVS.get(archiverAlias).addAll(extraFieldsPVs);
}
}

public static List<String> parseExtraFieldsProperty(String s) {
return Arrays.stream(s.split(" ")).toList();
}

private ArchiveAction pickArchiveAction(String archiveStatus, String pvStatus) {
if (archiveStatus.equals("Being archived") && (pvStatus.equals(PV_STATUS_INACTIVE))) {
return ArchiveAction.PAUSE;
Expand Down Expand Up @@ -216,12 +246,12 @@ private Map<ArchiveAction, List<ArchivePVOptions>> getArchiveActions(
}

private ArchivePVOptions createArchivePV(
List<String> policyList, Channel channel, String archiveProperty, String pvStaus) {
List<String> policyList, String channelName, String archiveProperty, String pvStaus) {
ArchivePVOptions newArchiverPV = new ArchivePVOptions();
if (aaPVA && !channel.getName().contains("://")) {
newArchiverPV.setPv("pva://" + channel.getName());
if (aaPVA && !channelName.contains("://")) {
newArchiverPV.setPv("pva://" + channelName);
} else {
newArchiverPV.setPv(channel.getName());
newArchiverPV.setPv(channelName);
}
newArchiverPV.setSamplingParameters(archiveProperty, policyList);
newArchiverPV.setPvStatus(pvStaus);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ aa.enabled=true
aa.pva=false
aa.archive_property_name=archive
aa.archiver_property_name=archiver
aa.archive_extra_fields_property_name=archive_extra_fields

# Set the auto pause behaviour
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.phoebus.channelfinder.processors;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.phoebus.channelfinder.entity.Channel;
import org.phoebus.channelfinder.entity.Property;
import org.phoebus.channelfinder.processors.aa.AAChannelProcessor;
import org.phoebus.channelfinder.processors.aa.ArchiveAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.TestPropertySource;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static org.phoebus.channelfinder.processors.AAChannelProcessorIT.activeProperty;
import static org.phoebus.channelfinder.processors.AAChannelProcessorIT.archiveProperty;
import static org.phoebus.channelfinder.processors.AAChannelProcessorIT.inactiveProperty;
import static org.phoebus.channelfinder.processors.AAChannelProcessorIT.paramableAAChannelProcessorTest;
import static org.phoebus.channelfinder.processors.AAChannelProcessorMultiIT.NOT_BEING_ARCHIVED;
import static org.phoebus.channelfinder.processors.AAChannelProcessorMultiIT.OWNER;

@WebMvcTest(AAChannelProcessor.class)
@TestPropertySource(locations = "classpath:application_test.properties")
class AAChannelProcessorExtraFieldsIT {
protected static Property extraFieldsProperty = new Property("archive_extra_fields", "owner", "a b c");
@Autowired
AAChannelProcessor aaChannelProcessor;

MockWebServer mockArchiverAppliance;
ObjectMapper objectMapper;

@BeforeEach
void setUp() throws IOException {
mockArchiverAppliance = new MockWebServer();
mockArchiverAppliance.start(17665);

objectMapper = new ObjectMapper();
}

@AfterEach
void teardown() throws IOException {
mockArchiverAppliance.shutdown();
}

static Stream<Arguments> provideArguments() {
List<Channel> channels = List.of(
new Channel("PVNoneActive", OWNER, List.of(archiveProperty, activeProperty, extraFieldsProperty), List.of())
);

Map<String, String> namesToStatuses = Map.of(
"PVNoneActive", NOT_BEING_ARCHIVED,
"PVNoneActive.a", NOT_BEING_ARCHIVED,
"PVNoneActive.b", NOT_BEING_ARCHIVED,
"PVNoneActive.c", NOT_BEING_ARCHIVED
);
Map<ArchiveAction, List<String>> actionsToNames = Map.of(
ArchiveAction.ARCHIVE, List.of("PVNoneActive", "PVNoneActive.a", "PVNoneActive.b", "PVNoneActive.c")
);
int expectedProcessedChannels = 4;

return Stream.of(
Arguments.of(channels, namesToStatuses, actionsToNames, expectedProcessedChannels));

}

@ParameterizedTest
@MethodSource("provideArguments")
void testProcessNotArchivedActive(
List<Channel> channels,
Map<String, String> namesToStatuses,
Map<ArchiveAction, List<String>> actionsToNames,
int expectedProcessedChannels)
throws JsonProcessingException, InterruptedException {
AAChannelProcessorMultiIT.paramableMultiAAChannelProcessorTest(
mockArchiverAppliance,
objectMapper,
aaChannelProcessor,
channels,
namesToStatuses,
actionsToNames,
expectedProcessedChannels
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ void testProcessMulti(List<Channel> channels,
Map<ArchiveAction, List<String>> actionsToNames,
int expectedProcessedChannels)
throws JsonProcessingException, InterruptedException {
paramableMultiAAChannelProcessorTest(mockArchiverAppliance, objectMapper, aaChannelProcessor,
channels, namesToStatuses, actionsToNames, expectedProcessedChannels
);
}

static void paramableMultiAAChannelProcessorTest(
MockWebServer mockArchiverAppliance,
ObjectMapper objectMapper,
AAChannelProcessor aaChannelProcessor,
List<Channel> channels,
Map<String, String> namesToStatuses,
Map<ArchiveAction, List<String>> actionsToNames,
int expectedProcessedChannels)
throws JsonProcessingException, InterruptedException {

// Request to version
Map<String, String> versions = Map.of("mgmt_version", "Archiver Appliance Version 1.1.0");
Expand Down Expand Up @@ -194,7 +208,7 @@ void testProcessMulti(List<Channel> channels,
}


public ArchiveAction actionFromEndpoint(final String endpoint) {
public static ArchiveAction actionFromEndpoint(final String endpoint) {
for (ArchiveAction action : ArchiveAction.values()) {
if (action.getEndpoint().equals(endpoint)) {
return action;
Expand Down
Loading
Loading