From 8423bae0aff00b15a8cdb8678a3e1a729dd114f3 Mon Sep 17 00:00:00 2001 From: "Blake J. Harnden" Date: Tue, 27 Nov 2018 13:37:15 -0800 Subject: [PATCH] corefx - updated node types to use sets and have the controller maintain default service mapping for other components to leverage --- corefx/src/main/java/com/core/Controller.java | 15 ++++----------- .../main/java/com/core/client/ICoreClient.java | 3 ++- .../com/core/client/rest/CoreRestClient.java | 7 ++----- .../src/main/java/com/core/data/NodeType.java | 2 +- .../com/core/ui/dialogs/NodeServicesDialog.java | 8 +------- .../com/core/ui/dialogs/NodeTypesDialog.java | 6 ++++-- .../main/java/com/core/utils/ConfigUtils.java | 17 +++++++---------- .../java/com/core/utils/NodeTypeConfig.java | 4 ++-- 8 files changed, 23 insertions(+), 39 deletions(-) diff --git a/corefx/src/main/java/com/core/Controller.java b/corefx/src/main/java/com/core/Controller.java index 4afff3bf..537e7e18 100644 --- a/corefx/src/main/java/com/core/Controller.java +++ b/corefx/src/main/java/com/core/Controller.java @@ -51,6 +51,7 @@ public class Controller implements Initializable { private Application application; private Stage window; private Configuration configuration; + private Map> 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> 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> 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> 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); diff --git a/corefx/src/main/java/com/core/client/ICoreClient.java b/corefx/src/main/java/com/core/client/ICoreClient.java index af7c3013..5635e615 100644 --- a/corefx/src/main/java/com/core/client/ICoreClient.java +++ b/corefx/src/main/java/com/core/client/ICoreClient.java @@ -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> getDefaultServices() throws IOException; - boolean setDefaultServices(Map> defaults) throws IOException; + boolean setDefaultServices(Map> defaults) throws IOException; CoreService getService(CoreNode node, String serviceName) throws IOException; diff --git a/corefx/src/main/java/com/core/client/rest/CoreRestClient.java b/corefx/src/main/java/com/core/client/rest/CoreRestClient.java index 15719e90..55f3cbff 100644 --- a/corefx/src/main/java/com/core/client/rest/CoreRestClient.java +++ b/corefx/src/main/java/com/core/client/rest/CoreRestClient.java @@ -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> defaults) throws IOException { + public boolean setDefaultServices(Map> defaults) throws IOException { String url = getUrl(String.format("sessions/%s/services/default", sessionId)); return WebUtils.postJson(url, defaults); } diff --git a/corefx/src/main/java/com/core/data/NodeType.java b/corefx/src/main/java/com/core/data/NodeType.java index 1e43d60f..4af7dc3d 100644 --- a/corefx/src/main/java/com/core/data/NodeType.java +++ b/corefx/src/main/java/com/core/data/NodeType.java @@ -22,7 +22,7 @@ public class NodeType { @EqualsAndHashCode.Include private final int id; private final int value; - private final List services = new ArrayList<>(); + private final Set services = new TreeSet<>(); private String display; private String model; private String icon; diff --git a/corefx/src/main/java/com/core/ui/dialogs/NodeServicesDialog.java b/corefx/src/main/java/com/core/ui/dialogs/NodeServicesDialog.java index dad760f3..c8e4b1a0 100644 --- a/corefx/src/main/java/com/core/ui/dialogs/NodeServicesDialog.java +++ b/corefx/src/main/java/com/core/ui/dialogs/NodeServicesDialog.java @@ -19,7 +19,6 @@ public class NodeServicesDialog extends StageDialog { private static final Logger logger = LogManager.getLogger(); private final Map> serviceItemGroups = new HashMap<>(); private final Map serviceItemMap = new HashMap<>(); - private final Map> 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> defaultServices) { - this.defaultServices.clear(); - this.defaultServices.putAll(defaultServices); - } - public void setServices(Map> serviceGroups) { serviceItemGroups.clear(); @@ -135,7 +129,7 @@ public class NodeServicesDialog extends StageDialog { Set nodeServices = node.getServices(); if (nodeServices.isEmpty()) { - nodeServices = defaultServices.get(node.getModel()); + nodeServices = getController().getDefaultServices().get(node.getModel()); } for (List items : serviceItemGroups.values()) { diff --git a/corefx/src/main/java/com/core/ui/dialogs/NodeTypesDialog.java b/corefx/src/main/java/com/core/ui/dialogs/NodeTypesDialog.java index aa2a0fb6..80810696 100644 --- a/corefx/src/main/java/com/core/ui/dialogs/NodeTypesDialog.java +++ b/corefx/src/main/java/com/core/ui/dialogs/NodeTypesDialog.java @@ -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 services = nodeType.getServices(); + Set 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(); }); diff --git a/corefx/src/main/java/com/core/utils/ConfigUtils.java b/corefx/src/main/java/com/core/utils/ConfigUtils.java index 9b2f99c6..d5c0fec7 100644 --- a/corefx/src/main/java/com/core/utils/ConfigUtils.java +++ b/corefx/src/main/java/com/core/utils/ConfigUtils.java @@ -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 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 services) throws IOException { + Set 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); } diff --git a/corefx/src/main/java/com/core/utils/NodeTypeConfig.java b/corefx/src/main/java/com/core/utils/NodeTypeConfig.java index dc4a29da..4b61999f 100644 --- a/corefx/src/main/java/com/core/utils/NodeTypeConfig.java +++ b/corefx/src/main/java/com/core/utils/NodeTypeConfig.java @@ -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 services; + private Set services; }