diff --git a/corefx/src/main/java/com/core/Controller.java b/corefx/src/main/java/com/core/Controller.java index 5f1b36c6..4afff3bf 100644 --- a/corefx/src/main/java/com/core/Controller.java +++ b/corefx/src/main/java/com/core/Controller.java @@ -8,6 +8,7 @@ import com.core.ui.*; import com.core.ui.dialogs.*; import com.core.utils.ConfigUtils; import com.core.utils.Configuration; +import com.core.utils.NodeTypeConfig; import com.core.websocket.CoreWebSocket; import com.jfoenix.controls.JFXProgressBar; import javafx.application.Application; @@ -79,6 +80,7 @@ public class Controller implements Initializable { private GeoDialog geoDialog = new GeoDialog(this); private ConnectDialog connectDialog = new ConnectDialog(this); private GuiPreferencesDialog guiPreferencesDialog = new GuiPreferencesDialog(this); + private NodeTypeCreateDialog nodeTypeCreateDialog = new NodeTypeCreateDialog(this); public void connectToCore(String coreUrl) { coreWebSocket.stop(); @@ -100,6 +102,7 @@ public class Controller implements Initializable { Map> serviceGroups = coreClient.getServices(); logger.info("core services: {}", serviceGroups); nodeServicesDialog.setServices(serviceGroups); + nodeTypeCreateDialog.setServices(serviceGroups); logger.info("initial core session join"); List sessions = coreClient.getSessions(); @@ -169,9 +172,11 @@ public class Controller implements Initializable { // update other components for new session graphToolbar.setRunButton(coreClient.isRunning()); hooksDialog.updateHooks(); - nodeTypesDialog.updateDefaultServices(); - // display first mobility script in player + // update session default services + setCoreDefaultServices(); + + // display first mobility script in player, if needed Map mobilityConfigMap = coreClient.getMobilityConfigs(); Optional nodeIdOptional = mobilityConfigMap.keySet().stream().findFirst(); if (nodeIdOptional.isPresent()) { @@ -230,6 +235,30 @@ public class Controller implements Initializable { return result; } + private void setCoreDefaultServices() { + try { + Map> defaults = configuration.getNodeTypeConfigs().stream() + .collect(Collectors.toMap(NodeTypeConfig::getModel, NodeTypeConfig::getServices)); + coreClient.setDefaultServices(defaults); + } catch (IOException ex) { + Toast.error("Error updating core default services", ex); + } + } + + 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); + } catch (IOException ex) { + Toast.error("Error saving configuration", ex); + } + } + public void deleteNode(CoreNode node) { networkGraph.removeNode(node); CoreNode mobilityNode = mobilityDialog.getNode(); @@ -253,6 +282,7 @@ public class Controller implements Initializable { locationDialog.setOwner(window); connectDialog.setOwner(window); guiPreferencesDialog.setOwner(window); + nodeTypeCreateDialog.setOwner(window); } @FXML @@ -401,6 +431,13 @@ public class Controller implements Initializable { logger.info("controller initialize"); swingNode.setContent(networkGraph.getGraphViewer()); + // 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); + // 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 b1a4eabb..af7c3013 100644 --- a/corefx/src/main/java/com/core/client/ICoreClient.java +++ b/corefx/src/main/java/com/core/client/ICoreClient.java @@ -31,7 +31,9 @@ public interface ICoreClient { Map> getServices() throws IOException; - Map> defaultServices() throws IOException; + Map> getDefaultServices() 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 c70e356e..15719e90 100644 --- a/corefx/src/main/java/com/core/client/rest/CoreRestClient.java +++ b/corefx/src/main/java/com/core/client/rest/CoreRestClient.java @@ -129,12 +129,18 @@ public class CoreRestClient implements ICoreClient { } @Override - public Map> defaultServices() throws IOException { + public Map> getDefaultServices() throws IOException { String url = getUrl(String.format("sessions/%s/services/default", sessionId)); GetDefaultServices getDefaultServices = WebUtils.getJson(url, GetDefaultServices.class); return getDefaultServices.getDefaults(); } + @Override + public boolean setDefaultServices(Map> defaults) throws IOException { + String url = getUrl(String.format("sessions/%s/services/default", sessionId)); + return WebUtils.postJson(url, defaults); + } + @Override public CoreService getService(CoreNode node, String serviceName) throws IOException { String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s", sessionId, node.getId(), serviceName)); diff --git a/corefx/src/main/java/com/core/data/NodeType.java b/corefx/src/main/java/com/core/data/NodeType.java index a4a69057..1e43d60f 100644 --- a/corefx/src/main/java/com/core/data/NodeType.java +++ b/corefx/src/main/java/com/core/data/NodeType.java @@ -5,9 +5,7 @@ import lombok.EqualsAndHashCode; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import java.util.concurrent.atomic.AtomicInteger; @Data @@ -24,6 +22,7 @@ public class NodeType { @EqualsAndHashCode.Include private final int id; private final int value; + private final List services = new ArrayList<>(); private String display; private String model; private String icon; @@ -39,13 +38,10 @@ public class NodeType { // EMANE_NET = 14; static { - addNodeType(new NodeType(DEFAULT, "host", "Host", "/icons/host-100.png")); - addNodeType(new NodeType(DEFAULT, "PC", "PC", "/icons/pc-100.png")); - addNodeType(new NodeType(DEFAULT, "mdr", "MDR", "/icons/router-100.png")); - addNodeType(new NodeType(SWITCH, "lanswitch", "Switch", "/icons/switch-100.png")); - addNodeType(new NodeType(HUB, "hub", "Hub", "/icons/hub-100.png")); - addNodeType(new NodeType(WLAN, "wlan", "WLAN", "/icons/wlan-100.png")); - addNodeType(new NodeType(EMANE, "wlan", "EMANE", "/icons/emane-100.png")); + add(new NodeType(SWITCH, "lanswitch", "Switch", "/icons/switch-100.png")); + add(new NodeType(HUB, "hub", "Hub", "/icons/hub-100.png")); + add(new NodeType(WLAN, "wlan", "WLAN", "/icons/wlan-100.png")); + add(new NodeType(EMANE, "wlan", "EMANE", "/icons/emane-100.png")); } @@ -57,10 +53,14 @@ public class NodeType { this.icon = icon; } - public static void addNodeType(NodeType nodeType) { + public static void add(NodeType nodeType) { ID_LOOKUP.put(nodeType.getId(), nodeType); } + public static void remove(NodeType nodeType) { + ID_LOOKUP.remove(nodeType.getId()); + } + public static NodeType get(Integer id) { return ID_LOOKUP.get(id); } diff --git a/corefx/src/main/java/com/core/ui/GraphToolbar.java b/corefx/src/main/java/com/core/ui/GraphToolbar.java index f0e69b9f..c24df381 100644 --- a/corefx/src/main/java/com/core/ui/GraphToolbar.java +++ b/corefx/src/main/java/com/core/ui/GraphToolbar.java @@ -63,8 +63,6 @@ public class GraphToolbar extends VBox { setupPickingButton(); setupEditingButton(); setupDrawingButton(); - - setupNodeTypes(); setupNodesButton(); setupDevicesButton(); @@ -119,7 +117,12 @@ public class GraphToolbar extends VBox { }); } - private void setupNodeTypes() { + public void setupNodeTypes() { + // clear existing configuration + labelMap.clear(); + nodesList.getItems().clear(); + devicesList.getItems().clear(); + for (NodeType nodeType : NodeType.getAll()) { ImageView icon = new ImageView(nodeType.getIcon()); icon.setFitWidth(NODES_ICON_SIZE); 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 fed4e7fe..dad760f3 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<>(); - // TODO: get this from core itself private final Map> defaultServices = new HashMap<>(); private CoreNode node; private int index = 0; @@ -47,12 +46,6 @@ public class NodeServicesDialog extends StageDialog { }); addCancelButton(); - defaultServices.put("mdr", new HashSet<>(Arrays.asList("zebra", "OSPFv3MDR", "IPForward"))); - defaultServices.put("PC", new HashSet<>(Arrays.asList("DefaultRoute"))); - defaultServices.put("prouter", new HashSet<>(Arrays.asList("zebra", "OSPFv2", "OSPFv3", "IPForward"))); - defaultServices.put("router", new HashSet<>(Arrays.asList("zebra", "OSPFv2", "OSPFv3", "IPForward"))); - defaultServices.put("host", new HashSet<>(Arrays.asList("DefaultRoute", "SSH"))); - groupListView.getSelectionModel().selectedItemProperty().addListener((ov, previous, current) -> { if (current == null) { return; @@ -80,6 +73,11 @@ public class NodeServicesDialog extends StageDialog { }); } + public void setDefaultServices(Map> defaultServices) { + this.defaultServices.clear(); + this.defaultServices.putAll(defaultServices); + } + public void setServices(Map> serviceGroups) { serviceItemGroups.clear(); diff --git a/corefx/src/main/java/com/core/ui/dialogs/NodeTypeCreateDialog.java b/corefx/src/main/java/com/core/ui/dialogs/NodeTypeCreateDialog.java new file mode 100644 index 00000000..e1cd164e --- /dev/null +++ b/corefx/src/main/java/com/core/ui/dialogs/NodeTypeCreateDialog.java @@ -0,0 +1,74 @@ +package com.core.ui.dialogs; + +import com.core.Controller; +import com.core.data.NodeType; +import com.jfoenix.controls.JFXButton; +import com.jfoenix.controls.JFXListView; +import com.jfoenix.controls.JFXTextField; +import javafx.fxml.FXML; +import javafx.scene.control.SelectionMode; +import lombok.Data; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@Data +public class NodeTypeCreateDialog extends StageDialog { + private static final Logger logger = LogManager.getLogger(); + @FXML private JFXListView servicesListView; + @FXML private JFXTextField modelTextField; + @FXML private JFXTextField displayTextField; + private Runnable onCreateHandler; + + public NodeTypeCreateDialog(Controller controller) { + super(controller, "/fxml/node_type_create_dialog.fxml"); + setTitle("Create Node Configuration"); + + JFXButton saveButton = createButton("Create"); + saveButton.setOnAction(event -> { + onCreateHandler.run(); + close(); + }); + addCancelButton(); + + servicesListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); + displayTextField.focusedProperty().addListener((obs, prev, current) -> { + if (!current) { + return; + } + + String model = modelTextField.getText(); + if (!model.isEmpty()) { + displayTextField.setText(model.substring(0, 1).toUpperCase() + model.substring(1)); + } + }); + } + + public NodeType getCreatedNodeType() { + NodeType nodeType = new NodeType(NodeType.DEFAULT, modelTextField.getText(), displayTextField.getText(), + "/icons/host-100.png"); + nodeType.getServices().addAll(servicesListView.getSelectionModel().getSelectedItems()); + return nodeType; + } + + public void setServices(Map> serviceGroups) { + List services = new ArrayList<>(); + for (List groupServices : serviceGroups.values()) { + services.addAll(groupServices); + } + services.sort(String::compareTo); + servicesListView.getItems().setAll(services); + } + + + public void showDialog(Runnable runnable) { + onCreateHandler = runnable; + modelTextField.setText(""); + displayTextField.setText(""); + servicesListView.getSelectionModel().clearSelection(); + show(); + } +} 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 ae816966..aa2a0fb6 100644 --- a/corefx/src/main/java/com/core/ui/dialogs/NodeTypesDialog.java +++ b/corefx/src/main/java/com/core/ui/dialogs/NodeTypesDialog.java @@ -4,6 +4,7 @@ import com.core.Controller; import com.core.data.CoreNode; import com.core.data.NodeType; import com.core.ui.Toast; +import com.core.utils.NodeTypeConfig; import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXListView; import com.jfoenix.controls.JFXTextField; @@ -15,8 +16,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.io.File; -import java.io.IOException; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -25,7 +24,6 @@ public class NodeTypesDialog extends StageDialog { private static final Logger logger = LogManager.getLogger(); private final Map nodeTypeMap = new HashMap<>(); private NodeType selectedNodeType; - private Map> defaultServices = new HashMap<>(); @FXML private JFXListView listView; @FXML private JFXTextField modelTextField; @FXML private JFXTextField displayTextField; @@ -40,7 +38,8 @@ public class NodeTypesDialog extends StageDialog { public NodeTypesDialog(Controller controller) { super(controller, "/fxml/node_types_dialog.fxml"); setTitle("Node Configuration"); - addCancelButton(); + JFXButton closeButton = createButton("Close"); + closeButton.setOnAction(event -> close()); listView.getSelectionModel().selectedItemProperty().addListener((ov, prev, current) -> { if (current == null) { @@ -53,14 +52,14 @@ public class NodeTypesDialog extends StageDialog { iconTextField.setText(nodeType.getIcon()); iconImage.setImage(new Image(nodeType.getIcon())); selectedNodeType = nodeType; - List services = defaultServices.getOrDefault(nodeType.getModel(), Collections.emptyList()); + List services = nodeType.getServices(); nodeServicesListView.getItems().setAll(services); }); iconButton.setOnAction(event -> { FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("Select Icon"); - fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); + fileChooser.setInitialDirectory(new File(getController().getConfiguration().getIconPath())); fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("PNG", "*.png")); File file = fileChooser.showOpenDialog(controller.getWindow()); if (file != null) { @@ -84,15 +83,38 @@ public class NodeTypesDialog extends StageDialog { controller.getGraphToolbar().updateNodeType(selectedNodeType.getId(), iconPath); Toast.info(String.format("Node %s Updated", selectedNodeType.getDisplay())); }); + + deleteButton.setOnAction(event -> { + String display = listView.getSelectionModel().getSelectedItem(); + NodeType nodeType = nodeTypeMap.get(display); + NodeType.remove(nodeType); + listView.getItems().remove(display); + NodeTypeConfig nodeTypeConfig = createNodeTypeConfig(nodeType); + getController().getConfiguration().getNodeTypeConfigs().remove(nodeTypeConfig); + getController().updateNodeTypes(); + }); + + addButton.setOnAction(event -> { + NodeTypeCreateDialog nodeTypeCreateDialog = getController().getNodeTypeCreateDialog(); + nodeTypeCreateDialog.showDialog(() -> { + NodeType nodeType = nodeTypeCreateDialog.getCreatedNodeType(); + NodeType.add(nodeType); + nodeTypeMap.put(nodeType.getDisplay(), nodeType); + listView.getItems().add(nodeType.getDisplay()); + NodeTypeConfig nodeTypeConfig = createNodeTypeConfig(nodeType); + getController().getConfiguration().getNodeTypeConfigs().add(nodeTypeConfig); + getController().updateNodeTypes(); + }); + }); } - public void updateDefaultServices() { - try { - defaultServices = getCoreClient().defaultServices(); - listView.getSelectionModel().selectFirst(); - } catch (IOException ex) { - Toast.error("Error getting default services", ex); - } + private NodeTypeConfig createNodeTypeConfig(NodeType nodeType) { + return new NodeTypeConfig( + nodeType.getModel(), + nodeType.getDisplay(), + nodeType.getIcon(), + nodeType.getServices() + ); } public void showDialog() { diff --git a/corefx/src/main/java/com/core/utils/ConfigUtils.java b/corefx/src/main/java/com/core/utils/ConfigUtils.java index 85635a3a..9b2f99c6 100644 --- a/corefx/src/main/java/com/core/utils/ConfigUtils.java +++ b/corefx/src/main/java/com/core/utils/ConfigUtils.java @@ -1,5 +1,6 @@ package com.core.utils; +import com.core.data.NodeType; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -9,6 +10,9 @@ 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; public final class ConfigUtils { private static final Logger logger = LogManager.getLogger(); @@ -18,6 +22,7 @@ public final class ConfigUtils { private static final Path CONFIG_FILE = Paths.get(HOME.toString(), CONFIG_FILE_NAME); private static final Path XML_DIR = Paths.get(HOME.toString(), "xml"); private static final Path MOBILITY_DIR = Paths.get(HOME.toString(), "mobility"); + private static final Path ICON_DIR = Paths.get(HOME.toString(), "icons"); private ConfigUtils() { @@ -35,6 +40,27 @@ public final class ConfigUtils { return JsonUtils.read(new FileInputStream(CONFIG_FILE.toFile()), Configuration.class); } + private static List createDefaults() throws IOException { + return Arrays.asList( + createDefault("host", "Host", "/icons/host-100.png", Arrays.asList( + "DefaultRoute", "SSH" + )), + createDefault("PC", "PC", "/icons/pc-100.png", + Collections.singletonList("DefaultRoute")), + createDefault("mdr", "MDR", "/icons/router-100.png", Arrays.asList( + "zebra", "OSPFv3MDR", "IPForward" + )) + ); + } + + private static NodeTypeConfig createDefault(String model, String display, String icon, + List 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); + return new NodeTypeConfig(model, display, iconPath.toUri().toString(), services); + } + public static Configuration load() { try { if (!HOME.toFile().exists()) { @@ -42,6 +68,7 @@ public final class ConfigUtils { Files.createDirectory(HOME); Files.createDirectory(XML_DIR); Files.createDirectory(MOBILITY_DIR); + Files.createDirectory(ICON_DIR); } Configuration configuration; @@ -51,11 +78,30 @@ public final class ConfigUtils { configuration = readConfig(); configuration.setXmlPath(XML_DIR.toString()); configuration.setMobilityPath(MOBILITY_DIR.toString()); + configuration.setIconPath(ICON_DIR.toString()); + configuration.setNodeTypeConfigs(createDefaults()); save(configuration); } else { configuration = readConfig(); } + // initialize node types + for (NodeTypeConfig nodeTypeConfig : configuration.getNodeTypeConfigs()) { + NodeType nodeType = new NodeType( + NodeType.DEFAULT, + nodeTypeConfig.getModel(), + nodeTypeConfig.getDisplay(), + nodeTypeConfig.getIcon() + ); + nodeTypeConfig.getServices().sort(String::compareTo); + nodeType.getServices().addAll(nodeTypeConfig.getServices()); + NodeType.add(nodeType); + } + + // override configuration from command line + String coreRest = System.getProperty("core-rest"); + configuration.setCoreRest(coreRest); + return configuration; } catch (IOException ex) { logger.error("error reading config file"); diff --git a/corefx/src/main/java/com/core/utils/Configuration.java b/corefx/src/main/java/com/core/utils/Configuration.java index 6dd2f352..e7815166 100644 --- a/corefx/src/main/java/com/core/utils/Configuration.java +++ b/corefx/src/main/java/com/core/utils/Configuration.java @@ -12,6 +12,7 @@ public class Configuration { private String coreRest; private String xmlPath; private String mobilityPath; + private String iconPath; private String shellCommand; private List nodeTypeConfigs = new ArrayList<>(); } diff --git a/corefx/src/main/java/com/core/utils/NodeTypeConfig.java b/corefx/src/main/java/com/core/utils/NodeTypeConfig.java index 7735130f..dc4a29da 100644 --- a/corefx/src/main/java/com/core/utils/NodeTypeConfig.java +++ b/corefx/src/main/java/com/core/utils/NodeTypeConfig.java @@ -1,16 +1,20 @@ package com.core.utils; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -import java.util.ArrayList; import java.util.List; @Data +@EqualsAndHashCode(onlyExplicitlyIncluded = true) @NoArgsConstructor +@AllArgsConstructor public class NodeTypeConfig { + @EqualsAndHashCode.Include private String model; private String display; private String icon; - private List services = new ArrayList<>(); + private List services; } diff --git a/corefx/src/main/resources/fxml/node_type_create_dialog.fxml b/corefx/src/main/resources/fxml/node_type_create_dialog.fxml new file mode 100644 index 00000000..dfe8c26d --- /dev/null +++ b/corefx/src/main/resources/fxml/node_type_create_dialog.fxml @@ -0,0 +1,18 @@ + + + + + + + + + + + +