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 | |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
| 22 | import org.onosproject.openstacknode.api.NeutronConfig; |
| 23 | import org.onosproject.openstacknode.api.DefaultNeutronConfig; |
| 24 | |
| 25 | import static org.onlab.util.Tools.nullIsIllegal; |
| 26 | |
| 27 | /** |
| 28 | * Neutron config codec used for serializing and de-serializing JSON string. |
| 29 | */ |
| 30 | public final class NeutronConfigCodec extends JsonCodec<NeutronConfig> { |
| 31 | |
| 32 | private static final String USE_METADATA_PROXY = "useMetadataProxy"; |
| 33 | private static final String METADATA_PROXY_SECRET = "metadataProxySecret"; |
| 34 | |
| 35 | private static final String MISSING_MESSAGE = " is required in OpenstackNode"; |
| 36 | |
| 37 | @Override |
| 38 | public ObjectNode encode(NeutronConfig entity, CodecContext context) { |
| 39 | return context.mapper().createObjectNode() |
| 40 | .put(USE_METADATA_PROXY, entity.useMetadataProxy()) |
| 41 | .put(METADATA_PROXY_SECRET, entity.metadataProxySecret()); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public NeutronConfig decode(ObjectNode json, CodecContext context) { |
| 46 | if (json == null || !json.isObject()) { |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | boolean useMetadataProxy = nullIsIllegal(json.get(USE_METADATA_PROXY).asBoolean(), |
| 51 | USE_METADATA_PROXY + MISSING_MESSAGE); |
| 52 | |
| 53 | String metadataProxySecret = nullIsIllegal(json.get(METADATA_PROXY_SECRET).asText(), |
| 54 | METADATA_PROXY_SECRET + MISSING_MESSAGE); |
| 55 | |
| 56 | return DefaultNeutronConfig.builder() |
| 57 | .useMetadataProxy(useMetadataProxy) |
| 58 | .metadataProxySecret(metadataProxySecret) |
| 59 | .build(); |
| 60 | } |
| 61 | } |