blob: 95866bfffffe409bd451d968452dd041a8469c82 [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 SHIMIZUc44c0c32015-06-01 11:40:48 -070035import org.onosproject.net.Lambda;
Ray Milkeydb358082015-01-13 16:34:38 -080036import org.onosproject.net.NetTestTools;
Sho SHIMIZU6c70f642015-05-29 17:27:22 -070037import org.onosproject.net.OchSignalType;
Ray Milkeydb358082015-01-13 16:34:38 -080038import org.onosproject.net.PortNumber;
39import org.onosproject.net.flow.DefaultTrafficSelector;
40import org.onosproject.net.flow.DefaultTrafficTreatment;
41import org.onosproject.net.flow.TrafficSelector;
42import org.onosproject.net.flow.TrafficTreatment;
Sho SHIMIZU6c70f642015-05-29 17:27:22 -070043import org.onosproject.net.flow.criteria.Criteria;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070044import org.onosproject.net.intent.AbstractIntentTest;
Ray Milkeydb358082015-01-13 16:34:38 -080045import org.onosproject.net.intent.Constraint;
46import org.onosproject.net.intent.HostToHostIntent;
Ray Milkey1534f8d2015-05-13 15:42:50 -070047import org.onosproject.net.intent.IntentService;
48import org.onosproject.net.intent.IntentServiceAdapter;
Ray Milkeydb358082015-01-13 16:34:38 -080049import org.onosproject.net.intent.PointToPointIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080050import org.onosproject.net.intent.constraint.AnnotationConstraint;
51import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080052import org.onosproject.net.intent.constraint.BandwidthConstraint;
53import org.onosproject.net.intent.constraint.LambdaConstraint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080054import org.onosproject.net.intent.constraint.LatencyConstraint;
55import org.onosproject.net.intent.constraint.ObstacleConstraint;
56import org.onosproject.net.intent.constraint.WaypointConstraint;
Brian O'Connor6de2e202015-05-21 14:30:41 -070057import org.onosproject.net.resource.link.BandwidthResource;
58import org.onosproject.net.resource.link.LambdaResource;
Ray Milkeydb358082015-01-13 16:34:38 -080059
60import com.fasterxml.jackson.databind.node.ObjectNode;
61import com.google.common.collect.ImmutableList;
62
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070063import static org.hamcrest.MatcherAssert.assertThat;
64import static org.hamcrest.Matchers.notNullValue;
Ray Milkeydb358082015-01-13 16:34:38 -080065import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080066import static org.onosproject.net.NetTestTools.did;
Ray Milkeydb358082015-01-13 16:34:38 -080067import static org.onosproject.net.NetTestTools.hid;
Ray Milkeydb358082015-01-13 16:34:38 -080068
69/**
70 * Unit tests for the host to host intent class codec.
71 */
72public class IntentCodecTest extends AbstractIntentTest {
73
74 private final HostId id1 = hid("12:34:56:78:91:ab/1");
75 private final HostId id2 = hid("12:34:56:78:92:ab/1");
Thomas Vachuska02aeb032015-01-06 22:36:30 -080076 private final ApplicationId appId = new DefaultApplicationId(3, "test");
Ray Milkeydb358082015-01-13 16:34:38 -080077 final TrafficSelector emptySelector =
Brian O'Connor6b528132015-03-10 16:39:52 -070078 DefaultTrafficSelector.emptySelector();
Ray Milkeydb358082015-01-13 16:34:38 -080079 final TrafficTreatment emptyTreatment =
Brian O'Connor6b528132015-03-10 16:39:52 -070080 DefaultTrafficTreatment.emptyTreatment();
Ray Milkey1534f8d2015-05-13 15:42:50 -070081 private final MockCodecContext context = new MockCodecContext();
82
83 @Before
84 public void setUpIntentService() {
85 final IntentService mockIntentService = new IntentServiceAdapter();
86 context.registerService(IntentService.class, mockIntentService);
87 }
Ray Milkeydb358082015-01-13 16:34:38 -080088
89 /**
90 * Tests the encoding of a host to host intent.
91 */
92 @Test
93 public void hostToHostIntent() {
94 final HostToHostIntent intent =
Ray Milkeyebc5d222015-03-18 15:45:36 -070095 HostToHostIntent.builder()
96 .appId(appId)
97 .one(id1)
98 .two(id2)
99 .build();
Ray Milkeydb358082015-01-13 16:34:38 -0800100
101 final JsonCodec<HostToHostIntent> intentCodec =
102 context.codec(HostToHostIntent.class);
103 assertThat(intentCodec, notNullValue());
104
105 final ObjectNode intentJson = intentCodec.encode(intent, context);
106 assertThat(intentJson, matchesIntent(intent));
107 }
108
109 /**
110 * Tests the encoding of a point to point intent.
111 */
112 @Test
113 public void pointToPointIntent() {
114 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
115 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
116
117 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700118 PointToPointIntent.builder()
119 .appId(appId)
120 .selector(emptySelector)
121 .treatment(emptyTreatment)
122 .ingressPoint(ingress)
123 .egressPoint(egress).build();
Ray Milkeydb358082015-01-13 16:34:38 -0800124
Ray Milkeydb358082015-01-13 16:34:38 -0800125 final JsonCodec<PointToPointIntent> intentCodec =
126 context.codec(PointToPointIntent.class);
127 assertThat(intentCodec, notNullValue());
128
129 final ObjectNode intentJson = intentCodec.encode(intent, context);
130 assertThat(intentJson, matchesIntent(intent));
131 }
132
133 /**
134 * Tests the encoding of an intent with treatment, selector and constraints
135 * specified.
136 */
137 @Test
138 public void intentWithTreatmentSelectorAndConstraints() {
139 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
140 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800141 DeviceId did1 = did("device1");
142 DeviceId did2 = did("device2");
143 DeviceId did3 = did("device3");
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700144 Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
Ray Milkeydb358082015-01-13 16:34:38 -0800145 final TrafficSelector selector = DefaultTrafficSelector.builder()
146 .matchIPProtocol((byte) 3)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100147 .matchMplsLabel(MplsLabel.mplsLabel(4))
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700148 .add(Criteria.matchOchSignalType(OchSignalType.FIXED_GRID))
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700149 .add(Criteria.matchLambda(ochSignal))
Ray Milkeydb358082015-01-13 16:34:38 -0800150 .matchEthDst(MacAddress.BROADCAST)
151 .matchIPDst(IpPrefix.valueOf("1.2.3.4/24"))
152 .build();
153 final TrafficTreatment treatment = DefaultTrafficTreatment.builder()
154 .setLambda((short) 33)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100155 .setMpls(MplsLabel.mplsLabel(44))
Ray Milkeydb358082015-01-13 16:34:38 -0800156 .setOutput(PortNumber.CONTROLLER)
157 .setEthDst(MacAddress.BROADCAST)
158 .build();
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800159
160 final List<Constraint> constraints =
161 ImmutableList.of(
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700162 new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(1.0))),
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -0700163 new LambdaConstraint(LambdaResource.valueOf(3)),
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800164 new AnnotationConstraint("key", 33.0),
165 new AsymmetricPathConstraint(),
166 new LatencyConstraint(Duration.ofSeconds(2)),
167 new ObstacleConstraint(did1, did2),
168 new WaypointConstraint(did3));
Ray Milkeydb358082015-01-13 16:34:38 -0800169
170 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700171 PointToPointIntent.builder()
172 .appId(appId)
173 .selector(selector)
174 .treatment(treatment)
175 .ingressPoint(ingress)
176 .egressPoint(egress)
177 .constraints(constraints)
178 .build();
179
Ray Milkeydb358082015-01-13 16:34:38 -0800180
Ray Milkeydb358082015-01-13 16:34:38 -0800181 final JsonCodec<PointToPointIntent> intentCodec =
182 context.codec(PointToPointIntent.class);
183 assertThat(intentCodec, notNullValue());
184
185 final ObjectNode intentJson = intentCodec.encode(intent, context);
186 assertThat(intentJson, matchesIntent(intent));
187
188 }
189}