Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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.cfm.web; |
| 17 | |
| 18 | import static org.onlab.util.Tools.nullIsIllegal; |
| 19 | |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.List; |
| 22 | |
| 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onosproject.codec.CodecContext; |
| 25 | import org.onosproject.codec.JsonCodec; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.Component; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultComponent; |
| 28 | |
| 29 | import com.fasterxml.jackson.databind.JsonNode; |
| 30 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 31 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 32 | |
| 33 | /** |
| 34 | * Encode and decode to/from JSON to Component object. |
| 35 | */ |
| 36 | public class ComponentCodec extends JsonCodec<Component> { |
| 37 | |
| 38 | private static final String COMPONENT_ID = "component-id"; |
| 39 | private static final String COMPONENT = "component"; |
| 40 | private static final String VID_LIST = "vid-list"; |
| 41 | private static final String TAG_TYPE = "tag-type"; |
| 42 | private static final String MHF_CREATION_TYPE = "mhf-creation-type"; |
| 43 | private static final String ID_PERMISSION = "id-permission"; |
| 44 | |
| 45 | @Override |
| 46 | public ObjectNode encode(Component component, CodecContext context) { |
| 47 | |
| 48 | ObjectNode node = context.mapper().createObjectNode() |
| 49 | .put(COMPONENT_ID, component.componentId()); |
| 50 | |
| 51 | node.set(VID_LIST, new VidCodec().encode(component.vidList(), context)); |
| 52 | |
| 53 | if (component.mhfCreationType() != null) { |
| 54 | node.put(MHF_CREATION_TYPE, component.mhfCreationType().name()); |
| 55 | } |
| 56 | if (component.idPermission() != null) { |
| 57 | node.put(ID_PERMISSION, component.idPermission().name()); |
| 58 | } |
| 59 | if (component.tagType() != null) { |
| 60 | node.put(TAG_TYPE, component.tagType().name()); |
| 61 | } |
| 62 | |
| 63 | return (ObjectNode) context.mapper().createObjectNode().set(COMPONENT, node); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public ArrayNode encode(Iterable<Component> components, CodecContext context) { |
| 68 | ArrayNode an = context.mapper().createArrayNode(); |
| 69 | components.forEach(component -> { |
| 70 | an.add(encode(component, context)); |
| 71 | }); |
| 72 | return an; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Component decode(ObjectNode json, CodecContext context) { |
| 77 | if (json == null || !json.isObject()) { |
| 78 | return null; |
| 79 | } |
| 80 | |
| 81 | JsonNode componentNode = json.get(COMPONENT); |
| 82 | |
| 83 | int componentId = nullIsIllegal(componentNode.get(COMPONENT_ID), |
| 84 | "component-id is required").asInt(); |
| 85 | Component.ComponentBuilder componentBuilder = |
| 86 | DefaultComponent.builder(componentId); |
| 87 | |
| 88 | List<VlanId> vidList = (new VidCodec()).decode((ArrayNode) |
| 89 | nullIsIllegal(componentNode.get(VID_LIST), "vid-list is required"), context); |
| 90 | if (vidList == null || vidList.size() < 1) { |
| 91 | throw new IllegalArgumentException("A least one VID is required in component: " + componentId); |
| 92 | } |
| 93 | for (VlanId vid:vidList) { |
| 94 | componentBuilder = componentBuilder.addToVidList(vid); |
| 95 | } |
| 96 | |
| 97 | if (componentNode.get(TAG_TYPE) != null) { |
| 98 | componentBuilder = componentBuilder |
| 99 | .tagType(Component.TagType.valueOf( |
| 100 | componentNode.get(TAG_TYPE).asText())); |
| 101 | } |
| 102 | |
| 103 | if (componentNode.get(MHF_CREATION_TYPE) != null) { |
| 104 | componentBuilder = componentBuilder |
| 105 | .mhfCreationType(Component.MhfCreationType.valueOf( |
| 106 | componentNode.get(MHF_CREATION_TYPE).asText())); |
| 107 | } |
| 108 | if (componentNode.get(ID_PERMISSION) != null) { |
| 109 | componentBuilder = componentBuilder |
| 110 | .idPermission(Component.IdPermissionType.valueOf( |
| 111 | componentNode.get(ID_PERMISSION).asText())); |
| 112 | } |
| 113 | |
| 114 | return componentBuilder.build(); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public List<Component> decode(ArrayNode json, CodecContext context) { |
| 119 | List<Component> componentList = new ArrayList<>(); |
| 120 | json.forEach(node -> componentList.add(decode((ObjectNode) node, context))); |
| 121 | return componentList; |
| 122 | } |
| 123 | } |