Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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.xosclient.api; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.onosproject.core.ApplicationId; |
| 20 | import org.onosproject.net.config.Config; |
| 21 | import org.slf4j.Logger; |
| 22 | |
| 23 | import static org.slf4j.LoggerFactory.getLogger; |
| 24 | |
| 25 | /** |
| 26 | * XOS API access information. |
| 27 | */ |
| 28 | public class XosAccessConfig extends Config<ApplicationId> { |
| 29 | |
| 30 | protected final Logger log = getLogger(getClass()); |
| 31 | |
| 32 | private static final String XOS_SERVICE_ENDPOINT = "serviceEndpoint"; |
| 33 | private static final String XOS_ADMIN_USER = "adminUser"; |
| 34 | private static final String XOS_ADMIN_PASSWORD = "adminPassword"; |
| 35 | |
| 36 | /** |
| 37 | * Returns XOS access information. |
| 38 | * |
| 39 | * @return XOS access, or null |
| 40 | */ |
| 41 | public XosAccess xosAccess() { |
| 42 | try { |
| 43 | return new XosAccess(getConfig(object, XOS_SERVICE_ENDPOINT), |
| 44 | getConfig(object, XOS_ADMIN_USER), |
| 45 | getConfig(object, XOS_ADMIN_PASSWORD)); |
| 46 | } catch (NullPointerException e) { |
| 47 | log.error("Failed to get XOS access"); |
| 48 | return null; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns value of a given path. If the path is missing, show log and return |
| 54 | * null. |
| 55 | * |
| 56 | * @param path path |
| 57 | * @return value or null |
| 58 | */ |
| 59 | private String getConfig(JsonNode jsonNode, String path) { |
| 60 | jsonNode = jsonNode.path(path); |
| 61 | |
| 62 | if (jsonNode.isMissingNode()) { |
| 63 | log.error("{} is not configured", path); |
| 64 | return null; |
| 65 | } else { |
| 66 | return jsonNode.asText(); |
| 67 | } |
| 68 | } |
| 69 | } |