blob: a2302732e804d0a38a0252b87b3750e6e185e9a8 [file] [log] [blame]
Ray Milkeydb358082015-01-13 16:34:38 -08001/*
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.codec.impl;
17
Ray Milkeyb9a8a182015-01-20 14:15:35 -080018import java.time.Duration;
Ray Milkeydb358082015-01-13 16:34:38 -080019import java.util.List;
20
21import org.junit.Test;
22import org.onlab.packet.IpPrefix;
23import org.onlab.packet.MacAddress;
24import org.onosproject.codec.CodecContext;
25import org.onosproject.codec.JsonCodec;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.DefaultApplicationId;
28import org.onosproject.net.ConnectPoint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080029import org.onosproject.net.DeviceId;
Ray Milkeydb358082015-01-13 16:34:38 -080030import org.onosproject.net.HostId;
31import org.onosproject.net.NetTestTools;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.flow.DefaultTrafficSelector;
34import org.onosproject.net.flow.DefaultTrafficTreatment;
35import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
37import org.onosproject.net.intent.Constraint;
38import org.onosproject.net.intent.HostToHostIntent;
39import org.onosproject.net.intent.AbstractIntentTest;
40import org.onosproject.net.intent.PointToPointIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080041import org.onosproject.net.intent.constraint.AnnotationConstraint;
42import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080043import org.onosproject.net.intent.constraint.BandwidthConstraint;
44import org.onosproject.net.intent.constraint.LambdaConstraint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080045import org.onosproject.net.intent.constraint.LatencyConstraint;
46import org.onosproject.net.intent.constraint.ObstacleConstraint;
47import org.onosproject.net.intent.constraint.WaypointConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080048import org.onosproject.net.resource.Bandwidth;
49import org.onosproject.net.resource.Lambda;
50
51import com.fasterxml.jackson.databind.node.ObjectNode;
52import com.google.common.collect.ImmutableList;
53
54import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080055import static org.onosproject.net.NetTestTools.did;
Ray Milkeydb358082015-01-13 16:34:38 -080056import static org.onosproject.net.NetTestTools.hid;
57
58
59import static org.hamcrest.MatcherAssert.assertThat;
60import static org.hamcrest.Matchers.notNullValue;
61
62/**
63 * Unit tests for the host to host intent class codec.
64 */
65public class IntentCodecTest extends AbstractIntentTest {
66
67 private final HostId id1 = hid("12:34:56:78:91:ab/1");
68 private final HostId id2 = hid("12:34:56:78:92:ab/1");
69 private final ApplicationId appId =
70 new DefaultApplicationId((short) 3, "test");
71 final TrafficSelector emptySelector =
72 DefaultTrafficSelector.builder().build();
73 final TrafficTreatment emptyTreatment =
74 DefaultTrafficTreatment.builder().build();
75 private final CodecContext context = new MockCodecContext();
76
77 /**
78 * Tests the encoding of a host to host intent.
79 */
80 @Test
81 public void hostToHostIntent() {
82 final HostToHostIntent intent =
83 new HostToHostIntent(appId, id1, id2);
84
85 final JsonCodec<HostToHostIntent> intentCodec =
86 context.codec(HostToHostIntent.class);
87 assertThat(intentCodec, notNullValue());
88
89 final ObjectNode intentJson = intentCodec.encode(intent, context);
90 assertThat(intentJson, matchesIntent(intent));
91 }
92
93 /**
94 * Tests the encoding of a point to point intent.
95 */
96 @Test
97 public void pointToPointIntent() {
98 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
99 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
100
101 final PointToPointIntent intent =
102 new PointToPointIntent(appId, emptySelector,
103 emptyTreatment, ingress, egress);
104
105 final CodecContext context = new MockCodecContext();
106 final JsonCodec<PointToPointIntent> intentCodec =
107 context.codec(PointToPointIntent.class);
108 assertThat(intentCodec, notNullValue());
109
110 final ObjectNode intentJson = intentCodec.encode(intent, context);
111 assertThat(intentJson, matchesIntent(intent));
112 }
113
114 /**
115 * Tests the encoding of an intent with treatment, selector and constraints
116 * specified.
117 */
118 @Test
119 public void intentWithTreatmentSelectorAndConstraints() {
120 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
121 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800122 DeviceId did1 = did("device1");
123 DeviceId did2 = did("device2");
124 DeviceId did3 = did("device3");
Ray Milkeydb358082015-01-13 16:34:38 -0800125 final TrafficSelector selector = DefaultTrafficSelector.builder()
126 .matchIPProtocol((byte) 3)
127 .matchMplsLabel(4)
128 .matchOpticalSignalType((short) 5)
129 .matchLambda((short) 6)
130 .matchEthDst(MacAddress.BROADCAST)
131 .matchIPDst(IpPrefix.valueOf("1.2.3.4/24"))
132 .build();
133 final TrafficTreatment treatment = DefaultTrafficTreatment.builder()
134 .setLambda((short) 33)
135 .setMpls(44)
136 .setOutput(PortNumber.CONTROLLER)
137 .setEthDst(MacAddress.BROADCAST)
138 .build();
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800139
140 final List<Constraint> constraints =
141 ImmutableList.of(
142 new BandwidthConstraint(Bandwidth.valueOf(1.0)),
143 new LambdaConstraint(Lambda.valueOf(3)),
144 new AnnotationConstraint("key", 33.0),
145 new AsymmetricPathConstraint(),
146 new LatencyConstraint(Duration.ofSeconds(2)),
147 new ObstacleConstraint(did1, did2),
148 new WaypointConstraint(did3));
Ray Milkeydb358082015-01-13 16:34:38 -0800149
150 final PointToPointIntent intent =
151 new PointToPointIntent(appId, selector, treatment,
152 ingress, egress, constraints);
153
154 final CodecContext context = new MockCodecContext();
155 final JsonCodec<PointToPointIntent> intentCodec =
156 context.codec(PointToPointIntent.class);
157 assertThat(intentCodec, notNullValue());
158
159 final ObjectNode intentJson = intentCodec.encode(intent, context);
160 assertThat(intentJson, matchesIntent(intent));
161
162 }
163}