Daniel Park | d02d7bd | 2018-08-23 23:04:31 +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.JsonNode; |
| 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.DpdkInterface; |
| 23 | import org.onosproject.openstacknode.api.DpdkInterface.Type; |
| 24 | import org.onosproject.openstacknode.impl.DefaultDpdkInterface; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | import static org.onlab.util.Tools.nullIsIllegal; |
| 28 | |
| 29 | /** |
| 30 | * DPDK interface codec used for serializing and de-serializing JSON string. |
| 31 | */ |
| 32 | public class DpdkInterfaceCodec extends JsonCodec<DpdkInterface> { |
| 33 | private static final String DEVICE_NAME = "deviceName"; |
| 34 | private static final String INTF = "intf"; |
| 35 | private static final String PCI_ADDRESS = "pciAddress"; |
| 36 | private static final String TYPE = "type"; |
| 37 | private static final String MTU = "mtu"; |
| 38 | |
| 39 | private static final String NOT_NULL_MESSAGE = "dpdk interface cannot be null"; |
| 40 | private static final String MISSING_MESSAGE = " is required in DpdkInterface"; |
| 41 | |
| 42 | @Override |
| 43 | public ObjectNode encode(DpdkInterface entity, CodecContext context) { |
| 44 | checkNotNull(entity, NOT_NULL_MESSAGE); |
| 45 | |
| 46 | return context.mapper().createObjectNode() |
| 47 | .put(DEVICE_NAME, entity.deviceName()) |
| 48 | .put(INTF, entity.intf()) |
| 49 | .put(PCI_ADDRESS, entity.pciAddress()) |
| 50 | .put(TYPE, entity.type().name()) |
| 51 | .put(MTU, entity.mtu().toString()); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public DpdkInterface decode(ObjectNode json, CodecContext context) { |
| 56 | if (json == null || !json.isObject()) { |
| 57 | return null; |
| 58 | } |
| 59 | |
| 60 | String deviceName = nullIsIllegal(json.get(DEVICE_NAME).asText(), |
| 61 | DEVICE_NAME + MISSING_MESSAGE); |
| 62 | String intf = nullIsIllegal(json.get(INTF).asText(), |
| 63 | INTF + MISSING_MESSAGE); |
| 64 | String pciAddress = nullIsIllegal(json.get(PCI_ADDRESS).asText(), |
| 65 | PCI_ADDRESS + MISSING_MESSAGE); |
| 66 | String typeString = nullIsIllegal(json.get(TYPE).asText(), |
| 67 | TYPE + MISSING_MESSAGE); |
| 68 | |
| 69 | Type type; |
| 70 | try { |
| 71 | type = Type.valueOf(typeString.toUpperCase()); |
| 72 | } catch (IllegalArgumentException e) { |
| 73 | throw new IllegalArgumentException(TYPE + MISSING_MESSAGE); |
| 74 | } |
| 75 | |
| 76 | DpdkInterface.Builder builder = DefaultDpdkInterface.builder() |
| 77 | .deviceName(deviceName) |
| 78 | .intf(intf) |
| 79 | .pciAddress(pciAddress) |
| 80 | .type(type); |
| 81 | |
| 82 | JsonNode mtuJson = json.get(MTU); |
| 83 | |
| 84 | if (mtuJson != null) { |
| 85 | builder.mtu(Long.parseLong(mtuJson.asText())); |
| 86 | } |
| 87 | |
| 88 | return builder.build(); |
| 89 | } |
| 90 | } |