/* * 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); }
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); }