lishuai | 2ddc469 | 2015-07-31 15:15:16 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | package org.onosproject.ovsdb.rfc.utils; |
| 17 | |
| 18 | import java.util.List; |
| 19 | |
| 20 | import org.onosproject.ovsdb.rfc.jsonrpc.JsonRpcRequest; |
| 21 | import org.onosproject.ovsdb.rfc.operations.Operation; |
| 22 | import org.onosproject.ovsdb.rfc.schema.DatabaseSchema; |
| 23 | |
| 24 | /** |
| 25 | * RPC Methods request utility class. Refer to RFC7047's Section 4.1. |
| 26 | */ |
| 27 | public final class JsonRpcWriterUtil { |
| 28 | |
| 29 | /** |
| 30 | * Constructs a JsonRpcWriterUtil object. Utility classes should not have a |
| 31 | * public or default constructor, otherwise IDE will compile unsuccessfully. |
| 32 | * This class should not be instantiated. |
| 33 | */ |
| 34 | private JsonRpcWriterUtil() { |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Returns string of RPC request. |
| 39 | * @param uuid id of request object |
| 40 | * @param methodName method of request object |
| 41 | * @param params params of request object |
| 42 | * @return RPC Request String |
| 43 | */ |
| 44 | private static String getRequestStr(String uuid, String methodName, |
| 45 | List params) { |
| 46 | JsonRpcRequest request; |
| 47 | if (params != null) { |
| 48 | request = new JsonRpcRequest(uuid, methodName, params); |
| 49 | } else { |
| 50 | request = new JsonRpcRequest(uuid, methodName); |
| 51 | } |
| 52 | String str = ObjectMapperUtil.convertToString(request); |
| 53 | return str; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns string of get_schema request. |
| 58 | * @param uuid id of get_schema request |
| 59 | * @param dbnames params of get_schema request |
| 60 | * @return get_schema Request String |
| 61 | */ |
| 62 | public static String getSchemaStr(String uuid, List<String> dbnames) { |
| 63 | String methodName = "get_schema"; |
| 64 | return getRequestStr(uuid, methodName, dbnames); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns string of echo request. |
| 69 | * @param uuid id of echo request |
| 70 | * @return echo Request String |
| 71 | */ |
| 72 | public static String echoStr(String uuid) { |
| 73 | String methodName = "echo"; |
| 74 | return getRequestStr(uuid, methodName, null); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns string of monitor request. |
| 79 | * @param uuid id of monitor request |
| 80 | * @param monotorId json-value in params of monitor request |
| 81 | * @param dbSchema DatabaseSchema entity |
| 82 | * @return monitor Request String |
| 83 | */ |
| 84 | public static String monitorStr(String uuid, String monotorId, |
| 85 | DatabaseSchema dbSchema) { |
| 86 | String methodName = "monitor"; |
| 87 | return getRequestStr(uuid, methodName, |
| 88 | ParamUtil.getMonitorParams(monotorId, dbSchema)); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns string of list_dbs request. |
| 93 | * @param uuid id of list_dbs request |
| 94 | * @return list_dbs Request String |
| 95 | */ |
| 96 | public static String listDbsStr(String uuid) { |
| 97 | String methodName = "list_dbs"; |
| 98 | return getRequestStr(uuid, methodName, null); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns string of transact request. |
| 103 | * @param uuid id of transact request |
| 104 | * @param dbSchema DatabaseSchema entity |
| 105 | * @param operations operation* in params of transact request |
| 106 | * @return transact Request String |
| 107 | */ |
| 108 | public static String transactStr(String uuid, DatabaseSchema dbSchema, |
| 109 | List<Operation> operations) { |
| 110 | String methodName = "transact"; |
| 111 | return getRequestStr(uuid, methodName, |
| 112 | ParamUtil.getTransactParams(dbSchema, operations)); |
| 113 | } |
| 114 | } |