blob: 01a104a789e24b98dbf18b40c6d8df019396021a [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.collect.ImmutableSet;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070021import com.google.common.collect.Sets;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070022import org.onlab.packet.IpAddress;
23import org.onlab.packet.MacAddress;
24import org.onlab.packet.VlanId;
25import org.onosproject.codec.CodecContext;
26import org.onosproject.codec.JsonCodec;
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070027import org.opencord.cordvtn.api.net.AddressPair;
Hyunsun Moonaf3822c2017-04-04 15:56:27 +090028import org.opencord.cordvtn.api.net.NetworkId;
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070029import 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;
Hyunsun Moonaf3822c2017-04-04 15:56:27 +090036import static com.google.common.base.Strings.isNullOrEmpty;
Hyunsun Moon4a94c2e2016-10-21 17:37:05 -070037
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070038/**
39 * Service port JSON codec.
40 */
41public final class ServicePortCodec extends JsonCodec<ServicePort> {
42
43 private static final String ID = "id";
Hyunsun Moon187bf532017-01-19 10:57:40 +090044 private static final String NAME = "name";
45 private static final String NETWORK_ID = "network_id";
46 private static final String MAC_ADDRESS = "mac_address";
47 private static final String IP_ADDRESS = "ip_address";
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070048 private static final String VLAN_ID = "vlan_id";
49 private static final String FLOATING_ADDRESS_PAIRS = "floating_address_pairs";
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070050
Hyunsun Moon4a94c2e2016-10-21 17:37:05 -070051 private static final String ERR_JSON = "Invalid ServicePort received";
Hyunsun Moon187bf532017-01-19 10:57:40 +090052 private static final String ERR_ID = "Service port ID cannot be null";
Hyunsun Moon4a94c2e2016-10-21 17:37:05 -070053
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070054 @Override
55 public ObjectNode encode(ServicePort sport, CodecContext context) {
Hyunsun Moonaf3822c2017-04-04 15:56:27 +090056 ObjectNode result = context.mapper().createObjectNode().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 }
Hyunsun Moonaf3822c2017-04-04 15:56:27 +090061 if (!isNullOrEmpty(sport.name())) {
Hyunsun Moon187bf532017-01-19 10:57:40 +090062 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
Hyunsun Moonaf3822c2017-04-04 15:56:27 +090085 // TODO allow removing existing value when explicit null received
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070086 @Override
87 public ServicePort decode(ObjectNode json, CodecContext context) {
Hyunsun Moon187bf532017-01-19 10:57:40 +090088 checkArgument(json != null && json.isObject(), ERR_JSON);
Hyunsun Moonaf3822c2017-04-04 15:56:27 +090089 checkArgument(!json.path(ID).isMissingNode() && !json.path(ID).isNull(), ERR_ID);
Hyunsun Moon187bf532017-01-19 10:57:40 +090090
Hyunsun Moonaf3822c2017-04-04 15:56:27 +090091 ServicePort.Builder sportBuilder =
92 DefaultServicePort.builder().id(PortId.of(json.get(ID).asText()));
93
94 if (!json.path(NETWORK_ID).isMissingNode()) {
95 if (json.path(NETWORK_ID).isNull() ||
96 isNullOrEmpty(json.path(NETWORK_ID).asText())) {
97 final String error = "Null or empty ServicePort network ID received";
98 throw new IllegalArgumentException(error);
99 } else {
100 sportBuilder.networkId(NetworkId.of(json.get(NETWORK_ID).asText()));
101 }
102 }
103
104 if (!json.path(NAME).isMissingNode()) {
105 if (json.path(NAME).isNull() || isNullOrEmpty(json.path(NAME).asText())) {
106 final String error = "Null or empty ServicePort name received";
107 throw new IllegalArgumentException(error);
108 } else {
109 sportBuilder.name(json.get(NAME).asText());
110 }
111 }
112
113 if (!json.path(MAC_ADDRESS).isMissingNode()) {
114 try {
115 sportBuilder.mac(MacAddress.valueOf(json.path(MAC_ADDRESS).asText()));
116 } catch (IllegalArgumentException | NullPointerException e) {
117 final String error = "Invalid ServicePort MAC address received";
Hyunsun Moon187bf532017-01-19 10:57:40 +0900118 throw new IllegalArgumentException(error);
119 }
120 }
121
Hyunsun Moonaf3822c2017-04-04 15:56:27 +0900122 if (!json.path(IP_ADDRESS).isMissingNode()) {
Hyunsun Moon187bf532017-01-19 10:57:40 +0900123 try {
Hyunsun Moonaf3822c2017-04-04 15:56:27 +0900124 sportBuilder.ip(IpAddress.valueOf(json.get(IP_ADDRESS).asText()));
125 } catch (IllegalArgumentException | NullPointerException e) {
126 final String error = "Invalid ServicePort IP address received";
127 throw new IllegalArgumentException(error);
Hyunsun Moon187bf532017-01-19 10:57:40 +0900128 }
129 }
130
Hyunsun Moonaf3822c2017-04-04 15:56:27 +0900131 if (!json.path(VLAN_ID).isMissingNode()) {
Hyunsun Moon187bf532017-01-19 10:57:40 +0900132 try {
Hyunsun Moonaf3822c2017-04-04 15:56:27 +0900133 sportBuilder.vlanId(VlanId.vlanId(json.get(VLAN_ID).asText()));
134 } catch (IllegalArgumentException | NullPointerException e) {
135 final String error = "Invalid VLAN ID is received";
136 throw new IllegalArgumentException(error);
Hyunsun Moon187bf532017-01-19 10:57:40 +0900137 }
138 }
139
Hyunsun Moonaf3822c2017-04-04 15:56:27 +0900140 if (!json.path(FLOATING_ADDRESS_PAIRS).isMissingNode() &&
141 json.path(FLOATING_ADDRESS_PAIRS).isNull()) {
142 sportBuilder.addressPairs(ImmutableSet.of());
143 } else if (!json.path(FLOATING_ADDRESS_PAIRS).isMissingNode()) {
144 Set<AddressPair> addressPairs = Sets.newHashSet();
145 json.path(FLOATING_ADDRESS_PAIRS).forEach(pair -> {
146 if (pair.path(IP_ADDRESS).isMissingNode() ||
147 pair.path(IP_ADDRESS).isNull() ||
148 pair.path(MAC_ADDRESS).isMissingNode() ||
149 pair.path(MAC_ADDRESS).isNull()) {
150 final String error = "Invalid floating address pair received: ";
151 throw new IllegalArgumentException(error + pair.asText());
Hyunsun Moon187bf532017-01-19 10:57:40 +0900152 }
153 try {
Hyunsun Moonaf3822c2017-04-04 15:56:27 +0900154 AddressPair addrPair = AddressPair.of(
155 IpAddress.valueOf(pair.get(IP_ADDRESS).asText()),
156 MacAddress.valueOf(pair.get(MAC_ADDRESS).asText()));
157 addressPairs.add(addrPair);
Hyunsun Moon187bf532017-01-19 10:57:40 +0900158 } catch (IllegalArgumentException e) {
Hyunsun Moonaf3822c2017-04-04 15:56:27 +0900159 final String error = "Invalid floating address pair received: ";
160 throw new IllegalArgumentException(error + pair.asText());
Hyunsun Moon187bf532017-01-19 10:57:40 +0900161 }
162 });
Hyunsun Moonaf3822c2017-04-04 15:56:27 +0900163 sportBuilder.addressPairs(addressPairs);
Hyunsun Moon187bf532017-01-19 10:57:40 +0900164 }
Hyunsun Moonaf3822c2017-04-04 15:56:27 +0900165 return sportBuilder.build();
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700166 }
167}