コード例 #1
0
ファイル: netconf.c プロジェクト: ADTRAN/libnetconf
static PyObject *ncDatastoreAddAugment(PyObject *self, PyObject *args, PyObject *keywords)
{

	const char *path, *transapi = NULL;
	char *name = NULL;
	PyObject *PyFeatures = NULL;
	char *kwlist[] = {"model", "transapi", "features", NULL};

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

	/* 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 (ncds_add_augment_transapi(path, transapi) == EXIT_FAILURE) {
			free(name);
			return (NULL);
		}
	} else {
		if (ncds_add_model(path) == EXIT_FAILURE) {
			free(name);
			return (NULL);
		}
	}

	set_features(name, PyFeatures);
	free(name);

	if (ncds_consolidate() != EXIT_SUCCESS) {
		return (NULL);
	}

	Py_RETURN_NONE;
}
コード例 #2
0
ファイル: netconf.c プロジェクト: ADTRAN/libnetconf
static PyObject *ncDatastoreAddImport(PyObject *self, PyObject *args, PyObject *keywords)
{
	const char *path;
	char *name = NULL;
	PyObject *PyFeatures = NULL;
	char *kwlist[] = {"model", "features", NULL};

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

	if (ncds_model_info(path, &name, NULL, NULL, NULL, NULL, NULL) != EXIT_SUCCESS ||
			ncds_add_model(path) != EXIT_SUCCESS) {
		free(name);
		return (NULL);
	}

	set_features(name, PyFeatures);

	free(name);
	Py_RETURN_NONE;
}
コード例 #3
0
ファイル: cfginterfaces-init.c プロジェクト: CESNET/netopeer
int main(int argc, char** argv)
{
	struct nc_session* dummy_session;
	struct nc_cpblts* capabs;
	struct ncds_ds* ds;
	nc_rpc* rpc;
	nc_reply* reply;
	char* new_startup_config;
	xmlDocPtr startup_doc = NULL;
	int ret = 0, i, j;

	if (argc < 2 || argv[1][0] == '-') {
		help(argv[0]);
		return 1;
	}

	/* set message printing callback */
	nc_callback_print(my_print);

	/* init libnetconf for messages  from transAPI function */
	if (nc_init(NC_INIT_ALL | NC_INIT_MULTILAYER) == -1) {
		my_print(NC_VERB_ERROR, "Could not initialize libnetconf.");
		return 1;
	}

	/* register the datastore */
	if ((ds = ncds_new(NCDS_TYPE_FILE, "./model/ietf-interfaces.yin", NULL)) == NULL) {
		nc_close();
		return 1;
	}

	/* add imports and augments */
	if (ncds_add_model("./model/ietf-yang-types.yin") != 0 || ncds_add_model("./model/ietf-inet-types.yin") != 0 ||
			ncds_add_model("./model/ietf-ip.yin") != 0) {
		nc_verb_error("Could not add import and augment models.");
		nc_close();
		return 1;
	}

	/* enable features */
	for (i = 2; i < argc; ++i) {
		if (strcmp(argv[i], "ipv4-non-contiguous-netmasks") == 0 || strcmp(argv[i], "ipv6-privacy-autoconf") == 0) {
			j = ncds_feature_enable("ietf-ip", argv[i]);
		} else {
			j = ncds_feature_enable("ietf-interfaces", argv[i]);
		}

		if (j != 0) {
			nc_verb_error("Could not enable feature \"%s\".", argv[i]);
			nc_close();
			return 1;
		}
	}

	/* set the path to the target file */
	if (ncds_file_set_path(ds, argv[1]) != 0) {
		nc_verb_error("Could not set \"%s\" to the datastore.", argv[1]);
		nc_close();
		return 1;
	}
	if (ncds_init(ds) < 0) {
		nc_verb_error("Failed to nitialize datastore.");
		nc_close();
		return 1;
	}
	if (ncds_consolidate() != 0) {
		nc_verb_error("Could not consolidate the datastore.");
		nc_close();
		return 1;
	}

	if (transapi_init(&startup_doc) != EXIT_SUCCESS) {
		nc_close();
		return 1;
	}

	if (startup_doc == NULL || startup_doc->children == NULL) {
		/* nothing to do */
		nc_close();
		return 0;
	}

	/* create the dummy session */
	capabs = nc_cpblts_new(capabilities);
	if ((dummy_session = nc_session_dummy("session0", "root", NULL, capabs)) == NULL) {
		nc_verb_error("Could not create a dummy session.");
		nc_close();
		return 1;
	}

	/* dump the new config */
	xmlDocDumpMemory(startup_doc, (xmlChar**)&new_startup_config, NULL);
	xmlFreeDoc(startup_doc);

	/* apply edit-config rpc on the datastore */
	if ((rpc = nc_rpc_editconfig(NC_DATASTORE_STARTUP, NC_DATASTORE_CONFIG, 0, 0, 0, new_startup_config)) == NULL) {
		nc_verb_error("Could not create edit-config RPC.");
		nc_close();
		return 1;
	}
	free(new_startup_config);
	reply = ncds_apply_rpc2all(dummy_session, rpc, NULL);
	if (nc_reply_get_type(reply) != NC_REPLY_OK) {
		nc_verb_error("Edit-config RPC failed.");
		nc_close();
		return 1;
	}

	nc_reply_free(reply);
	nc_rpc_free(rpc);
	nc_cpblts_free(capabs);
	nc_session_free(dummy_session);
	nc_close();
	return ret;
}
コード例 #4
0
/*
 * if repo_type is -1, then we are working with augment models specifications
 */
static int parse_model_cfg(struct np_module* module, xmlNodePtr node, NCDS_TYPE repo_type) {
	char *transapi_path = NULL, *model_path = NULL, *feature, *name, *aux;
	struct transapi *st = NULL;
	xmlNodePtr aux_node;

	if (strcmp(module->name, NETOPEER_MODULE_NAME) == 0) {
		st = &netopeer_transapi;
	} else if (strcmp(module->name, NCSERVER_MODULE_NAME) == 0) {
		st = &server_transapi;
	}

	for (aux_node = node->children; aux_node != NULL; aux_node = aux_node->next) {
		if (xmlStrcmp(aux_node->name, BAD_CAST "path") == 0) {
			model_path = (char*)xmlNodeGetContent(aux_node);
		}
		if (xmlStrcmp(aux_node->name, BAD_CAST "transapi") == 0) {
			transapi_path = (char*)xmlNodeGetContent(aux_node);
		}
		if (model_path && transapi_path) {
			break;
		}
	}
	/* Netopeer module is something extra */
	if (st != NULL && model_path) {
		/* internal static server (Netopeer) module */
		if (repo_type == -1 && transapi_path) {
			/* augment transapi module */
			nc_verb_verbose("Adding augment transapi \"%s\"", model_path);
			ncds_add_augment_transapi(model_path, transapi_path);
		} else if (repo_type == -1) {
			/* augment model */
			nc_verb_verbose("Adding augment model \"%s\"", model_path);
			ncds_add_model(model_path);
		} else {
			nc_verb_verbose("Adding static transapi \"%s\"", model_path);
			if ((module->ds = ncds_new_transapi_static(repo_type, model_path, st)) == NULL) {
				free(model_path);
				free(transapi_path);
				return (EXIT_FAILURE);
			}
		}
	} else if (model_path && transapi_path) {
		if (repo_type == -1) {
			/* augment transapi module */
			nc_verb_verbose("Adding augment transapi \"%s\"", model_path);
			ncds_add_augment_transapi(model_path, transapi_path);
		} else {
			/* base transapi module for datastore */
			nc_verb_verbose("Adding transapi \"%s\"", model_path);
			if ((module->ds = ncds_new_transapi(repo_type, model_path, transapi_path)) == NULL) {
				free(model_path);
				free(transapi_path);
				return (EXIT_FAILURE);
			}
		}
	} else if (model_path) {
		if (repo_type == -1) {
			/* augment model */
			nc_verb_verbose("Adding augment model \"%s\"", model_path);
			ncds_add_model(model_path);
		} else {
			/* base model for datastore */
			nc_verb_verbose("Adding base model \"%s\"", model_path);
			if ((module->ds = ncds_new2(repo_type, model_path, NULL)) == NULL) {
				free(model_path);
				return (EXIT_FAILURE);
			}
		}
	} else {
		nc_verb_error("Configuration mismatch: missing model path in %s config.", module->name);
	}
	name = strdup(basename(model_path));
	/* cut off the .yin suffix */
	aux = strrchr(name, '.');
	if (aux) { *aux = '\0';}

	free(model_path);
	free(transapi_path);

	/* set features */
	for (aux_node = node->children; aux_node != NULL; aux_node = aux_node->next) {
		if (xmlStrcmp(aux_node->name, BAD_CAST "feature") == 0) {
			feature = (char*)xmlNodeGetContent(aux_node);
			if (strcmp(feature, "*") == 0) {
				ncds_features_enableall(name);
			} else {
				ncds_feature_enable(name, feature);
			}
			free(feature);
		}
	}
	free(name);

	return (EXIT_SUCCESS);
}
コード例 #5
0
ファイル: server.c プロジェクト: JimBrv/of-config
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);
}