Address yang name change for transportpce-pce
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / networkanalyzer / port / PreferenceFactory.java
1 /*
2  * Copyright (c) 2024 Smartoptics and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.transportpce.pce.networkanalyzer.port;
10
11 import java.util.Arrays;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Map;
15 import java.util.Set;
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;
24
25 public class PreferenceFactory implements Factory {
26
27     private static final Logger LOG = LoggerFactory.getLogger(PreferenceFactory.class);
28
29     private String portNamePattern;
30
31     public PreferenceFactory() {
32         this.portNamePattern = "(?i)SRG\\d+-PP\\d+-(TXRX|TX|RX)";
33     }
34
35     @Override
36     public Preference portPreference(PathComputationRequestInput pathComputationRequestInput) {
37
38         Map<String, Set<String>> map = nodePortMap(pathComputationRequestInput);
39
40         if (map.isEmpty()) {
41             LOG.debug("No port preference found in path computation request.");
42             return new NoPreference();
43         }
44
45         LOG.debug("Port preference in path computation request: {}." , map);
46         return new ClientPreference(map);
47     }
48
49     /**
50      * Create a key value mapper from PCRI where key is the node and the value is
51      * a unique list of port names.
52      *
53      * @return Client port preference map
54      */
55     Map<String, Set<String>> nodePortMap(PathComputationRequestInput pathComputationRequestInput) {
56
57         Map<String, Set<String>> mapper = new HashMap<>();
58
59         ServiceAEnd serviceAEnd = pathComputationRequestInput.getServiceAEnd();
60         if (serviceAEnd != null) {
61
62             RxDirection rxAzDirection = serviceAEnd.getRxDirection();
63             if (rxAzDirection != null) {
64
65                 Port rxAZport = rxAzDirection.getPort();
66                 if (rxAZport != null) {
67                     add(rxAZport.getPortDeviceName(), rxAZport.getPortName(), mapper);
68                 }
69             }
70
71             TxDirection txAzDirection = serviceAEnd.getTxDirection();
72             if (txAzDirection != null) {
73
74                 Port txAZport = txAzDirection.getPort();
75                 if (txAZport != null) {
76                     add(txAZport.getPortDeviceName(), txAZport.getPortName(), mapper);
77                 }
78             }
79         }
80
81         ServiceZEnd serviceZEnd = pathComputationRequestInput.getServiceZEnd();
82         if (serviceZEnd != null) {
83
84             RxDirection rxZaDirection = serviceZEnd.getRxDirection();
85             if (rxZaDirection != null) {
86
87                 Port rxZAport = rxZaDirection.getPort();
88                 if (rxZAport != null) {
89                     add(rxZAport.getPortDeviceName(), rxZAport.getPortName(), mapper);
90                 }
91             }
92
93             TxDirection txZaDirection = serviceZEnd.getTxDirection();
94             if (txZaDirection != null) {
95
96                 Port txZAport = txZaDirection.getPort();
97                 if (txZAport != null) {
98                     add(txZAport.getPortDeviceName(), txZAport.getPortName(), mapper);
99                 }
100             }
101         }
102
103         return mapper;
104     }
105
106     /**
107      * Add node/port name to key value map. Mutable method, modifies the argument nodePortMap.
108      */
109     boolean add(String node, String port, Map<String, Set<String>> nodePortMap) {
110
111         if (node == null || port == null) {
112             return false;
113         }
114
115         String nodeTrimmed = node.trim();
116         String portTrimmed = port.trim();
117
118         if (nodeTrimmed.isEmpty() || portTrimmed.isEmpty()) {
119             return false;
120         }
121
122         if (!portTrimmed.matches(portNamePattern)) {
123             LOG.warn("Preferred port name '{}' on node {} doesn't match pattern '{}'",
124                 portTrimmed,
125                 nodeTrimmed,
126                 portNamePattern
127             );
128         }
129
130         if (nodePortMap.containsKey(nodeTrimmed)) {
131             boolean added = nodePortMap.get(nodeTrimmed).add(portTrimmed);
132             if (added) {
133                 LOG.debug("Preferred port '{}' for node '{}' registered.", portTrimmed, nodeTrimmed);
134             } else {
135                 LOG.debug("Failed registering port '{}' for node '{}'.", portTrimmed, nodeTrimmed);
136             }
137             return added;
138         }
139
140         nodePortMap.put(nodeTrimmed, new HashSet<>(Arrays.asList(portTrimmed)));
141
142         return true;
143     }
144 }