blob: fba6ff9e0548f9fc9be0e6171ef7e0afebcedf0d [file] [log] [blame]
Andreas Pantelopouloscd339592018-02-23 14:18:00 -08001/*
2 * Copyright 2015-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.segmentrouting.web;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onlab.packet.MplsLabel;
20import org.onlab.packet.VlanId;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
25import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
26import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
27import org.onosproject.segmentrouting.pwaas.L2Mode;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import static org.onosproject.segmentrouting.pwaas.PwaasUtil.*;
32
33/**
34 * Codec of PseudowireCodec class.
35 */
36public final class PseudowireCodec extends JsonCodec<DefaultL2TunnelDescription> {
37
38 // JSON field names
39 private static final String PW_ID = "pwId";
40 private static final String CP1 = "cP1";
41 private static final String CP2 = "cP2";
42 private static final String CP1_INNER_TAG = "cP1InnerTag";
43 private static final String CP1_OUTER_TAG = "cP1OuterTag";
44 private static final String CP2_INNER_TAG = "cP2InnerTag";
45 private static final String CP2_OUTER_TAG = "cP2OouterTag";
46 private static final String MODE = "mode";
47 private static final String SERVICE_DELIM_TAG = "serviceTag";
48 private static final String PW_LABEL = "pwLabel";
49
50 private static Logger log = LoggerFactory
51 .getLogger(PseudowireCodec.class);
52
53 @Override
54 public ObjectNode encode(DefaultL2TunnelDescription pseudowire, CodecContext context) {
55 final ObjectNode result = context.mapper().createObjectNode()
56 .put(PW_ID, pseudowire.l2Tunnel().tunnelId());
57
58 result.put(CP1, pseudowire.l2TunnelPolicy().cP1().toString());
59 result.put(CP2, pseudowire.l2TunnelPolicy().cP2().toString());
60
61 result.put(CP1_INNER_TAG, pseudowire.l2TunnelPolicy().cP1InnerTag().toString());
62 result.put(CP1_OUTER_TAG, pseudowire.l2TunnelPolicy().cP1OuterTag().toString());
63
64
65 result.put(CP2_INNER_TAG, pseudowire.l2TunnelPolicy().cP2InnerTag().toString());
66 result.put(CP2_OUTER_TAG, pseudowire.l2TunnelPolicy().cP2OuterTag().toString());
67
68 result.put(MODE, pseudowire.l2Tunnel().pwMode() == L2Mode.RAW ? "RAW" : "TAGGED");
69 result.put(SERVICE_DELIM_TAG, pseudowire.l2Tunnel().sdTag().toString());
70 result.put(PW_LABEL, pseudowire.l2Tunnel().pwLabel().toString());
71
72 return result;
73 }
74
75 /**
76 * Decodes a json containg a single field with the pseudowire id.
77 *
78 * @param json Json to decode.
79 * @return The pseudowire id.
80 */
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -070081 public static Integer decodeId(ObjectNode json) {
Andreas Pantelopouloscd339592018-02-23 14:18:00 -080082
83 Integer id = parsePwId(json.path(PW_ID).asText());
84 if (id == null) {
85 log.error("Pseudowire id is not an integer!");
86 return null;
87 }
88
89 return id;
90 }
91
92 @Override
93 public DefaultL2TunnelDescription decode(ObjectNode json, CodecContext context) {
94
95 String tempString;
96
97 Integer id = parsePwId(json.path(PW_ID).asText());
98 if (id == null) {
99 log.error("Pseudowire id is not an integer");
100 return null;
101 }
102
103 ConnectPoint cP1, cP2;
104 try {
105 tempString = json.path(CP1).asText();
106 cP1 = ConnectPoint.deviceConnectPoint(tempString);
107 } catch (Exception e) {
108 log.error("cP1 is not a valid connect point!");
109 return null;
110 }
111
112 try {
113 tempString = json.path(CP2).asText();
114 cP2 = ConnectPoint.deviceConnectPoint(tempString);
115 } catch (Exception e) {
116 log.error("cP2 is not a valid connect point!");
117 return null;
118 }
119
120 VlanId cP1InnerVlan = parseVlan(json.path(CP1_INNER_TAG).asText());
121 VlanId cP1OuterVlan = parseVlan(json.path(CP1_OUTER_TAG).asText());
122 VlanId cP2InnerVlan = parseVlan(json.path(CP2_INNER_TAG).asText());
123 VlanId cP2OuterVlan = parseVlan(json.path(CP2_OUTER_TAG).asText());
124 if ((cP1InnerVlan == null) || (cP1OuterVlan == null) ||
125 (cP2InnerVlan == null) || (cP2OuterVlan == null)) {
126 log.error("One or more vlan for cp1 or cp2 is malformed, it shouldbe an integer / Any / None / *");
127 return null;
128 }
129
130 L2Mode mode = parseMode(json.path(MODE).asText());
131 if (mode == null) {
132 log.error("Mode should be RAW or TAGGED!");
133 return null;
134 }
135
136 VlanId sdTag = parseVlan(json.path(SERVICE_DELIM_TAG).asText());
137 if (sdTag == null) {
138 log.error("SD tag is malformed, it should be an integer / Any / None / *");
139 return null;
140 }
141
142 MplsLabel pwLabel = parsePWLabel(json.path(PW_LABEL).asText());
143 if (pwLabel == null) {
144 log.error("PW label is malformed, should be an integer!");
145 return null;
146 }
147
148 DefaultL2Tunnel l2Tunnel;
149 DefaultL2TunnelPolicy l2Policy;
150
151 l2Tunnel = new DefaultL2Tunnel(mode, sdTag, id, pwLabel);
152 l2Policy = new DefaultL2TunnelPolicy(id, cP1, cP1InnerVlan, cP1OuterVlan,
153 cP2, cP2InnerVlan, cP2OuterVlan);
154
155 return new DefaultL2TunnelDescription(l2Tunnel, l2Policy);
156
157 }
158}