Committed by
Gerrit Code Review
[Emu] [ONOS-3109] Codec for port chain in SFC
Change-Id: Ief55a9726426953877d786404d22abc934ec6f86
Showing
1 changed file
with
105 additions
and
0 deletions
| 1 | +/* | ||
| 2 | + * Copyright 2015 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.onosproject.vtnweb.web; | ||
| 17 | + | ||
| 18 | + | ||
| 19 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 20 | +import static org.onlab.util.Tools.nullIsIllegal; | ||
| 21 | + | ||
| 22 | +import java.util.List; | ||
| 23 | +import java.util.UUID; | ||
| 24 | + | ||
| 25 | +import org.onosproject.codec.CodecContext; | ||
| 26 | +import org.onosproject.codec.JsonCodec; | ||
| 27 | +import org.onosproject.vtnrsc.TenantId; | ||
| 28 | +import org.onosproject.vtnrsc.DefaultPortChain; | ||
| 29 | +import org.onosproject.vtnrsc.FlowClassifierId; | ||
| 30 | +import org.onosproject.vtnrsc.PortChain; | ||
| 31 | +import org.onosproject.vtnrsc.PortChainId; | ||
| 32 | +import org.onosproject.vtnrsc.PortPairGroupId; | ||
| 33 | + | ||
| 34 | +import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| 35 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 36 | +import com.google.common.collect.Lists; | ||
| 37 | + | ||
| 38 | +/** | ||
| 39 | + * Port chain JSON codec. | ||
| 40 | + */ | ||
| 41 | +public final class PortChainCodec extends JsonCodec<PortChain> { | ||
| 42 | + | ||
| 43 | + private static final String ID = "id"; | ||
| 44 | + private static final String TENANT_ID = "tenant_id"; | ||
| 45 | + private static final String NAME = "name"; | ||
| 46 | + private static final String DESCRIPTION = "description"; | ||
| 47 | + private static final String PORT_PAIR_GROUPS = "port_pair_groups"; | ||
| 48 | + private static final String FLOW_CLASSIFIERS = "flow_classifiers"; | ||
| 49 | + private static final String MISSING_MEMBER_MESSAGE = | ||
| 50 | + " member is required in PortChain"; | ||
| 51 | + | ||
| 52 | + @Override | ||
| 53 | + public PortChain decode(ObjectNode json, CodecContext context) { | ||
| 54 | + if (json == null || !json.isObject()) { | ||
| 55 | + return null; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + PortChain.Builder resultBuilder = new DefaultPortChain.Builder(); | ||
| 59 | + | ||
| 60 | + String id = nullIsIllegal(json.get(ID), | ||
| 61 | + ID + MISSING_MEMBER_MESSAGE).asText(); | ||
| 62 | + resultBuilder.setId(PortChainId.portChainId(id)); | ||
| 63 | + | ||
| 64 | + String tenantId = nullIsIllegal(json.get(TENANT_ID), | ||
| 65 | + TENANT_ID + MISSING_MEMBER_MESSAGE).asText(); | ||
| 66 | + resultBuilder.setTenantId(TenantId.tenantId(tenantId)); | ||
| 67 | + | ||
| 68 | + String name = nullIsIllegal(json.get(NAME), | ||
| 69 | + NAME + MISSING_MEMBER_MESSAGE).asText(); | ||
| 70 | + resultBuilder.setName(name); | ||
| 71 | + | ||
| 72 | + String description = nullIsIllegal(json.get(DESCRIPTION), | ||
| 73 | + DESCRIPTION + MISSING_MEMBER_MESSAGE).asText(); | ||
| 74 | + resultBuilder.setDescription(description); | ||
| 75 | + | ||
| 76 | + ArrayNode arrayNode = (ArrayNode) json.path(PORT_PAIR_GROUPS); | ||
| 77 | + if (arrayNode != null) { | ||
| 78 | + List<PortPairGroupId> list = Lists.newArrayList(); | ||
| 79 | + arrayNode.forEach(i -> list.add(PortPairGroupId.portPairGroupId(i.asText()))); | ||
| 80 | + resultBuilder.setPortPairGroups(list); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + arrayNode = (ArrayNode) json.path(FLOW_CLASSIFIERS); | ||
| 84 | + if (arrayNode != null) { | ||
| 85 | + List<FlowClassifierId> list = Lists.newArrayList(); | ||
| 86 | + arrayNode.forEach(i -> list.add(FlowClassifierId.flowClassifierId(UUID.fromString(i.asText())))); | ||
| 87 | + resultBuilder.setFlowClassifiers(list); | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + return resultBuilder.build(); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + @Override | ||
| 94 | + public ObjectNode encode(PortChain portChain, CodecContext context) { | ||
| 95 | + checkNotNull(portChain, "port pair cannot be null"); | ||
| 96 | + ObjectNode result = context.mapper().createObjectNode() | ||
| 97 | + .put(ID, portChain.portChainId().toString()) | ||
| 98 | + .put(TENANT_ID, portChain.tenantId().toString()) | ||
| 99 | + .put(NAME, portChain.name()) | ||
| 100 | + .put(DESCRIPTION, portChain.description()) | ||
| 101 | + .put(PORT_PAIR_GROUPS, portChain.portPairGroups().toString()) | ||
| 102 | + .put(FLOW_CLASSIFIERS, portChain.flowClassifiers().toString()); | ||
| 103 | + return result; | ||
| 104 | + } | ||
| 105 | +} |
-
Please register or login to post a comment