blob: 331bffe1170de9082d75bce3ac741db024fd8ce8 [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.ImmutableMap;
22import com.google.common.collect.Maps;
23import org.onlab.packet.IpAddress;
24import org.onlab.packet.IpPrefix;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070025import org.onosproject.codec.CodecContext;
26import org.onosproject.codec.JsonCodec;
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070027import org.opencord.cordvtn.api.net.NetworkId;
Hyunsun Moon187bf532017-01-19 10:57:40 +090028import org.opencord.cordvtn.api.net.SegmentId;
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070029import org.opencord.cordvtn.api.net.ServiceNetwork;
Hyunsun Moon187bf532017-01-19 10:57:40 +090030import org.opencord.cordvtn.api.net.ServiceNetwork.DependencyType;
31import org.opencord.cordvtn.impl.DefaultServiceNetwork;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070032
Hyunsun Moon187bf532017-01-19 10:57:40 +090033import java.util.Map;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070034
Hyunsun Moon4a94c2e2016-10-21 17:37:05 -070035import static com.google.common.base.Preconditions.checkArgument;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070036import static java.lang.Boolean.FALSE;
37import static java.lang.Boolean.TRUE;
Hyunsun Moon187bf532017-01-19 10:57:40 +090038import static org.opencord.cordvtn.api.net.ServiceNetwork.DependencyType.BIDIRECTIONAL;
39import static org.opencord.cordvtn.api.net.ServiceNetwork.DependencyType.UNIDIRECTIONAL;
40import static org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType.valueOf;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070041
42/**
43 * Service network JSON codec.
44 */
45public final class ServiceNetworkCodec extends JsonCodec<ServiceNetwork> {
46
47 private static final String ID = "id";
Hyunsun Moon187bf532017-01-19 10:57:40 +090048 private static final String NAME = "name";
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070049 private static final String TYPE = "type";
Hyunsun Moon187bf532017-01-19 10:57:40 +090050 private static final String SEGMENT_ID = "segment_id";
51 private static final String SUBNET = "subnet";
52 private static final String SERVICE_IP = "service_ip";
53 @Deprecated
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070054 private static final String PROVIDER_NETWORKS = "providerNetworks";
Hyunsun Moon187bf532017-01-19 10:57:40 +090055 private static final String PROVIDERS = "providers";
56 private static final String DEP_TYPE = "bidirectional";
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070057
Hyunsun Moon4a94c2e2016-10-21 17:37:05 -070058 private static final String ERR_JSON = "Invalid ServiceNetwork received";
Hyunsun Moon187bf532017-01-19 10:57:40 +090059 private static final String ERR_ID = "Service network ID cannot be null";
Hyunsun Moon4a94c2e2016-10-21 17:37:05 -070060
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070061 @Override
62 public ObjectNode encode(ServiceNetwork snet, CodecContext context) {
Hyunsun Moon187bf532017-01-19 10:57:40 +090063 ObjectNode result = context.mapper().createObjectNode().put(ID, snet.id().id());
64 if (!Strings.isNullOrEmpty(snet.name())) {
65 result.put(NAME, snet.name());
66 }
67 if (snet.type() != null) {
68 result.put(TYPE, snet.type().name());
69 }
70 if (snet.segmentId() != null) {
71 result.put(SEGMENT_ID, snet.segmentId().id());
72 }
73 if (snet.subnet() != null) {
74 result.put(SUBNET, snet.subnet().toString());
75 }
76 if (snet.serviceIp() != null) {
77 result.put(SERVICE_IP, snet.serviceIp().toString());
78 }
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070079 ArrayNode providers = context.mapper().createArrayNode();
Hyunsun Moon187bf532017-01-19 10:57:40 +090080 snet.providers().entrySet().forEach(provider -> {
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070081 ObjectNode providerJson = context.mapper().createObjectNode()
Hyunsun Moon187bf532017-01-19 10:57:40 +090082 .put(ID, provider.getKey().id())
83 .put(DEP_TYPE, provider.getValue() == BIDIRECTIONAL ? TRUE : FALSE);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070084 providers.add(providerJson);
85 });
Hyunsun Moon187bf532017-01-19 10:57:40 +090086 result.set(PROVIDERS, providers);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070087 return result;
88 }
89
90 @Override
91 public ServiceNetwork decode(ObjectNode json, CodecContext context) {
Hyunsun Moon187bf532017-01-19 10:57:40 +090092 validateJson(json);
93 ServiceNetwork.Builder snetBuilder = DefaultServiceNetwork.builder()
94 .id(NetworkId.of(json.get(ID).asText()));
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070095
Hyunsun Moon187bf532017-01-19 10:57:40 +090096 // TODO remove existing values when explicit null received
97 if (json.get(NAME) != null && !json.get(NAME).isNull()) {
98 snetBuilder.name(json.get(NAME).asText());
99 }
100 if (json.get(TYPE) != null && !json.get(TYPE).isNull()) {
101 snetBuilder.type(valueOf(json.get(TYPE).asText().toUpperCase()));
102 }
103 if (json.get(SEGMENT_ID) != null && !json.get(SEGMENT_ID).isNull()) {
104 snetBuilder.segmentId(SegmentId.of(json.get(SEGMENT_ID).asLong()));
105 }
106 if (json.get(SUBNET) != null && !json.get(SUBNET).isNull()) {
107 snetBuilder.subnet(IpPrefix.valueOf(json.get(SUBNET).asText()));
108 }
109 if (json.get(SERVICE_IP) != null && !json.get(SERVICE_IP).isNull()) {
110 snetBuilder.serviceIp(IpAddress.valueOf(json.get(SERVICE_IP).asText()));
111 }
112 if (json.get(PROVIDERS) != null) {
113 if (json.get(PROVIDERS).isNull()) {
114 snetBuilder.providers(ImmutableMap.of());
115 } else {
116 Map<NetworkId, DependencyType> providers = Maps.newHashMap();
117 json.get(PROVIDERS).forEach(provider -> {
118 DependencyType type = provider.get(DEP_TYPE).asBoolean() ?
119 BIDIRECTIONAL : UNIDIRECTIONAL;
120 providers.put(NetworkId.of(provider.get(ID).asText()), type);
121 });
122 snetBuilder.providers(providers);
123 }
124 }
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700125 if (json.get(PROVIDER_NETWORKS) != null) {
Hyunsun Moon187bf532017-01-19 10:57:40 +0900126 if (json.get(PROVIDER_NETWORKS).isNull()) {
127 snetBuilder.providers(ImmutableMap.of());
128 } else {
129 Map<NetworkId, DependencyType> providers = Maps.newHashMap();
130 json.get(PROVIDER_NETWORKS).forEach(provider -> {
131 DependencyType type = provider.get(DEP_TYPE).asBoolean() ?
132 BIDIRECTIONAL : UNIDIRECTIONAL;
133 providers.put(NetworkId.of(provider.get(ID).asText()), type);
134 });
135 snetBuilder.providers(providers);
136 }
137 }
138 return snetBuilder.build();
139 }
140
141 private void validateJson(ObjectNode json) {
142 checkArgument(json != null && json.isObject(), ERR_JSON);
143 checkArgument(json.get(ID) != null && !json.get(ID).isNull(), ERR_ID);
144 checkArgument(!Strings.isNullOrEmpty(json.get(ID).asText()), ERR_ID);
145
146 // allow explicit null for removing the existing value
147 if (json.get(NAME) != null && !json.get(NAME).isNull()) {
148 if (Strings.isNullOrEmpty(json.get(NAME).asText())) {
149 final String error = "Null or empty ServiceNetwork name received";
150 throw new IllegalArgumentException(error);
151 }
152 }
153
154 if (json.get(TYPE) != null && !json.get(TYPE).isNull()) {
155 try {
156 valueOf(json.get(TYPE).asText().toUpperCase());
157 } catch (IllegalArgumentException e) {
158 final String error = "Invalid ServiceNetwork type received: ";
159 throw new IllegalArgumentException(error + json.get(TYPE).asText());
160 }
161 }
162
163 if (json.get(SEGMENT_ID) != null && !json.get(SEGMENT_ID).isNull()) {
164 if (json.get(SEGMENT_ID).asLong() == 0) {
165 final String error = "Invalid ServiecNetwork segment ID received: ";
166 throw new IllegalArgumentException(error + json.get(SEGMENT_ID).asText());
167 }
168 }
169
170 if (json.get(SUBNET) != null && !json.get(SUBNET).isNull()) {
171 try {
172 IpPrefix.valueOf(json.get(SUBNET).asText());
173 } catch (IllegalArgumentException e) {
174 final String error = "Invalid ServiceNetwork subnet received: ";
175 throw new IllegalArgumentException(error + json.get(SUBNET).asText());
176 }
177 }
178
179 if (json.get(SERVICE_IP) != null && !json.get(SERVICE_IP).isNull()) {
180 try {
181 IpAddress.valueOf(json.get(SERVICE_IP).asText());
182 } catch (IllegalArgumentException e) {
183 final String error = "Invalid ServiceNetwork service IP address received: ";
184 throw new IllegalArgumentException(error + json.get(SERVICE_IP).asText());
185 }
186 }
187
188 if (json.get(PROVIDERS) != null && !json.get(PROVIDERS).isNull()) {
189 json.get(PROVIDERS).forEach(provider -> {
190 if (provider.get(ID) == null || provider.get(ID).isNull() ||
191 Strings.isNullOrEmpty(provider.get(ID).asText())) {
192 final String error = "Null or empty provider network ID received";
193 throw new IllegalArgumentException(error);
194 }
195
196 if (provider.get(DEP_TYPE) == null || provider.get(DEP_TYPE).isNull()
197 || !provider.get(DEP_TYPE).isBoolean()) {
198 final String error = "Non-boolean bidirectional received";
199 throw new IllegalArgumentException(error);
200 }
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700201 });
202 }
Hyunsun Moon187bf532017-01-19 10:57:40 +0900203
204 if (json.get(PROVIDER_NETWORKS) != null && !json.get(PROVIDER_NETWORKS).isNull()) {
205 json.get(PROVIDER_NETWORKS).forEach(provider -> {
206 if (provider.get(ID) == null || provider.get(ID).isNull() ||
207 Strings.isNullOrEmpty(provider.get(ID).asText())) {
208 final String error = "Null or empty provider network ID received";
209 throw new IllegalArgumentException(error);
210 }
211
212 if (provider.get(DEP_TYPE) == null || provider.get(DEP_TYPE).isNull()
213 || !provider.get(DEP_TYPE).isBoolean()) {
214 final String error = "Non-boolean bidirectional received";
215 throw new IllegalArgumentException(error);
216 }
217 });
218 }
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700219 }
220}