f59a3e7d64cddd8357c3a024d6ed810611f5c725
[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.rev211210.ConnectionType;
11 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.RpcActions;
12 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.sdnc.request.header.SdncRequestHeader;
13 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.add.openroadm.operational.modes.to.catalog.input.OperationalModeInfo;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * Class for checking service sdnc-request-header compliance.
19  *
20  */
21 public final class ServicehandlerComplianceCheck {
22
23     private static final Logger LOG = LoggerFactory.getLogger(ServicehandlerComplianceCheck.class);
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         public static final String SERVICENAME_NOT_SET;
29         public static final String CONNECTIONTYPE_NOT_SET;
30         public static final String REQUESTID_NOT_SET;
31         public static final String RPCACTION_NOT_SET;
32         public static final String HEADER_NOT_SET;
33
34         public static final String CATALOG_REQUESTID_NOT_SET;
35         public static final String CATALOG_HEADER_NOT_SET;
36         public static final String CATALOG_REQUESTSYSTEMID_NOT_SET;
37         public static final String CATALOG_RPCACTION_NOT_SET;
38         public static final String CATALOG_OPERATIONAL_MODE_INFO_NOT_SET;
39
40         // Static blocks are generated once and spare memory.
41         static {
42             SERVICENAME_NOT_SET = "Service Name (common-id for Temp service) is not set";
43             CONNECTIONTYPE_NOT_SET = "Service ConnectionType is not set";
44             REQUESTID_NOT_SET = "Service sdncRequestHeader 'request-id' is not set";
45             RPCACTION_NOT_SET = "Service sdncRequestHeader 'rpc-action' is not set";
46             HEADER_NOT_SET = "Service sdncRequestHeader is not set";
47             CATALOG_REQUESTID_NOT_SET = "sdnc-request-header 'request-id' is not set";
48             CATALOG_REQUESTSYSTEMID_NOT_SET = "sdnc-request-header 'request-system-id' is not set";
49             CATALOG_HEADER_NOT_SET = "sdnc-request-header is not set";
50             CATALOG_RPCACTION_NOT_SET = "sdnc-request-header 'rpc-action' is not set";
51             CATALOG_OPERATIONAL_MODE_INFO_NOT_SET = "operational-mode-info is not set";
52         }
53
54         public static String rpcactionsDiffers(RpcActions action1, RpcActions action2) {
55             return
56                 "Service sdncRequestHeader rpc-action '" + action1.name() + "' not equal to '" + action2.name() + "'";
57         }
58
59         public static String catalogRpcactionsDiffers(RpcActions action1, RpcActions action2) {
60             return
61                     "Catalog sdnc-request-header rpc-action '" + action1.name() + "' not equal to '" + action2.name()
62                             + "'";
63         }
64
65         private LogMessages() {
66         }
67     }
68
69     /**
70      * Check if a String is not null and not equal to void.
71      *
72      * @param value
73      *            String value
74      * @return true if String ok false if not
75      */
76     public static boolean checkString(String value) {
77         return ((value != null) && (!value.isEmpty()));
78     }
79
80     /**
81      * Check Compliance of Service request.
82      *
83      * @param serviceName
84      *            Service Name
85      * @param sdncRequestHeader
86      *            sdncRequestHeader
87      * @param conType
88      *            Connection type
89      * @param action
90      *            RPC Actions
91      * @param contype
92      *            Boolean to check connection Type
93      * @param sdncRequest
94      *            Boolean to check sdncRequestHeader
95      *
96      * @return true if Service Request OK and false if not
97      */
98     public static ComplianceCheckResult check(String serviceName, SdncRequestHeader sdncRequestHeader,
99                                        ConnectionType conType, RpcActions action,
100                                        Boolean contype, Boolean sdncRequest) {
101         if (!checkString(serviceName)) {
102             return new ComplianceCheckResult(false, LogMessages.SERVICENAME_NOT_SET);
103         }
104         if (contype && (conType == null)) {
105             return new ComplianceCheckResult(false, LogMessages.CONNECTIONTYPE_NOT_SET);
106         }
107         if (sdncRequest) {
108             if (sdncRequestHeader == null) {
109                 return new ComplianceCheckResult(false, LogMessages.HEADER_NOT_SET);
110             }
111             RpcActions serviceAction = sdncRequestHeader.getRpcAction();
112             String requestId = sdncRequestHeader.getRequestId();
113             if (!checkString(requestId)) {
114                 return new ComplianceCheckResult(false, LogMessages.REQUESTID_NOT_SET);
115             }
116             if (serviceAction == null) {
117                 return new ComplianceCheckResult(false, LogMessages.RPCACTION_NOT_SET);
118             }
119             if (serviceAction.compareTo(action) != 0) {
120                 return new ComplianceCheckResult(false, LogMessages.rpcactionsDiffers(serviceAction, action));
121             }
122         }
123         return new ComplianceCheckResult(true, "");
124     }
125
126     public static ComplianceCheckResult checkORCatalog(SdncRequestHeader sdncRequestHeader,
127                                                        OperationalModeInfo operationalModeInfo, RpcActions action,
128                                                        Boolean sdncRequest) {
129
130         ComplianceCheckResult result = sdncRequestHeaderValidate(sdncRequest, sdncRequestHeader, action);
131         if (result.getMessage().contains("sdnc-request-header")) {
132             return new ComplianceCheckResult(false,result.getMessage());
133         }
134         if (operationalModeInfo == null) {
135             return new ComplianceCheckResult(false, LogMessages.CATALOG_OPERATIONAL_MODE_INFO_NOT_SET);
136         }
137
138         return new ComplianceCheckResult(true, "");
139     }
140
141     public static ComplianceCheckResult checkSpecificCatalog(SdncRequestHeader sdncRequestHeader, org.opendaylight.yang
142             .gen.v1.http.org.openroadm.service.rev211210.add.specific.operational.modes.to.catalog.input
143             .OperationalModeInfo operationalModeInfoSpecific, RpcActions action, Boolean sdncRequest) {
144
145         ComplianceCheckResult result = sdncRequestHeaderValidate(sdncRequest, sdncRequestHeader, action);
146         if (result.getMessage().contains("sdnc-request-header")) {
147             return new ComplianceCheckResult(false,result.getMessage());
148         }
149         if (operationalModeInfoSpecific == null) {
150             return new ComplianceCheckResult(false, LogMessages.CATALOG_OPERATIONAL_MODE_INFO_NOT_SET);
151         }
152         return new ComplianceCheckResult(true, "");
153     }
154
155     public static ComplianceCheckResult sdncRequestHeaderValidate(Boolean sdncRequest, SdncRequestHeader
156             sdncRequestHeader, RpcActions action) {
157         if (sdncRequest) {
158             if (sdncRequestHeader == null) {
159                 return new ComplianceCheckResult(false, LogMessages.CATALOG_HEADER_NOT_SET);
160             }
161             RpcActions serviceAction = sdncRequestHeader.getRpcAction();
162             String requestId = sdncRequestHeader.getRequestId();
163             String requestSystemId = sdncRequestHeader.getRequestSystemId();
164             if (!checkString(requestId)) {
165                 return new ComplianceCheckResult(false, LogMessages.CATALOG_REQUESTID_NOT_SET);
166             }
167             if (!checkString(requestSystemId)) {
168                 return new ComplianceCheckResult(false, LogMessages.CATALOG_REQUESTSYSTEMID_NOT_SET);
169             }
170             if (serviceAction == null) {
171                 return new ComplianceCheckResult(false, LogMessages.CATALOG_RPCACTION_NOT_SET);
172             }
173             if (serviceAction.compareTo(action) != 0) {
174                 return new ComplianceCheckResult(false, LogMessages.catalogRpcactionsDiffers(serviceAction, action));
175             }
176         }
177         return new ComplianceCheckResult(true, "");
178     }
179
180
181     private ServicehandlerComplianceCheck() {
182     }
183
184 }