Skip to content
Snippets Groups Projects
pathComp_tools.c 89.9 KiB
Newer Older
 */
 /////////////////////////////////////////////////////////////////////////////////////////
struct vertices_t* find_vertex_in_graph_context(struct graph_t *g, gchar* deviceId) {

	for (gint i = 0; i < g->numVertices; i++)
	{
		struct vertices_t* v = &(g->vertices[i]);
		if (strcmp(v->verticeId.nodeId, deviceId) == 0) {
			return v;
		}
	}
	return NULL;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Adding a deviceId into a graph
 *
 * @param g
 * @param deviceId
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
 /////////////////////////////////////////////////////////////////////////////////////////
struct vertices_t* add_vertex_in_graph(struct graph_t* g, gchar* deviceId) {
	g->numVertices++;
	struct vertices_t* v = &(g->vertices[g->numVertices - 1]);
	duplicate_string(v->verticeId.nodeId, deviceId);
	return v;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Construct the graphs (vertices and edges) bound to every individual context
 *
 * @param cSet
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
/////////////////////////////////////////////////////////////////////////////////////////
void build_contextSet_deviceList(struct contextSet_t* cSet) {
	// Check every device their endpoints
	for (gint i = 0; i < deviceList->numDevices; i++) {
		struct device_t* d = &(deviceList->devices[i]);
		//DEBUG_PC("Exploring DeviceId: %s", d->deviceId);

		// Check the associated endPoints
		for (gint j = 0; j < d->numEndPoints; j++) {
			struct endPoint_t* eP = &(d->endPoints[j]);
			// Get endPointId (topology, context, device Id and endpoint uuid)
			struct endPointId_t* ePid = &(eP->endPointId);  //end point id
			//DEBUG_PC("   EndPointId: %s || Type: %s", eP->endPointId.endpoint_uuid, d->deviceType);
			//DEBUG_PC("   TopologyId: %s || ContextId: %s", eP->endPointId.topology_id.topology_uuid, eP->endPointId.topology_id.contextId);

			// Add contextId in ContextSet and the deviceId (+endpoint) into the vertex set
			struct context_t *c = find_contextId_in_set(eP->endPointId.topology_id.contextId, cSet);
			if (c == NULL) {
				//DEBUG_PC("   contextUuid: %s MUST BE ADDED to ContextSet", eP->endPointId.topology_id.contextId);
				c = add_contextId_in_set(eP->endPointId.topology_id.contextId, cSet);
			}
			// Check if the deviceId and endPointUuid are already considered in the graph of the context c
			struct vertices_t* v = find_vertex_in_graph_context(&c->g, d->deviceId);
			if (v == NULL) {
				//DEBUG_PC("  deviceId: %s MUST BE ADDED to the Context Graph", d->deviceId);
				v = add_vertex_in_graph(&c->g, d->deviceId);
			}
		}
	}
	//print_contextSet(cSet);
	return;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Determine whether a deviceId is in the targetNode list of a specific vertex v
 *
 * @param v
 * @param deviceId
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
 /////////////////////////////////////////////////////////////////////////////////////////
struct targetNodes_t* find_targeted_vertex_in_graph_context(struct vertices_t* v, gchar *deviceId) {
	for (gint k = 0; k < v->numTargetedVertices; k++) {
		struct targetNodes_t* w = &(v->targetedVertices[k]);
		if (strcmp(w->tVertice.nodeId, deviceId) == 0) {
			return w;
		}
	}
	return NULL;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Add a deviceId a targetNode of a specific vertex v
 *
 * @param v
 * @param deviceId
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
 /////////////////////////////////////////////////////////////////////////////////////////
struct targetNodes_t* add_targeted_vertex_in_graph_context(struct vertices_t* v, gchar* bDeviceId) {
	v->numTargetedVertices++;
	struct targetNodes_t* w = &(v->targetedVertices[v->numTargetedVertices - 1]);
	duplicate_string(w->tVertice.nodeId, bDeviceId);
	return w;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Returns the structure of a device endpoint bound to a specific deviceId and endPointId
 *
 * @param devId
 * @param endPointUuid
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
 /////////////////////////////////////////////////////////////////////////////////////////
struct endPoint_t* find_device_tied_endpoint(gchar* devId, gchar* endPointUuid) {
	//DEBUG_PC("devId: %s ePId: %s", devId, endPointUuid);
	for (gint i = 0; i < deviceList->numDevices; i++) {
		struct device_t* d = &(deviceList->devices[i]);
		if (strcmp(d->deviceId, devId) != 0) {
			continue;
		}
		// Iterate over the endpoints tied to the deviceId
		for (gint j = 0; j < d->numEndPoints; j++) {
			struct endPoint_t* eP = &(d->endPoints[j]);
			//DEBUG_PC("looked endPointId: %s", eP->endPointId.endpoint_uuid);
			if (strcmp(eP->endPointId.endpoint_uuid, endPointUuid) == 0) {
				return eP;
			}
		}
	}
	return NULL;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Adding the edge/linnk in the targetedNodes w list
 *
 * @param w
 * @param l
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
 /////////////////////////////////////////////////////////////////////////////////////////
void add_edge_in_targetedVertice_set(struct targetNodes_t* w, struct link_t* l) {
	//DEBUG_PC("\t targetedVertex: %s", w->tVertice.nodeId);
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703
	w->numEdges++;
	struct edges_t* e = &(w->edges[w->numEdges - 1]);
	// Copy the link Id UUID
	duplicate_string(e->linkId, l->linkId);

	// copy the deviceId and endpointsIds (A --> Z)
	struct link_endpointId_t* aEndpointId = &(l->linkEndPointId[0]);
	duplicate_string(e->aNodeId.nodeId, aEndpointId->deviceId);
	duplicate_string(e->aEndPointId, aEndpointId->endPointId);
	duplicate_string(e->aTopologyId, aEndpointId->topology_id.topology_uuid);

	struct link_endpointId_t* zEndpointId = &(l->linkEndPointId[1]);
	duplicate_string(e->zNodeId.nodeId, zEndpointId->deviceId);
	duplicate_string(e->zEndPointId, zEndpointId->endPointId);
	duplicate_string(e->zTopologyId, zEndpointId->topology_id.topology_uuid);

	// The potential and available capacity is indeed retrieved using aEndpointId in the deviceList
	struct endPoint_t* eP = find_device_tied_endpoint(aEndpointId->deviceId, aEndpointId->endPointId);
	if (eP == NULL) {
		DEBUG_PC("devId: %s endPointUuid: %s NOT in Device List!!--- Weird", aEndpointId->deviceId, aEndpointId->endPointId);
		exit(-1);
	}
	//Potential(total) and available capacity
	e->unit = eP->potential_capacity.unit;
	memcpy(&e->totalCap, &eP->potential_capacity.value, sizeof(gdouble));
	memcpy(&e->availCap, &eP->available_capacity.value, sizeof(gdouble));

	// Copy interdomain local/remote Ids
	memcpy(e->interDomain_localId, eP->inter_domain_plug_in.inter_domain_plug_in_local_id, 
		strlen(eP->inter_domain_plug_in.inter_domain_plug_in_local_id));
	memcpy(e->interDomain_remoteId, eP->inter_domain_plug_in.inter_domain_plug_in_remote_id,
		strlen(eP->inter_domain_plug_in.inter_domain_plug_in_remote_id));

	// cost value
	memcpy(&e->cost, &l->cost_characteristics.cost_value, sizeof(gdouble));

	// latency
	memcpy(&e->delay, &l->latency_characteristics.fixed_latency, sizeof(gdouble));
	return;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Searching a specific edge/link by the linkId(UUID)
 *
 * @param w
 * @param l
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
 /////////////////////////////////////////////////////////////////////////////////////////
struct edges_t* find_edge_in_targetedVertice_set(struct targetNodes_t* w, struct link_t* l) {
		
	for (gint i = 0; i < w->numEdges; i++) {
		struct edges_t* e = &(w->edges[i]);
		if (strcmp(e->linkId, l->linkId) == 0) {
			return e;
		}
	}
	return NULL;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief supporting the construction of the graph per context using the explicit
 * contents/info of the link list
 *
 * @param set
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
 /////////////////////////////////////////////////////////////////////////////////////////
void build_contextSet_linklList(struct contextSet_t* set) {
	g_assert(set);
	
	// for each link in linkList:
	// 1st- Retrieve endpoints A --> B feauture (context Id, device Id, endpoint Id)
	// 2st - In the graph associated to the contextId, check wheter A (deviceId) is in the vertices list
	// o No, this is weird ... exist
	// o Yes, get the other link endpoint (i.e., B) and check whether it exists. If NOT add it, considering
	// all the attributes; Otherwise, check whether the link is different from existing edges between A and B

	for (gint j = 0; j < linkList->numLinks; j++) {
		struct link_t* l = &(linkList->links[j]);
		// link assumed to be P2P A --> B; I.e. 2 endPoints; 1st specifies A and 2nd specifie B
		struct link_endpointId_t* aEndpointId = &(l->linkEndPointId[0]);
		struct topology_id_t* topologyId = &(aEndpointId->topology_id);
		// get the contextId
		gchar contextUuid[UUID_CHAR_LENGTH];
		duplicate_string(contextUuid, topologyId->contextId);
		//DEBUG_PC("Link: %s in Context: %s", l->linkId, contextUuid);

		// Check first contextUuid exists in the cSet
		struct context_t* c = find_contextId_in_set(contextUuid, set);
		if (c == NULL) {
			DEBUG_PC("ContextId: %s does NOT exist... weird", contextUuid);
			exit(-1);
		}

		// get the device ID of A
		gchar aDeviceId[UUID_CHAR_LENGTH];
		duplicate_string(aDeviceId, aEndpointId->deviceId);

		struct graph_t* g = &(c->g); // get the graph associated to the context c
		struct vertices_t* v = find_vertex_in_graph_context(g, aDeviceId);
		if (v == NULL) {
			DEBUG_PC("aDeviceId: %s IS NOT IN Vertices of contextId: %s", aDeviceId, contextUuid);
			exit(-1);
		}		
		// get the bEndpointId
		struct link_endpointId_t* bEndpointId = &(l->linkEndPointId[1]);
		gchar bDeviceId[UUID_CHAR_LENGTH];
		duplicate_string(bDeviceId, bEndpointId->deviceId);
		// Check whether device B is in the targeted Vertices from A (i.e., v)?
		// If not, add B in the targeted vertices B + create the edge and add it
		// If B exist, check whether the explored link/edge is already in the list of edges
		struct targetNodes_t* w = find_targeted_vertex_in_graph_context(v, bDeviceId);
		if (w == NULL) {
			//DEBUG_PC("B device [%s] is PEER of A device [%s]", bDeviceId, v->verticeId.nodeId);
			w = add_targeted_vertex_in_graph_context(v, bDeviceId);
			add_edge_in_targetedVertice_set(w, l);
		}
		else {
			// w exists, it is needed to check whether the edge (link) should be added
			struct edges_t* e = find_edge_in_targetedVertice_set(w, l);
			if (e == NULL) {
				// Add the link into the list
				add_edge_in_targetedVertice_set(w, l);
			}
			else {
				DEBUG_PC("The link already exists ...");
				continue;
			}
		}
	}
	return;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Create the set of (distinct) contexts with the deviceList and linkList
 *
 * @param cSet
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
 /////////////////////////////////////////////////////////////////////////////////////////
void build_contextSet(struct contextSet_t* cSet) {
	g_assert(cSet);
	g_assert(deviceList);
	g_assert(linkList);

	// devices are tied to contexts, i.e. depending on the contextId of the devices
	build_contextSet_deviceList(cSet);

	// Once the diverse contexts are created and the devices/endpoints asigned to the 
	// respective graph tied to each context, it is needed to create the edges
	build_contextSet_linklList(cSet);

	return;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Print the contents of the ContextIds
 *
 * @param set
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
 /////////////////////////////////////////////////////////////////////////////////////////
void print_contextSet(struct contextSet_t* set) {
	g_assert(set);

	for (gint i = 0; i < set->num_context_set; i++) {
		struct context_t* c = &(set->contextList[i]);
		DEBUG_PC("-------------------------------------------------------------");
		DEBUG_PC(" Context Id: %s", c->contextId);
		DEBUG_PC("-------------------------------------------------------------");

		struct graph_t* g = &(c->g);
		for (gint j = 0; j < g->numVertices; j++) {
			struct vertices_t* v = &(g->vertices[j]);
			DEBUG_PC("  Head Device Id: %s", v->verticeId.nodeId);
			for (gint k = 0; k < v->numTargetedVertices; k++) {
				struct targetNodes_t* w = &(v->targetedVertices[k]);
				DEBUG_PC("  [%d] --- Peer Device Id: %s", k, w->tVertice.nodeId);
				for (gint l = 0; l < w->numEdges; l++) {
					struct edges_t* e = &(w->edges[l]);
					DEBUG_PC("   \t link Id: %s", e->linkId);
					DEBUG_PC("   \t aEndPointId: %s", e->aEndPointId);
					DEBUG_PC("   \t zEndPointId: %s", e->zEndPointId);
					DEBUG_PC("   \t Available Capacity: %f, Latency: %f, Cost: %f", e->availCap, e->delay, e->cost);
					DEBUG_PC("   \t aTopologyId: %s", e->aTopologyId);
					DEBUG_PC("   \t zTopologyId: %s", e->zTopologyId);
				}
			}
		}
	}
	return;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Check whether src and dst PE nodeId of the req are the same
 *
 *	@param r
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
/////////////////////////////////////////////////////////////////////////////////////////
gint same_src_dst_pe_nodeid(struct service_t* s)
{
	// Check that source PE and dst PE are NOT the same, i.e., different ingress and egress endpoints (iEp, eEp)
	struct service_endpoints_id_t* iEp = &(s->service_endpoints_id[0]);
	struct service_endpoints_id_t* eEp = &(s->service_endpoints_id[1]);

	gchar* iEpUUID = iEp->endpoint_uuid;
	gchar* eEpUUID = eEp->endpoint_uuid;
	gchar* iDevUUID = iEp->device_uuid;
	gchar* eDevUUID = eEp->device_uuid;

	// Compare the device uuids
	if (strcmp(iDevUUID, eDevUUID) != 0) {
		DEBUG_PC("DIFFERENT --- iDevId: %s and eDevId: %s", iDevUUID, eDevUUID);
		return 1;
	}
	// Compare the endpoints (ports)
	if (strcmp(iEpUUID, eEpUUID) != 0) {
		DEBUG_PC("DIFFERENT --- iEpUUID: %s and eEpUUID: %s", iEpUUID, eEpUUID);
		return 1;
	}
	return 0;	
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Handles issues with the route computation
 *
 *	@param route
 *	@param s
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
/////////////////////////////////////////////////////////////////////////////////////////
void comp_route_connection_issue_handler (struct compRouteOutput_t *path, struct service_t *s)
{
	g_assert(path);
	g_assert(s);

	// Increase the number of computed routes/paths despite there was an issue to be reported		
	path->numPaths++;	
	// Copy the serviceId
	copy_service_id(&(path->serviceId), &(s->serviceId));

	// copy the service endpoints, in general, there will be 2 (point-to-point network connectivity services)
	for (gint i = 0; i < s->num_service_endpoints_id; i++) {
		struct service_endpoints_id_t* iEp = &(s->service_endpoints_id[i]);
		struct service_endpoints_id_t* oEp = &(path->service_endpoints_id[i]);
		copy_service_endpoint_id(oEp, iEp);
	}
	path->num_service_endpoints_id = s->num_service_endpoints_id;
	path->noPathIssue = NO_PATH_CONS_ISSUE;
	return;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief released the allocated memory fo compRouteOutputList_t
 *
 *	@param ro
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
/////////////////////////////////////////////////////////////////////////////////////////
void destroy_compRouteOutputList (struct compRouteOutputList_t *ro)
{
	g_assert (ro);	
	g_free (ro);	
	return;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief creates a copy of the underlying graph
 *
 *	@param originalGraph
 *	@param destGraph
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
/////////////////////////////////////////////////////////////////////////////////////////
void duplicate_graph (struct graph_t *originalGraph, struct graph_t *destGraph)	{
	g_assert (originalGraph);
	g_assert (destGraph);
	
	destGraph->numVertices = originalGraph->numVertices;
	for (gint i = 0; i < originalGraph->numVertices; i++) {
		struct vertices_t *oVertex = &(originalGraph->vertices[i]);
		struct vertices_t *dVertex = &(destGraph->vertices[i]);
		dVertex->numTargetedVertices = oVertex->numTargetedVertices;		
		duplicate_node_id (&oVertex->verticeId, &dVertex->verticeId);
		
		for (gint j = 0; j < oVertex->numTargetedVertices; j++)	{
			struct targetNodes_t *oTargetedVertex = &(oVertex->targetedVertices[j]);
			struct targetNodes_t *dTargetedVertex = &(dVertex->targetedVertices[j]);
			duplicate_node_id (&oTargetedVertex->tVertice, &dTargetedVertex->tVertice);
			dTargetedVertex->numEdges = oTargetedVertex->numEdges;
			
			for (gint k = 0; k < oTargetedVertex->numEdges; k++) {
				struct edges_t *oEdge = &(oTargetedVertex->edges[k]);
				struct edges_t *dEdge = &(dTargetedVertex->edges[k]);
				duplicate_edge (dEdge, oEdge);						
			}
		}	
	}	
	return;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Function used to retrieve from the graph the edge instance associated to the
 * pathLink (pL)
 *
 *	@param pL
 *	@parma g
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
 /////////////////////////////////////////////////////////////////////////////////////////
struct edges_t* get_edge_from_graph_by_linkId(struct pathLink_t* pL, struct graph_t* g) {
	g_assert(pL);
	g_assert(g);

	for (gint i = 0; i < g->numVertices; i++) {
		struct vertices_t* v = &(g->vertices[i]);
		for (gint j = 0; j < v->numTargetedVertices; j++) {
			struct targetNodes_t* tv = &(v->targetedVertices[j]);
			for (gint k = 0; k < tv->numEdges; k++) {
				struct edges_t* e = &(tv->edges[k]);
				if (strcmp(e->linkId, pL->linkId) == 0) {
					return e;
				}
			}		
		}
	}
	return NULL;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Function used to retrieve from the graph the reverse edge (rev_e) associated to an edge (e)
 *
 *	@param e
 *	@parma g
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
 /////////////////////////////////////////////////////////////////////////////////////////
struct edges_t* get_reverse_edge_from_the_graph(struct edges_t* e, struct graph_t* g) {
	g_assert(e);
	g_assert(g);

	for (gint i = 0; i < g->numVertices; i++) {
		struct vertices_t* v = &(g->vertices[i]);
		// Check Route Element zNodeId with the v->verticeId
		if (compare_node_id(&e->zNodeId, &v->verticeId) != 0)
			continue;
		// Check Route Element zNodeis with any of reachable targeted vertices from v
		gboolean foundTargVert = FALSE;
		gint indexTargVert = -1;
		for (gint j = 0; j < v->numTargetedVertices; j++) {
			struct targetNodes_t* tv = &(v->targetedVertices[j]);
			if (compare_node_id(&e->aNodeId, &tv->tVertice) == 0)
			{
				foundTargVert = TRUE;
				indexTargVert = j;
				break;
			}
		}
		if (foundTargVert == FALSE) {
			 continue;
		}			

		// The targeted vertice is found, then check matching with the endpoints
		struct targetNodes_t* tv = &(v->targetedVertices[indexTargVert]);
		for (gint k = 0; k < tv->numEdges; k++) {
			struct edges_t* rev_e = &(tv->edges[k]);
			if ((strcmp(rev_e->aEndPointId, e->zEndPointId) == 0) &&
				(strcmp(rev_e->zEndPointId, e->aEndPointId) == 0)) {
				return rev_e;
			}				
		}
	}
	return NULL;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Function used to reflect in the graph the assigned/allocated resources contained in the path p
 * 	considering the needs (e.g., bandwidth) of service s
 *
 *	@param p
 *	@param s
 *	@parma g
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2022
 */
/////////////////////////////////////////////////////////////////////////////////////////
void allocate_graph_resources (struct path_t *p, struct service_t *s, struct graph_t *g)
{
	g_assert (p);
	g_assert (s);
	g_assert (g);
	// Retrieve the requested bw by the service
	struct path_constraints_t* pathCons = get_path_constraints(s);

	for (gint i = 0; i < p->numPathLinks; i++) {
		struct pathLink_t* pL = &(p->pathLinks[i]);
		// get the edge associated to the linkId in the graph
		struct edges_t* e = get_edge_from_graph_by_linkId(pL, g);
		if (e == NULL) {
			DEBUG_PC("The linkId: %s is NOT found in the Graph!!!", pL->linkId);
			exit(-1);
		}
		//Update the availBw in the edge
		gdouble resBw = e->availCap - pathCons->bwConstraint;
		DEBUG_PC("Updating the Avail Bw @ edge/link: %s", e->linkId);
		DEBUG_PC("Initial avaiCap @ e/link: %f, demanded Bw: %f, resulting Avail Bw: %f", e->availCap, pathCons->bwConstraint, resBw);
		memcpy(&e->availCap, &resBw, sizeof(gdouble));
		DEBUG_PC("Final e/link avail Bw: %f", e->availCap);	
	}
	g_free(pathCons);
	
	return;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Function used to reflect in the graph the assigned/allocated resources contained in the reverse direction of the path p
 * 	considering the needs (e.g., bandwidth) of service s
 *
 *	@param p
 *	@param s
 *	@parma g
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2021
 */
 /////////////////////////////////////////////////////////////////////////////////////////
void allocate_graph_reverse_resources(struct path_t* p, struct service_t * s, struct graph_t* g)
{
	g_assert(p);
	g_assert(s);
	g_assert(g);

	struct path_constraints_t* pathCons = get_path_constraints(s);
	for (gint i = 0; i < p->numPathLinks; i++) {
		struct pathLink_t* pL = &(p->pathLinks[i]);
		struct edges_t* e = get_edge_from_graph_by_linkId(pL, g);
		if (e == NULL) {
			DEBUG_PC("The linkId: %s is NOT found in the Graph!!!", pL->linkId);
			exit(-1);
		}
		struct edges_t* rev_e = get_reverse_edge_from_the_graph(e, g);
		if (rev_e == NULL) {
			DEBUG_PC("the reverse edge of linkId: %s is NOT found in the Graph!!!", pL->linkId);
			exit(-1);
		}
		//Update the availBw in the edge
		gdouble resBw = rev_e->availCap - pathCons->bwConstraint;
		DEBUG_PC("Updating the Avail Bw @ reverse edge/link: %s", rev_e->linkId);
		DEBUG_PC("Initial avaiCap @ reverse edge e/link: %f, demanded Bw: %f, resulting Avail Bw: %f", rev_e->availCap, pathCons->bwConstraint, resBw);
		memcpy(&rev_e->availCap, &resBw, sizeof(gdouble));
		DEBUG_PC("Final reverse edge e/link avail Bw: %f", rev_e->availCap);
	}
	g_free(pathCons);
	return;
}

////////////////////////////////////////////////////////////////////////////////////////
/**
 * 	@file pathComp_tools.c
 * 	@brief Function used to printall the computed paths for the requested network connectivity services
 *
 *	@param routeList
 *
 * 	@author Ricardo Martínez <ricardo.martinez@cttc.es>
 *	@date 2021
 */
 /////////////////////////////////////////////////////////////////////////////////////////
void print_path_connection_list(struct compRouteOutputList_t* routeList) {
	g_assert(routeList);
	for (gint i = 0; i < routeList->numCompRouteConnList; i++) {
		DEBUG_PC("==================== Service Item: %d ===================", i);
		struct compRouteOutput_t* rO = &(routeList->compRouteConnection[i]);
		DEBUG_PC("num service endpoints: %d", rO->num_service_endpoints_id);
		struct serviceId_t* s = &(rO->serviceId);
		DEBUG_PC("ContextId: %s, ServiceId: %s", s->contextId, s->service_uuid);
		DEBUG_PC("ingress --- %s [%s]", rO->service_endpoints_id[0].device_uuid, 
			rO->service_endpoints_id[0].endpoint_uuid);
		DEBUG_PC("egress --- %s [%s]", rO->service_endpoints_id[1].device_uuid,
			rO->service_endpoints_id[1].endpoint_uuid);

		if (rO->noPathIssue == NO_PATH_CONS_ISSUE) {
			DEBUG_PC("NO PATH SUCCESSFULLY COMPUTED");
			continue;
		}
		// Path
		DEBUG_PC("Number of paths: %d", rO->numPaths);
		for (gint j = 0; j < rO->numPaths; j++) {
			struct path_t* p = &(rO->paths[j]);
			print_path_t(p);
		}
	}
	return;
}