fbca473aff2a63f1cac8913bd778bbb4b7d0cb92
[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         if (port == null) {
90             return false;
91         }
92         String portDeviceName = port.getPortDeviceName();
93         String portType = port.getPortType();
94         String portName = port.getPortName();
95         String portRack = port.getPortRack();
96         String portShelf = port.getPortShelf();
97
98         return checkString(portDeviceName)
99                 && checkString(portType)
100                 && checkString(portName)
101                 && checkString(portRack)
102                 && checkString(portShelf);
103     }
104
105     /**
106      * Check if lgx info is compliant.
107      *
108      * @param lgx
109      *            lgx info
110      * @return true if String ok false if not
111      */
112     public static boolean checkLgx(Lgx lgx) {
113         if (lgx == null) {
114             return false;
115         }
116         String lgxDeviceName = lgx.getLgxDeviceName();
117         String lgxPortName = lgx.getLgxPortName();
118         String lgxPortRack = lgx.getLgxPortRack();
119         String lgxPortShelf = lgx.getLgxPortShelf();
120         return checkString(lgxDeviceName)
121                 && checkString(lgxPortName)
122                 && checkString(lgxPortRack)
123                 && checkString(lgxPortShelf);
124     }
125
126     /**
127      * Check if Tx/Rx Direction complaincy info.
128      *
129      * @param txDirection
130      *            TxDirection
131      * @param rxDirection
132      *            RxDirection
133      *
134      * @return <code>true</code> if check is ok <code>false</code> else
135      */
136     public static ComplianceCheckResult checkTxOrRxInfo(TxDirection txDirection, RxDirection rxDirection) {
137         if (txDirection == null) {
138             return new ComplianceCheckResult(false, LogMessages.TXDIR_NOT_SET);
139         }
140         if (rxDirection == null) {
141             return new ComplianceCheckResult(false, LogMessages.RXDIR_NOT_SET);
142         }
143         if (!checkPort(txDirection.getPort())) {
144             return new ComplianceCheckResult(false, LogMessages.TXDIR_PORT_NOT_SET);
145         }
146         if (!checkLgx(txDirection.getLgx())) {
147             return new ComplianceCheckResult(false, LogMessages.TXDIR_LGX_NOT_SET);
148         }
149         if (!checkPort(rxDirection.getPort())) {
150             return new ComplianceCheckResult(false, LogMessages.RXDIR_PORT_NOT_SET);
151         }
152         if (!checkLgx(rxDirection.getLgx())) {
153             return new ComplianceCheckResult(false, LogMessages.RXDIR_LGX_NOT_SET);
154         }
155         return new ComplianceCheckResult(true, "");
156     }
157
158     /**
159      * Check Compliance of Service TxRx info.
160      * @param serviceEnd Service Endpoint
161      * @param endpointType Endpoint type
162      *
163      * @return true if String ok false if not
164      */
165     public static ComplianceCheckResult check(ServiceEndpoint serviceEnd, ServiceEndpointType endpointType) {
166         if (serviceEnd == null) {
167             return new ComplianceCheckResult(false, LogMessages.endpointTypeNotSet(endpointType));
168         }
169
170         if (serviceEnd.getServiceRate() == null) {
171             String message = "Something wrong when accessing Service " + endpointType + " rate, format or clli";
172             return new ComplianceCheckResult(false, message);
173         }
174         Long serviceRate = serviceEnd.getServiceRate().toJava();
175         ServiceFormat serviceformat = serviceEnd.getServiceFormat();
176         String clli = serviceEnd.getClli();
177         if (serviceRate <= 0) {
178             return new ComplianceCheckResult(false, LogMessages.rateNotSet(endpointType));
179         }
180         if (serviceformat == null) {
181             return new ComplianceCheckResult(false, LogMessages.formatNotSet(endpointType));
182         }
183         if (!checkString(clli)) {
184             return new ComplianceCheckResult(false, LogMessages.clliNotSet(endpointType));
185         }
186
187         ComplianceCheckResult complianceCheckResult
188                 = checkTxOrRxInfo(serviceEnd.getTxDirection(), serviceEnd.getRxDirection());
189         if (!complianceCheckResult.hasPassed()) {
190             return new ComplianceCheckResult(false, complianceCheckResult.getMessage());
191         }
192
193         return new ComplianceCheckResult(true, "");
194     }
195
196     private ServicehandlerTxRxCheck() {
197     }
198
199 }