Exemplo n.º 1
0
int module_enable(struct np_module* module, int add) {
	char *config_path = NULL, *repo_path = NULL, *repo_type_str = NULL;
	int repo_type = -1, main_model_count;
	xmlDocPtr module_config;
	xmlNodePtr node;
	xmlXPathContextPtr xpath_ctxt;
	xmlXPathObjectPtr xpath_obj;

	if (asprintf(&config_path, "%s/%s.xml", MODULES_CFG_DIR, module->name) == -1) {
		nc_verb_error("asprintf() failed (%s:%d).", __FILE__, __LINE__);
		return(EXIT_FAILURE);
	}
	if ((module_config = xmlReadFile(config_path, NULL, XML_PARSE_NOBLANKS|XML_PARSE_NSCLEAN|XML_PARSE_NOWARNING|XML_PARSE_NOERROR)) == NULL) {
		nc_verb_error("Reading configuration for %s module failed", module->name);
		free(config_path);
		return(EXIT_FAILURE);
	}
	free(config_path);

	if ((xpath_ctxt = xmlXPathNewContext(module_config)) == NULL) {
		nc_verb_error("Creating XPath context failed (%s:%d - module %s)", __FILE__, __LINE__, module->name);
		return (EXIT_FAILURE);
	}

	/* get datastore information */
	if ((xpath_obj = xmlXPathEvalExpression(BAD_CAST "/device/repo", xpath_ctxt)) == NULL) {
		nc_verb_error("XPath evaluating error (%s:%d)", __FILE__, __LINE__);
		goto err_cleanup;
	} else if (xpath_obj->nodesetval == NULL || xpath_obj->nodesetval->nodeNr != 1) {
		nc_verb_verbose("repo is not unique in %s transAPI module configuration.", module->name);
		xmlXPathFreeObject(xpath_obj);
		goto err_cleanup;
	}

	for (node = xpath_obj->nodesetval->nodeTab[0]->children; node != NULL; node = node->next) {
		if (node->type != XML_ELEMENT_NODE) {
			continue;
		}
		if (xmlStrcmp(node->name, BAD_CAST "type") == 0) {
			repo_type_str = (char*)xmlNodeGetContent(node);
		} else if (xmlStrcmp(node->name, BAD_CAST "path") == 0) {
			repo_path = (char*)xmlNodeGetContent(node);
		}
	}
	if (repo_type_str == NULL) {
		nc_verb_warning("Missing attribute \'type\' in repo element for %s transAPI module.", module->name);
		repo_type_str = strdup("unknown");
	}
	if (strcmp(repo_type_str, "empty") == 0) {
		repo_type = NCDS_TYPE_EMPTY;
	} else if (strcmp(repo_type_str, "file") == 0) {
		repo_type = NCDS_TYPE_FILE;
	} else {
		nc_verb_warning("Unknown repo type \'%s\' in %s transAPI module configuration", repo_type_str, module->name);
		nc_verb_warning("Continuing with \'empty\' datastore type.");
		repo_type = NCDS_TYPE_EMPTY;
	}
	free(repo_type_str);

	if (repo_type == NCDS_TYPE_FILE && repo_path == NULL) {
		nc_verb_error("Missing path for \'file\' datastore type in %s transAPI module configuration.", module->name);
		xmlXPathFreeObject(xpath_obj);
		goto err_cleanup;
	}
	xmlXPathFreeObject(xpath_obj);

	/* get data-models element */
	if ((xpath_obj = xmlXPathEvalExpression(BAD_CAST "/device/data-models", xpath_ctxt)) == NULL) {
		nc_verb_error("XPath evaluating error (%s:%d)", __FILE__, __LINE__);
		goto err_cleanup;
	} else if (xpath_obj->nodesetval == NULL || xpath_obj->nodesetval->nodeNr != 1) {
		nc_verb_verbose("data-models is not unique in %s transAPI module configuration.", module->name);
		xmlXPathFreeObject(xpath_obj);
		goto err_cleanup;
	}

	/* parse models in the config-defined order, both main and augments */
	main_model_count = 0;
	for (node = xpath_obj->nodesetval->nodeTab[0]->children; node != NULL; node = node->next) {
		if (node->type != XML_ELEMENT_NODE) {
			continue;
		}
		if (xmlStrcmp(node->name, BAD_CAST "model") == 0) {
			parse_model_cfg(module, node, -1);
		}
		if (xmlStrcmp(node->name, BAD_CAST "model-main") == 0) {
			parse_model_cfg(module, node, repo_type);
			main_model_count++;
		}
	}
	xmlXPathFreeObject(xpath_obj);
	if (main_model_count == 0) {
		nc_verb_verbose("model-main is not present in %s transAPI module configuration.", module->name);
		goto err_cleanup;
	} else if (main_model_count > 1) {
		nc_verb_verbose("model-main is not unique in %s transAPI module configuration.", module->name);
		goto err_cleanup;
	}

	if (repo_type == NCDS_TYPE_FILE) {
		if (ncds_file_set_path(module->ds, repo_path)) {
			nc_verb_verbose("Unable to set path to datastore of the \'%s\' transAPI module.", module->name);
			goto err_cleanup;
		}
	}
	free(repo_path);
	repo_path = NULL;

	if ((module->id = ncds_init(module->ds)) < 0) {
		goto err_cleanup;
	}

	xmlXPathFreeContext(xpath_ctxt);
	xmlFreeDoc(module_config);

	if (ncds_consolidate() != 0) {
		nc_verb_warning("%s: consolidating libnetconf datastores failed for module %s.", __func__, module->name);
		return (EXIT_FAILURE);
	}

	/* remove datastore locks if any kept */
	if (server_start) {
		ncds_break_locks(NULL);
	}
	if (ncds_device_init(&(module->id), NULL, 1) != 0) {
		nc_verb_error("Device initialization of module %s failed.", module->name);
		ncds_free(module->ds);
		module->ds = NULL;
		return (EXIT_FAILURE);
	}

	if (add) {
		if (netopeer_options.modules) {
			netopeer_options.modules->prev = module;
		}
		module->next = netopeer_options.modules;
		netopeer_options.modules = module;
	}

	return (EXIT_SUCCESS);

err_cleanup:

	xmlXPathFreeContext(xpath_ctxt);
	xmlFreeDoc(module_config);

	ncds_free(module->ds);
	module->ds = NULL;

	free(repo_path);

	return (EXIT_FAILURE);
}
Exemplo n.º 2
0
static PyObject *ncDatastoreAdd(PyObject *self, PyObject *args, PyObject *keywords)
{
	const char *path, *datastore = NULL, *transapi = NULL;
	char *name = NULL;
	NCDS_TYPE type = NCDS_TYPE_EMPTY;
	struct ncds_ds *ds;
	ncds_id dsid;
	PyObject *PyFeatures = NULL;
	char *kwlist[] = {"model", "datastore", "transapi", "features", NULL};

	/* Get input parameters */
	if (! PyArg_ParseTupleAndKeywords(args, keywords, "s|zzO!", kwlist, &path, &datastore, &transapi, &PyList_Type, &PyFeatures)) {
		return (NULL);
	}

	/* set correct type according to provided parameters */
	if (datastore) {
		type = NCDS_TYPE_FILE;
	}

	/* get name of the datastore for further referencing */
	if (ncds_model_info(path, &name, NULL, NULL, NULL, NULL, NULL) != EXIT_SUCCESS) {
		return (NULL);
	}

	/* create datastore */
	if (transapi) {
		if ((ds = ncds_new_transapi(type, path, transapi)) == NULL) {
			free(name);
			return (NULL);
		}
	} else {
		/* todo get_state() */
		if ((ds = ncds_new(type, path, NULL)) == NULL) {
			free(name);
			return (NULL);
		}
	}

	if (datastore) {
		if (ncds_file_set_path(ds, datastore) != EXIT_SUCCESS) {
			ncds_free(ds);
			free(name);
			return (NULL);
		}
	}

	if ((dsid = ncds_init(ds)) <= 0) {
		ncds_free(ds);
		free(name);
		return (NULL);
	}

	set_features(name, PyFeatures);

	if (ncds_consolidate() != EXIT_SUCCESS) {
		ncds_free(ds);
		free(name);
		return (NULL);
	}

	if (ncds_device_init(&dsid, global_cpblts, 0)) {
		ncds_free(ds);
		free(name);
		return (NULL);
	}

	PyDict_SetItem(datastores, PyUnicode_FromFormat("%d", dsid), PyUnicode_FromString(name));

	free(name);
	Py_RETURN_NONE;
}
Exemplo n.º 3
0
int
main(int argc, char **argv)
{
    const char *optstring = "d:fhv:";

    const struct option longopts[] = {
        {"db", required_argument, 0, 'd'},
        {"foreground", no_argument, 0, 'f'},
        {"help", no_argument, 0, 'h'},
        {"verbose", required_argument, 0, 'v'},
        {0, 0, 0, 0}
    };
    int longindex, next_option;
    int verbose = 0;
    int retval = EXIT_SUCCESS, r;
    char *aux_string;
    struct sigaction action;
    sigset_t block_mask;

    struct {
        struct ncds_ds *server;
        ncds_id server_id;
        struct ncds_ds *ofc;
        ncds_id ofc_id;
    } ds = {NULL, -1, NULL, -1};

    /* connection channel to agents */
    comm_t *c = NULL;

    /* initialize message system and set verbose and debug variables */
    if ((aux_string = getenv(ENVIRONMENT_VERBOSE)) == NULL) {
        /* default verbose level */
        verbose = NC_VERB_ERROR;
    } else {
        verbose = atoi(aux_string);
    }

    /* parse given options */
    while ((next_option = getopt_long(argc, argv, optstring, longopts,
                                      &longindex)) != -1) {
        switch (next_option) {
        case 'd':
            ovsdb_path = strdup(optarg);
            break;
        case 'f':
            ofc_daemonize = 0;
            break;
        case 'h':
            print_usage(argv[0]);
            break;
        case 'v':
            verbose = atoi(optarg);
            break;
        default:
            print_usage(argv[0]);
            break;
        }
    }

    /* set signal handler */
    sigfillset(&block_mask);
    action.sa_handler = signal_handler;
    action.sa_mask = block_mask;
    action.sa_flags = 0;
    sigaction(SIGINT, &action, NULL);
    sigaction(SIGQUIT, &action, NULL);
    sigaction(SIGABRT, &action, NULL);
    sigaction(SIGTERM, &action, NULL);
    sigaction(SIGKILL, &action, NULL);

    /* set verbose message printer callback */
    nc_callback_print(clb_print);

    /* normalize value if not from the enum */
    if (verbose < NC_VERB_ERROR) {
        nc_verbosity(NC_VERB_ERROR);
    } else if (verbose > NC_VERB_DEBUG) {
        nc_verbosity(NC_VERB_DEBUG);
    } else {
        nc_verbosity(verbose);
    }

    /* go to the background as a daemon */
    if (ofc_daemonize == 1) {
        if (daemon(0, 0) != 0) {
            nc_verb_error("Going to background failed (%s)", strerror(errno));
            return (EXIT_FAILURE);
        }
        openlog("ofc-server", LOG_PID, LOG_DAEMON);
    } else {
        openlog("ofc-server", LOG_PID | LOG_PERROR, LOG_DAEMON);
    }

    /* make sure we have sufficient rights to communicate with OVSDB */
    /* TODO */

    /* init libnetconf for a multilayer server */
    r = nc_init((NC_INIT_ALL & ~NC_INIT_NACM) | NC_INIT_MULTILAYER);
    if (r < 0) {
        nc_verb_error("libnetconf initialization failed.");
        return (EXIT_FAILURE);
    }

    /* Initiate communication subsystem for communication with agents */
    if ((c = comm_init(r)) == NULL) {
        nc_verb_error("Communication subsystem not initiated.");
        return (EXIT_FAILURE);
    }

    /* prepare the ietf-netconf-server module */
    ncds_add_model(OFC_DATADIR
                   "/ietf-netconf-server/ietf-x509-cert-to-name.yin");
    ds.server =
        ncds_new_transapi_static(NCDS_TYPE_FILE,
                                 OFC_DATADIR
                                 "/ietf-netconf-server/ietf-netconf-server.yin",
                                 &server_transapi);
    if (ds.server == NULL) {
        retval = EXIT_FAILURE;
        nc_verb_error("Creating ietf-netconf-server datastore failed.");
        goto cleanup;
    }
    ncds_file_set_path(ds.server,
                       OFC_DATADIR "/ietf-netconf-server/datastore.xml");
    ncds_feature_enable("ietf-netconf-server", "ssh");
    ncds_feature_enable("ietf-netconf-server", "inbound-ssh");
    if ((ds.server_id = ncds_init(ds.server)) < 0) {
        retval = EXIT_FAILURE;
        nc_verb_error
            ("Initiating ietf-netconf-server datastore failed (error code %d).",
             ds.ofc_id);
        goto cleanup;
    }

    /* prepare the of-config module */
    ds.ofc = ncds_new_transapi_static(NCDS_TYPE_CUSTOM,
                                      OFC_DATADIR "/of-config/of-config.yin",
                                      &ofc_transapi);
    if (ds.ofc == NULL) {
        retval = EXIT_FAILURE;
        nc_verb_error("Creating of-config datastore failed.");
        goto cleanup;
    }
    ncds_custom_set_data(ds.ofc, NULL, &ofcds_funcs);
    if ((ds.ofc_id = ncds_init(ds.ofc)) < 0) {
        retval = EXIT_FAILURE;
        nc_verb_error("Initiating of-config datastore failed (error code %d).",
                      ds.ofc_id);
        goto cleanup;
    }

    if (ncds_consolidate() != 0) {
        retval = EXIT_FAILURE;
        nc_verb_error("Consolidating data models failed.");
        goto cleanup;
    }

    if (ncds_device_init(&(ds.server_id), NULL, 1) != 0) {
        retval = EXIT_FAILURE;
        nc_verb_error("Initiating ietf-netconf-server module failed.");
        goto cleanup;
    }

    if (ncds_device_init(&(ds.ofc_id), NULL, 1) != 0) {
        retval = EXIT_FAILURE;
        nc_verb_error("Initiating of-config module failed.");
        goto cleanup;
    }

    nc_verb_verbose("OF-CONFIG server successfully initialized.");

    while (!mainloop) {
        comm_loop(c, 500);
    }

cleanup:

    /* cleanup */
    nc_close();

    return (retval);
}