2 * Copyright (c) 2024 Smartoptics and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.transportpce.pce.networkanalyzer.port;
11 import java.util.Arrays;
12 import java.util.HashMap;
13 import java.util.HashSet;
16 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev230925.PathComputationRequestInput;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev230925.path.computation.request.input.ServiceAEnd;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev230925.path.computation.request.input.ServiceZEnd;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.service.port.Port;
20 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.service.endpoint.sp.RxDirection;
21 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.service.endpoint.sp.TxDirection;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class PreferenceFactory implements Factory {
27 private static final Logger LOG = LoggerFactory.getLogger(PreferenceFactory.class);
29 private final String portNamePattern = "(?i)SRG\\d+-PP\\d+-(TXRX|TX|RX)";
32 public Preference portPreference(PathComputationRequestInput pathComputationRequestInput) {
34 Map<String, Set<String>> map = nodePortMap(pathComputationRequestInput);
37 LOG.debug("No port preference found in path computation request.");
38 return new NoPreference();
41 LOG.debug("Port preference in path computation request: {}." , map);
42 return new ClientPreference(map);
46 * Create a key value mapper from PCRI where key is the node and the value is
47 * a unique list of port names.
49 * @return Client port preference map
51 Map<String, Set<String>> nodePortMap(PathComputationRequestInput pathComputationRequestInput) {
53 Map<String, Set<String>> mapper = new HashMap<>();
55 ServiceAEnd serviceAEnd = pathComputationRequestInput.getServiceAEnd();
56 if (serviceAEnd != null) {
58 RxDirection rxAzDirection = serviceAEnd.getRxDirection();
59 if (rxAzDirection != null) {
61 Port rxAZport = rxAzDirection.getPort();
62 if (rxAZport != null) {
63 add(rxAZport.getPortDeviceName(), rxAZport.getPortName(), mapper);
67 TxDirection txAzDirection = serviceAEnd.getTxDirection();
68 if (txAzDirection != null) {
70 Port txAZport = txAzDirection.getPort();
71 if (txAZport != null) {
72 add(txAZport.getPortDeviceName(), txAZport.getPortName(), mapper);
77 ServiceZEnd serviceZEnd = pathComputationRequestInput.getServiceZEnd();
78 if (serviceZEnd != null) {
80 RxDirection rxZaDirection = serviceZEnd.getRxDirection();
81 if (rxZaDirection != null) {
83 Port rxZAport = rxZaDirection.getPort();
84 if (rxZAport != null) {
85 add(rxZAport.getPortDeviceName(), rxZAport.getPortName(), mapper);
89 TxDirection txZaDirection = serviceZEnd.getTxDirection();
90 if (txZaDirection != null) {
92 Port txZAport = txZaDirection.getPort();
93 if (txZAport != null) {
94 add(txZAport.getPortDeviceName(), txZAport.getPortName(), mapper);
103 * Add node/port name to key value map. Mutable method, modifies the argument nodePortMap.
105 boolean add(String node, String port, Map<String, Set<String>> nodePortMap) {
107 if (node == null || port == null) {
111 String nodeTrimmed = node.trim();
112 String portTrimmed = port.trim();
114 if (nodeTrimmed.isEmpty() || portTrimmed.isEmpty()) {
118 if (!portTrimmed.matches(portNamePattern)) {
119 LOG.warn("Preferred port name '{}' on node {} doesn't match pattern '{}'",
126 if (nodePortMap.containsKey(nodeTrimmed)) {
127 boolean added = nodePortMap.get(nodeTrimmed).add(portTrimmed);
129 LOG.debug("Preferred port '{}' for node '{}' registered.", portTrimmed, nodeTrimmed);
131 LOG.debug("Failed registering port '{}' for node '{}'.", portTrimmed, nodeTrimmed);
136 nodePortMap.put(nodeTrimmed, new HashSet<>(Arrays.asList(portTrimmed)));