Update Servicehandler checks
[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.service.format.rev190531.ServiceFormat;
14
15 /**
16  * Class for checking missing info on Tx/Rx for A/Z end.
17  *
18  */
19 public final class ServicehandlerTxRxCheck {
20
21     // This is class is public so that these messages can be accessed from Junit (avoid duplications).
22     public static final class LogMessages {
23
24         private static final String SERVICE = "Service ";
25         public static final String TXDIR_NOT_SET;
26         public static final String TXDIR_PORT_NOT_SET;
27         public static final String TXDIR_LGX_NOT_SET;
28         public static final String RXDIR_NOT_SET;
29         public static final String RXDIR_PORT_NOT_SET;
30         public static final String RXDIR_LGX_NOT_SET;
31
32         // Static blocks are generated once and spare memory.
33         static {
34             TXDIR_NOT_SET = "Service TxDirection is not correctly set";
35             RXDIR_NOT_SET = "Service RxDirection is not correctly set";
36             TXDIR_PORT_NOT_SET = "Service TxDirection Port is not correctly set";
37             TXDIR_LGX_NOT_SET = "Service TxDirection Lgx is not correctly set";
38             RXDIR_PORT_NOT_SET = "Service RxDirection Port is not correctly set";
39             RXDIR_LGX_NOT_SET = "Service RxDirection Lgx is not correctly set";
40         }
41
42         public static String endpointTypeNotSet(ServiceEndpointType endpointType) {
43             return SERVICE + endpointType + " is not set";
44         }
45
46         public static String rateNull(ServiceEndpointType endpointType) {
47             return "Something wrong when accessing " + SERVICE + endpointType + " rate, format or clli";
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      * Check if a String is not null and not equal to ''.
68      *
69      * @param value
70      *            String value
71      * @return true if String ok false if not
72      */
73     public static boolean checkString(String value) {
74         return (value != null && !value.isEmpty());
75     }
76
77
78     /**
79      * Check Compliance of Service TxRx info.
80      * @param serviceEnd Service Endpoint
81      * @param endpointType Endpoint type
82      *
83      * @return true if String ok false if not
84      */
85     public static ComplianceCheckResult check(ServiceEndpoint serviceEnd, ServiceEndpointType endpointType) {
86         if (serviceEnd == null) {
87             return new ComplianceCheckResult(false, LogMessages.endpointTypeNotSet(endpointType));
88         }
89
90         if (serviceEnd.getServiceRate() == null) {
91             String message = "Something wrong when accessing Service " + endpointType + " rate, format or clli";
92             return new ComplianceCheckResult(false, message);
93         }
94         Long serviceRate = serviceEnd.getServiceRate().toJava();
95         ServiceFormat serviceformat = serviceEnd.getServiceFormat();
96         String clli = serviceEnd.getClli();
97         if (serviceRate <= 0) {
98             return new ComplianceCheckResult(false, LogMessages.rateNotSet(endpointType));
99         }
100         if (serviceformat == null) {
101             return new ComplianceCheckResult(false, LogMessages.formatNotSet(endpointType));
102         }
103         if (!checkString(clli)) {
104             return new ComplianceCheckResult(false, LogMessages.clliNotSet(endpointType));
105         }
106
107         return new ComplianceCheckResult(true, "");
108     }
109
110     private ServicehandlerTxRxCheck() {
111     }
112
113 }