blob: 0141a739df260f3e30d267f50c5340e8ce415147 [file] [log] [blame]
Sean Condon0e89bda2017-03-21 14:23:19 +00001/*
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 */
16package org.onosproject.cfm.web;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onlab.packet.MacAddress;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMepLtCreate;
24import org.onosproject.incubator.net.l2monitoring.cfm.MepLtCreate;
25import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
26
27import java.util.BitSet;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Encode and decode to/from JSON to MepLtCreate object.
33 */
34public class MepLtCreateCodec extends JsonCodec<MepLtCreate> {
35
36 private static final String REMOTE_MEP_ID = "remoteMepId";
37 private static final String REMOTE_MEP_MAC = "remoteMepMac";
38 private static final String DEFAULT_TTL = "defaultTtl";
39 private static final String TRANSMIT_LTM_FLAGS = "transmitLtmFlags";
40 private static final String LINKTRACE = "linktrace";
41 private static final String USE_FDB_ONLY = "use-fdb-only";
42
43 @Override
44 public ObjectNode encode(MepLtCreate mepLtCreate, CodecContext context) {
45 checkNotNull(mepLtCreate, "Mep Lt Create cannot be null");
46 ObjectNode result = context.mapper().createObjectNode();
47
48 if (mepLtCreate.remoteMepId() != null) {
49 result.put(REMOTE_MEP_ID, mepLtCreate.remoteMepId().value());
50 } else {
51 result.put(REMOTE_MEP_MAC, mepLtCreate.remoteMepAddress().toString());
52 }
53
54 if (mepLtCreate.defaultTtl() != null) {
55 result.put(DEFAULT_TTL, mepLtCreate.defaultTtl());
56 }
57 if (mepLtCreate.transmitLtmFlags() != null) {
58 result.put(TRANSMIT_LTM_FLAGS,
59 mepLtCreate.transmitLtmFlags().get(0) ? USE_FDB_ONLY : "");
60 }
61
62 return result;
63 }
64
65
66 @Override
67 public MepLtCreate decode(ObjectNode json, CodecContext context) {
68 if (json == null || !json.isObject()) {
69 return null;
70 }
71
72 JsonNode linktraceNode = json.get(LINKTRACE);
73
74 JsonNode remoteMepIdNode = linktraceNode.get(REMOTE_MEP_ID);
75 JsonNode remoteMepMacNode = linktraceNode.get(REMOTE_MEP_MAC);
76
77 MepLtCreate.MepLtCreateBuilder ltCreateBuilder = null;
78 if (remoteMepIdNode != null) {
79 MepId remoteMepId = MepId.valueOf((short) remoteMepIdNode.asInt());
80 ltCreateBuilder = DefaultMepLtCreate.builder(remoteMepId);
81 } else if (remoteMepMacNode != null) {
82 MacAddress remoteMepMac = MacAddress.valueOf(
83 remoteMepMacNode.asText());
84 ltCreateBuilder = DefaultMepLtCreate.builder(remoteMepMac);
85 } else {
86 throw new IllegalArgumentException(
87 "Either a remoteMepId or a remoteMepMac");
88 }
89
90 JsonNode defaultTtlNode = linktraceNode.get(DEFAULT_TTL);
91 if (defaultTtlNode != null) {
92 short defaultTtl = (short) defaultTtlNode.asInt();
93 ltCreateBuilder.defaultTtl(defaultTtl);
94 }
95
96 JsonNode transmitLtmFlagsNode = linktraceNode.get(TRANSMIT_LTM_FLAGS);
97 if (transmitLtmFlagsNode != null) {
98 if (transmitLtmFlagsNode.asText().isEmpty()) {
99 ltCreateBuilder.transmitLtmFlags(BitSet.valueOf(new long[]{0}));
100 } else if (transmitLtmFlagsNode.asText().equals(USE_FDB_ONLY)) {
101 ltCreateBuilder.transmitLtmFlags(BitSet.valueOf(new long[]{1}));
102 } else {
103 throw new IllegalArgumentException("Expecting value 'use-fdb-only' " +
104 "or '' for " + TRANSMIT_LTM_FLAGS);
105 }
106 }
107
108 return ltCreateBuilder.build();
109 }
110}