blob: 5556d9c27bbd68531af6a1e2c07b747964a485d6 [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;
22import org.onosproject.openstacknode.impl.DefaultOpenstackAuth;
23import 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";
38 private static final String PORT = "port";
39 private static final String PROTOCOL = "protocol";
40 private static final String USERNAME = "username";
41 private static final String PASSWORD = "password";
42 private static final String PROJECT = "project";
43 private static final String PERSPECTIVE = "perspective";
44
45 private static final String MISSING_MESSAGE = " is required in OpenstackAuth";
46
47 @Override
48 public ObjectNode encode(OpenstackAuth auth, CodecContext context) {
49 checkNotNull(auth, "Openstack auth cannot be null");
50
51 ObjectNode result = context.mapper().createObjectNode()
52 .put(VERSION, auth.version())
53 .put(PORT, auth.port())
54 .put(PROTOCOL, auth.protocol().name())
55 .put(USERNAME, auth.username())
56 .put(PASSWORD, auth.password())
57 .put(PROJECT, auth.project());
58
59 if (auth.perspective() != null) {
60 result.put(PERSPECTIVE, auth.perspective().name());
61 }
62
63 return result;
64 }
65
66 @Override
67 public OpenstackAuth decode(ObjectNode json, CodecContext context) {
68 if (json == null || !json.isObject()) {
69 return null;
70 }
71
72 String version = nullIsIllegal(json.get(VERSION).asText(),
73 VERSION + MISSING_MESSAGE);
74 Integer port = nullIsIllegal(json.get(PORT).asInt(),
75 PORT + MISSING_MESSAGE);
76 String protocol = nullIsIllegal(json.get(PROTOCOL).asText(),
77 PROTOCOL + MISSING_MESSAGE);
78 String username = nullIsIllegal(json.get(USERNAME).asText(),
79 USERNAME + MISSING_MESSAGE);
80 String password = nullIsIllegal(json.get(PASSWORD).asText(),
81 PASSWORD + MISSING_MESSAGE);
82 String project = nullIsIllegal(json.get(PROJECT).asText(),
83 PROJECT + MISSING_MESSAGE);
84
85 DefaultOpenstackAuth.Builder authBuilder = DefaultOpenstackAuth.builder()
86 .version(version)
87 .port(port)
88 .protocol(OpenstackAuth.Protocol.valueOf(protocol))
89 .username(username)
90 .password(password)
91 .project(project);
92
93 if (json.get(PERSPECTIVE) != null) {
94 authBuilder.perspective(
95 OpenstackAuth.Perspective.valueOf(json.get(PERSPECTIVE).asText()));
96 }
97
98 return authBuilder.build();
99 }
100}