Fix for ONOS-5020:Encoder for Disjoint Rest API with NPE Issue Fix
Change-Id: I1036e806943c53bc00cb7cde73b66c115c1eeffe
Showing
6 changed files
with
80 additions
and
5 deletions
... | @@ -45,6 +45,16 @@ public class DefaultDisjointPath extends DefaultPath implements DisjointPath { | ... | @@ -45,6 +45,16 @@ public class DefaultDisjointPath extends DefaultPath implements DisjointPath { |
45 | this.path2 = path2; | 45 | this.path2 = path2; |
46 | } | 46 | } |
47 | 47 | ||
48 | + /** | ||
49 | + * Creates a disjoint path pair from single default paths. | ||
50 | + * | ||
51 | + * @param providerId provider identity | ||
52 | + * @param path1 primary path | ||
53 | + */ | ||
54 | + public DefaultDisjointPath(ProviderId providerId, DefaultPath path1) { | ||
55 | + this(providerId, path1, null); | ||
56 | + } | ||
57 | + | ||
48 | @Override | 58 | @Override |
49 | public List<Link> links() { | 59 | public List<Link> links() { |
50 | if (usingPath1) { | 60 | if (usingPath1) { | ... | ... |
... | @@ -42,6 +42,7 @@ import org.onosproject.net.HostLocation; | ... | @@ -42,6 +42,7 @@ import org.onosproject.net.HostLocation; |
42 | import org.onosproject.net.Link; | 42 | import org.onosproject.net.Link; |
43 | import org.onosproject.net.MastershipRole; | 43 | import org.onosproject.net.MastershipRole; |
44 | import org.onosproject.net.Path; | 44 | import org.onosproject.net.Path; |
45 | +import org.onosproject.net.DisjointPath; | ||
45 | import org.onosproject.net.Port; | 46 | import org.onosproject.net.Port; |
46 | import org.onosproject.net.device.PortStatistics; | 47 | import org.onosproject.net.device.PortStatistics; |
47 | import org.onosproject.net.driver.Driver; | 48 | import org.onosproject.net.driver.Driver; |
... | @@ -120,6 +121,7 @@ public class CodecManager implements CodecService { | ... | @@ -120,6 +121,7 @@ public class CodecManager implements CodecService { |
120 | registerCodec(Topology.class, new TopologyCodec()); | 121 | registerCodec(Topology.class, new TopologyCodec()); |
121 | registerCodec(TopologyCluster.class, new TopologyClusterCodec()); | 122 | registerCodec(TopologyCluster.class, new TopologyClusterCodec()); |
122 | registerCodec(Path.class, new PathCodec()); | 123 | registerCodec(Path.class, new PathCodec()); |
124 | + registerCodec(DisjointPath.class, new DisjointPathCodec()); | ||
123 | registerCodec(Group.class, new GroupCodec()); | 125 | registerCodec(Group.class, new GroupCodec()); |
124 | registerCodec(Driver.class, new DriverCodec()); | 126 | registerCodec(Driver.class, new DriverCodec()); |
125 | registerCodec(GroupBucket.class, new GroupBucketCodec()); | 127 | registerCodec(GroupBucket.class, new GroupBucketCodec()); | ... | ... |
1 | +/* | ||
2 | + * Copyright 2016-present 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.codec.impl; | ||
17 | + | ||
18 | +import org.onosproject.codec.CodecContext; | ||
19 | +import org.onosproject.codec.JsonCodec; | ||
20 | +import org.onosproject.net.Link; | ||
21 | +import org.onosproject.net.DisjointPath; | ||
22 | +import com.fasterxml.jackson.databind.node.ArrayNode; | ||
23 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
24 | + | ||
25 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
26 | + | ||
27 | +/** | ||
28 | + * DisjointPath JSON codec. | ||
29 | + */ | ||
30 | +public final class DisjointPathCodec extends AnnotatedCodec<DisjointPath> { | ||
31 | + @Override | ||
32 | + public ObjectNode encode(DisjointPath disjointPath, CodecContext context) { | ||
33 | + checkNotNull(disjointPath, "Path cannot be null"); | ||
34 | + JsonCodec<Link> codec = context.codec(Link.class); | ||
35 | + ObjectNode result = context.mapper() | ||
36 | + .createObjectNode(); | ||
37 | + | ||
38 | + ObjectNode primary = context.mapper() | ||
39 | + .createObjectNode() | ||
40 | + .put("cost", disjointPath.primary().cost()); | ||
41 | + | ||
42 | + result.set("Primary", primary); | ||
43 | + ArrayNode jsonLinks = primary.putArray("links"); | ||
44 | + for (Link link : disjointPath.primary().links()) { | ||
45 | + jsonLinks.add(codec.encode(link, context)); | ||
46 | + } | ||
47 | + if (disjointPath.backup() != null) { | ||
48 | + ObjectNode backup = context.mapper() | ||
49 | + .createObjectNode() | ||
50 | + .put("cost", disjointPath.backup().cost()); | ||
51 | + result.set("Backup", backup); | ||
52 | + ArrayNode jsonLinks1 = backup.putArray("links"); | ||
53 | + for (Link link1 : disjointPath.backup().links()) { | ||
54 | + jsonLinks1.add(codec.encode(link1, context)); | ||
55 | + } | ||
56 | + } | ||
57 | + return annotate(result, disjointPath, context); | ||
58 | + } | ||
59 | + | ||
60 | +} | ||
61 | + |
... | @@ -485,8 +485,7 @@ public class DefaultTopology extends AbstractModel implements Topology { | ... | @@ -485,8 +485,7 @@ public class DefaultTopology extends AbstractModel implements Topology { |
485 | if (!path.hasBackup()) { | 485 | if (!path.hasBackup()) { |
486 | // There was no secondary path available. | 486 | // There was no secondary path available. |
487 | return new DefaultDisjointPath(CORE_PROVIDER_ID, | 487 | return new DefaultDisjointPath(CORE_PROVIDER_ID, |
488 | - (DefaultPath) networkPath(path.primary()), | 488 | + (DefaultPath) networkPath(path.primary())); |
489 | - null); | ||
490 | } | 489 | } |
491 | return new DefaultDisjointPath(CORE_PROVIDER_ID, | 490 | return new DefaultDisjointPath(CORE_PROVIDER_ID, |
492 | (DefaultPath) networkPath(path.primary()), | 491 | (DefaultPath) networkPath(path.primary()), | ... | ... |
... | @@ -292,6 +292,9 @@ public class PathManager implements PathService { | ... | @@ -292,6 +292,9 @@ public class PathManager implements PathService { |
292 | primary = path.primary(); | 292 | primary = path.primary(); |
293 | backup = path.backup(); | 293 | backup = path.backup(); |
294 | } | 294 | } |
295 | + if (backup == null) { | ||
296 | + return new DefaultDisjointPath(PID, (DefaultPath) edgeToEdgePath(srcLink, dstLink, primary)); | ||
297 | + } | ||
295 | return new DefaultDisjointPath(PID, (DefaultPath) edgeToEdgePath(srcLink, dstLink, primary), | 298 | return new DefaultDisjointPath(PID, (DefaultPath) edgeToEdgePath(srcLink, dstLink, primary), |
296 | (DefaultPath) edgeToEdgePath(srcLink, dstLink, backup)); | 299 | (DefaultPath) edgeToEdgePath(srcLink, dstLink, backup)); |
297 | } | 300 | } | ... | ... |
... | @@ -77,12 +77,12 @@ public class PathsWebResource extends AbstractWebResource { | ... | @@ -77,12 +77,12 @@ public class PathsWebResource extends AbstractWebResource { |
77 | } | 77 | } |
78 | 78 | ||
79 | /** | 79 | /** |
80 | - * Gets all shortest disjoint paths between any two hosts or devices. | 80 | + * Gets all shortest disjoint path pairs between any two hosts or devices. |
81 | - * Returns array of all shortest disjoint paths between any two elements. | 81 | + * Returns array of all shortest disjoint path pairs between any two elements. |
82 | * @onos.rsModel Paths | 82 | * @onos.rsModel Paths |
83 | * @param src source identifier | 83 | * @param src source identifier |
84 | * @param dst destination identifier | 84 | * @param dst destination identifier |
85 | - * @return 200 OK with array of all shortest disjoint paths between any two elements | 85 | + * @return 200 OK with array of all shortest disjoint path pairs between any two elements |
86 | */ | 86 | */ |
87 | @GET | 87 | @GET |
88 | @Produces(MediaType.APPLICATION_JSON) | 88 | @Produces(MediaType.APPLICATION_JSON) | ... | ... |
-
Please register or login to post a comment