blob: d3ddabb468d5f1fad51ac8f051d76687d725ba01 [file] [log] [blame]
Jian Li27841662018-04-14 01:59:47 +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
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
21import org.onosproject.openstacknode.api.OpenstackAuth;
Daniel Park2ff66b42018-08-01 11:52:45 +090022import org.onosproject.openstacknode.api.DefaultOpenstackAuth;
Jian Li27841662018-04-14 01:59:47 +090023import org.slf4j.Logger;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26import static org.onlab.util.Tools.nullIsIllegal;
27import static org.slf4j.LoggerFactory.getLogger;
28
29/**
30 * Openstack keystone authentication codec used for serializing and
31 * de-serializing JSON string.
32 */
33public class OpenstackAuthCodec extends JsonCodec<OpenstackAuth> {
34
35 private final Logger log = getLogger(getClass());
36
37 private static final String VERSION = "version";
Jian Li27841662018-04-14 01:59:47 +090038 private static final String PROTOCOL = "protocol";
39 private static final String USERNAME = "username";
40 private static final String PASSWORD = "password";
41 private static final String PROJECT = "project";
42 private static final String PERSPECTIVE = "perspective";
43
44 private static final String MISSING_MESSAGE = " is required in OpenstackAuth";
45
46 @Override
47 public ObjectNode encode(OpenstackAuth auth, CodecContext context) {
48 checkNotNull(auth, "Openstack auth cannot be null");
49
50 ObjectNode result = context.mapper().createObjectNode()
51 .put(VERSION, auth.version())
Jian Li27841662018-04-14 01:59:47 +090052 .put(PROTOCOL, auth.protocol().name())
53 .put(USERNAME, auth.username())
54 .put(PASSWORD, auth.password())
55 .put(PROJECT, auth.project());
56
57 if (auth.perspective() != null) {
58 result.put(PERSPECTIVE, auth.perspective().name());
59 }
60
61 return result;
62 }
63
64 @Override
65 public OpenstackAuth decode(ObjectNode json, CodecContext context) {
66 if (json == null || !json.isObject()) {
67 return null;
68 }
69
70 String version = nullIsIllegal(json.get(VERSION).asText(),
71 VERSION + MISSING_MESSAGE);
Jian Li27841662018-04-14 01:59:47 +090072 String protocol = nullIsIllegal(json.get(PROTOCOL).asText(),
73 PROTOCOL + MISSING_MESSAGE);
74 String username = nullIsIllegal(json.get(USERNAME).asText(),
75 USERNAME + MISSING_MESSAGE);
76 String password = nullIsIllegal(json.get(PASSWORD).asText(),
77 PASSWORD + MISSING_MESSAGE);
78 String project = nullIsIllegal(json.get(PROJECT).asText(),
79 PROJECT + MISSING_MESSAGE);
80
81 DefaultOpenstackAuth.Builder authBuilder = DefaultOpenstackAuth.builder()
82 .version(version)
Jian Li27841662018-04-14 01:59:47 +090083 .protocol(OpenstackAuth.Protocol.valueOf(protocol))
84 .username(username)
85 .password(password)
86 .project(project);
87
88 if (json.get(PERSPECTIVE) != null) {
89 authBuilder.perspective(
90 OpenstackAuth.Perspective.valueOf(json.get(PERSPECTIVE).asText()));
91 }
92
93 return authBuilder.build();
94 }
95}