Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 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 | package org.onosproject.codec.impl; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.net.key.DeviceKey; |
| 22 | import org.onosproject.net.key.DeviceKeyId; |
| 23 | import org.onosproject.net.key.DeviceKeyService; |
| 24 | import org.slf4j.Logger; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | import static org.slf4j.LoggerFactory.getLogger; |
| 28 | |
| 29 | /** |
| 30 | * Device key JSON codec. |
| 31 | */ |
| 32 | public class DeviceKeyCodec extends AnnotatedCodec<DeviceKey> { |
| 33 | |
| 34 | private final Logger log = getLogger(getClass()); |
| 35 | |
| 36 | // JSON fieldNames |
| 37 | private static final String ID = "id"; |
| 38 | private static final String TYPE = "type"; |
| 39 | private static final String LABEL = "label"; |
| 40 | private static final String COMMUNITY_NAME = "community_name"; |
| 41 | private static final String USERNAME = "username"; |
| 42 | private static final String PASSWORD = "password"; |
| 43 | |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 44 | @Override |
| 45 | public ObjectNode encode(DeviceKey deviceKey, CodecContext context) { |
| 46 | checkNotNull(deviceKey, "Device key cannot be null"); |
| 47 | DeviceKeyService service = context.getService(DeviceKeyService.class); |
| 48 | ObjectNode result = context.mapper().createObjectNode() |
Brian Stanke | b8ff641 | 2016-02-25 14:16:19 -0500 | [diff] [blame] | 49 | .put(ID, deviceKey.deviceKeyId().id()) |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 50 | .put(TYPE, deviceKey.type().toString()) |
Brian Stanke | b8ff641 | 2016-02-25 14:16:19 -0500 | [diff] [blame] | 51 | .put(LABEL, deviceKey.label()); |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 52 | |
| 53 | if (deviceKey.type().equals(DeviceKey.Type.COMMUNITY_NAME)) { |
| 54 | result.put(COMMUNITY_NAME, deviceKey.asCommunityName().name()); |
| 55 | } else if (deviceKey.type().equals(DeviceKey.Type.USERNAME_PASSWORD)) { |
Brian Stanke | b8ff641 | 2016-02-25 14:16:19 -0500 | [diff] [blame] | 56 | result.put(USERNAME, deviceKey.asUsernamePassword().username()); |
| 57 | result.put(PASSWORD, deviceKey.asUsernamePassword().password()); |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | return annotate(result, deviceKey, context); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public DeviceKey decode(ObjectNode json, CodecContext context) { |
| 65 | if (json == null || !json.isObject()) { |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | DeviceKeyId id = DeviceKeyId.deviceKeyId(json.get(ID).asText()); |
| 70 | |
| 71 | DeviceKey.Type type = DeviceKey.Type.valueOf(json.get(TYPE).asText()); |
Brian Stanke | b8ff641 | 2016-02-25 14:16:19 -0500 | [diff] [blame] | 72 | String label = extract(json, LABEL); |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 73 | |
| 74 | if (type.equals(DeviceKey.Type.COMMUNITY_NAME)) { |
Brian Stanke | b8ff641 | 2016-02-25 14:16:19 -0500 | [diff] [blame] | 75 | String communityName = extract(json, COMMUNITY_NAME); |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 76 | return DeviceKey.createDeviceKeyUsingCommunityName(id, label, communityName); |
| 77 | } else if (type.equals(DeviceKey.Type.USERNAME_PASSWORD)) { |
Brian Stanke | b8ff641 | 2016-02-25 14:16:19 -0500 | [diff] [blame] | 78 | String username = extract(json, USERNAME); |
| 79 | String password = extract(json, PASSWORD); |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 80 | return DeviceKey.createDeviceKeyUsingUsernamePassword(id, label, username, password); |
| 81 | } else { |
| 82 | log.error("Unknown device key type: ", type); |
| 83 | return null; |
| 84 | } |
| 85 | } |
Brian Stanke | b8ff641 | 2016-02-25 14:16:19 -0500 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Extract the key from the json node. |
| 89 | * |
| 90 | * @param json json object |
| 91 | * @param key key to use extract the value from the json object |
| 92 | * @return extracted value from the json object |
| 93 | */ |
| 94 | private String extract(ObjectNode json, String key) { |
| 95 | return json.get(key) == null ? null : json.get(key).asText(); |
| 96 | } |
Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 97 | } |