blob: 95da63a69e53bab5a3d0f24c274533639a9b01a8 [file] [log] [blame]
Jian Lic704b672018-09-04 18:52:53 +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 */
16
17package org.onosproject.openstacknode.codec;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.openstacknode.api.KeystoneConfig;
24import org.onosproject.openstacknode.api.OpenstackAuth;
25import org.onosproject.openstacknode.api.DefaultKeystoneConfig;
26
27import static org.onlab.util.Tools.nullIsIllegal;
28
29/**
30 * Keystone config codec used for serializing and de-serializing JSON string.
31 */
32public final class KeystoneConfigCodec extends JsonCodec<KeystoneConfig> {
33
34 private static final String ENDPOINT = "endpoint";
35 private static final String AUTHENTICATION = "authentication";
36
37 private static final String MISSING_MESSAGE = " is required in OpenstackNode";
38
39 @Override
40 public ObjectNode encode(KeystoneConfig entity, CodecContext context) {
41 ObjectNode result = context.mapper().createObjectNode()
42 .put(ENDPOINT, entity.endpoint());
43
44 ObjectNode authJson = context.codec(OpenstackAuth.class)
45 .encode(entity.authentication(), context);
46 result.set(AUTHENTICATION, authJson);
47
48 return result;
49 }
50
51 @Override
52 public KeystoneConfig decode(ObjectNode json, CodecContext context) {
53 if (json == null || !json.isObject()) {
54 return null;
55 }
56
57 String endpoint = nullIsIllegal(json.get(ENDPOINT).asText(),
58 ENDPOINT + MISSING_MESSAGE);
59
60 // parse authentication
61 JsonNode authJson = nullIsIllegal(json.get(AUTHENTICATION),
62 AUTHENTICATION + MISSING_MESSAGE);
63
64
65 final JsonCodec<OpenstackAuth> authCodec = context.codec(OpenstackAuth.class);
66 OpenstackAuth auth = authCodec.decode((ObjectNode) authJson.deepCopy(), context);
67
68 return DefaultKeystoneConfig.builder()
69 .endpoint(endpoint)
70 .authentication(auth)
71 .build();
72 }
73}