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.rev240205.PathComputationRequestInput;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev240205.path.computation.request.input.ServiceAEnd;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev240205.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 String portNamePattern;
31 public PreferenceFactory() {
32 this.portNamePattern = "(?i)SRG\\d+-PP\\d+-(TXRX|TX|RX)";
36 public Preference portPreference(PathComputationRequestInput pathComputationRequestInput) {
38 Map<String, Set<String>> map = nodePortMap(pathComputationRequestInput);
41 LOG.debug("No port preference found in path computation request.");
42 return new NoPreference();
45 LOG.debug("Port preference in path computation request: {}." , map);
46 return new ClientPreference(map);
50 * Create a key value mapper from PCRI where key is the node and the value is
51 * a unique list of port names.
53 * @return Client port preference map
55 Map<String, Set<String>> nodePortMap(PathComputationRequestInput pathComputationRequestInput) {
57 Map<String, Set<String>> mapper = new HashMap<>();
59 ServiceAEnd serviceAEnd = pathComputationRequestInput.getServiceAEnd();
60 if (serviceAEnd != null) {
62 RxDirection rxAzDirection = serviceAEnd.getRxDirection();
63 if (rxAzDirection != null) {
65 Port rxAZport = rxAzDirection.getPort();
66 if (rxAZport != null) {
67 add(rxAZport.getPortDeviceName(), rxAZport.getPortName(), mapper);
71 TxDirection txAzDirection = serviceAEnd.getTxDirection();
72 if (txAzDirection != null) {
74 Port txAZport = txAzDirection.getPort();
75 if (txAZport != null) {
76 add(txAZport.getPortDeviceName(), txAZport.getPortName(), mapper);
81 ServiceZEnd serviceZEnd = pathComputationRequestInput.getServiceZEnd();
82 if (serviceZEnd != null) {
84 RxDirection rxZaDirection = serviceZEnd.getRxDirection();
85 if (rxZaDirection != null) {
87 Port rxZAport = rxZaDirection.getPort();
88 if (rxZAport != null) {
89 add(rxZAport.getPortDeviceName(), rxZAport.getPortName(), mapper);
93 TxDirection txZaDirection = serviceZEnd.getTxDirection();
94 if (txZaDirection != null) {
96 Port txZAport = txZaDirection.getPort();
97 if (txZAport != null) {
98 add(txZAport.getPortDeviceName(), txZAport.getPortName(), mapper);
107 * Add node/port name to key value map. Mutable method, modifies the argument nodePortMap.
109 boolean add(String node, String port, Map<String, Set<String>> nodePortMap) {
111 if (node == null || port == null) {
115 String nodeTrimmed = node.trim();
116 String portTrimmed = port.trim();
118 if (nodeTrimmed.isEmpty() || portTrimmed.isEmpty()) {
122 if (!portTrimmed.matches(portNamePattern)) {
123 LOG.warn("Preferred port name '{}' on node {} doesn't match pattern '{}'",
130 if (nodePortMap.containsKey(nodeTrimmed)) {
131 boolean added = nodePortMap.get(nodeTrimmed).add(portTrimmed);
133 LOG.debug("Preferred port '{}' for node '{}' registered.", portTrimmed, nodeTrimmed);
135 LOG.debug("Failed registering port '{}' for node '{}'.", portTrimmed, nodeTrimmed);
140 nodePortMap.put(nodeTrimmed, new HashSet<>(Arrays.asList(portTrimmed)));