blob: b765ecae3e38312676548ef146843dcf618d86d0 [file] [log] [blame]
Esin Karaman971fb7f2017-12-28 13:44:52 +00001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.onosproject.drivers.bmv2.ctl;
19
20
21import org.apache.thrift.TException;
22import org.onosproject.bmv2.thriftapi.InvalidMcOperation;
23import org.onosproject.drivers.bmv2.api.runtime.Bmv2RuntimeException;
24
25import static org.onosproject.drivers.bmv2.api.runtime.Bmv2RuntimeException.Code;
26
27/**
28 * Utility class to translate a Thrift exception into a Bmv2RuntimeException.
29 */
30final class Bmv2TExceptionParser {
31
32 private Bmv2TExceptionParser() {
33 // ban constructor.
34 }
35
36 static Bmv2RuntimeException parseTException(TException cause) {
37 try {
38 return new Bmv2RuntimeException(getCode(cause));
39 } catch (ParserException e) {
40 return new Bmv2RuntimeException(e.codeString);
41 }
42 }
43
44 private static Code getCode(TException e) throws ParserException {
45 if (e instanceof InvalidMcOperation) {
46 switch (((InvalidMcOperation) e).getCode()) {
47 case TABLE_FULL:
48 return Code.TABLE_FULL;
49 case INVALID_MGID:
50 return Code.INVALID_MGID;
51 case INVALID_L1_HANDLE:
52 return Code.INVALID_L1_HANDLE;
53 case INVALID_MGRP_HANDLE:
54 return Code.INVALID_MGRP_HANDLE;
55 case ERROR:
56 return Code.MC_GENERAL_ERROR;
57 default:
58 return Code.MC_UNKNOWN_ERROR;
59 }
60 } else {
61 throw new ParserException(e.toString());
62 }
63 }
64
65 private static class ParserException extends Exception {
66
67 private String codeString;
68
69 public ParserException(String codeString) {
70 this.codeString = codeString;
71 }
72 }
73}