blob: c7b4b4933808ad09e045ae58d592d6cdf5ca31e1 [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;
Jian Li2a2d26c2018-10-08 11:29:31 +090022import org.onosproject.openstacknode.api.DefaultOpenstackSshAuth;
Daniel Park4b24cec2018-11-28 19:21:25 +090023import org.onosproject.openstacknode.api.OpenstackSshAuth;
Daniel Parkdeefa702018-07-17 17:55:51 +090024
25import static com.google.common.base.Preconditions.checkNotNull;
26import static org.onlab.util.Tools.nullIsIllegal;
Daniel Parkdeefa702018-07-17 17:55:51 +090027
28/**
29 * Node ssh authentication info codec used for serializing and
30 * de-serializing JSON string.
31 */
32public class OpenstackSshAuthCodec extends JsonCodec<OpenstackSshAuth> {
33
Daniel Parkdeefa702018-07-17 17:55:51 +090034 private static final String ID = "id";
35 private static final String PASSWORD = "password";
36
37 private static final String MISSING_MESSAGE = " is required in OpenstackSshAuth";
38 private static final String ERROR_MSG_NOT_NULL = "Openstack SSH auth cannot be null";
39
40 @Override
41 public ObjectNode encode(OpenstackSshAuth sshAuth, CodecContext context) {
42 checkNotNull(sshAuth, ERROR_MSG_NOT_NULL);
43
Daniel Park3436fde2018-08-24 18:41:25 +090044 return context.mapper().createObjectNode()
Daniel Parkdeefa702018-07-17 17:55:51 +090045 .put(ID, sshAuth.id())
46 .put(PASSWORD, sshAuth.password());
Daniel Parkdeefa702018-07-17 17:55:51 +090047 }
48
49 @Override
50 public OpenstackSshAuth decode(ObjectNode json, CodecContext context) {
51 if (json == null || !json.isObject()) {
52 return null;
53 }
54
55 String id = nullIsIllegal(json.get(ID).asText(),
56 ID + MISSING_MESSAGE);
57
58 String password = nullIsIllegal(json.get(PASSWORD).asText(),
59 PASSWORD + MISSING_MESSAGE);
60
61 return DefaultOpenstackSshAuth.builder()
62 .id(id)
63 .password(password)
64 .build();
65 }
66}