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.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.openstacknode.api.DpdkConfig; |
| 24 | import org.onosproject.openstacknode.api.DpdkInterface; |
| 25 | import org.onosproject.openstacknode.impl.DefaultDpdkConfig; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.List; |
| 29 | import java.util.stream.IntStream; |
| 30 | |
| 31 | import static org.onlab.util.Tools.nullIsIllegal; |
| 32 | |
| 33 | /** |
| 34 | * dpdk config codec used for serializing and de-serializing JSON string. |
| 35 | */ |
| 36 | public class DpdkConfigCodec extends JsonCodec<DpdkConfig> { |
| 37 | private static final String DATA_PATH_TYPE = "datapathType"; |
| 38 | private static final String SOCKET_DIR = "socketDir"; |
| 39 | private static final String DPDK_INTFS = "dpdkIntfs"; |
| 40 | |
| 41 | private static final String MISSING_MESSAGE = " is required in DpdkInterface"; |
| 42 | |
| 43 | @Override |
| 44 | public ObjectNode encode(DpdkConfig entity, CodecContext context) { |
| 45 | ObjectNode result = context.mapper().createObjectNode() |
| 46 | .put(DATA_PATH_TYPE, entity.datapathType().name()); |
| 47 | |
| 48 | if (entity.socketDir() != null) { |
| 49 | result.put(SOCKET_DIR, entity.socketDir()); |
| 50 | } |
| 51 | |
| 52 | ArrayNode dpdkIntfs = context.mapper().createArrayNode(); |
| 53 | entity.dpdkIntfs().forEach(dpdkIntf -> { |
| 54 | ObjectNode dpdkIntfJson = context.codec(DpdkInterface.class) |
| 55 | .encode(dpdkIntf, context); |
| 56 | dpdkIntfs.add(dpdkIntfJson); |
| 57 | }); |
| 58 | result.set(DPDK_INTFS, dpdkIntfs); |
| 59 | |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public DpdkConfig decode(ObjectNode json, CodecContext context) { |
| 65 | if (json == null || !json.isObject()) { |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | String datapathType = nullIsIllegal(json.get(DATA_PATH_TYPE).asText(), |
| 70 | DATA_PATH_TYPE + MISSING_MESSAGE); |
| 71 | |
| 72 | DefaultDpdkConfig.Builder builder = DefaultDpdkConfig.builder() |
| 73 | .datapathType(DpdkConfig.DatapathType.valueOf(datapathType.toUpperCase())); |
| 74 | |
| 75 | if (json.get(SOCKET_DIR) != null) { |
| 76 | builder.socketDir(json.get(SOCKET_DIR).asText()); |
| 77 | } |
| 78 | |
| 79 | List<DpdkInterface> dpdkInterfaces = new ArrayList<>(); |
| 80 | JsonNode dpdkIntfsJson = json.get(DPDK_INTFS); |
| 81 | |
| 82 | if (dpdkIntfsJson != null) { |
| 83 | final JsonCodec<DpdkInterface> |
| 84 | dpdkIntfCodec = context.codec(DpdkInterface.class); |
| 85 | |
| 86 | IntStream.range(0, dpdkIntfsJson.size()).forEach(i -> { |
| 87 | ObjectNode intfJson = get(dpdkIntfsJson, i); |
| 88 | dpdkInterfaces.add(dpdkIntfCodec.decode(intfJson, context)); |
| 89 | }); |
| 90 | } |
| 91 | builder.dpdkIntfs(dpdkInterfaces); |
| 92 | |
| 93 | return builder.build(); |
| 94 | } |
| 95 | } |