blob: 4dbe1d62e5095d1ed92d0eac0f9597aeb68e04ce [file] [log] [blame]
Daniel Parkdeefa702018-07-17 17:55:51 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.openstacknode.codec;
17
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.onosproject.openstacknode.api.OpenstackSshAuth;
23import org.onosproject.openstacknode.impl.DefaultOpenstackSshAuth;
24import org.slf4j.Logger;
25
26import static com.google.common.base.Preconditions.checkNotNull;
27import static org.onlab.util.Tools.nullIsIllegal;
28import static org.slf4j.LoggerFactory.getLogger;
29
30/**
31 * Node ssh authentication info codec used for serializing and
32 * de-serializing JSON string.
33 */
34public class OpenstackSshAuthCodec extends JsonCodec<OpenstackSshAuth> {
35
36 private final Logger log = getLogger(getClass());
37
38 private static final String ID = "id";
39 private static final String PASSWORD = "password";
40
41 private static final String MISSING_MESSAGE = " is required in OpenstackSshAuth";
42 private static final String ERROR_MSG_NOT_NULL = "Openstack SSH auth cannot be null";
43
44 @Override
45 public ObjectNode encode(OpenstackSshAuth sshAuth, CodecContext context) {
46 checkNotNull(sshAuth, ERROR_MSG_NOT_NULL);
47
48 ObjectNode result = context.mapper().createObjectNode()
49 .put(ID, sshAuth.id())
50 .put(PASSWORD, sshAuth.password());
51
52 return result;
53 }
54
55 @Override
56 public OpenstackSshAuth decode(ObjectNode json, CodecContext context) {
57 if (json == null || !json.isObject()) {
58 return null;
59 }
60
61 String id = nullIsIllegal(json.get(ID).asText(),
62 ID + MISSING_MESSAGE);
63
64 String password = nullIsIllegal(json.get(PASSWORD).asText(),
65 PASSWORD + MISSING_MESSAGE);
66
67 return DefaultOpenstackSshAuth.builder()
68 .id(id)
69 .password(password)
70 .build();
71 }
72}