blob: 48ca3ac92ef1b2009c3573fa71f5a22fe08686bc [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
Jian Li92b6f292018-09-06 10:57:18 +090019import com.fasterxml.jackson.databind.JsonNode;
Jian Lic704b672018-09-04 18:52:53 +090020import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
Jian Lic704b672018-09-04 18:52:53 +090023import org.onosproject.openstacknode.api.DefaultNeutronConfig;
Jian Li92b6f292018-09-06 10:57:18 +090024import org.onosproject.openstacknode.api.NeutronConfig;
Jian Lic704b672018-09-04 18:52:53 +090025
26import static org.onlab.util.Tools.nullIsIllegal;
27
28/**
29 * Neutron config codec used for serializing and de-serializing JSON string.
30 */
31public final class NeutronConfigCodec extends JsonCodec<NeutronConfig> {
32
33 private static final String USE_METADATA_PROXY = "useMetadataProxy";
34 private static final String METADATA_PROXY_SECRET = "metadataProxySecret";
Jian Li92b6f292018-09-06 10:57:18 +090035 private static final String NOVA_METADATA_IP = "novaMetadataIp";
36 private static final String NOVA_METADATA_PORT = "novaMetadataPort";
Jian Lic704b672018-09-04 18:52:53 +090037
Jian Li92b6f292018-09-06 10:57:18 +090038 private static final String MISSING_MESSAGE = " is required in NeutronConfig";
Jian Lic704b672018-09-04 18:52:53 +090039
40 @Override
41 public ObjectNode encode(NeutronConfig entity, CodecContext context) {
Jian Li92b6f292018-09-06 10:57:18 +090042 ObjectNode node = context.mapper().createObjectNode();
43 node.put(USE_METADATA_PROXY, entity.useMetadataProxy())
Jian Lic704b672018-09-04 18:52:53 +090044 .put(METADATA_PROXY_SECRET, entity.metadataProxySecret());
Jian Li92b6f292018-09-06 10:57:18 +090045
46 if (entity.novaMetadataIp() != null) {
47 node.put(NOVA_METADATA_IP, entity.novaMetadataIp());
48 }
49
50 if (entity.novaMetadataPort() != null) {
51 node.put(NOVA_METADATA_PORT, entity.novaMetadataPort());
52 }
53
54 return node;
Jian Lic704b672018-09-04 18:52:53 +090055 }
56
57 @Override
58 public NeutronConfig decode(ObjectNode json, CodecContext context) {
59 if (json == null || !json.isObject()) {
60 return null;
61 }
62
63 boolean useMetadataProxy = nullIsIllegal(json.get(USE_METADATA_PROXY).asBoolean(),
64 USE_METADATA_PROXY + MISSING_MESSAGE);
65
66 String metadataProxySecret = nullIsIllegal(json.get(METADATA_PROXY_SECRET).asText(),
67 METADATA_PROXY_SECRET + MISSING_MESSAGE);
68
Jian Li92b6f292018-09-06 10:57:18 +090069 NeutronConfig.Builder builder = DefaultNeutronConfig.builder()
Jian Lic704b672018-09-04 18:52:53 +090070 .useMetadataProxy(useMetadataProxy)
Jian Li92b6f292018-09-06 10:57:18 +090071 .metadataProxySecret(metadataProxySecret);
72
73 JsonNode novaMetadataIp = json.get(NOVA_METADATA_IP);
74
75 if (novaMetadataIp != null) {
76 builder.novaMetadataIp(novaMetadataIp.asText());
77 }
78
79 JsonNode novaMetadataPort = json.get(NOVA_METADATA_PORT);
80
81 if (novaMetadataPort != null) {
82 builder.novaMetadataPort(novaMetadataPort.asInt());
83 }
84
85 return builder.build();
Jian Lic704b672018-09-04 18:52:53 +090086 }
87}