blob: afd102506b08256b156788ea2b060f71dcdbeced [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 =
Ray Milkeyebc5d222015-03-18 15:45:36 -070081 HostToHostIntent.builder()
82 .appId(appId)
83 .one(id1)
84 .two(id2)
85 .build();
Ray Milkeydb358082015-01-13 16:34:38 -080086
87 final JsonCodec<HostToHostIntent> intentCodec =
88 context.codec(HostToHostIntent.class);
89 assertThat(intentCodec, notNullValue());
90
91 final ObjectNode intentJson = intentCodec.encode(intent, context);
92 assertThat(intentJson, matchesIntent(intent));
93 }
94
95 /**
96 * Tests the encoding of a point to point intent.
97 */
98 @Test
99 public void pointToPointIntent() {
100 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
101 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
102
103 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700104 PointToPointIntent.builder()
105 .appId(appId)
106 .selector(emptySelector)
107 .treatment(emptyTreatment)
108 .ingressPoint(ingress)
109 .egressPoint(egress).build();
Ray Milkeydb358082015-01-13 16:34:38 -0800110
111 final CodecContext context = new MockCodecContext();
112 final JsonCodec<PointToPointIntent> intentCodec =
113 context.codec(PointToPointIntent.class);
114 assertThat(intentCodec, notNullValue());
115
116 final ObjectNode intentJson = intentCodec.encode(intent, context);
117 assertThat(intentJson, matchesIntent(intent));
118 }
119
120 /**
121 * Tests the encoding of an intent with treatment, selector and constraints
122 * specified.
123 */
124 @Test
125 public void intentWithTreatmentSelectorAndConstraints() {
126 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
127 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800128 DeviceId did1 = did("device1");
129 DeviceId did2 = did("device2");
130 DeviceId did3 = did("device3");
Ray Milkeydb358082015-01-13 16:34:38 -0800131 final TrafficSelector selector = DefaultTrafficSelector.builder()
132 .matchIPProtocol((byte) 3)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100133 .matchMplsLabel(MplsLabel.mplsLabel(4))
Ray Milkeydb358082015-01-13 16:34:38 -0800134 .matchOpticalSignalType((short) 5)
135 .matchLambda((short) 6)
136 .matchEthDst(MacAddress.BROADCAST)
137 .matchIPDst(IpPrefix.valueOf("1.2.3.4/24"))
138 .build();
139 final TrafficTreatment treatment = DefaultTrafficTreatment.builder()
140 .setLambda((short) 33)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100141 .setMpls(MplsLabel.mplsLabel(44))
Ray Milkeydb358082015-01-13 16:34:38 -0800142 .setOutput(PortNumber.CONTROLLER)
143 .setEthDst(MacAddress.BROADCAST)
144 .build();
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800145
146 final List<Constraint> constraints =
147 ImmutableList.of(
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800148 new BandwidthConstraint(Bandwidth.bps(1.0)),
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800149 new LambdaConstraint(Lambda.valueOf(3)),
150 new AnnotationConstraint("key", 33.0),
151 new AsymmetricPathConstraint(),
152 new LatencyConstraint(Duration.ofSeconds(2)),
153 new ObstacleConstraint(did1, did2),
154 new WaypointConstraint(did3));
Ray Milkeydb358082015-01-13 16:34:38 -0800155
156 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700157 PointToPointIntent.builder()
158 .appId(appId)
159 .selector(selector)
160 .treatment(treatment)
161 .ingressPoint(ingress)
162 .egressPoint(egress)
163 .constraints(constraints)
164 .build();
165
Ray Milkeydb358082015-01-13 16:34:38 -0800166
167 final CodecContext context = new MockCodecContext();
168 final JsonCodec<PointToPointIntent> intentCodec =
169 context.codec(PointToPointIntent.class);
170 assertThat(intentCodec, notNullValue());
171
172 final ObjectNode intentJson = intentCodec.encode(intent, context);
173 assertThat(intentJson, matchesIntent(intent));
174
175 }
176}