aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/config/commandParser.c
blob: 84d273c9df074ac760f449201134a2ef9d52341e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <src/config.h>

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

#include <parc/assert/parc_Assert.h>
#include <string.h>

#include <parc/security/parc_Security.h>

#include <parc/algol/parc_List.h>
#include <parc/algol/parc_Memory.h>
#include <parc/algol/parc_Time.h>
#include <parc/algol/parc_TreeRedBlack.h>

#include <src/config/commandParser.h>

#ifndef __ANDROID__
#ifdef HAVE_ERRNO_H
#include <errno.h>
#else
extern int errno;
#endif
#endif

struct command_parser {
  // key = command, value = CommandOps
  PARCTreeRedBlack *commandTree;
  bool debugFlag;
};

static int _stringCompare(const void *key1, const void *key2) {
  return strcasecmp((const char *)key1, (const char *)key2);
}

CommandParser *commandParser_Create(void) {
  CommandParser *state = parcMemory_AllocateAndClear(sizeof(CommandParser));
  parcAssertNotNull(state, "parcMemory_AllocateAndClear(%zu) returned NULL",
                    sizeof(CommandParser));

  state->commandTree = parcTreeRedBlack_Create(_stringCompare,  // key compare
                                               NULL,            // key free
                                               NULL,            // key copy
                                               NULL,            // value equals
                                               NULL,            // value free
                                               NULL             // value copy
  );
  state->debugFlag = false;
  return state;
}

void commandParser_Destroy(CommandParser **parserPtr) {
  CommandParser *parser = *parserPtr;

  // destroy every element if it has a destroyer
  PARCArrayList *values = parcTreeRedBlack_Values(parser->commandTree);
  if (values) {
    for (int i = 0; i < parcArrayList_Size(values); i++) {
      CommandOps *ops = parcArrayList_Get(values, i);
      parcTreeRedBlack_Remove(parser->commandTree, ops->command);
      if (ops->destroyer) {
        ops->destroyer(&ops);
      }
    }
    parcArrayList_Destroy(&values);
  }

  parcTreeRedBlack_Destroy(&parser->commandTree);

  parcMemory_Deallocate((void **)&parser);
  *parserPtr = NULL;
}

void commandParser_SetDebug(CommandParser *state, bool debugFlag) {
  state->debugFlag = debugFlag;
}

bool commandParser_GetDebug(CommandParser *state) { return state->debugFlag; }

void commandParser_RegisterCommand(CommandParser *state, CommandOps *ops) {
  parcAssertNotNull(state, "Parameter state must be non-null");
  parcAssertNotNull(ops, "Parameter ops must be non-null");
  parcAssertNotNull(ops->command, "Operation command string must be non-null");

  void *exists = parcTreeRedBlack_Get(state->commandTree, ops->command);
  parcAssertNull(exists, "Command '%s' already exists in the tree %p\n",
                 ops->command, (void *)exists);

  parcTreeRedBlack_Insert(state->commandTree, (void *)ops->command,
                          (void *)ops);

  // if the command being registered asked for an init function to be called,
  // call it
  if (ops->init != NULL) {
    ops->init(state, ops);
  }
}

static PARCList *parseStringIntoTokens(const char *originalString) {
  PARCList *list =
      parcList(parcArrayList_Create(parcArrayList_StdlibFreeFunction),
               PARCArrayListAsPARCList);

  char *token;

  char *tofree =
      parcMemory_StringDuplicate(originalString, strlen(originalString) + 1);
  char *string = tofree;

  while ((token = strsep(&string, " \t\n")) != NULL) {
    if (strlen(token) > 0) {
      parcList_Add(list, strdup(token));
    }
  }

  parcMemory_Deallocate((void **)&tofree);

  return list;
}

/**
 * Matches the user arguments to available commands, returning the command or
 * NULL if not found
 *
 * <#Paragraphs Of Explanation#>
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return <#value#> <#explanation#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static CommandOps *commandParser_MatchCommand(CommandParser *state,
                                              PARCList *args) {
  // Find the longest matching prefix command.
  // Pretty wildly inefficient

  size_t longest_token_count = 0;
  char *longest_command = NULL;

  PARCArrayList *commands = parcTreeRedBlack_Keys(state->commandTree);
  for (int i = 0; i < parcArrayList_Size(commands); i++) {
    char *command = parcArrayList_Get(commands, i);
    PARCList *command_tokens = parseStringIntoTokens(command);

    // is it a prefix match?
    if (parcList_Size(args) >= parcList_Size(command_tokens)) {
      bool possible_match = true;
      for (int i = 0; i < parcList_Size(command_tokens) && possible_match;
           i++) {
        const char *a = parcList_GetAtIndex(command_tokens, i);
        const char *b = parcList_GetAtIndex(args, i);
        if (strncasecmp(a, b, strlen(a) + 1) != 0) {
          possible_match = false;
        }
      }

      if (possible_match &&
          parcList_Size(command_tokens) > longest_token_count) {
        longest_token_count = parcList_Size(command_tokens);
        longest_command = command;
      }
    }

    parcList_Release(&command_tokens);
  }

  parcArrayList_Destroy(&commands);

  if (longest_token_count == 0) {
    return NULL;
  } else {
    CommandOps *ops = parcTreeRedBlack_Get(state->commandTree, longest_command);
    parcAssertNotNull(ops, "Got null operations for command '%s'\n",
                      longest_command);
    return ops;
  }
}

CommandReturn commandParser_DispatchCommand(CommandParser *state,
                                            PARCList *args) {
  CommandOps *ops = commandParser_MatchCommand(state, args);

  if (ops == NULL) {
    printf("Command not found.\n");
    return CommandReturn_Failure;
  } else {
    return ops->execute(state, ops, args);
  }
}

bool commandParser_ContainsCommand(CommandParser *parser, const char *command) {
  CommandOps *ops = parcTreeRedBlack_Get(parser->commandTree, command);
  return (ops != NULL);
}
">" ] then # Candidate is in good status. Add to array. VIRL_PROD_SERVERS+=(${VIRL_SERVERS[$index]}) fi done VIRL_SERVERS=("${VIRL_PROD_SERVERS[@]}") echo "VIRL servers in production: ${VIRL_SERVERS[@]}" num_hosts=${#VIRL_SERVERS[@]} if [ $num_hosts == 0 ] then echo "No more VIRL candidate hosts available, failing." exit 127 fi # Get the LOAD of each server based on number of active simulations (testcases) VIRL_SERVER_LOAD=() for index in "${!VIRL_SERVERS[@]}"; do VIRL_SERVER_LOAD[${index}]=$(ssh ${SSH_OPTIONS} ${VIRL_USERNAME}@${VIRL_SERVERS[$index]} "list-testcases | grep session | wc -l") done # Pick for each TEST_GROUP least loaded server VIRL_SERVER=() for index in "${!TEST_GROUPS[@]}"; do least_load_server_idx=$(echo "${VIRL_SERVER_LOAD[*]}" | tr -s ' ' '\n' | awk '{print($0" "NR)}' | sort -g -k1,1 | head -1 | cut -f2 -d' ') least_load_server=${VIRL_SERVERS[$least_load_server_idx-1]} VIRL_SERVER+=($least_load_server) # Adjusting load as we are not going run simulation immediately VIRL_SERVER_LOAD[$least_load_server_idx-1]=$((VIRL_SERVER_LOAD[$least_load_server_idx-1]+1)) done echo "Selected VIRL servers: ${VIRL_SERVER[@]}" # Temporarily download DPDK packages DMM_TAR_FILE="dmm_depends.tar.gz" cd dmm/scripts/ ./build.sh all cd - DPDK_DOWNLOAD_PATH=$(cat dmm/scripts/build_dpdk.sh | grep DPDK_DOWNLOAD_PATH= | cut -d "=" -f2) mv $DPDK_DOWNLOAD_PATH/dpdk-18.02.tar.xz . wget http://security.ubuntu.com/ubuntu/pool/main/n/numactl/libnuma1_2.0.11-1ubuntu1.1_amd64.deb wget http://security.ubuntu.com/ubuntu/pool/main/n/numactl/libnuma-dev_2.0.11-1ubuntu1.1_amd64.deb wget http://security.ubuntu.com/ubuntu/pool/main/e/ethtool/ethtool_4.5-1_amd64.deb wget http://security.ubuntu.com/ubuntu/pool/main/l/lsof/lsof_4.89+dfsg-0.1_amd64.deb tar zcf ${DMM_TAR_FILE} dpdk-18.02.tar.xz ./dmm/ libnuma*.deb VIRL_DIR_LOC="/tmp" cat ${VIRL_PKEY} # Copy the files to VIRL hosts DONE="" for index in "${!VIRL_SERVER[@]}"; do # Do not copy files in case they have already been copied to the VIRL host [[ "${DONE[@]}" =~ "${VIRL_SERVER[${index}]}" ]] && copy=0 || copy=1 if [ "${copy}" -eq "0" ]; then echo "DMM_TAR_FILE has already been copied to the VIRL host ${VIRL_SERVER[${index}]}" else scp ${SSH_OPTIONS} ${DMM_TAR_FILE} \ ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}:${VIRL_DIR_LOC}/ result=$? if [ "${result}" -ne "0" ]; then echo "Failed to copy DMM_TAR_FILE to VIRL host ${VIRL_SERVER[${index}]}" echo ${result} exit ${result} else echo "DMM_TAR_FILE successfully copied to the VIRL host ${VIRL_SERVER[${index}]}" fi DONE+=(${VIRL_SERVER[${index}]}) fi done # Start a simulation on VIRL server function stop_virl_simulation { for index in "${!VIRL_SERVER[@]}"; do ssh ${SSH_OPTIONS} ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}\ "stop-testcase ${VIRL_SID[${index}]}" done } # Upon script exit, cleanup the simulation execution trap stop_virl_simulation EXIT for index in "${!VIRL_SERVER[@]}"; do echo "Starting simulation nr. ${index} on VIRL server ${VIRL_SERVER[${index}]}" # Get given VIRL server limits for max. number of VMs and IPs max_ips=$(get_max_ip_nr ${VIRL_SERVER[${index}]}) max_ips_from_sims=$(($(get_max_sim_nr ${VIRL_SERVER[${index}]})*IPS_PER_SIMULATION)) # Set quota to lower value IP_QUOTA=$([ $max_ips -le $max_ips_from_sims ] && echo "$max_ips" || echo "$max_ips_from_sims") # Start the simulation VIRL_SID[${index}]=$(ssh ${SSH_OPTIONS} \ ${VIRL_USERNAME}@${VIRL_SERVER[${index}]} \ "start-testcase-DMM -vv --quota ${IP_QUOTA} --copy ${VIRL_TOPOLOGY} \ --release ${VIRL_RELEASE} ${VIRL_DIR_LOC}/${DMM_TAR_FILE}") # TODO: remove param ${DMM_TAR_FILE} when start-testcase script is # updated on all virl servers retval=$? if [ ${retval} -ne "0" ]; then echo "VIRL simulation start failed on ${VIRL_SERVER[${index}]}" exit ${retval} fi if [[ ! "${VIRL_SID[${index}]}" =~ session-[a-zA-Z0-9_]{6} ]]; then echo "No VIRL session ID reported." exit 127 fi echo "VIRL simulation nr. ${index} started on ${VIRL_SERVER[${index}]}" ssh_do ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}\ cat /scratch/${VIRL_SID[${index}]}/topology.yaml # Download the topology file from VIRL session and rename it scp ${SSH_OPTIONS} \ ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}:/scratch/${VIRL_SID[${index}]}/topology.yaml \ topologies/enabled/topology${index}.yaml retval=$? if [ ${retval} -ne "0" ]; then echo "Failed to copy topology file from VIRL simulation nr. ${index} on VIRL server ${VIRL_SERVER[${index}]}" exit ${retval} fi done echo ${VIRL_SID[@]} virtualenv --system-site-packages env . env/bin/activate echo pip install pip install -r ${SCRIPT_DIR}/requirements.txt for index in "${!VIRL_SERVER[@]}"; do pykwalify -s ${SCRIPT_DIR}/resources/topology_schemas/3_node_topology.sch.yaml \ -s ${SCRIPT_DIR}/resources/topology_schemas/topology.sch.yaml \ -d ${SCRIPT_DIR}/topologies/enabled/topology${index}.yaml \ -vvv if [ "$?" -ne "0" ]; then echo "Topology${index} schema validation failed." echo "However, the tests will start." fi done function run_test_set() { set +x OLDIFS=$IFS IFS="," nr=$(echo $1) rm -f ${LOG_PATH}/test_run${nr}.log exec &> >(while read line; do echo "$(date +'%H:%M:%S') $line" \ >> ${LOG_PATH}/test_run${nr}.log; done;) suite_str="" for suite in ${TEST_GROUPS[${nr}]}; do suite_str="${suite_str} --suite ${SUITE_PATH}.${suite}" done IFS=$OLDIFS echo "PYTHONPATH=`pwd` pybot -L TRACE -W 136\ -v TOPOLOGY_PATH:${SCRIPT_DIR}/topologies/enabled/topology${nr}.yaml \ ${suite_str} \ --include vm_envAND3_node_single_link_topo \ --include vm_envAND3_node_double_link_topo \ --exclude PERFTEST \ --exclude ${SKIP_PATCH} \ --noncritical EXPECTED_FAILING \ --output ${LOG_PATH}/log_test_set_run${nr} \ tests/" PYTHONPATH=`pwd` pybot -L TRACE -W 136\ -v TOPOLOGY_PATH:${SCRIPT_DIR}/topologies/enabled/topology${nr}.yaml \ ${suite_str} \ --include vm_envAND3_node_single_link_topo \ --include vm_envAND3_node_double_link_topo \ --exclude PERFTEST \ --exclude ${SKIP_PATCH} \ --noncritical EXPECTED_FAILING \ --output ${LOG_PATH}/log_test_set_run${nr} \ tests/ local local_run_rc=$? set -x echo ${local_run_rc} > ${LOG_PATH}/rc_test_run${nr} } set +x # Send to background an instance of the run_test_set() function for each number, # record the pid. for index in "${!VIRL_SERVER[@]}"; do run_test_set ${index} & pid=$! echo "Sent to background: Test_set${index} (pid=$pid)" pids[$pid]=$index done echo echo -n "Waiting..." # Watch the stable of background processes. # If a pid goes away, remove it from the array. while [ -n "${pids[*]}" ]; do for i in $(seq 0 9); do sleep 1 echo -n "." done for pid in "${!pids[@]}"; do if ! ps "$pid" >/dev/null; then echo -e "\n" echo "Test_set${pids[$pid]} with PID $pid finished." unset pids[$pid] fi done if [ -z "${!pids[*]}" ]; then break fi echo -n -e "\nStill waiting for test set(s): ${pids[*]} ..." done echo echo "All test set runs finished." echo set -x RC=0 for index in "${!VIRL_SERVER[@]}"; do echo "Test_set${index} log:" cat ${LOG_PATH}/test_run${index}.log RC_PARTIAL_RUN=$(cat ${LOG_PATH}/rc_test_run${index}) if [ -z "$RC_PARTIAL_RUN" ]; then echo "Failed to retrieve return code from test run ${index}" exit 1 fi RC=$((RC+RC_PARTIAL_RUN)) rm -f ${LOG_PATH}/rc_test_run${index} rm -f ${LOG_PATH}/test_run${index}.log echo done # Log the final result if [ "${RC}" -eq "0" ]; then set +x echo echo "========================================================================================================================================" echo "Final result of all test loops: | PASS |" echo "All critical tests have passed." echo "========================================================================================================================================" echo set -x else if [ "${RC}" -eq "1" ]; then HLP_STR="test has" else HLP_STR="tests have" fi set +x echo echo "========================================================================================================================================" echo "Final result of all test loops: | FAIL |" echo "${RC} critical ${HLP_STR} failed." echo "========================================================================================================================================" echo set -x fi echo Post-processing test data... partial_logs="" for index in "${!VIRL_SERVER[@]}"; do partial_logs="${partial_logs} ${LOG_PATH}/log_test_set_run${index}.xml" done # Rebot output post-processing rebot --noncritical EXPECTED_FAILING \ --output output.xml ${partial_logs} # Remove unnecessary log files rm -f ${partial_logs} # Archive JOB artifacts in jenkins for i in ${JOB_ARCHIVE_ARTIFACTS[@]}; do cp $( readlink -f ${i} | tr '\n' ' ' ) ${JOB_ARCHIVE_DIR}/ done # Archive JOB artifacts to logs.fd.io for i in ${LOG_ARCHIVE_ARTIFACTS[@]}; do cp $( readlink -f ${i} | tr '\n' ' ' ) ${LOG_ARCHIVE_DIR}/ done echo Post-processing finished. if [ ${RC} -eq 0 ]; then RETURN_STATUS=0 else RETURN_STATUS=1 fi exit ${RETURN_STATUS}