blob: 57274d84cd1e27410a1046f54a866d827e7a893a [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;
Michele Santuari4b6019e2014-12-19 11:31:45 +010024import org.onlab.packet.MplsLabel;
Ray Milkeydb358082015-01-13 16:34:38 -080025import org.onosproject.codec.CodecContext;
26import org.onosproject.codec.JsonCodec;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.DefaultApplicationId;
29import org.onosproject.net.ConnectPoint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080030import org.onosproject.net.DeviceId;
Ray Milkeydb358082015-01-13 16:34:38 -080031import org.onosproject.net.HostId;
32import org.onosproject.net.NetTestTools;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.flow.DefaultTrafficSelector;
35import org.onosproject.net.flow.DefaultTrafficTreatment;
36import org.onosproject.net.flow.TrafficSelector;
37import org.onosproject.net.flow.TrafficTreatment;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070038import org.onosproject.net.intent.AbstractIntentTest;
Ray Milkeydb358082015-01-13 16:34:38 -080039import org.onosproject.net.intent.Constraint;
40import org.onosproject.net.intent.HostToHostIntent;
Ray Milkeydb358082015-01-13 16:34:38 -080041import org.onosproject.net.intent.PointToPointIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080042import org.onosproject.net.intent.constraint.AnnotationConstraint;
43import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080044import org.onosproject.net.intent.constraint.BandwidthConstraint;
45import org.onosproject.net.intent.constraint.LambdaConstraint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080046import org.onosproject.net.intent.constraint.LatencyConstraint;
47import org.onosproject.net.intent.constraint.ObstacleConstraint;
48import org.onosproject.net.intent.constraint.WaypointConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080049import org.onosproject.net.resource.Bandwidth;
50import org.onosproject.net.resource.Lambda;
51
52import com.fasterxml.jackson.databind.node.ObjectNode;
53import com.google.common.collect.ImmutableList;
54
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070055import static org.hamcrest.MatcherAssert.assertThat;
56import static org.hamcrest.Matchers.notNullValue;
Ray Milkeydb358082015-01-13 16:34:38 -080057import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080058import static org.onosproject.net.NetTestTools.did;
Ray Milkeydb358082015-01-13 16:34:38 -080059import static org.onosproject.net.NetTestTools.hid;
Ray Milkeydb358082015-01-13 16:34:38 -080060
61/**
62 * Unit tests for the host to host intent class codec.
63 */
64public class IntentCodecTest extends AbstractIntentTest {
65
66 private final HostId id1 = hid("12:34:56:78:91:ab/1");
67 private final HostId id2 = hid("12:34:56:78:92:ab/1");
Thomas Vachuska02aeb032015-01-06 22:36:30 -080068 private final ApplicationId appId = new DefaultApplicationId(3, "test");
Ray Milkeydb358082015-01-13 16:34:38 -080069 final TrafficSelector emptySelector =
Brian O'Connor6b528132015-03-10 16:39:52 -070070 DefaultTrafficSelector.emptySelector();
Ray Milkeydb358082015-01-13 16:34:38 -080071 final TrafficTreatment emptyTreatment =
Brian O'Connor6b528132015-03-10 16:39:52 -070072 DefaultTrafficTreatment.emptyTreatment();
Ray Milkeydb358082015-01-13 16:34:38 -080073 private final CodecContext context = new MockCodecContext();
74
75 /**
76 * Tests the encoding of a host to host intent.
77 */
78 @Test
79 public void hostToHostIntent() {
80 final HostToHostIntent intent =
81 new HostToHostIntent(appId, id1, id2);
82
83 final JsonCodec<HostToHostIntent> intentCodec =
84 context.codec(HostToHostIntent.class);
85 assertThat(intentCodec, notNullValue());
86
87 final ObjectNode intentJson = intentCodec.encode(intent, context);
88 assertThat(intentJson, matchesIntent(intent));
89 }
90
91 /**
92 * Tests the encoding of a point to point intent.
93 */
94 @Test
95 public void pointToPointIntent() {
96 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
97 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
98
99 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700100 PointToPointIntent.builder()
101 .appId(appId)
102 .selector(emptySelector)
103 .treatment(emptyTreatment)
104 .ingressPoint(ingress)
105 .egressPoint(egress).build();
Ray Milkeydb358082015-01-13 16:34:38 -0800106
107 final CodecContext context = new MockCodecContext();
108 final JsonCodec<PointToPointIntent> intentCodec =
109 context.codec(PointToPointIntent.class);
110 assertThat(intentCodec, notNullValue());
111
112 final ObjectNode intentJson = intentCodec.encode(intent, context);
113 assertThat(intentJson, matchesIntent(intent));
114 }
115
116 /**
117 * Tests the encoding of an intent with treatment, selector and constraints
118 * specified.
119 */
120 @Test
121 public void intentWithTreatmentSelectorAndConstraints() {
122 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
123 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800124 DeviceId did1 = did("device1");
125 DeviceId did2 = did("device2");
126 DeviceId did3 = did("device3");
Ray Milkeydb358082015-01-13 16:34:38 -0800127 final TrafficSelector selector = DefaultTrafficSelector.builder()
128 .matchIPProtocol((byte) 3)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100129 .matchMplsLabel(MplsLabel.mplsLabel(4))
Ray Milkeydb358082015-01-13 16:34:38 -0800130 .matchOpticalSignalType((short) 5)
131 .matchLambda((short) 6)
132 .matchEthDst(MacAddress.BROADCAST)
133 .matchIPDst(IpPrefix.valueOf("1.2.3.4/24"))
134 .build();
135 final TrafficTreatment treatment = DefaultTrafficTreatment.builder()
136 .setLambda((short) 33)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100137 .setMpls(MplsLabel.mplsLabel(44))
Ray Milkeydb358082015-01-13 16:34:38 -0800138 .setOutput(PortNumber.CONTROLLER)
139 .setEthDst(MacAddress.BROADCAST)
140 .build();
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800141
142 final List<Constraint> constraints =
143 ImmutableList.of(
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800144 new BandwidthConstraint(Bandwidth.bps(1.0)),
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800145 new LambdaConstraint(Lambda.valueOf(3)),
146 new AnnotationConstraint("key", 33.0),
147 new AsymmetricPathConstraint(),
148 new LatencyConstraint(Duration.ofSeconds(2)),
149 new ObstacleConstraint(did1, did2),
150 new WaypointConstraint(did3));
Ray Milkeydb358082015-01-13 16:34:38 -0800151
152 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700153 PointToPointIntent.builder()
154 .appId(appId)
155 .selector(selector)
156 .treatment(treatment)
157 .ingressPoint(ingress)
158 .egressPoint(egress)
159 .constraints(constraints)
160 .build();
161
Ray Milkeydb358082015-01-13 16:34:38 -0800162
163 final CodecContext context = new MockCodecContext();
164 final JsonCodec<PointToPointIntent> intentCodec =
165 context.codec(PointToPointIntent.class);
166 assertThat(intentCodec, notNullValue());
167
168 final ObjectNode intentJson = intentCodec.encode(intent, context);
169 assertThat(intentJson, matchesIntent(intent));
170
171 }
172}