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