blob: 07e85dafafa58f04ccb5bf084d088ea2eca65d62 [file] [log] [blame]
Jian Li9ee9c8b2019-01-24 11:48:12 +09001/*
2 * Copyright 2019-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.k8snetworking.codec;
17
Jian Li2778ffa2019-05-07 13:21:52 +090018import com.fasterxml.jackson.databind.JsonNode;
Jian Li9ee9c8b2019-01-24 11:48:12 +090019import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Li9ee9c8b2019-01-24 11:48:12 +090020import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.onosproject.k8snetworking.api.DefaultK8sNetwork;
23import org.onosproject.k8snetworking.api.K8sNetwork;
24import org.slf4j.Logger;
25
26import static com.google.common.base.Preconditions.checkNotNull;
27import static org.onlab.util.Tools.nullIsIllegal;
28import static org.slf4j.LoggerFactory.getLogger;
29
30/**
31 * Kubernetes network codec used for serializing and de-serializing JSON string.
32 */
33public final class K8sNetworkCodec extends JsonCodec<K8sNetwork> {
34
35 private final Logger log = getLogger(getClass());
36
37 private static final String NETWORK_ID = "networkId";
Jian Li7e8f57e2019-01-24 18:31:03 +090038 private static final String NAME = "name";
Jian Li9ee9c8b2019-01-24 11:48:12 +090039 private static final String TYPE = "type";
40 private static final String MTU = "mtu";
41 private static final String SEGMENT_ID = "segmentId";
Jian Li9ee9c8b2019-01-24 11:48:12 +090042 private static final String CIDR = "cidr";
43
44 private static final String MISSING_MESSAGE = " is required in K8sNetwork";
45
46 @Override
47 public ObjectNode encode(K8sNetwork network, CodecContext context) {
48 checkNotNull(network, "Kubernetes network cannot be null");
49
50 ObjectNode result = context.mapper().createObjectNode()
51 .put(NETWORK_ID, network.networkId())
Jian Li7e8f57e2019-01-24 18:31:03 +090052 .put(NAME, network.name())
Jian Li9ee9c8b2019-01-24 11:48:12 +090053 .put(CIDR, network.cidr());
54
Jian Li2778ffa2019-05-07 13:21:52 +090055 if (network.type() != null) {
56 result.put(TYPE, network.type().name());
57 }
58
59 if (network.segmentId() != null) {
60 result.put(SEGMENT_ID, network.segmentId());
61 }
62
Jian Li9ee9c8b2019-01-24 11:48:12 +090063 if (network.mtu() != null) {
64 result.put(MTU, network.mtu());
65 }
66
67 return result;
68 }
69
70 @Override
71 public K8sNetwork decode(ObjectNode json, CodecContext context) {
72 if (json == null || !json.isObject()) {
73 return null;
74 }
75
76 String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(),
77 NETWORK_ID + MISSING_MESSAGE);
Jian Li7e8f57e2019-01-24 18:31:03 +090078 String name = nullIsIllegal(json.get(NAME).asText(),
79 NAME + MISSING_MESSAGE);
Jian Li9ee9c8b2019-01-24 11:48:12 +090080 String cidr = nullIsIllegal(json.get(CIDR).asText(),
81 CIDR + MISSING_MESSAGE);
82
Jian Li2778ffa2019-05-07 13:21:52 +090083 JsonNode type = json.get(TYPE);
84 JsonNode segmentId = json.get(SEGMENT_ID);
85
Jian Li9ee9c8b2019-01-24 11:48:12 +090086 DefaultK8sNetwork.Builder networkBuilder = DefaultK8sNetwork.builder()
87 .networkId(networkId)
Jian Li7e8f57e2019-01-24 18:31:03 +090088 .name(name)
Jian Li9ee9c8b2019-01-24 11:48:12 +090089 .cidr(cidr);
90
Jian Li2778ffa2019-05-07 13:21:52 +090091 if (type != null) {
92 networkBuilder.type(K8sNetwork.Type.valueOf(type.asText()));
93 }
94
95 if (segmentId != null) {
96 networkBuilder.segmentId(segmentId.asText());
97 }
98
Jian Li9ee9c8b2019-01-24 11:48:12 +090099 if (json.get(MTU) != null) {
100 networkBuilder.mtu(json.get(MTU).asInt());
101 }
102
103 return networkBuilder.build();
104 }
105}