Jian Li | e631216 | 2018-03-21 21:41:00 +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.openstacknode.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onosproject.codec.CodecContext; |
| 20 | import org.onosproject.codec.JsonCodec; |
| 21 | import org.onosproject.openstacknode.api.OpenstackPhyInterface; |
| 22 | import org.onosproject.openstacknode.impl.DefaultOpenstackPhyInterface; |
| 23 | import org.slf4j.Logger; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | import static org.onlab.util.Tools.nullIsIllegal; |
| 27 | import static org.slf4j.LoggerFactory.getLogger; |
| 28 | |
| 29 | /** |
| 30 | * Openstack physical interface codec used for serializing and de-serializing JSON string. |
| 31 | */ |
| 32 | public final class OpenstackPhyInterfaceCodec extends JsonCodec<OpenstackPhyInterface> { |
| 33 | |
| 34 | private final Logger log = getLogger(getClass()); |
| 35 | |
| 36 | private static final String NETWORK = "network"; |
| 37 | private static final String INTERFACE = "intf"; |
| 38 | |
| 39 | private static final String MISSING_MESSAGE = " is required in OpenstackPhyInterface"; |
| 40 | |
| 41 | @Override |
| 42 | public ObjectNode encode(OpenstackPhyInterface phyIntf, CodecContext context) { |
| 43 | checkNotNull(phyIntf, "Openstack physical interface cannot be null"); |
| 44 | |
| 45 | return context.mapper().createObjectNode() |
| 46 | .put(NETWORK, phyIntf.network()) |
| 47 | .put(INTERFACE, phyIntf.intf()); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public OpenstackPhyInterface decode(ObjectNode json, CodecContext context) { |
| 52 | if (json == null || !json.isObject()) { |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | String network = nullIsIllegal(json.get(NETWORK).asText(), |
| 57 | NETWORK + MISSING_MESSAGE); |
| 58 | String intf = nullIsIllegal(json.get(INTERFACE).asText(), |
| 59 | INTERFACE + MISSING_MESSAGE); |
| 60 | |
| 61 | return DefaultOpenstackPhyInterface.builder() |
| 62 | .network(network) |
| 63 | .intf(intf) |
| 64 | .build(); |
| 65 | } |
| 66 | } |