fix some compilation warnings
[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.rev161014.ServiceEndpoint;
13 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.ServiceFormat;
14 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.service.endpoint.RxDirection;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.service.endpoint.TxDirection;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.service.lgx.Lgx;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.service.port.Port;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Class for checking missing info on Tx/Rx for A/Z end.
23  *
24  */
25 public final class ServicehandlerTxRxCheck {
26
27     private static final Logger LOG = LoggerFactory.getLogger(ServicehandlerTxRxCheck.class);
28
29     /**
30      * Check if a String is not null and not equal to ''.
31      *
32      * @param value
33      *            String value
34      * @return true if String ok false if not
35      */
36     public static boolean checkString(String value) {
37         return ((value != null) && (value.compareTo("") != 0));
38     }
39
40     /**
41      * check if Port info is compliant.
42      *
43      * @param port
44      *            port info
45      * @return true if String ok false if not
46      */
47     public static boolean checkPort(Port port) {
48         boolean result = false;
49         if (port != null) {
50             String portDeviceName = port.getPortDeviceName();
51             String portType = port.getPortType();
52             String portName = port.getPortName();
53             String portRack = port.getPortRack();
54             String portShelf = port.getPortShelf();
55
56             if (checkString(portDeviceName) && checkString(portType) && checkString(portName) && checkString(portRack)
57                     && checkString(portShelf)) {
58                 result = true;
59             }
60         }
61         return result;
62     }
63
64     /**
65      * Check if lgx info is compliant.
66      *
67      * @param lgx
68      *            lgx info
69      * @return true if String ok false if not
70      */
71     public static boolean checkLgx(Lgx lgx) {
72         boolean result = false;
73         if (lgx != null) {
74             String lgxDeviceName = lgx.getLgxDeviceName();
75             String lgxPortName = lgx.getLgxPortName();
76             String lgxPortRack = lgx.getLgxPortRack();
77             String lgxPortShelf = lgx.getLgxPortShelf();
78             if (checkString(lgxDeviceName) && checkString(lgxPortName) && checkString(lgxPortRack)
79                     && checkString(lgxPortShelf)) {
80                 result = true;
81             }
82         }
83         return result;
84     }
85
86     /**
87      * Check if Tx/Rx Direction complaincy info.
88      *
89      * @param txDirection
90      *            TxDirection
91      * @param rxDirection
92      *            RxDirection
93      *
94      * @return <code>true</code> if check is ok <code>false</code> else
95      */
96     public static ComplianceCheckResult checkTxOrRxInfo(TxDirection txDirection, RxDirection rxDirection) {
97         boolean result = true;
98         String message = "";
99         if (txDirection != null) {
100             if (!checkPort(txDirection.getPort())) {
101                 result = false;
102                 message = "Service TxDirection Port is not correctly set";
103             } else if (!checkLgx(txDirection.getLgx())) {
104                 result = false;
105                 message = "Service TxDirection Lgx is not correctly set";
106             } else if (rxDirection != null) {
107                 if (!checkPort(rxDirection.getPort())) {
108                     result = false;
109                     message = "Service RxDirection Port is not correctly set";
110                 } else if (!checkLgx(rxDirection.getLgx())) {
111                     result = false;
112                     message = "Service RxDirection Lgx is not correctly set";
113                 }
114             } else {
115                 result = false;
116                 message = "Service RxDirection is not correctly set";
117             }
118         } else {
119             result = false;
120             message = "Service TxDirection is not correctly set";
121         }
122         return new ComplianceCheckResult(result, message);
123     }
124
125     /**
126      * Check Compliancy of Service TxRx info.
127      *
128      * @return true if String ok false if not
129      */
130     public static ComplianceCheckResult check(ServiceEndpoint serviceEnd, ServiceEndpointType endpointType) {
131         boolean result = true;
132         String message = "";
133         if (serviceEnd != null) {
134             try {
135                 Long serviceRate = serviceEnd.getServiceRate();
136                 ServiceFormat serviceformat = serviceEnd.getServiceFormat();
137                 String clli = serviceEnd.getClli();
138                 if ((serviceRate == null) || (serviceRate <= 0)) {
139                     result = false;
140                     message = "Service " + endpointType + " rate is not set";
141                     LOG.debug(message);
142                 } else if (serviceformat == null) {
143                     result = false;
144                     message = "Service " + endpointType + " format is not set";
145                     LOG.debug(message);
146                 } else if (!checkString(clli)) {
147                     result = false;
148                     message = "Service" + endpointType + " clli format is not set";
149                     LOG.debug(message);
150                 } else {
151                     ComplianceCheckResult complianceCheckResult
152                             = checkTxOrRxInfo(serviceEnd.getTxDirection(), serviceEnd.getRxDirection());
153                     if (!complianceCheckResult.hasPassed()) {
154                         result = false;
155                         message = complianceCheckResult.getMessage();
156                     }
157                 }
158             } catch (NullPointerException e) {
159                 message = "Service " + endpointType + " rate, format or clli is not set";
160                 LOG.error(message, e);
161                 return new ComplianceCheckResult(false, message);
162             }
163         } else {
164             result = false;
165             message = endpointType + " is not set";
166             LOG.debug(message);
167         }
168         return new ComplianceCheckResult(result, message);
169     }
170
171     public ServicehandlerTxRxCheck() {
172     }
173
174 }