Jian Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.openstacknode.codec; |
| 18 | |
Jian Li | 92b6f29 | 2018-09-06 10:57:18 +0900 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
Jian Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
Jian Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 23 | import org.onosproject.openstacknode.api.DefaultNeutronConfig; |
Jian Li | 92b6f29 | 2018-09-06 10:57:18 +0900 | [diff] [blame] | 24 | import org.onosproject.openstacknode.api.NeutronConfig; |
Jian Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 25 | |
| 26 | import static org.onlab.util.Tools.nullIsIllegal; |
| 27 | |
| 28 | /** |
| 29 | * Neutron config codec used for serializing and de-serializing JSON string. |
| 30 | */ |
| 31 | public 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 Li | 92b6f29 | 2018-09-06 10:57:18 +0900 | [diff] [blame] | 35 | private static final String NOVA_METADATA_IP = "novaMetadataIp"; |
| 36 | private static final String NOVA_METADATA_PORT = "novaMetadataPort"; |
Jian Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 37 | |
Jian Li | 92b6f29 | 2018-09-06 10:57:18 +0900 | [diff] [blame] | 38 | private static final String MISSING_MESSAGE = " is required in NeutronConfig"; |
Jian Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 39 | |
| 40 | @Override |
| 41 | public ObjectNode encode(NeutronConfig entity, CodecContext context) { |
Jian Li | 92b6f29 | 2018-09-06 10:57:18 +0900 | [diff] [blame] | 42 | ObjectNode node = context.mapper().createObjectNode(); |
| 43 | node.put(USE_METADATA_PROXY, entity.useMetadataProxy()) |
Jian Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 44 | .put(METADATA_PROXY_SECRET, entity.metadataProxySecret()); |
Jian Li | 92b6f29 | 2018-09-06 10:57:18 +0900 | [diff] [blame] | 45 | |
| 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 Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 55 | } |
| 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 Li | 92b6f29 | 2018-09-06 10:57:18 +0900 | [diff] [blame] | 69 | NeutronConfig.Builder builder = DefaultNeutronConfig.builder() |
Jian Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 70 | .useMetadataProxy(useMetadataProxy) |
Jian Li | 92b6f29 | 2018-09-06 10:57:18 +0900 | [diff] [blame] | 71 | .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 Li | c704b67 | 2018-09-04 18:52:53 +0900 | [diff] [blame] | 86 | } |
| 87 | } |