Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.opencord.cordvtn.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 20 | import com.google.common.base.Strings; |
| 21 | import com.google.common.collect.ImmutableMap; |
| 22 | import com.google.common.collect.Maps; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onlab.packet.IpPrefix; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 25 | import org.onosproject.codec.CodecContext; |
| 26 | import org.onosproject.codec.JsonCodec; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 27 | import org.opencord.cordvtn.api.net.NetworkId; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 28 | import org.opencord.cordvtn.api.net.SegmentId; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 29 | import org.opencord.cordvtn.api.net.ServiceNetwork; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 30 | import org.opencord.cordvtn.api.net.ServiceNetwork.DependencyType; |
| 31 | import org.opencord.cordvtn.impl.DefaultServiceNetwork; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 32 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 33 | import java.util.Map; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 34 | |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 35 | import static com.google.common.base.Preconditions.checkArgument; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 36 | import static java.lang.Boolean.FALSE; |
| 37 | import static java.lang.Boolean.TRUE; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 38 | import static org.opencord.cordvtn.api.net.ServiceNetwork.DependencyType.BIDIRECTIONAL; |
| 39 | import static org.opencord.cordvtn.api.net.ServiceNetwork.DependencyType.UNIDIRECTIONAL; |
| 40 | import static org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType.valueOf; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Service network JSON codec. |
| 44 | */ |
| 45 | public final class ServiceNetworkCodec extends JsonCodec<ServiceNetwork> { |
| 46 | |
| 47 | private static final String ID = "id"; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 48 | private static final String NAME = "name"; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 49 | private static final String TYPE = "type"; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 50 | 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 Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 54 | private static final String PROVIDER_NETWORKS = "providerNetworks"; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 55 | private static final String PROVIDERS = "providers"; |
| 56 | private static final String DEP_TYPE = "bidirectional"; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 57 | |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 58 | private static final String ERR_JSON = "Invalid ServiceNetwork received"; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 59 | private static final String ERR_ID = "Service network ID cannot be null"; |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 60 | |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 61 | @Override |
| 62 | public ObjectNode encode(ServiceNetwork snet, CodecContext context) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 63 | 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 Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 79 | ArrayNode providers = context.mapper().createArrayNode(); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 80 | snet.providers().entrySet().forEach(provider -> { |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 81 | ObjectNode providerJson = context.mapper().createObjectNode() |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 82 | .put(ID, provider.getKey().id()) |
| 83 | .put(DEP_TYPE, provider.getValue() == BIDIRECTIONAL ? TRUE : FALSE); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 84 | providers.add(providerJson); |
| 85 | }); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 86 | result.set(PROVIDERS, providers); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 87 | return result; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public ServiceNetwork decode(ObjectNode json, CodecContext context) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 92 | validateJson(json); |
| 93 | ServiceNetwork.Builder snetBuilder = DefaultServiceNetwork.builder() |
| 94 | .id(NetworkId.of(json.get(ID).asText())); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 95 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 96 | // 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 Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 125 | if (json.get(PROVIDER_NETWORKS) != null) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 126 | 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 Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 201 | }); |
| 202 | } |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 203 | |
| 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 Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 219 | } |
| 220 | } |