Use version 13.1.0 of openroadm-service models
[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.rev230526.ConnectionType;
11 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.RpcActions;
12 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.sdnc.request.header.SdncRequestHeader;
13 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.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         private LogMessages() {
65         }
66     }
67
68     /**
69      * Check if a String is not null and not equal to void.
70      *
71      * @param value
72      *            String value
73      * @return true if String ok false if not
74      */
75     public static boolean checkString(String value) {
76         return value != null && !value.isEmpty();
77     }
78
79     /**
80      * Check Compliance of Service request.
81      *
82      * @param serviceName
83      *            Service Name
84      * @param sdncRequestHeader
85      *            sdncRequestHeader
86      * @param conType
87      *            Connection type
88      * @param action
89      *            RPC Actions
90      * @param contype
91      *            Boolean to check connection Type
92      * @param sdncRequest
93      *            Boolean to check sdncRequestHeader
94      *
95      * @return true if Service Request OK and false if not
96      */
97     public static ComplianceCheckResult check(String serviceName, SdncRequestHeader sdncRequestHeader,
98                                        ConnectionType conType, RpcActions action,
99                                        Boolean contype, Boolean sdncRequest) {
100         if (!checkString(serviceName)) {
101             return new ComplianceCheckResult(false, LogMessages.SERVICENAME_NOT_SET);
102         }
103         if (contype && (conType == null)) {
104             return new ComplianceCheckResult(false, LogMessages.CONNECTIONTYPE_NOT_SET);
105         }
106         if (sdncRequest) {
107             if (sdncRequestHeader == null) {
108                 return new ComplianceCheckResult(false, LogMessages.HEADER_NOT_SET);
109             }
110             if (!checkString(sdncRequestHeader.getRequestId())) {
111                 return new ComplianceCheckResult(false, LogMessages.REQUESTID_NOT_SET);
112             }
113             RpcActions serviceAction = sdncRequestHeader.getRpcAction();
114             if (serviceAction == null) {
115                 return new ComplianceCheckResult(false, LogMessages.RPCACTION_NOT_SET);
116             }
117             if (serviceAction.compareTo(action) != 0) {
118                 return new ComplianceCheckResult(false, LogMessages.rpcactionsDiffers(serviceAction, action));
119             }
120         }
121         return new ComplianceCheckResult(true, "");
122     }
123
124     public static ComplianceCheckResult checkORCatalog(SdncRequestHeader sdncRequestHeader,
125                                                        OperationalModeInfo operationalModeInfo, RpcActions action,
126                                                        Boolean sdncRequest) {
127         String resultMsg = sdncRequestHeaderValidate(sdncRequest, sdncRequestHeader, action).getMessage();
128         if (resultMsg.contains("sdnc-request-header")) {
129             return new ComplianceCheckResult(false, resultMsg);
130         }
131         if (operationalModeInfo == null) {
132             return new ComplianceCheckResult(false, LogMessages.CATALOG_OPERATIONAL_MODE_INFO_NOT_SET);
133         }
134         return new ComplianceCheckResult(true, "");
135     }
136
137     public static ComplianceCheckResult checkSpecificCatalog(SdncRequestHeader sdncRequestHeader,
138             org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526
139                 .add.specific.operational.modes.to.catalog.input.OperationalModeInfo operationalModeInfoSpecific,
140             RpcActions action, Boolean sdncRequest) {
141         String resultMsg = sdncRequestHeaderValidate(sdncRequest, sdncRequestHeader, action).getMessage();
142         if (resultMsg.contains("sdnc-request-header")) {
143             return new ComplianceCheckResult(false,resultMsg);
144         }
145         if (operationalModeInfoSpecific == null) {
146             return new ComplianceCheckResult(false, LogMessages.CATALOG_OPERATIONAL_MODE_INFO_NOT_SET);
147         }
148         return new ComplianceCheckResult(true, "");
149     }
150
151     public static ComplianceCheckResult sdncRequestHeaderValidate(Boolean sdncRequest,
152             SdncRequestHeader sdncRequestHeader, RpcActions action) {
153         if (sdncRequest) {
154             if (sdncRequestHeader == null) {
155                 return new ComplianceCheckResult(false, LogMessages.CATALOG_HEADER_NOT_SET);
156             }
157             if (!checkString(sdncRequestHeader.getRequestId())) {
158                 return new ComplianceCheckResult(false, LogMessages.CATALOG_REQUESTID_NOT_SET);
159             }
160             if (!checkString(sdncRequestHeader.getRequestSystemId())) {
161                 return new ComplianceCheckResult(false, LogMessages.CATALOG_REQUESTSYSTEMID_NOT_SET);
162             }
163             RpcActions serviceAction = sdncRequestHeader.getRpcAction();
164             if (serviceAction == null) {
165                 return new ComplianceCheckResult(false, LogMessages.CATALOG_RPCACTION_NOT_SET);
166             }
167             if (serviceAction.compareTo(action) != 0) {
168                 return new ComplianceCheckResult(false, LogMessages.catalogRpcactionsDiffers(serviceAction, action));
169             }
170         }
171         return new ComplianceCheckResult(true, "");
172     }
173
174     private ServicehandlerComplianceCheck() {
175     }
176
177 }