Technical debt - Service handler Sonar issues
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / validation / checks / ServicehandlerTxRxCheck.java
1 /*
2  * Copyright © 2017 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.servicehandler.validation.checks;
10
11 import org.opendaylight.transportpce.servicehandler.ServiceEndpointType;
12 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.ServiceEndpoint;
13 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.endpoint.RxDirection;
14 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.endpoint.TxDirection;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.lgx.Lgx;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.port.Port;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.format.rev190531.ServiceFormat;
18
19 /**
20  * Class for checking missing info on Tx/Rx for A/Z end.
21  *
22  */
23 public final class ServicehandlerTxRxCheck {
24
25     // This is class is public so that these messages can be accessed from Junit (avoid duplications).
26     public static final class LogMessages {
27
28         private static final String SERVICE = "Service ";
29         public static final String TXDIR_NOT_SET;
30         public static final String TXDIR_PORT_NOT_SET;
31         public static final String TXDIR_LGX_NOT_SET;
32         public static final String RXDIR_NOT_SET;
33         public static final String RXDIR_PORT_NOT_SET;
34         public static final String RXDIR_LGX_NOT_SET;
35
36         // Static blocks are generated once and spare memory.
37         static {
38             TXDIR_NOT_SET = "Service TxDirection is not correctly set";
39             RXDIR_NOT_SET = "Service RxDirection is not correctly set";
40             TXDIR_PORT_NOT_SET = "Service TxDirection Port is not correctly set";
41             TXDIR_LGX_NOT_SET = "Service TxDirection Lgx is not correctly set";
42             RXDIR_PORT_NOT_SET = "Service RxDirection Port is not correctly set";
43             RXDIR_LGX_NOT_SET = "Service RxDirection Lgx is not correctly set";
44         }
45
46         public static String endpointTypeNotSet(ServiceEndpointType endpointType) {
47             return SERVICE + endpointType + " is not set";
48         }
49
50         public static String rateNotSet(ServiceEndpointType endpointType) {
51             return SERVICE + endpointType + " rate is not set";
52         }
53
54         public static String formatNotSet(ServiceEndpointType endpointType) {
55             return SERVICE + endpointType + " format is not set";
56         }
57
58         public static String clliNotSet(ServiceEndpointType endpointType) {
59             return SERVICE + endpointType + " clli is not set";
60         }
61
62         private LogMessages() {
63         }
64     }
65
66
67     /**
68      * Check if a String is not null and not equal to ''.
69      *
70      * @param value
71      *            String value
72      * @return true if String ok false if not
73      */
74     public static boolean checkString(String value) {
75         return (value != null && !value.isEmpty());
76     }
77
78     /**
79      * check if Port info is compliant.
80      *
81      * @param port
82      *            port info
83      * @return true if String ok false if not
84      */
85     @SuppressWarnings("java:S1067")
86     //sonar issue Reduce the number of conditional operators (4) used in the expression (maximum allowed 3)
87     //won't be fixed because of functional checks needed
88     public static boolean checkPort(Port port) {
89         boolean result = false;
90         if (port != null) {
91             String portDeviceName = port.getPortDeviceName();
92             String portType = port.getPortType();
93             String portName = port.getPortName();
94             String portRack = port.getPortRack();
95             String portShelf = port.getPortShelf();
96
97             return checkString(portDeviceName)
98                     && checkString(portType)
99                     && checkString(portName)
100                     && checkString(portRack)
101                     && checkString(portShelf);
102         }
103         return result;
104     }
105
106     /**
107      * Check if lgx info is compliant.
108      *
109      * @param lgx
110      *            lgx info
111      * @return true if String ok false if not
112      */
113     public static boolean checkLgx(Lgx lgx) {
114         boolean result = false;
115         if (lgx != null) {
116             String lgxDeviceName = lgx.getLgxDeviceName();
117             String lgxPortName = lgx.getLgxPortName();
118             String lgxPortRack = lgx.getLgxPortRack();
119             String lgxPortShelf = lgx.getLgxPortShelf();
120             if (checkString(lgxDeviceName) && checkString(lgxPortName) && checkString(lgxPortRack)
121                     && checkString(lgxPortShelf)) {
122                 result = true;
123             }
124         }
125         return result;
126     }
127
128     /**
129      * Check if Tx/Rx Direction complaincy info.
130      *
131      * @param txDirection
132      *            TxDirection
133      * @param rxDirection
134      *            RxDirection
135      *
136      * @return <code>true</code> if check is ok <code>false</code> else
137      */
138     public static ComplianceCheckResult checkTxOrRxInfo(TxDirection txDirection, RxDirection rxDirection) {
139         if (txDirection == null) {
140             return new ComplianceCheckResult(false, LogMessages.TXDIR_NOT_SET);
141         }
142         if (rxDirection == null) {
143             return new ComplianceCheckResult(false, LogMessages.RXDIR_NOT_SET);
144         }
145         if (!checkPort(txDirection.getPort())) {
146             return new ComplianceCheckResult(false, LogMessages.TXDIR_PORT_NOT_SET);
147         }
148         if (!checkLgx(txDirection.getLgx())) {
149             return new ComplianceCheckResult(false, LogMessages.TXDIR_LGX_NOT_SET);
150         }
151         if (!checkPort(rxDirection.getPort())) {
152             return new ComplianceCheckResult(false, LogMessages.RXDIR_PORT_NOT_SET);
153         }
154         if (!checkLgx(rxDirection.getLgx())) {
155             return new ComplianceCheckResult(false, LogMessages.RXDIR_LGX_NOT_SET);
156         }
157         return new ComplianceCheckResult(true, "");
158     }
159
160     /**
161      * Check Compliance of Service TxRx info.
162      * @param serviceEnd Service Endpoint
163      * @param endpointType Endpoint type
164      *
165      * @return true if String ok false if not
166      */
167     public static ComplianceCheckResult check(ServiceEndpoint serviceEnd, ServiceEndpointType endpointType) {
168         if (serviceEnd == null) {
169             return new ComplianceCheckResult(false, LogMessages.endpointTypeNotSet(endpointType));
170         }
171
172         if (serviceEnd.getServiceRate() == null) {
173             String message = "Something wrong when accessing Service " + endpointType + " rate, format or clli";
174             return new ComplianceCheckResult(false, message);
175         }
176         Long serviceRate = serviceEnd.getServiceRate().toJava();
177         ServiceFormat serviceformat = serviceEnd.getServiceFormat();
178         String clli = serviceEnd.getClli();
179         if (serviceRate <= 0) {
180             return new ComplianceCheckResult(false, LogMessages.rateNotSet(endpointType));
181         }
182         if (serviceformat == null) {
183             return new ComplianceCheckResult(false, LogMessages.formatNotSet(endpointType));
184         }
185         if (!checkString(clli)) {
186             return new ComplianceCheckResult(false, LogMessages.clliNotSet(endpointType));
187         }
188
189         ComplianceCheckResult complianceCheckResult
190                 = checkTxOrRxInfo(serviceEnd.getTxDirection(), serviceEnd.getRxDirection());
191         if (!complianceCheckResult.hasPassed()) {
192             return new ComplianceCheckResult(false, complianceCheckResult.getMessage());
193         }
194
195         return new ComplianceCheckResult(true, "");
196     }
197
198     private ServicehandlerTxRxCheck() {
199     }
200
201 }