Brian Stanke | b9170d9 | 2016-02-19 14:18:42 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 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 | |
| 44 | |
| 45 | @Override |
| 46 | public ObjectNode encode(DeviceKey deviceKey, CodecContext context) { |
| 47 | checkNotNull(deviceKey, "Device key cannot be null"); |
| 48 | DeviceKeyService service = context.getService(DeviceKeyService.class); |
| 49 | ObjectNode result = context.mapper().createObjectNode() |
| 50 | .put(ID, deviceKey.deviceKeyId().id().toString()) |
| 51 | .put(TYPE, deviceKey.type().toString()) |
| 52 | .put(LABEL, deviceKey.label().toString()); |
| 53 | |
| 54 | if (deviceKey.type().equals(DeviceKey.Type.COMMUNITY_NAME)) { |
| 55 | result.put(COMMUNITY_NAME, deviceKey.asCommunityName().name()); |
| 56 | } else if (deviceKey.type().equals(DeviceKey.Type.USERNAME_PASSWORD)) { |
| 57 | result.put(USERNAME, deviceKey.asUsernamePassword().username().toString()); |
| 58 | result.put(PASSWORD, deviceKey.asUsernamePassword().password().toString()); |
| 59 | } |
| 60 | |
| 61 | return annotate(result, deviceKey, context); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public DeviceKey decode(ObjectNode json, CodecContext context) { |
| 66 | if (json == null || !json.isObject()) { |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | DeviceKeyId id = DeviceKeyId.deviceKeyId(json.get(ID).asText()); |
| 71 | |
| 72 | DeviceKey.Type type = DeviceKey.Type.valueOf(json.get(TYPE).asText()); |
| 73 | String label = json.get(LABEL).asText(); |
| 74 | |
| 75 | if (type.equals(DeviceKey.Type.COMMUNITY_NAME)) { |
| 76 | String communityName = json.get(COMMUNITY_NAME).asText(); |
| 77 | return DeviceKey.createDeviceKeyUsingCommunityName(id, label, communityName); |
| 78 | } else if (type.equals(DeviceKey.Type.USERNAME_PASSWORD)) { |
| 79 | String username = json.get(USERNAME).asText(); |
| 80 | String password = json.get(PASSWORD).asText(); |
| 81 | return DeviceKey.createDeviceKeyUsingUsernamePassword(id, label, username, password); |
| 82 | } else { |
| 83 | log.error("Unknown device key type: ", type); |
| 84 | return null; |
| 85 | } |
| 86 | } |
| 87 | } |