Open Kilda Java Documentation
BasicService.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.northbound.service;
17 
20 
27 
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 
34 public interface BasicService {
38  Logger logger = LoggerFactory.getLogger(BasicService.class);
39 
48  default InfoData validateInfoMessage(final Message requestMessage,
49  final Message responseMessage,
50  final String correlationId) {
51  InfoData data;
52  if (responseMessage != null) {
53  if (responseMessage instanceof ErrorMessage) {
54  ErrorData error = ((ErrorMessage) responseMessage).getData();
55  logger.error("Response message is error: {}={}, command={}, error={}",
56  CORRELATION_ID, correlationId, requestMessage, error);
57  throw new MessageException((ErrorMessage) responseMessage);
58  } else if (responseMessage instanceof InfoMessage) {
59  InfoMessage info = (InfoMessage) responseMessage;
60  data = info.getData();
61  if (data == null) {
62  String errorMessage = "Response message data is empty";
63  logger.error("{}: {}={}, command={}, info={}", errorMessage,
64  CORRELATION_ID, correlationId, requestMessage, info);
65  throw new MessageException(responseMessage.getCorrelationId(), responseMessage.getTimestamp(),
66  INTERNAL_ERROR, errorMessage, requestMessage.toString());
67  }
68  } else {
69  String errorMessage = "Response message type is unexpected";
70  logger.error("{}: {}:{}, command={}, message={}", errorMessage,
71  CORRELATION_ID, correlationId, requestMessage, responseMessage);
72  throw new MessageException(responseMessage.getCorrelationId(), responseMessage.getTimestamp(),
73  INTERNAL_ERROR, errorMessage, requestMessage.toString());
74  }
75  } else {
76  String errorMessage = "Response message is empty";
77  logger.error("{}: {}={}, command={}", errorMessage,
78  CORRELATION_ID, correlationId, requestMessage);
79  throw new MessageException(requestMessage.getCorrelationId(), System.currentTimeMillis(),
80  INTERNAL_ERROR, errorMessage, requestMessage.toString());
81  }
82  return data;
83  }
84 }
default InfoData validateInfoMessage(final Message requestMessage, final Message responseMessage, final String correlationId)
static final String CORRELATION_ID
Definition: Utils.java:43