blob: b63b555a345fba32bc0857b16c27bc6dc086ce92 [file] [log] [blame]
Jian Li1c10cf22021-03-05 01:32:04 +09001/*
2 * Copyright 2021-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.kubevirtnetworking.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onlab.packet.IpPrefix;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.kubevirtnetworking.api.DefaultKubevirtSecurityGroupRule;
24import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupRule;
25import org.slf4j.Logger;
26
27import static com.google.common.base.Preconditions.checkNotNull;
28import static org.onlab.util.Tools.nullIsIllegal;
29import static org.slf4j.LoggerFactory.getLogger;
30
31/**
32 * Kubevirt security group rule codec used for serializing and de-serializing JSON string.
33 */
34public final class KubevirtSecurityGroupRuleCodec extends JsonCodec<KubevirtSecurityGroupRule> {
35
36 private final Logger log = getLogger(getClass());
37
38 private static final String ID = "id";
39 private static final String SECURITY_GROUP_ID = "securityGroupId";
40 private static final String DIRECTION = "direction";
41 private static final String ETHER_TYPE = "etherType";
42 private static final String PORT_RANGE_MAX = "portRangeMax";
43 private static final String PORT_RANGE_MIN = "portRangeMin";
44 private static final String PROTOCOL = "protocol";
45 private static final String REMOTE_IP_PREFIX = "remoteIpPrefix";
46 private static final String REMOTE_GROUP_ID = "remoteGroupId";
47
48 private static final String MISSING_MESSAGE = " is required in KubevirtSecurityGroupRule";
49
50 @Override
51 public ObjectNode encode(KubevirtSecurityGroupRule sgRule, CodecContext context) {
52 checkNotNull(sgRule, "Kubevirt security group rule cannot be null");
53
54 ObjectNode result = context.mapper().createObjectNode()
55 .put(ID, sgRule.id())
56 .put(SECURITY_GROUP_ID, sgRule.securityGroupId())
57 .put(DIRECTION, sgRule.direction());
58
59 if (sgRule.etherType() != null) {
60 result.put(ETHER_TYPE, sgRule.etherType());
61 }
62
63 if (sgRule.portRangeMax() != null) {
64 result.put(PORT_RANGE_MAX, sgRule.portRangeMax());
65 }
66
67 if (sgRule.portRangeMin() != null) {
68 result.put(PORT_RANGE_MIN, sgRule.portRangeMin());
69 }
70
71 if (sgRule.protocol() != null) {
72 result.put(PROTOCOL, sgRule.protocol());
73 }
74
75 if (sgRule.remoteIpPrefix() != null) {
76 result.put(REMOTE_IP_PREFIX, sgRule.remoteIpPrefix().toString());
77 }
78
79 if (sgRule.remoteGroupId() != null) {
80 result.put(REMOTE_GROUP_ID, sgRule.remoteGroupId());
81 }
82
83 return result;
84 }
85
86 @Override
87 public KubevirtSecurityGroupRule decode(ObjectNode json, CodecContext context) {
88 if (json == null || !json.isObject()) {
89 return null;
90 }
91
92 String id = nullIsIllegal(json.get(ID).asText(), ID + MISSING_MESSAGE);
93 String securityGroupId = nullIsIllegal(json.get(SECURITY_GROUP_ID).asText(),
94 SECURITY_GROUP_ID + MISSING_MESSAGE);
95 String direction = nullIsIllegal(json.get(DIRECTION).asText(),
96 DIRECTION + MISSING_MESSAGE);
97
98 KubevirtSecurityGroupRule.Builder builder = DefaultKubevirtSecurityGroupRule.builder()
99 .id(id)
100 .securityGroupId(securityGroupId)
101 .direction(direction);
102
103 JsonNode etherType = json.get(ETHER_TYPE);
104 if (etherType != null) {
105 builder.etherType(etherType.asText());
106 }
107
108 JsonNode portRangeMax = json.get(PORT_RANGE_MAX);
109 if (portRangeMax != null) {
110 builder.portRangeMax(portRangeMax.asInt());
111 }
112
113 JsonNode portRangeMin = json.get(PORT_RANGE_MIN);
114 if (portRangeMin != null) {
115 builder.portRangeMin(portRangeMin.asInt());
116 }
117
118 JsonNode protocol = json.get(PROTOCOL);
119 if (protocol != null) {
120 builder.protocol(protocol.asText());
121 }
122
123 JsonNode remoteIpPrefix = json.get(REMOTE_IP_PREFIX);
124 if (remoteIpPrefix != null) {
125 builder.remoteIpPrefix(IpPrefix.valueOf(remoteIpPrefix.asText()));
126 }
127
128 JsonNode remoteGroupId = json.get(REMOTE_GROUP_ID);
129 if (remoteGroupId != null) {
130 builder.remoteGroupId(remoteGroupId.asText());
131 }
132
133 return builder.build();
134 }
135}