blob: 52926044a0b393d43c23abaebd64ce1d6980a7e3 [file] [log] [blame]
Hyunsun Moone5a1fc32016-09-02 16:01:01 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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.opencord.cordvtn.codec;
17
18import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Hyunsun Moon187bf532017-01-19 10:57:40 +090020import com.google.common.base.Strings;
21import com.google.common.collect.ImmutableSet;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070022import com.google.common.collect.Sets;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070023import org.onlab.packet.IpAddress;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.VlanId;
26import org.onosproject.codec.CodecContext;
27import org.onosproject.codec.JsonCodec;
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070028import org.opencord.cordvtn.api.net.AddressPair;
29import org.opencord.cordvtn.api.net.PortId;
30import org.opencord.cordvtn.api.net.ServicePort;
Hyunsun Moon187bf532017-01-19 10:57:40 +090031import org.opencord.cordvtn.impl.DefaultServicePort;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070032
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070033import java.util.Set;
34
Hyunsun Moon4a94c2e2016-10-21 17:37:05 -070035import static com.google.common.base.Preconditions.checkArgument;
36
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070037/**
38 * Service port JSON codec.
39 */
40public final class ServicePortCodec extends JsonCodec<ServicePort> {
41
42 private static final String ID = "id";
Hyunsun Moon187bf532017-01-19 10:57:40 +090043 private static final String NAME = "name";
44 private static final String NETWORK_ID = "network_id";
45 private static final String MAC_ADDRESS = "mac_address";
46 private static final String IP_ADDRESS = "ip_address";
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070047 private static final String VLAN_ID = "vlan_id";
48 private static final String FLOATING_ADDRESS_PAIRS = "floating_address_pairs";
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070049
Hyunsun Moon4a94c2e2016-10-21 17:37:05 -070050 private static final String ERR_JSON = "Invalid ServicePort received";
Hyunsun Moon187bf532017-01-19 10:57:40 +090051 private static final String ERR_ID = "Service port ID cannot be null";
Hyunsun Moon4a94c2e2016-10-21 17:37:05 -070052
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070053 @Override
54 public ObjectNode encode(ServicePort sport, CodecContext context) {
55 ObjectNode result = context.mapper().createObjectNode()
56 .put(ID, sport.id().id());
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070057
Hyunsun Moon187bf532017-01-19 10:57:40 +090058 if (sport.networkId() != null) {
59 result.put(NETWORK_ID, sport.networkId().id());
60 }
61 if (!Strings.isNullOrEmpty(sport.name())) {
62 result.put(NAME, sport.name());
63 }
64 if (sport.vlanId() != null) {
65 result.put(VLAN_ID, sport.vlanId().id());
66 }
67 if (sport.mac() != null) {
68 result.put(MAC_ADDRESS, sport.mac().toString());
69 }
70 if (sport.ip() != null) {
71 result.put(IP_ADDRESS, sport.ip().toString());
72 }
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070073 ArrayNode addressPairs = context.mapper().createArrayNode();
74 sport.addressPairs().forEach(pair -> {
75 ObjectNode pairJson = context.mapper().createObjectNode()
76 .put(IP_ADDRESS, pair.ip().toString())
77 .put(MAC_ADDRESS, pair.mac().toString());
78 addressPairs.add(pairJson);
79 });
80 result.set(FLOATING_ADDRESS_PAIRS, addressPairs);
Hyunsun Moon187bf532017-01-19 10:57:40 +090081
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070082 return result;
83 }
84
85 @Override
86 public ServicePort decode(ObjectNode json, CodecContext context) {
Hyunsun Moon187bf532017-01-19 10:57:40 +090087 validateJson(json);
88 ServicePort.Builder sportBuilder = DefaultServicePort.builder()
89 .id(PortId.of(json.get(ID).asText()));
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070090
Hyunsun Moon187bf532017-01-19 10:57:40 +090091 // TODO allow removing existing value when explicit null received
92 if (json.get(NAME) != null && !json.get(NAME).isNull()) {
93 sportBuilder.name(json.get(NAME).asText());
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070094 }
Hyunsun Moon187bf532017-01-19 10:57:40 +090095 if (json.get(MAC_ADDRESS) != null && !json.get(MAC_ADDRESS).isNull()) {
96 sportBuilder.mac(MacAddress.valueOf(json.get(MAC_ADDRESS).asText()));
97 }
98 if (json.get(IP_ADDRESS) != null && !json.get(IP_ADDRESS).isNull()) {
99 sportBuilder.ip(IpAddress.valueOf(json.get(IP_ADDRESS).asText()));
100 }
101 if (json.get(VLAN_ID) != null && !json.get(VLAN_ID).isNull()) {
102 sportBuilder.vlanId(VlanId.vlanId(json.get(VLAN_ID).asText()));
103 }
104 if (json.get(FLOATING_ADDRESS_PAIRS).isNull()) {
105 sportBuilder.addressPairs(ImmutableSet.of());
106 } else if (json.get(FLOATING_ADDRESS_PAIRS) != null) {
107 Set<AddressPair> addressPairs = Sets.newHashSet();
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700108 json.get(FLOATING_ADDRESS_PAIRS).forEach(pair -> {
109 AddressPair addrPair = AddressPair.of(
110 IpAddress.valueOf(pair.get(IP_ADDRESS).asText()),
111 MacAddress.valueOf(pair.get(MAC_ADDRESS).asText()));
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700112 addressPairs.add(addrPair);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700113 });
Hyunsun Moon187bf532017-01-19 10:57:40 +0900114 sportBuilder.addressPairs(addressPairs);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700115 }
Hyunsun Moon187bf532017-01-19 10:57:40 +0900116 return sportBuilder.build();
117 }
118
119 private void validateJson(ObjectNode json) {
120 checkArgument(json != null && json.isObject(), ERR_JSON);
121 checkArgument(json.get(ID) != null && !json.get(ID).isNull(), ERR_ID);
122
123 // allow explicit null for removing the existing value
124 if (json.get(NAME) != null && !json.get(NAME).isNull()) {
125 if (Strings.isNullOrEmpty(json.get(NAME).asText())) {
126 final String error = "Null or empty ServiceNetwork name received";
127 throw new IllegalArgumentException(error);
128 }
129 }
130
131 if (json.get(MAC_ADDRESS) != null && !json.get(MAC_ADDRESS).isNull()) {
132 try {
133 MacAddress.valueOf(json.get(MAC_ADDRESS).asText());
134 } catch (IllegalArgumentException e) {
135 final String error = "Invalid ServicePort MAC address received: ";
136 throw new IllegalArgumentException(error + json.get(MAC_ADDRESS).asText());
137 }
138 }
139
140 if (json.get(IP_ADDRESS) != null && !json.get(IP_ADDRESS).isNull()) {
141 try {
142 IpAddress.valueOf(json.get(IP_ADDRESS).asText());
143 } catch (IllegalArgumentException e) {
144 final String error = "Invalid ServicePort IP address received: ";
145 throw new IllegalArgumentException(error + json.get(IP_ADDRESS).asText());
146 }
147 }
148
149 if (json.get(VLAN_ID) != null && !json.get(VLAN_ID).isNull()) {
150 try {
151 VlanId.vlanId(json.get(VLAN_ID).asText());
152 } catch (IllegalArgumentException e) {
153 final String error = "Invalid VLAN ID is received: ";
154 throw new IllegalArgumentException(error + json.get(VLAN_ID).asText());
155 }
156 }
157
158 if (json.get(FLOATING_ADDRESS_PAIRS) != null &&
159 !json.get(FLOATING_ADDRESS_PAIRS).isNull()) {
160 json.get(FLOATING_ADDRESS_PAIRS).forEach(pair -> {
161 if (pair.get(IP_ADDRESS) == null ||
162 pair.get(IP_ADDRESS).isNull() ||
163 pair.get(MAC_ADDRESS) == null ||
164 pair.get(MAC_ADDRESS).isNull()) {
165 final String error = "Invalid floating address pair received";
166 throw new IllegalArgumentException(error);
167 }
168 try {
169 IpAddress.valueOf(pair.get(IP_ADDRESS).asText());
170 } catch (IllegalArgumentException e) {
171 final String error = "Invalid floating address pair IP: ";
172 throw new IllegalArgumentException(error + pair.get(IP_ADDRESS).asText());
173 }
174
175 try {
176 MacAddress.valueOf(pair.get(MAC_ADDRESS).asText());
177 } catch (IllegalArgumentException e) {
178 final String error = "Invalid floating address pair MAC: ";
179 throw new IllegalArgumentException(error + pair.get(MAC_ADDRESS).asText());
180 }
181 });
182 }
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700183 }
184}