blob: f64d664a856f1da7970541dbfcd93e66714f92cf [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
Daniel Park3436fde2018-08-24 18:41:25 +090048 return context.mapper().createObjectNode()
Daniel Parkdeefa702018-07-17 17:55:51 +090049 .put(ID, sshAuth.id())
50 .put(PASSWORD, sshAuth.password());
Daniel Parkdeefa702018-07-17 17:55:51 +090051 }
52
53 @Override
54 public OpenstackSshAuth decode(ObjectNode json, CodecContext context) {
55 if (json == null || !json.isObject()) {
56 return null;
57 }
58
59 String id = nullIsIllegal(json.get(ID).asText(),
60 ID + MISSING_MESSAGE);
61
62 String password = nullIsIllegal(json.get(PASSWORD).asText(),
63 PASSWORD + MISSING_MESSAGE);
64
65 return DefaultOpenstackSshAuth.builder()
66 .id(id)
67 .password(password)
68 .build();
69 }
70}