示例#1
0
static PyObject *ncOpEditConfig(ncSessionObject *self, PyObject *args, PyObject *keywords)
{
	int source = NC_DATASTORE_ERROR, target = NC_DATASTORE_ERROR;
	int defop = 0, erroropt = NC_EDIT_ERROPT_NOTSET, testopt = NC_EDIT_TESTOPT_TESTSET;
	char *data1 = NULL;
	int i;
	PyObject *PySource;
	nc_rpc *rpc = NULL;
	char *kwlist[] = {"target", "source", "defop", "erropt", "testopt", NULL};

	SESSION_CHECK(self);

	/* Get input parameters */
	if (! PyArg_ParseTupleAndKeywords(args, keywords, "iO|iii", kwlist, &target, &PySource,
			&defop, &erroropt, &testopt)) {
		return (NULL);
	}

	/* get source type */
	if (strcmp(Py_TYPE(PySource)->tp_name, "int") == 0) {
		if (!PyArg_Parse(PySource, "i", &source)) {
			return (NULL);
		}
	} else if (strcmp(Py_TYPE(PySource)->tp_name, "str") == 0) {
		if (!PyArg_Parse(PySource, "s", &data1)) {
			return (NULL);
		}
		for (i = 0; data1[i] != '\0' && isspace(data1[i]); i++);
		if (data1[i] == '<') {
			source = NC_DATASTORE_CONFIG;
		} else {
			if (strcasestr(data1, "://") != NULL) {
				source = NC_DATASTORE_URL;
			} else {
				PyErr_SetString(PyExc_ValueError, "Invalid \'source\' value.");
				return (NULL);
			}
		}
	}

	/* create RPC */
	rpc = nc_rpc_editconfig(target, source, defop, erroropt, testopt, data1);

	if (rpc == NULL) {
		Py_RETURN_FALSE;
	}

	/* send request ... */
	if (op_send_recv(self, rpc, NULL) == EXIT_SUCCESS) {
		/* ... and return the result */
		Py_RETURN_TRUE;
	} else {
		Py_RETURN_FALSE;
	}
}
示例#2
0
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;
}