Enforce SpotBugs in Service handler
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / validation / checks / ServicehandlerComplianceCheck.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 package org.opendaylight.transportpce.servicehandler.validation.checks;
9
10 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.ConnectionType;
11 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.RpcActions;
12 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.sdnc.request.header.SdncRequestHeader;
13
14 /**
15  * Class for checking service sdnc-request-header compliance.
16  *
17  */
18 public final class ServicehandlerComplianceCheck {
19
20     // This is class is public so that these messages can be accessed from Junit (avoid duplications).
21     public static final class LogMessages {
22
23         public static final String SERVICENAME_NOT_SET;
24         public static final String CONNECTIONTYPE_NOT_SET;
25         public static final String REQUESTID_NOT_SET;
26         public static final String RPCACTION_NOT_SET;
27         public static final String HEADER_NOT_SET;
28
29         // Static blocks are generated once and spare memory.
30         static {
31             SERVICENAME_NOT_SET = "Service Name (common-id for Temp service) is not set";
32             CONNECTIONTYPE_NOT_SET = "Service ConnectionType is not set";
33             REQUESTID_NOT_SET = "Service sdncRequestHeader 'request-id' is not set";
34             RPCACTION_NOT_SET = "Service sdncRequestHeader 'rpc-action' is not set";
35             HEADER_NOT_SET = "Service sdncRequestHeader is not set";
36         }
37
38         public static String rpcactionsDiffers(RpcActions action1, RpcActions action2) {
39             return
40                 "Service sdncRequestHeader rpc-action '" + action1.name() + "' not equal to '" + action2.name() + "'";
41         }
42
43         private LogMessages() {
44         }
45     }
46
47     /**
48      * Check if a String is not null and not equal to void.
49      *
50      * @param value
51      *            String value
52      * @return true if String ok false if not
53      */
54     public static boolean checkString(String value) {
55         return ((value != null) && (!value.isEmpty()));
56     }
57
58     /**
59      * Check Compliance of Service request.
60      *
61      * @param serviceName
62      *            Service Name
63      * @param sdncRequestHeader
64      *            sdncRequestHeader
65      * @param conType
66      *            Connection type
67      * @param action
68      *            RPC Actions
69      * @param contype
70      *            Boolean to check connection Type
71      * @param sdncRequest
72      *            Boolean to check sdncRequestHeader
73      *
74      * @return true if Service Request OK and false if not
75      */
76     public static ComplianceCheckResult check(String serviceName, SdncRequestHeader sdncRequestHeader,
77                                        ConnectionType conType, RpcActions action,
78                                        Boolean contype, Boolean sdncRequest) {
79         if (!checkString(serviceName)) {
80             return new ComplianceCheckResult(false, LogMessages.SERVICENAME_NOT_SET);
81         }
82         if (contype && (conType == null)) {
83             return new ComplianceCheckResult(false, LogMessages.CONNECTIONTYPE_NOT_SET);
84         }
85         if (sdncRequest) {
86             if (sdncRequestHeader == null) {
87                 return new ComplianceCheckResult(false, LogMessages.HEADER_NOT_SET);
88             }
89             RpcActions serviceAction = sdncRequestHeader.getRpcAction();
90             String requestId = sdncRequestHeader.getRequestId();
91             if (!checkString(requestId)) {
92                 return new ComplianceCheckResult(false, LogMessages.REQUESTID_NOT_SET);
93             }
94             if (serviceAction == null) {
95                 return new ComplianceCheckResult(false, LogMessages.RPCACTION_NOT_SET);
96             }
97             if (serviceAction.compareTo(action) != 0) {
98                 return new ComplianceCheckResult(false, LogMessages.rpcactionsDiffers(serviceAction, action));
99             }
100         }
101         return new ComplianceCheckResult(true, "");
102     }
103
104     private ServicehandlerComplianceCheck() {
105     }
106
107 }