Use constants for PortMapping log messages Step 3
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / mapping / PortMappingUtils.java
1 /*
2  * Copyright © 2020 Orange, Inc. 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.common.mapping;
10
11 import java.math.BigInteger;
12 import java.nio.charset.StandardCharsets;
13 import java.util.ArrayList;
14 import java.util.Base64;
15 import org.opendaylight.transportpce.common.StringConstants;
16
17 public final class PortMappingUtils {
18
19     //FNV1 64 bit hash constants
20     private static final BigInteger FNV_PRIME = new BigInteger("100000001b3", 16);
21     private static final BigInteger FNV_INIT = new BigInteger("cbf29ce484222325", 16);
22     private static final BigInteger FNV_MOD = new BigInteger("2").pow(64);
23
24
25     public static final String ASSOCIATED_LCP_LOGMSG =
26         "{} : port {} on {} - associated Logical Connection Point is {}";
27     public static final String CANNOT_AS_LCP_LOGMSG =
28         " - cannot assign Logical Connection Point";
29     public static final String CANNOT_CREATE_LCP_LOGMSG =
30         "{} : port {} on {} - Impossible to create logical connection point";
31     public static final String CANNOT_GET_DEV_CONF_LOGMSG =
32         "{} : impossible to get device configuration";
33     public static final String CANNOT_GET_LLDP_CONF_LOGMSG =
34         "- cannot find port config under LLDP";
35     public static final String CONMAP_ISSUE_LOGMSG =
36         "{} : connection-map analysis for source {} and dest (CP+port) {} - no entry found for source in LCP map";
37     public static final String CREATE_MAPPING_DATA_LOGMSG =
38         "{} : OpenROADM version {} node - Creating Mapping Data";
39     public static final String DEVICE_HAS_LOGMSG =
40         "{} : device has {} {}";
41     public static final String FETCH_CONNECTIONPORT_LOGMSG =
42         "{} : fetching connection-port {} at circuit-pack {}";
43     public static final String GETTING_CONPORT_LOGMSG =
44         "{} : getting connection-ports for degree number {}";
45     public static final String GETTING_CP_LOGMSG =
46         "{} : getting circuit-packs for SRG number {}";
47     public static final String GETTING_ETH_LIST_LOGMSG =
48         "{} : getting Ethernet interfaces list";
49     public static final String GETTING_MC_CAPA_LOGMSG =
50         "{} : getting MC capabilities for {}";
51     public static final String GOT_INTF_LOGMSG =
52         "{} : got interface {} of type {}";
53     public static final String INTF_ISSUE_LOGMSG =
54         "{} : error while getting interface {} ";
55     public static final String MAP_LOOKS_LOGMSG =
56         "{} : map looks like this {}";
57     public static final String MISSING_CP_LOGMSG =
58         "{} : no circuit-pack {} in the device";
59     public static final String NO_ASSOC_FOUND_LOGMSG =
60         "{} : port {} on {} - no association found {}";
61     public static final String NO_CONMAP_LOGMSG =
62         "{} : no connection-map found inside device configuration";
63     public static final String NO_MC_LOGMSG =
64         "{} : no MC profile found on {} {} - assuming fixed grid";
65     public static final String NO_PORT_ON_CP_LOGMSG =
66         "{} : no port {} on circuit pack {}";
67     public static final String NO_VALID_PARTNERPORT_LOGMSG =
68         "{} : port {} on {} has no valid partner port";
69     public static final String NODE_TYPE_LOGMSG =
70         "{} : node-type {}";
71     public static final String NOT_CORRECT_CONPORT_LOGMSG =
72         "{} : the number of connection-port is not correct for DEG{}";
73     public static final String NOT_CORRECT_PARTNERPORT_LOGMSG =
74         "{} : port {} on {} is not a correct partner port of {} on {}";
75     public static final String PARTNERPORT_CONF_ERROR_LOGMSG =
76         " - error with partner port configuration";
77     public static final String PARTNERPORT_GET_ERROR_LOGMSG =
78         " - error getting partner port";
79     public static final String PORT_ALREADY_HANDLED_LOGMSG =
80         "{} : port {} on {} has already been handled";
81     public static final String PORT_NOT_RDMEXT_LOGMSG =
82         "{} : port {} on {} is not roadm-external";
83     public static final String PORTDIR_ERROR_LOGMSG =
84         " - error in configuration with port-direction";
85     public static final String PORTMAPPING_IGNORE_LOGMSG =
86         " - ignoring it in port-mapping";
87     public static final String PORTMAPPING_POST_FAIL_LOGMSG =
88         "{} : port-mapping post-treatment failure for {}";
89     public static final String PORTQUAL_ERROR_LOGMSG =
90         " - error in configuration with port-qual";
91     public static final String PORTQUAL_LOGMSG =
92         "{} : port {} on {} - PortQual {}";
93     public static final String PROCESSING_DONE_LOGMSG =
94         "{} : processing done {}";
95     public static final String UNABLE_MAPPING_LOGMSG =
96         "{} : unable to {} mapping for {}";
97     public static final String UNABLE_MC_CAPA_LOGMSG =
98         "{} : unable to create MC capabilities";
99     public static final String UNSUPPORTED_DIR_LOGMSG =
100         "{} : port {} on {} - unsupported Direction {}";
101     public static final String UPDATE_MAPPING_LOGMSG =
102         "{} : Updating old mapping data {} for {} by new mapping data {}";
103     public static final String XPDR_LIST_IN_CONF_LOGMSG =
104         "{} : list of Xponders {} in configuration";
105
106     /**
107      * Implements the FNV-1 64bit algorithm.
108      * FNV-1 128bit would be ideal for 16 bytes but we need an overhead for Base64 encoding.
109      * Otherwise, the hash cannot be stored in a UTF-8 string.
110      * https://www.wikiwand.com/en/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#/FNV-1_hash
111      * https://github.com/pmdamora/fnv-cracker-app/blob/master/src/main/java/passwordcrack/cracking/HashChecker.java
112      * @param stringdata the String to be hashed
113      * @return the hash string
114      */
115     protected static String fnv1size64(String stringdata) {
116         BigInteger hash = FNV_INIT;
117         byte[] data = stringdata.getBytes(StandardCharsets.UTF_8);
118
119         for (byte b : data) {
120             hash = hash.multiply(FNV_PRIME).mod(FNV_MOD);
121             hash = hash.xor(BigInteger.valueOf((int) b & 0xff));
122         }
123
124         return Base64.getEncoder().encodeToString(hash.toByteArray());
125     }
126
127     protected static String degreeTtpNodeName(String cpIndex, String direction) {
128         ArrayList<String> array = new ArrayList<>();
129         array.add("DEG" + cpIndex);
130         array.add(StringConstants.TTP_TOKEN);
131         if (direction != null) {
132             array.add(direction);
133         }
134         return String.join("-", array);
135     }
136
137     protected static String createXpdrLogicalConnectionPort(int xponderNb, int lcpNb, String token) {
138         return new StringBuilder("XPDR").append(xponderNb)
139                 .append("-")
140                 .append(token).append(lcpNb)
141                 .toString();
142     }
143
144     private PortMappingUtils() {
145         //Noop - should not be called
146     }
147 }