blob: cdbd53379bdf4272e77c61bbf052bbd45bfd2e9c [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;
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -070030import org.onosproject.net.ChannelSpacing;
Ray Milkeydb358082015-01-13 16:34:38 -080031import org.onosproject.net.ConnectPoint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080032import org.onosproject.net.DeviceId;
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -070033import org.onosproject.net.GridType;
Ray Milkeydb358082015-01-13 16:34:38 -080034import org.onosproject.net.HostId;
Sho SHIMIZU9553bb82015-06-30 16:02:45 -070035import org.onosproject.net.IndexedLambda;
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -070036import org.onosproject.net.Lambda;
Ray Milkeydb358082015-01-13 16:34:38 -080037import org.onosproject.net.NetTestTools;
Sho SHIMIZU6c70f642015-05-29 17:27:22 -070038import org.onosproject.net.OchSignalType;
Ray Milkeydb358082015-01-13 16:34:38 -080039import org.onosproject.net.PortNumber;
40import org.onosproject.net.flow.DefaultTrafficSelector;
41import org.onosproject.net.flow.DefaultTrafficTreatment;
42import org.onosproject.net.flow.TrafficSelector;
43import org.onosproject.net.flow.TrafficTreatment;
Sho SHIMIZU6c70f642015-05-29 17:27:22 -070044import org.onosproject.net.flow.criteria.Criteria;
Sho SHIMIZU9553bb82015-06-30 16:02:45 -070045import org.onosproject.net.flow.instructions.Instructions;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070046import org.onosproject.net.intent.AbstractIntentTest;
Ray Milkeydb358082015-01-13 16:34:38 -080047import org.onosproject.net.intent.Constraint;
48import org.onosproject.net.intent.HostToHostIntent;
Ray Milkey1534f8d2015-05-13 15:42:50 -070049import org.onosproject.net.intent.IntentService;
50import org.onosproject.net.intent.IntentServiceAdapter;
Ray Milkeydb358082015-01-13 16:34:38 -080051import org.onosproject.net.intent.PointToPointIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080052import org.onosproject.net.intent.constraint.AnnotationConstraint;
53import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080054import org.onosproject.net.intent.constraint.BandwidthConstraint;
55import org.onosproject.net.intent.constraint.LambdaConstraint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080056import org.onosproject.net.intent.constraint.LatencyConstraint;
57import org.onosproject.net.intent.constraint.ObstacleConstraint;
58import org.onosproject.net.intent.constraint.WaypointConstraint;
Brian O'Connor6de2e202015-05-21 14:30:41 -070059import org.onosproject.net.resource.link.BandwidthResource;
60import org.onosproject.net.resource.link.LambdaResource;
Ray Milkeydb358082015-01-13 16:34:38 -080061
62import com.fasterxml.jackson.databind.node.ObjectNode;
63import com.google.common.collect.ImmutableList;
64
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070065import static org.hamcrest.MatcherAssert.assertThat;
66import static org.hamcrest.Matchers.notNullValue;
Ray Milkeydb358082015-01-13 16:34:38 -080067import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080068import static org.onosproject.net.NetTestTools.did;
Ray Milkeydb358082015-01-13 16:34:38 -080069import static org.onosproject.net.NetTestTools.hid;
Ray Milkeydb358082015-01-13 16:34:38 -080070
71/**
72 * Unit tests for the host to host intent class codec.
73 */
74public class IntentCodecTest extends AbstractIntentTest {
75
76 private final HostId id1 = hid("12:34:56:78:91:ab/1");
77 private final HostId id2 = hid("12:34:56:78:92:ab/1");
Thomas Vachuska02aeb032015-01-06 22:36:30 -080078 private final ApplicationId appId = new DefaultApplicationId(3, "test");
Ray Milkeydb358082015-01-13 16:34:38 -080079 final TrafficSelector emptySelector =
Brian O'Connor6b528132015-03-10 16:39:52 -070080 DefaultTrafficSelector.emptySelector();
Ray Milkeydb358082015-01-13 16:34:38 -080081 final TrafficTreatment emptyTreatment =
Brian O'Connor6b528132015-03-10 16:39:52 -070082 DefaultTrafficTreatment.emptyTreatment();
Ray Milkey1534f8d2015-05-13 15:42:50 -070083 private final MockCodecContext context = new MockCodecContext();
84
85 @Before
86 public void setUpIntentService() {
87 final IntentService mockIntentService = new IntentServiceAdapter();
88 context.registerService(IntentService.class, mockIntentService);
89 }
Ray Milkeydb358082015-01-13 16:34:38 -080090
91 /**
92 * Tests the encoding of a host to host intent.
93 */
94 @Test
95 public void hostToHostIntent() {
96 final HostToHostIntent intent =
Ray Milkeyebc5d222015-03-18 15:45:36 -070097 HostToHostIntent.builder()
98 .appId(appId)
99 .one(id1)
100 .two(id2)
101 .build();
Ray Milkeydb358082015-01-13 16:34:38 -0800102
103 final JsonCodec<HostToHostIntent> intentCodec =
104 context.codec(HostToHostIntent.class);
105 assertThat(intentCodec, notNullValue());
106
107 final ObjectNode intentJson = intentCodec.encode(intent, context);
108 assertThat(intentJson, matchesIntent(intent));
109 }
110
111 /**
112 * Tests the encoding of a point to point intent.
113 */
114 @Test
115 public void pointToPointIntent() {
116 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
117 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
118
119 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700120 PointToPointIntent.builder()
121 .appId(appId)
122 .selector(emptySelector)
123 .treatment(emptyTreatment)
124 .ingressPoint(ingress)
125 .egressPoint(egress).build();
Ray Milkeydb358082015-01-13 16:34:38 -0800126
Ray Milkeydb358082015-01-13 16:34:38 -0800127 final JsonCodec<PointToPointIntent> intentCodec =
128 context.codec(PointToPointIntent.class);
129 assertThat(intentCodec, notNullValue());
130
131 final ObjectNode intentJson = intentCodec.encode(intent, context);
132 assertThat(intentJson, matchesIntent(intent));
133 }
134
135 /**
136 * Tests the encoding of an intent with treatment, selector and constraints
137 * specified.
138 */
139 @Test
140 public void intentWithTreatmentSelectorAndConstraints() {
141 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
142 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800143 DeviceId did1 = did("device1");
144 DeviceId did2 = did("device2");
145 DeviceId did3 = did("device3");
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700146 Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
Ray Milkeydb358082015-01-13 16:34:38 -0800147 final TrafficSelector selector = DefaultTrafficSelector.builder()
148 .matchIPProtocol((byte) 3)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100149 .matchMplsLabel(MplsLabel.mplsLabel(4))
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700150 .add(Criteria.matchOchSignalType(OchSignalType.FIXED_GRID))
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700151 .add(Criteria.matchLambda(ochSignal))
Ray Milkeydb358082015-01-13 16:34:38 -0800152 .matchEthDst(MacAddress.BROADCAST)
153 .matchIPDst(IpPrefix.valueOf("1.2.3.4/24"))
154 .build();
155 final TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Sho SHIMIZU9553bb82015-06-30 16:02:45 -0700156 .add(Instructions.modL0Lambda(new IndexedLambda(33)))
Michele Santuari4b6019e2014-12-19 11:31:45 +0100157 .setMpls(MplsLabel.mplsLabel(44))
Ray Milkeydb358082015-01-13 16:34:38 -0800158 .setOutput(PortNumber.CONTROLLER)
159 .setEthDst(MacAddress.BROADCAST)
160 .build();
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800161
162 final List<Constraint> constraints =
163 ImmutableList.of(
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700164 new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(1.0))),
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -0700165 new LambdaConstraint(LambdaResource.valueOf(3)),
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800166 new AnnotationConstraint("key", 33.0),
167 new AsymmetricPathConstraint(),
168 new LatencyConstraint(Duration.ofSeconds(2)),
169 new ObstacleConstraint(did1, did2),
170 new WaypointConstraint(did3));
Ray Milkeydb358082015-01-13 16:34:38 -0800171
172 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700173 PointToPointIntent.builder()
174 .appId(appId)
175 .selector(selector)
176 .treatment(treatment)
177 .ingressPoint(ingress)
178 .egressPoint(egress)
179 .constraints(constraints)
180 .build();
181
Ray Milkeydb358082015-01-13 16:34:38 -0800182
Ray Milkeydb358082015-01-13 16:34:38 -0800183 final JsonCodec<PointToPointIntent> intentCodec =
184 context.codec(PointToPointIntent.class);
185 assertThat(intentCodec, notNullValue());
186
187 final ObjectNode intentJson = intentCodec.encode(intent, context);
188 assertThat(intentJson, matchesIntent(intent));
189
190 }
191}