blob: 3e8156d01c41e7c5d6083a28d3e83edee54acac9 [file] [log] [blame]
Daniel Parkd02d7bd2018-08-23 23:04:31 +09001/*
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 */
16package org.onosproject.openstacknode.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.openstacknode.api.DpdkConfig;
24import org.onosproject.openstacknode.api.DpdkInterface;
25import org.onosproject.openstacknode.impl.DefaultDpdkConfig;
26
27import java.util.ArrayList;
28import java.util.List;
29import java.util.stream.IntStream;
30
31import static org.onlab.util.Tools.nullIsIllegal;
32
33/**
34 * dpdk config codec used for serializing and de-serializing JSON string.
35 */
36public 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}