corefx - updated node types to use sets and have the controller maintain default service mapping for other components to leverage

This commit is contained in:
Blake J. Harnden 2018-11-27 13:37:15 -08:00
parent 7e2a79335c
commit 8423bae0af
8 changed files with 23 additions and 39 deletions

View file

@ -51,6 +51,7 @@ public class Controller implements Initializable {
private Application application;
private Stage window;
private Configuration configuration;
private Map<String, Set<String>> defaultServices = new HashMap<>();
// core client utilities
private ICoreClient coreClient = new CoreRestClient();
@ -237,9 +238,7 @@ public class Controller implements Initializable {
private void setCoreDefaultServices() {
try {
Map<String, List<String>> defaults = configuration.getNodeTypeConfigs().stream()
.collect(Collectors.toMap(NodeTypeConfig::getModel, NodeTypeConfig::getServices));
coreClient.setDefaultServices(defaults);
coreClient.setDefaultServices(defaultServices);
} catch (IOException ex) {
Toast.error("Error updating core default services", ex);
}
@ -247,10 +246,6 @@ public class Controller implements Initializable {
public void updateNodeTypes() {
graphToolbar.setupNodeTypes();
Map<String, Set<String>> defaults = configuration.getNodeTypeConfigs().stream()
.collect(Collectors.toMap(NodeTypeConfig::getModel,
nodeTypeConfig -> new HashSet<>(nodeTypeConfig.getServices())));
nodeServicesDialog.setDefaultServices(defaults);
setCoreDefaultServices();
try {
ConfigUtils.save(configuration);
@ -433,10 +428,8 @@ public class Controller implements Initializable {
// set node types / default services
graphToolbar.setupNodeTypes();
Map<String, Set<String>> defaults = configuration.getNodeTypeConfigs().stream()
.collect(Collectors.toMap(NodeTypeConfig::getModel,
nodeTypeConfig -> new HashSet<>(nodeTypeConfig.getServices())));
nodeServicesDialog.setDefaultServices(defaults);
defaultServices = configuration.getNodeTypeConfigs().stream()
.collect(Collectors.toMap(NodeTypeConfig::getModel, NodeTypeConfig::getServices));
// set graph toolbar
borderPane.setLeft(graphToolbar);

View file

@ -9,6 +9,7 @@ import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
public interface ICoreClient {
void setUrl(String url);
@ -33,7 +34,7 @@ public interface ICoreClient {
Map<String, List<String>> getDefaultServices() throws IOException;
boolean setDefaultServices(Map<String, List<String>> defaults) throws IOException;
boolean setDefaultServices(Map<String, Set<String>> defaults) throws IOException;
CoreService getService(CoreNode node, String serviceName) throws IOException;

View file

@ -9,10 +9,7 @@ import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
@Data
public class CoreRestClient implements ICoreClient {
@ -136,7 +133,7 @@ public class CoreRestClient implements ICoreClient {
}
@Override
public boolean setDefaultServices(Map<String, List<String>> defaults) throws IOException {
public boolean setDefaultServices(Map<String, Set<String>> defaults) throws IOException {
String url = getUrl(String.format("sessions/%s/services/default", sessionId));
return WebUtils.postJson(url, defaults);
}

View file

@ -22,7 +22,7 @@ public class NodeType {
@EqualsAndHashCode.Include
private final int id;
private final int value;
private final List<String> services = new ArrayList<>();
private final Set<String> services = new TreeSet<>();
private String display;
private String model;
private String icon;

View file

@ -19,7 +19,6 @@ public class NodeServicesDialog extends StageDialog {
private static final Logger logger = LogManager.getLogger();
private final Map<String, List<ServiceItem>> serviceItemGroups = new HashMap<>();
private final Map<String, ServiceItem> serviceItemMap = new HashMap<>();
private final Map<String, Set<String>> defaultServices = new HashMap<>();
private CoreNode node;
private int index = 0;
@FXML private GridPane gridPane;
@ -73,11 +72,6 @@ public class NodeServicesDialog extends StageDialog {
});
}
public void setDefaultServices(Map<String, Set<String>> defaultServices) {
this.defaultServices.clear();
this.defaultServices.putAll(defaultServices);
}
public void setServices(Map<String, List<String>> serviceGroups) {
serviceItemGroups.clear();
@ -135,7 +129,7 @@ public class NodeServicesDialog extends StageDialog {
Set<String> nodeServices = node.getServices();
if (nodeServices.isEmpty()) {
nodeServices = defaultServices.get(node.getModel());
nodeServices = getController().getDefaultServices().get(node.getModel());
}
for (List<ServiceItem> items : serviceItemGroups.values()) {

View file

@ -17,8 +17,8 @@ import org.apache.logging.log4j.Logger;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class NodeTypesDialog extends StageDialog {
private static final Logger logger = LogManager.getLogger();
@ -52,7 +52,7 @@ public class NodeTypesDialog extends StageDialog {
iconTextField.setText(nodeType.getIcon());
iconImage.setImage(new Image(nodeType.getIcon()));
selectedNodeType = nodeType;
List<String> services = nodeType.getServices();
Set<String> services = nodeType.getServices();
nodeServicesListView.getItems().setAll(services);
});
@ -90,6 +90,7 @@ public class NodeTypesDialog extends StageDialog {
NodeType.remove(nodeType);
listView.getItems().remove(display);
NodeTypeConfig nodeTypeConfig = createNodeTypeConfig(nodeType);
getController().getDefaultServices().remove(nodeTypeConfig.getModel());
getController().getConfiguration().getNodeTypeConfigs().remove(nodeTypeConfig);
getController().updateNodeTypes();
});
@ -102,6 +103,7 @@ public class NodeTypesDialog extends StageDialog {
nodeTypeMap.put(nodeType.getDisplay(), nodeType);
listView.getItems().add(nodeType.getDisplay());
NodeTypeConfig nodeTypeConfig = createNodeTypeConfig(nodeType);
getController().getDefaultServices().put(nodeTypeConfig.getModel(), nodeTypeConfig.getServices());
getController().getConfiguration().getNodeTypeConfigs().add(nodeTypeConfig);
getController().updateNodeTypes();
});

View file

@ -10,9 +10,7 @@ import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.*;
public final class ConfigUtils {
private static final Logger logger = LogManager.getLogger();
@ -42,19 +40,19 @@ public final class ConfigUtils {
private static List<NodeTypeConfig> createDefaults() throws IOException {
return Arrays.asList(
createDefault("host", "Host", "/icons/host-100.png", Arrays.asList(
createDefault("host", "Host", "/icons/host-100.png", new TreeSet<>(Arrays.asList(
"DefaultRoute", "SSH"
)),
))),
createDefault("PC", "PC", "/icons/pc-100.png",
Collections.singletonList("DefaultRoute")),
createDefault("mdr", "MDR", "/icons/router-100.png", Arrays.asList(
new TreeSet<>(Collections.singletonList("DefaultRoute"))),
createDefault("mdr", "MDR", "/icons/router-100.png", new TreeSet<>(Arrays.asList(
"zebra", "OSPFv3MDR", "IPForward"
))
)))
);
}
private static NodeTypeConfig createDefault(String model, String display, String icon,
List<String> services) throws IOException {
Set<String> services) throws IOException {
String fileName = Paths.get(icon).getFileName().toString();
Path iconPath = Paths.get(ICON_DIR.toString(), fileName);
Files.copy(ConfigUtils.class.getResourceAsStream(icon), iconPath);
@ -93,7 +91,6 @@ public final class ConfigUtils {
nodeTypeConfig.getDisplay(),
nodeTypeConfig.getIcon()
);
nodeTypeConfig.getServices().sort(String::compareTo);
nodeType.getServices().addAll(nodeTypeConfig.getServices());
NodeType.add(nodeType);
}

View file

@ -5,7 +5,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.util.List;
import java.util.Set;
@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@ -16,5 +16,5 @@ public class NodeTypeConfig {
private String model;
private String display;
private String icon;
private List<String> services;
private Set<String> services;
}