Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +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 | package org.onosproject.openstackvtap.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
| 22 | import org.onosproject.openstackvtap.api.OpenstackVtapNetwork; |
| 23 | import org.onosproject.openstackvtap.impl.DefaultOpenstackVtapNetwork; |
| 24 | import org.slf4j.Logger; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | import static com.google.common.base.Preconditions.checkState; |
| 28 | import static org.slf4j.LoggerFactory.getLogger; |
| 29 | |
| 30 | /** |
| 31 | * Openstack vtap network codec used for serializing and de-serializing JSON string. |
| 32 | */ |
| 33 | public final class OpenstackVtapNetworkCodec extends JsonCodec<OpenstackVtapNetwork> { |
| 34 | |
| 35 | private final Logger log = getLogger(getClass()); |
| 36 | |
| 37 | private static final String MODE = "mode"; |
| 38 | private static final String NETWORK_ID = "networkId"; |
| 39 | private static final String SERVER_IP = "serverIp"; |
| 40 | private static final String NODES = "nodes"; |
| 41 | |
| 42 | private static final String JSON_NULL_MESSAGE = "% cannot be null"; |
| 43 | private static final String JSON_MISSING_MESSAGE = "% is required"; |
| 44 | private static final String JSON_TYPE_MESSAGE = "% is not json object type"; |
| 45 | |
| 46 | @Override |
| 47 | public ObjectNode encode(OpenstackVtapNetwork network, CodecContext context) { |
| 48 | checkNotNull(network, JSON_NULL_MESSAGE, "OpenstackVtapNetwork object"); |
| 49 | |
| 50 | ObjectNode result = context.mapper().createObjectNode() |
| 51 | .put(MODE, network.mode().toString()) |
| 52 | .put(NETWORK_ID, network.networkId()) |
| 53 | .put(SERVER_IP, network.serverIp().toString()); |
| 54 | |
| 55 | return result; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public OpenstackVtapNetwork decode(ObjectNode json, CodecContext context) { |
| 60 | checkNotNull(json, JSON_NULL_MESSAGE, "OpenstackVtapNetwork json"); |
| 61 | checkState(json.isObject(), JSON_TYPE_MESSAGE, "OpenstackVtapNetwork json"); |
| 62 | |
| 63 | DefaultOpenstackVtapNetwork.Builder builder = DefaultOpenstackVtapNetwork.builder() |
| 64 | .mode(OpenstackVtapNetwork.Mode.valueOf(checkNotNull(json.get(MODE).asText(null), |
| 65 | JSON_MISSING_MESSAGE, MODE))) |
| 66 | .networkId(json.get(NETWORK_ID).asInt(0)) |
| 67 | .serverIp(IpAddress.valueOf(checkNotNull(json.get(SERVER_IP).asText(null), |
| 68 | JSON_MISSING_MESSAGE, SERVER_IP))); |
| 69 | |
| 70 | log.debug("OpenstackVtapNetwork is {}", builder.build().toString()); |
| 71 | return builder.build(); |
| 72 | } |
| 73 | } |