Skip to content
Open
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 @@ -40,6 +40,8 @@ interface AddressTemplates {
String SELECTED_CHANNEL_FORK_ADDRESS = JGROUPS_ADDRESS + "/channel={selected.channel}/fork=*";
String SELECTED_CHANNEL_FORK_PROTOCOL_ADDRESS = JGROUPS_ADDRESS
+ "/channel={selected.channel}/fork={selected.fork}/protocol=*";
String AUTH_TOKEN_ADDRESS = JGROUPS_ADDRESS + "/stack=*/protocol=AUTH/token=*";
String SELECTED_AUTH_TOKEN_ADDRESS = JGROUPS_ADDRESS + "/stack={selected.stack}/protocol=AUTH/token=*";

AddressTemplate JGROUPS_TEMPLATE = AddressTemplate.of(JGROUPS_ADDRESS);
AddressTemplate STACK_TEMPLATE = AddressTemplate.of(STACK_ADDRESS);
Expand All @@ -64,5 +66,7 @@ interface AddressTemplates {
AddressTemplate CHANNEL_FORK_PROTOCOL_TEMPLATE = AddressTemplate.of(CHANNEL_FORK_PROTOCOL_ADDRESS);
AddressTemplate SELECTED_CHANNEL_FORK_PROTOCOL_TEMPLATE = AddressTemplate.of(
SELECTED_CHANNEL_FORK_PROTOCOL_ADDRESS);
AddressTemplate AUTH_TOKEN_TEMPLATE = AddressTemplate.of(AUTH_TOKEN_ADDRESS);
AddressTemplate SELECTED_AUTH_TOKEN_TEMPLATE = AddressTemplate.of(SELECTED_AUTH_TOKEN_ADDRESS);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2022 Red Hat
*
* Licensed 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
*
* https://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.jboss.hal.client.configuration.subsystem.jgroups;

import java.util.Set;

import org.jboss.elemento.InputType;
import org.jboss.hal.ballroom.wizard.WizardStep;
import org.jboss.hal.resources.Ids;
import org.jboss.hal.resources.Resources;
import org.jboss.hal.resources.UIConstants;

import elemental2.dom.HTMLElement;

import static org.jboss.elemento.Elements.div;
import static org.jboss.elemento.Elements.input;
import static org.jboss.elemento.Elements.label;
import static org.jboss.elemento.Elements.p;
import static org.jboss.elemento.Elements.span;
import static org.jboss.elemento.EventType.click;
import static org.jboss.hal.dmr.ModelDescriptionConstants.CUSTOM;
import static org.jboss.hal.resources.CSS.radio;

class ChooseProtocolStep extends WizardStep<ProtocolWizard.Context, ProtocolWizard.State> {

private final HTMLElement root;

ChooseProtocolStep(Resources resources, Set<String> protocolNames, ProtocolWizard wizard) {
super(resources.constants().chooseProtocol());
root = div()
.add(p().textContent(resources.messages().chooseProtocol(CUSTOM))).element();

for (String protocolName : protocolNames) {
String name = protocolName;
if (name.equals("*")) {
name = CUSTOM;
}
root.appendChild(div().css(radio)
.add(label()
.add(input(InputType.radio)
.id(Ids.build(Ids.JGROUPS_PROTOCOL, name))
.attr(UIConstants.NAME, Ids.JGROUPS_PROTOCOL)
.on(click, e -> wizard.setProtocol(protocolName)).element())
.add(span().textContent(name)))
.element());
}
}

@Override
public HTMLElement element() {
return root;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
*/
package org.jboss.hal.client.configuration.subsystem.jgroups;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import javax.inject.Inject;

import org.jboss.hal.ballroom.LabelBuilder;
import org.jboss.hal.ballroom.autocomplete.StaticAutoComplete;
import org.jboss.hal.ballroom.form.Form;
import org.jboss.hal.ballroom.form.Form.FinishReset;
import org.jboss.hal.ballroom.form.TextBoxItem;
Expand All @@ -48,6 +53,7 @@
import org.jboss.hal.meta.Metadata;
import org.jboss.hal.meta.MetadataRegistry;
import org.jboss.hal.meta.StatementContext;
import org.jboss.hal.meta.description.ResourceDescription;
import org.jboss.hal.meta.token.NameTokens;
import org.jboss.hal.resources.Ids;
import org.jboss.hal.resources.Names;
Expand All @@ -62,18 +68,30 @@
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;

import static org.jboss.hal.client.configuration.subsystem.jgroups.AddressTemplates.AUTH_TOKEN_TEMPLATE;
import static org.jboss.hal.client.configuration.subsystem.jgroups.AddressTemplates.JGROUPS_TEMPLATE;
import static org.jboss.hal.client.configuration.subsystem.jgroups.AddressTemplates.PROTOCOL_TEMPLATE;
import static org.jboss.hal.client.configuration.subsystem.jgroups.AddressTemplates.SELECTED_AUTH_TOKEN_TEMPLATE;
import static org.jboss.hal.client.configuration.subsystem.jgroups.AddressTemplates.SELECTED_PROTOCOL_TEMPLATE;
import static org.jboss.hal.client.configuration.subsystem.jgroups.AddressTemplates.SELECTED_RELAY_TEMPLATE;
import static org.jboss.hal.client.configuration.subsystem.jgroups.AddressTemplates.STACK_TEMPLATE;
import static org.jboss.hal.client.configuration.subsystem.jgroups.AddressTemplates.TRANSPORT_TEMPLATE;
import static org.jboss.hal.dmr.ModelDescriptionConstants.ADD;
import static org.jboss.hal.dmr.ModelDescriptionConstants.ADD_INDEX;
import static org.jboss.hal.dmr.ModelDescriptionConstants.CHANNEL;
import static org.jboss.hal.dmr.ModelDescriptionConstants.DEPRECATED;
import static org.jboss.hal.dmr.ModelDescriptionConstants.FORK;
import static org.jboss.hal.dmr.ModelDescriptionConstants.INCLUDE_SINGLETONS;
import static org.jboss.hal.dmr.ModelDescriptionConstants.JGROUPS;
import static org.jboss.hal.dmr.ModelDescriptionConstants.MODULE;
import static org.jboss.hal.dmr.ModelDescriptionConstants.PROPERTIES;
import static org.jboss.hal.dmr.ModelDescriptionConstants.PROTOCOL;
import static org.jboss.hal.dmr.ModelDescriptionConstants.READ_CHILDREN_TYPES_OPERATION;
import static org.jboss.hal.dmr.ModelDescriptionConstants.RELAY;
import static org.jboss.hal.dmr.ModelDescriptionConstants.RESULT;
import static org.jboss.hal.dmr.ModelDescriptionConstants.SOCKET_BINDING;
import static org.jboss.hal.dmr.ModelDescriptionConstants.STACK;
import static org.jboss.hal.dmr.ModelDescriptionConstants.STATISTICS_ENABLED;
import static org.jboss.hal.dmr.ModelDescriptionConstants.TRANSPORT;
import static org.jboss.hal.dmr.ModelNodeHelper.asNamedNodes;
import static org.jboss.hal.dmr.ModelNodeHelper.failSafePropertyList;
Expand All @@ -93,6 +111,12 @@ public class JGroupsPresenter extends ApplicationFinderPresenter<JGroupsPresente
private String currentChannel;
private String selectedFork;

public static final String AUTH = "AUTH";
private static final String PROTOCOL_AUTH = PROTOCOL + "=" + AUTH;
private static final String[] AUTH_ATTRIBUTES = { ADD_INDEX, MODULE, PROPERTIES, STATISTICS_ENABLED };

private Map<String, Metadata> stackSingletons;

@Inject
public JGroupsPresenter(EventBus eventBus,
MyView view, Resources resources,
Expand Down Expand Up @@ -243,6 +267,67 @@ void addResourceDialog(AddressTemplate template, String resourceid, String displ

// stack resources

public void processStackSingletons() {
Operation readProtocolSingletons = new Operation.Builder(STACK_TEMPLATE.resolve(filterStatementContext),
READ_CHILDREN_TYPES_OPERATION)
.param(INCLUDE_SINGLETONS, true)
.build();

Operation readTokenSingletons = new Operation.Builder(
PROTOCOL_TEMPLATE.resolve(filterStatementContext, "*", AUTH),
READ_CHILDREN_TYPES_OPERATION)
.param(INCLUDE_SINGLETONS, true)
.build();

Metadata authMd = metadataRegistry.lookup(PROTOCOL_TEMPLATE.replaceWildcards("*", AUTH));

dispatcher.execute(new Composite(readProtocolSingletons, readTokenSingletons), (CompositeResult result) -> {
stackSingletons = result.step(0).get(RESULT).asList().stream()
.map(s -> {
String name = s.asString();
if (!name.contains("=")) {
name += "=*";
}
return metadataRegistry.lookup(STACK_TEMPLATE.append(name));
})
.filter(metadata -> !metadata.getDescription().has(DEPRECATED))
.collect(Collectors.toMap(m -> m.getTemplate().lastName() + "=" + m.getTemplate().lastValue(),
m -> m, (m1, m2) -> null, TreeMap::new));

// AUTH has child singletons, we present them as separate AUTH options
result.step(1).get(RESULT).asList().forEach(token -> {
Metadata mdCopy = new Metadata(authMd.getTemplate(), authMd::getSecurityContext,
new ResourceDescription(authMd.getDescription()), authMd.getCapabilities());
String tokenName = token.asString().substring(6);

Metadata tokenMd = metadataRegistry
.lookup(AUTH_TOKEN_TEMPLATE.replaceWildcards("*", tokenName));
mdCopy.getDescription().requestProperties().addAll(tokenMd.getDescription().requestProperties());
stackSingletons.put(PROTOCOL_AUTH + " (" + tokenName + ")", mdCopy);
});
stackSingletons.remove(PROTOCOL_AUTH);
});
}

private Set<String> getNamesOf(String resource) {
return stackSingletons.keySet().stream()
.filter(name -> name.startsWith(resource))
.map(name -> name.substring(resource.length() + 1))
.collect(Collectors.toSet());
}

public Set<String> getProtocolNames() {
return getNamesOf(PROTOCOL);
}

public Set<String> getTransportNames() {
return getNamesOf(TRANSPORT);
}

public Metadata getAuthTokenMetadata(String token) {
return metadataRegistry.lookup(AUTH_TOKEN_TEMPLATE.replaceWildcards("*", token));
}

@SuppressWarnings("ConstantConditions")
void addStack() {
Metadata metadata = metadataRegistry.lookup(STACK_TEMPLATE);
Expand All @@ -254,6 +339,7 @@ void addStack() {
String transportLabel = new LabelBuilder().label(TRANSPORT);
TextBoxItem transportItem = new TextBoxItem(TRANSPORT, transportLabel);
transportItem.setRequired(true);
transportItem.registerSuggestHandler(new StaticAutoComplete(new ArrayList<>(getTransportNames())));
String id = Ids.build(Ids.JGROUPS_STACK_CONFIG, Ids.ADD);
ModelNodeForm<ModelNode> form = new ModelNodeForm.Builder<>(id, metadata)
.unboundFormItem(nameItem, 0)
Expand Down Expand Up @@ -314,6 +400,54 @@ void showRemoteSites(NamedNode row) {

// protocol resources

public Metadata getProtocolMetadata(String name) {
Metadata metadata = stackSingletons.get(PROTOCOL + "=" + name);
if (metadata == null) {
metadata = stackSingletons.get(PROTOCOL + "=*");
}
return metadata;
}

public void addProtocol(Set<String> currentProtocols) {
Set<String> availableProtocols = getProtocolNames().stream()
.filter(prot -> !currentProtocols.contains(prot)
&& !((prot.startsWith(AUTH + " (") && currentProtocols.contains(AUTH))))
.collect(Collectors.toSet());
new ProtocolWizard(resources, availableProtocols, this::getProtocolMetadata,
(wizard, context) -> addProtocolSingleton(dispatcher, filterStatementContext, context.protocolName,
context.payload, (n, a) -> {
MessageEvent.fire(getEventBus(),
Message.success(resources.messages().addResourceSuccess(PROTOCOL, context.protocolName)));
reload();
}))
.show();
}

private void addProtocolSingleton(Dispatcher dispatcher, StatementContext statementContext, String protocolName,
ModelNode payload, CrudOperations.AddCallback callback) {
if (!protocolName.startsWith(AUTH + " (")) {
crud.add(PROTOCOL, protocolName,
SELECTED_PROTOCOL_TEMPLATE.resolve(filterStatementContext, protocolName),
payload, (n, a) -> reload());
return;
}

List<Operation> operations = new ArrayList<>();
ModelNode protocolPayload = new ModelNode();
for (String s : AUTH_ATTRIBUTES) {
if (payload.has(s)) {
ModelNode value = payload.remove(s);
protocolPayload.get(s).set(value);
}
}
final String tokenName = protocolName.substring(AUTH.length() + 2, protocolName.length() - 1);
operations.add(new Operation.Builder(SELECTED_PROTOCOL_TEMPLATE.resolve(statementContext, AUTH), ADD)
.payload(protocolPayload).build());
operations.add(new Operation.Builder(SELECTED_AUTH_TOKEN_TEMPLATE.resolve(statementContext, tokenName), ADD)
.payload(payload).build());
dispatcher.execute(new Composite(operations), (CompositeResult result) -> callback.execute(null, null));
}

void showProtocols(NamedNode selectedStack) {
this.selectedStack = selectedStack.getName();
getView().updateProtocols(asNamedNodes(failSafePropertyList(selectedStack, PROTOCOL)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,9 @@ public void updateChannelProtocols(List<NamedNode> model) {
channelConfig.updateProtocol(model);
}

public void attach() {
super.attach();
presenter.processStackSingletons();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2022 Red Hat
*
* Licensed 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
*
* https://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.jboss.hal.client.configuration.subsystem.jgroups;

import org.jboss.hal.ballroom.wizard.WizardStep;
import org.jboss.hal.core.mbui.dialog.NameItem;
import org.jboss.hal.core.mbui.form.ModelNodeForm;
import org.jboss.hal.dmr.ModelNode;
import org.jboss.hal.dmr.ModelNodeHelper;
import org.jboss.hal.resources.Ids;
import org.jboss.hal.resources.Resources;

import elemental2.dom.HTMLElement;

import static org.jboss.elemento.Elements.div;

class PropertiesStep extends WizardStep<ProtocolWizard.Context, ProtocolWizard.State> {

private final HTMLElement root;
private ModelNodeForm<ModelNode> form;
private String selectedProtocol;
private final Resources resources;

PropertiesStep(Resources resources) {
super(resources.constants().attributes());
this.resources = resources;
root = div().element();
}

@Override
public HTMLElement element() {
return root;
}

@Override
protected void onShow(ProtocolWizard.Context context) {
if (context.protocolName.equals(selectedProtocol)) {
return;
}
selectedProtocol = context.protocolName;

ModelNodeForm.Builder<ModelNode> builder = new ModelNodeForm.Builder<>(
Ids.build(Ids.JGROUPS_PROTOCOL, Ids.ADD, Ids.FORM), context.protocolMetadata);
if (context.protocolName.equals("*")) {
builder.unboundFormItem(new NameItem(), 0);
}
builder.fromRequestProperties();

form = builder.build();
form.attach();
ProtocolElement.addCrValidation(resources, form, context.protocolMetadata.getDescription().requestProperties());
root.replaceChildren(form.element());
form.edit(new ModelNode());
}

@Override
protected boolean onNext(ProtocolWizard.Context context) {
boolean valid = form.save();
context.payload = ModelNodeHelper.flatToNested(form.getModel());
return valid;
}
}
Loading