All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.sdnwiselab.sdnwise.node.NodeFactory Maven / Gradle / Ivy

There is a newer version: 2.0.13
Show newest version
/* 
 * Copyright (C) 2015 SDN-WISE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package com.github.sdnwiselab.sdnwise.node;

import com.github.sdnwiselab.sdnwise.util.NodeAddress;
import java.util.logging.*;
import org.apache.commons.cli.*;

/**
 *
 * @author Seby
 */
public class NodeFactory {

    private static final Logger LOGGER = Logger.getLogger(NodeFactory.class.getName());

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Option netId = Option.builder("n")
                .argName("network-id")
                .hasArg()
                .required()
                .desc("Network ID of the node")
                .numberOfArgs(1)
                .build();

        Option address = Option.builder("a")
                .argName("node-address")
                .hasArg()
                .required()
                .desc("Address of the node <0-65535>")
                .numberOfArgs(1)
                .build();

        Option port = Option.builder("p")
                .argName("port")
                .hasArg()
                .required()
                .desc("Listening UDP port")
                .numberOfArgs(1)
                .build();

        Option neighbors = Option.builder("t")
                .argName("topology-file")
                .hasArg()
                .required()
                .desc("Use given file for neighbors discovery")
                .numberOfArgs(1)
                .build();

        Option controller = Option.builder("c")
                .argName("controller")
                .hasArg()
                .desc("IP address and port of the controller. Example:  [SINK ONLY]")
                .numberOfArgs(1)
                .build();

        Option switchPort = Option.builder("sp")
                .argName("switch-port")
                .hasArg()
                .desc("Port number of the switch [SINK ONLY]")
                .numberOfArgs(1)
                .build();

        Option switchMac = Option.builder("sm")
                .argName("switch-mac")
                .hasArg()
                .desc("MAC address of the switch. Example: <00:00:00:00:00:00> [SINK ONLY]")
                .numberOfArgs(1)
                .build();

        Option switchDPID = Option.builder("sd")
                .argName("switch-dpid")
                .hasArg()
                .desc("DPID of the switch [SINK ONLY]")
                .numberOfArgs(1)
                .build();

        Option logfile = Option.builder("l")
                .argName("log-file")
                .hasArg()
                .desc("Use given file for log")
                .numberOfArgs(1)
                .build();

        Option loglvl = Option.builder("ll")
                .argName("log-level")
                .hasArg()
                .desc("Use given level for log. Values: SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST")
                .numberOfArgs(1)
                .optionalArg(true)
                .build();

        Options options = new Options();
        options.addOption(netId);
        options.addOption(address);
        options.addOption(port);
        options.addOption(neighbors);
        options.addOption(controller);
        options.addOption(logfile);
        options.addOption(loglvl);
        options.addOption(switchPort);
        options.addOption(switchMac);
        options.addOption(switchDPID);

        // create the parser
        CommandLineParser parser = new DefaultParser();
        try {
            CommandLine line = parser.parse(options, args);
            Thread th;

            byte cmdNetId = (byte) Integer.parseInt(line.getOptionValue("n"));
            NodeAddress cmdAddress = new NodeAddress(Integer.parseInt(line.getOptionValue("a")));
            int cmdPort = Integer.parseInt(line.getOptionValue("p"));
            String cmdTopo = line.getOptionValue("t");
            String cmdLog = line.getOptionValue("l");

            String cmdLevel;

            if (!line.hasOption("ll")) {
                cmdLevel = "SEVERE";
            } else {
                cmdLevel = line.getOptionValue("ll");
            }

            if (line.hasOption("c")) {

                if (!line.hasOption("sd")) {
                    throw new ParseException("-sd option missing");
                }

                if (!line.hasOption("sp")) {
                    throw new ParseException("-sp option missing");
                }

                if (!line.hasOption("sm")) {
                    throw new ParseException("-sm option missing");
                }

                String cmdSDpid = line.getOptionValue("sd");
                String cmdSMac = line.getOptionValue("sm");
                long cmdSPort = Long.parseLong(line.getOptionValue("sp"));

                String[] ipport = line.getOptionValue("c").split(":");
                th = new Thread(new SinkNode(
                        cmdNetId,
                        cmdAddress,
                        cmdPort,
                        ipport[0],
                        Integer.parseInt(ipport[1]),
                        cmdTopo,
                        cmdLog,
                        cmdLevel,
                        cmdSDpid,
                        cmdSMac,
                        cmdSPort));

            } else {
                th = new Thread(new SensorNode(
                        cmdNetId,
                        cmdAddress,
                        cmdPort,
                        cmdTopo,
                        cmdLog,
                        cmdLevel));
            }
            th.start();
        } catch (ParseException exp) {
            LOGGER.log(Level.SEVERE, "Parsing failed.  Reason: {0}", exp.getMessage());
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("sdn-wise-node", options);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy