blob: c4f18527007d5c2500fa1fd349e8680a1ff2a395 [file] [log] [blame]
Rusty Eddyc0e3fea2015-09-28 21:20:41 +00001/*
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 */
16package org.onosproject.mfwd.impl;
17
18import org.onosproject.codec.CodecContext;
19import org.onosproject.codec.JsonCodec;
20
21import org.onlab.packet.IpPrefix;
22
23import java.util.Set;
24import java.util.Map;
25import java.util.Collection;
26import java.util.Iterator;
27import java.util.Optional;
28
29import com.fasterxml.jackson.databind.node.ArrayNode;
30import com.fasterxml.jackson.databind.node.ObjectNode;
31import com.fasterxml.jackson.databind.JsonNode;
32import com.fasterxml.jackson.databind.node.JsonNodeFactory;
33
34import org.slf4j.Logger;
35import static org.slf4j.LoggerFactory.getLogger;
36
37/**
38 * Encode and Decode the Multicast Route Table in JSON for CLI and REST commands.
39 */
40public class MRibCodec extends JsonCodec<McastRouteTable> {
41
42 private final Logger log = getLogger(getClass());
43 private static final String SOURCE_ADDRESS = "sourceAddress";
44 private static final String GROUP_ADDRESS = "groupAddress";
45 private static final String INGRESS_POINT = "ingressPoint";
46 private static final String EGRESS_POINT = "egressPoint";
47 private static final String MCASTCONNECTPOINT = "McastConnectPoint";
48 private static final String ELEMENTID = "elementId";
49 private static final String PORTNUMBER = "portNumber";
50 private static final String MCAST_GROUP = "mcastGroup";
51
52 /**
53 * Encode the MRIB into json format.
54 *
55 * @param mcastRouteTable McastRouteTable
56 * @param context CodecContext
57 * @return result ObjectNode
58 */
59 @Override
60 public ObjectNode encode(McastRouteTable mcastRouteTable, CodecContext context) {
61
62 final JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
63 final ObjectNode macastRouteTabNode = nodeFactory.objectNode();
64 ArrayNode mcastGroupNode = context.mapper().createArrayNode();
65 Optional<McastRouteTable> mcastRouteTabOpt = Optional.ofNullable(mcastRouteTable);
66
67 //checking whether the McastRouteTable is present.
68 if (mcastRouteTabOpt.isPresent()) {
69 Map<IpPrefix, McastRouteGroup> mrib4 = mcastRouteTabOpt.get().getMrib4();
70 Optional<Map<IpPrefix, McastRouteGroup>> mrib4Opt = Optional.ofNullable(mrib4);
71
72 //checking whether the mrib4 is present.
73 if (mrib4Opt.isPresent()) {
74
75 for (McastRouteGroup mg : mrib4Opt.get().values()) {
76 Collection<McastRouteSource> mcastRoute = mg.getSources().values();
77 Optional<Collection<McastRouteSource>> mcastRouteOpt = Optional.ofNullable(mcastRoute);
78
79 //checking whether the McastRouteSource List is present.
80 if (mcastRouteOpt.isPresent()) {
81 for (McastRouteSource mcastRouteSource : mcastRouteOpt.get()) {
82 mcastGroupNode.add(createMcastGroupNode(mcastRouteSource, context));
83 }
84 macastRouteTabNode.put(MCAST_GROUP, mcastGroupNode);
85 }
86 }
87 }
88 }
89 return macastRouteTabNode;
90 }
91 /**
92 * Method for creating the McastGroup object node.
93 *
94 * @param mcastRouteSource McastRouteSource
95 */
96 private ObjectNode createMcastGroupNode(McastRouteSource mcastRouteSource, CodecContext context) {
97
98 final ObjectNode mcastGroupNode = context.mapper().createObjectNode();
99 final ObjectNode ingressNode = context.mapper().createObjectNode();
100 final ObjectNode egressNode = context.mapper().createObjectNode();
101 final ArrayNode jsonLabelIds = context.mapper().createArrayNode();
102 final String sAddr = mcastRouteSource.getSaddr().toString();
103 final String gAddr = mcastRouteSource.getGaddr().toString();
104
105 Optional<String> saddrOpt = Optional.ofNullable(sAddr);
106 Optional<String> gaddrOpt = Optional.ofNullable(gAddr);
107
108 //checking source address and group address are present.
109 if (saddrOpt.isPresent() && gaddrOpt.isPresent()) {
110 mcastGroupNode.put(SOURCE_ADDRESS, saddrOpt.get().toString());
111 mcastGroupNode.put(GROUP_ADDRESS, gaddrOpt.get().toString());
112 McastConnectPoint mcastIngCP = mcastRouteSource.getIngressPoint();
113 Optional<McastConnectPoint> mcastIngCPOpt = Optional.ofNullable(mcastIngCP);
114
115 //checking whether the ingress connection point is present.
116 if (mcastIngCPOpt.isPresent()) {
117 ingressNode.put(MCASTCONNECTPOINT, mcastConnectPoint(mcastIngCPOpt.get(), context));
118 }
119
120 mcastGroupNode.put(INGRESS_POINT , ingressNode);
121 Set<McastConnectPoint> mcastEgCPSet = mcastRouteSource.getEgressPoints();
122 Optional<Set<McastConnectPoint>> mcastEgCPOpt = Optional.ofNullable(mcastEgCPSet);
123
124 //checking whether the egress connection points are present.
125 if (mcastEgCPOpt.isPresent()) {
126 for (final McastConnectPoint mcastConnectPoint : mcastEgCPOpt.get()) {
127 jsonLabelIds.add(mcastConnectPoint(mcastConnectPoint, context));
128 }
129 }
130
131 egressNode.put(MCASTCONNECTPOINT , jsonLabelIds);
132 mcastGroupNode.put(EGRESS_POINT , egressNode);
133 }
134 return mcastGroupNode;
135 }
136
137 /**
138 * Method for creating the McastConnectPoint object node.
139 *
140 * @param mcastConnectPoint McastConnectPoint
141 * @param context CodecContext
142 * @return mcastCpNode ObjectNode
143 */
144 private ObjectNode mcastConnectPoint(McastConnectPoint mcastConnectPoint, CodecContext context) {
145 final ObjectNode mcastCpNode = context.mapper().createObjectNode();
146 mcastCpNode.put(ELEMENTID , mcastConnectPoint.getConnectPoint().elementId().toString());
147 mcastCpNode.put(PORTNUMBER , mcastConnectPoint.getConnectPoint().port().toLong());
148 return mcastCpNode;
149 }
150
151 /**
152 * Decode json format and insert into the flow table.
153 *
154 * @param json ObjectNode
155 * @param context CodecContext
156 * @return mr McastRouteBase
157 */
158 @Override
159 public McastRouteTable decode(ObjectNode json, CodecContext context) {
160
161 String macAddr = null;
162 String portNo = null;
163 String sAddr = json.path(SOURCE_ADDRESS).asText();
164 String gAddr = json.path(GROUP_ADDRESS).asText();
165 JsonNode inPntObjNode = (JsonNode) json.path(INGRESS_POINT);
166 JsonNode egPntArrNode = (JsonNode) json.path(EGRESS_POINT);
167
168 log.debug("sAddr :" + sAddr + " gAddr :" + gAddr + " inPntObjNode :" + inPntObjNode);
169 log.debug("egPntArrNode :" + egPntArrNode.toString());
170
171 McastRouteTable mrib = McastRouteTable.getInstance();
172 McastRouteBase mr = mrib.addRoute(sAddr, gAddr);
173 Optional<JsonNode> inPntOpt = Optional.ofNullable(inPntObjNode);
174
175 if (inPntOpt.isPresent()) {
176
177 JsonNode inMcastCP = inPntOpt.get().path(MCASTCONNECTPOINT);
178 Optional<JsonNode> inCpOpt = Optional.ofNullable(inMcastCP);
179
180 if (inCpOpt.isPresent()) {
181 macAddr = inCpOpt.get().path(ELEMENTID).asText();
182 portNo = inCpOpt.get().path(PORTNUMBER).asText();
183 mr.addIngressPoint(macAddr + "/" + Long.parseLong(portNo));
184 }
185 }
186
187 Optional<JsonNode> egPntOpt = Optional.ofNullable(egPntArrNode);
188
189 if (egPntOpt.isPresent()) {
190 JsonNode egMcastCP = egPntOpt.get().path(MCASTCONNECTPOINT);
191 Optional<JsonNode> egMcCpOpt = Optional.ofNullable(egMcastCP);
192
193 if (egMcCpOpt.isPresent()) {
194 Iterator<JsonNode> egCpIt = egMcCpOpt.get().elements();
195
196 while (egCpIt.hasNext()) {
197
198 JsonNode egMcastCPObj = egCpIt.next();
199 Optional<JsonNode> egMcCpObOpt = Optional.ofNullable(egMcastCPObj);
200 if (egMcCpObOpt.isPresent()) {
201 macAddr = egMcCpObOpt.get().path(ELEMENTID).asText();
202 portNo = egMcCpObOpt.get().path(PORTNUMBER).asText();
203 log.debug("macAddr egPort : " + macAddr + " portNo egPort :" + portNo);
204 mr.addEgressPoint(macAddr + "/" + Long.parseLong(portNo), McastConnectPoint.JoinSource.STATIC);
205 }
206 }
207 }
208 }
209 return mrib;
210 }
211}