DScano | 55a0103 | 2020-04-09 20:01:50 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020-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 | */ |
| 16 | package org.onosproject.vpls.rest; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.net.EncapsulationType; |
| 24 | import org.onosproject.net.intf.Interface; |
| 25 | import org.onosproject.vpls.api.VplsData; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Collection; |
| 30 | import java.util.stream.IntStream; |
| 31 | |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | import static org.slf4j.LoggerFactory.getLogger; |
| 34 | /** |
| 35 | * Vpls JSON codec. |
| 36 | */ |
| 37 | public final class VplsCodec extends JsonCodec<VplsData> { |
| 38 | |
| 39 | // JSON field names |
| 40 | private static final String NAME = "name"; |
| 41 | private static final String ENCAPSULATION_TYPE = "encapsulation"; |
| 42 | private static final String INTERFACES = "interfaces"; |
| 43 | |
| 44 | private final Logger log = getLogger(getClass()); |
| 45 | |
| 46 | @Override |
| 47 | public ObjectNode encode(VplsData vplsData, CodecContext context) { |
| 48 | checkNotNull(vplsData, "Vpls cannot be null"); |
| 49 | ObjectNode result = context.mapper().createObjectNode() |
| 50 | .put(NAME, vplsData.name()) |
| 51 | .put(ENCAPSULATION_TYPE, vplsData.encapsulationType().toString()); |
| 52 | ArrayNode interfaces = context.mapper().createArrayNode(); |
| 53 | vplsData.interfaces().forEach(interf -> { |
| 54 | ObjectNode bandJson = context.codec(Interface.class).encode(interf, context); |
| 55 | interfaces.add(bandJson); |
| 56 | }); |
| 57 | result.set(INTERFACES, interfaces); |
| 58 | return result; |
| 59 | } |
| 60 | @Override |
| 61 | public VplsData decode(ObjectNode json, CodecContext context) { |
| 62 | if (json == null || !json.isObject()) { |
| 63 | return null; |
| 64 | } |
| 65 | |
| 66 | String vplsName = json.findValue(NAME).asText(); |
| 67 | EncapsulationType encap = EncapsulationType.enumFromString(json.findValue(ENCAPSULATION_TYPE).asText()); |
| 68 | VplsData vplsData = VplsData.of(vplsName, encap); |
| 69 | |
| 70 | Collection<Interface> interfaceList = new ArrayList<>(); |
| 71 | JsonNode interfacesJeson = json.findValue(INTERFACES); |
| 72 | JsonCodec<Interface> interfaceCodec = context.codec(Interface.class); |
| 73 | if (interfacesJeson != null) { |
| 74 | IntStream.range(0, interfacesJeson.size()) |
| 75 | .forEach(i -> interfaceList.add( |
| 76 | interfaceCodec.decode(get(interfacesJeson, i), |
| 77 | context))); |
| 78 | vplsData.addInterfaces(interfaceList); |
| 79 | } |
| 80 | return vplsData; |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |