blob: 104b13477802bb904f67224ec872fb5a492f1eb3 [file] [log] [blame]
Brian Stankeb9170d92016-02-19 14:18:42 -05001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Brian Stankeb9170d92016-02-19 14:18:42 -05003 *
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
17package org.onosproject.codec.impl;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.net.key.DeviceKey;
22import org.onosproject.net.key.DeviceKeyId;
23import org.onosproject.net.key.DeviceKeyService;
24import org.slf4j.Logger;
25
26import static com.google.common.base.Preconditions.checkNotNull;
27import static org.slf4j.LoggerFactory.getLogger;
28
29/**
30 * Device key JSON codec.
31 */
32public 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 Stankeb9170d92016-02-19 14:18:42 -050044 @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 Stankeb8ff6412016-02-25 14:16:19 -050049 .put(ID, deviceKey.deviceKeyId().id())
Brian Stankeb9170d92016-02-19 14:18:42 -050050 .put(TYPE, deviceKey.type().toString())
Brian Stankeb8ff6412016-02-25 14:16:19 -050051 .put(LABEL, deviceKey.label());
Brian Stankeb9170d92016-02-19 14:18:42 -050052
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 Stankeb8ff6412016-02-25 14:16:19 -050056 result.put(USERNAME, deviceKey.asUsernamePassword().username());
57 result.put(PASSWORD, deviceKey.asUsernamePassword().password());
Brian Stankeb9170d92016-02-19 14:18:42 -050058 }
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 Stankeb8ff6412016-02-25 14:16:19 -050072 String label = extract(json, LABEL);
Brian Stankeb9170d92016-02-19 14:18:42 -050073
74 if (type.equals(DeviceKey.Type.COMMUNITY_NAME)) {
Brian Stankeb8ff6412016-02-25 14:16:19 -050075 String communityName = extract(json, COMMUNITY_NAME);
Brian Stankeb9170d92016-02-19 14:18:42 -050076 return DeviceKey.createDeviceKeyUsingCommunityName(id, label, communityName);
77 } else if (type.equals(DeviceKey.Type.USERNAME_PASSWORD)) {
Brian Stankeb8ff6412016-02-25 14:16:19 -050078 String username = extract(json, USERNAME);
79 String password = extract(json, PASSWORD);
Brian Stankeb9170d92016-02-19 14:18:42 -050080 return DeviceKey.createDeviceKeyUsingUsernamePassword(id, label, username, password);
81 } else {
82 log.error("Unknown device key type: ", type);
83 return null;
84 }
85 }
Brian Stankeb8ff6412016-02-25 14:16:19 -050086
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 Stankeb9170d92016-02-19 14:18:42 -050097}