blob: 94a5bdad8a7c9f838f4ee31021a285b0307c8a8e [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
Ray Milkey1534f8d2015-05-13 15:42:50 -070021import org.junit.Before;
Ray Milkeydb358082015-01-13 16:34:38 -080022import org.junit.Test;
23import org.onlab.packet.IpPrefix;
24import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010025import org.onlab.packet.MplsLabel;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070026import org.onlab.util.Bandwidth;
Ray Milkeydb358082015-01-13 16:34:38 -080027import org.onosproject.codec.JsonCodec;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.DefaultApplicationId;
30import org.onosproject.net.ConnectPoint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080031import org.onosproject.net.DeviceId;
Ray Milkeydb358082015-01-13 16:34:38 -080032import org.onosproject.net.HostId;
33import org.onosproject.net.NetTestTools;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.flow.DefaultTrafficSelector;
36import org.onosproject.net.flow.DefaultTrafficTreatment;
37import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070039import org.onosproject.net.intent.AbstractIntentTest;
Ray Milkeydb358082015-01-13 16:34:38 -080040import org.onosproject.net.intent.Constraint;
41import org.onosproject.net.intent.HostToHostIntent;
Ray Milkey1534f8d2015-05-13 15:42:50 -070042import org.onosproject.net.intent.IntentService;
43import org.onosproject.net.intent.IntentServiceAdapter;
Ray Milkeydb358082015-01-13 16:34:38 -080044import org.onosproject.net.intent.PointToPointIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080045import org.onosproject.net.intent.constraint.AnnotationConstraint;
46import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080047import org.onosproject.net.intent.constraint.BandwidthConstraint;
48import org.onosproject.net.intent.constraint.LambdaConstraint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080049import org.onosproject.net.intent.constraint.LatencyConstraint;
50import org.onosproject.net.intent.constraint.ObstacleConstraint;
51import org.onosproject.net.intent.constraint.WaypointConstraint;
Brian O'Connor6de2e202015-05-21 14:30:41 -070052import org.onosproject.net.resource.link.BandwidthResource;
53import org.onosproject.net.resource.link.LambdaResource;
Ray Milkeydb358082015-01-13 16:34:38 -080054
55import com.fasterxml.jackson.databind.node.ObjectNode;
56import com.google.common.collect.ImmutableList;
57
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070058import static org.hamcrest.MatcherAssert.assertThat;
59import static org.hamcrest.Matchers.notNullValue;
Ray Milkeydb358082015-01-13 16:34:38 -080060import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080061import static org.onosproject.net.NetTestTools.did;
Ray Milkeydb358082015-01-13 16:34:38 -080062import static org.onosproject.net.NetTestTools.hid;
Ray Milkeydb358082015-01-13 16:34:38 -080063
64/**
65 * Unit tests for the host to host intent class codec.
66 */
67public class IntentCodecTest extends AbstractIntentTest {
68
69 private final HostId id1 = hid("12:34:56:78:91:ab/1");
70 private final HostId id2 = hid("12:34:56:78:92:ab/1");
Thomas Vachuska02aeb032015-01-06 22:36:30 -080071 private final ApplicationId appId = new DefaultApplicationId(3, "test");
Ray Milkeydb358082015-01-13 16:34:38 -080072 final TrafficSelector emptySelector =
Brian O'Connor6b528132015-03-10 16:39:52 -070073 DefaultTrafficSelector.emptySelector();
Ray Milkeydb358082015-01-13 16:34:38 -080074 final TrafficTreatment emptyTreatment =
Brian O'Connor6b528132015-03-10 16:39:52 -070075 DefaultTrafficTreatment.emptyTreatment();
Ray Milkey1534f8d2015-05-13 15:42:50 -070076 private final MockCodecContext context = new MockCodecContext();
77
78 @Before
79 public void setUpIntentService() {
80 final IntentService mockIntentService = new IntentServiceAdapter();
81 context.registerService(IntentService.class, mockIntentService);
82 }
Ray Milkeydb358082015-01-13 16:34:38 -080083
84 /**
85 * Tests the encoding of a host to host intent.
86 */
87 @Test
88 public void hostToHostIntent() {
89 final HostToHostIntent intent =
Ray Milkeyebc5d222015-03-18 15:45:36 -070090 HostToHostIntent.builder()
91 .appId(appId)
92 .one(id1)
93 .two(id2)
94 .build();
Ray Milkeydb358082015-01-13 16:34:38 -080095
96 final JsonCodec<HostToHostIntent> intentCodec =
97 context.codec(HostToHostIntent.class);
98 assertThat(intentCodec, notNullValue());
99
100 final ObjectNode intentJson = intentCodec.encode(intent, context);
101 assertThat(intentJson, matchesIntent(intent));
102 }
103
104 /**
105 * Tests the encoding of a point to point intent.
106 */
107 @Test
108 public void pointToPointIntent() {
109 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
110 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
111
112 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700113 PointToPointIntent.builder()
114 .appId(appId)
115 .selector(emptySelector)
116 .treatment(emptyTreatment)
117 .ingressPoint(ingress)
118 .egressPoint(egress).build();
Ray Milkeydb358082015-01-13 16:34:38 -0800119
Ray Milkeydb358082015-01-13 16:34:38 -0800120 final JsonCodec<PointToPointIntent> intentCodec =
121 context.codec(PointToPointIntent.class);
122 assertThat(intentCodec, notNullValue());
123
124 final ObjectNode intentJson = intentCodec.encode(intent, context);
125 assertThat(intentJson, matchesIntent(intent));
126 }
127
128 /**
129 * Tests the encoding of an intent with treatment, selector and constraints
130 * specified.
131 */
132 @Test
133 public void intentWithTreatmentSelectorAndConstraints() {
134 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
135 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800136 DeviceId did1 = did("device1");
137 DeviceId did2 = did("device2");
138 DeviceId did3 = did("device3");
Ray Milkeydb358082015-01-13 16:34:38 -0800139 final TrafficSelector selector = DefaultTrafficSelector.builder()
140 .matchIPProtocol((byte) 3)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100141 .matchMplsLabel(MplsLabel.mplsLabel(4))
Ray Milkeydb358082015-01-13 16:34:38 -0800142 .matchOpticalSignalType((short) 5)
143 .matchLambda((short) 6)
144 .matchEthDst(MacAddress.BROADCAST)
145 .matchIPDst(IpPrefix.valueOf("1.2.3.4/24"))
146 .build();
147 final TrafficTreatment treatment = DefaultTrafficTreatment.builder()
148 .setLambda((short) 33)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100149 .setMpls(MplsLabel.mplsLabel(44))
Ray Milkeydb358082015-01-13 16:34:38 -0800150 .setOutput(PortNumber.CONTROLLER)
151 .setEthDst(MacAddress.BROADCAST)
152 .build();
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800153
154 final List<Constraint> constraints =
155 ImmutableList.of(
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700156 new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(1.0))),
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -0700157 new LambdaConstraint(LambdaResource.valueOf(3)),
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800158 new AnnotationConstraint("key", 33.0),
159 new AsymmetricPathConstraint(),
160 new LatencyConstraint(Duration.ofSeconds(2)),
161 new ObstacleConstraint(did1, did2),
162 new WaypointConstraint(did3));
Ray Milkeydb358082015-01-13 16:34:38 -0800163
164 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700165 PointToPointIntent.builder()
166 .appId(appId)
167 .selector(selector)
168 .treatment(treatment)
169 .ingressPoint(ingress)
170 .egressPoint(egress)
171 .constraints(constraints)
172 .build();
173
Ray Milkeydb358082015-01-13 16:34:38 -0800174
Ray Milkeydb358082015-01-13 16:34:38 -0800175 final JsonCodec<PointToPointIntent> intentCodec =
176 context.codec(PointToPointIntent.class);
177 assertThat(intentCodec, notNullValue());
178
179 final ObjectNode intentJson = intentCodec.encode(intent, context);
180 assertThat(intentJson, matchesIntent(intent));
181
182 }
183}