blob: 0318b8f8df965d9018dd0c1c83325e29a4b40885 [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;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070025import org.onlab.util.Bandwidth;
Ray Milkeydb358082015-01-13 16:34:38 -080026import org.onosproject.codec.CodecContext;
27import 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 Milkeydb358082015-01-13 16:34:38 -080042import org.onosproject.net.intent.PointToPointIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080043import org.onosproject.net.intent.constraint.AnnotationConstraint;
44import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080045import org.onosproject.net.intent.constraint.BandwidthConstraint;
46import org.onosproject.net.intent.constraint.LambdaConstraint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080047import org.onosproject.net.intent.constraint.LatencyConstraint;
48import org.onosproject.net.intent.constraint.ObstacleConstraint;
49import org.onosproject.net.intent.constraint.WaypointConstraint;
Sho SHIMIZU63feca72015-05-07 10:44:25 -070050import org.onosproject.net.resource.BandwidthResource;
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -070051import org.onosproject.net.resource.LambdaResource;
Ray Milkeydb358082015-01-13 16:34:38 -080052
53import com.fasterxml.jackson.databind.node.ObjectNode;
54import com.google.common.collect.ImmutableList;
55
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070056import static org.hamcrest.MatcherAssert.assertThat;
57import static org.hamcrest.Matchers.notNullValue;
Ray Milkeydb358082015-01-13 16:34:38 -080058import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080059import static org.onosproject.net.NetTestTools.did;
Ray Milkeydb358082015-01-13 16:34:38 -080060import static org.onosproject.net.NetTestTools.hid;
Ray Milkeydb358082015-01-13 16:34:38 -080061
62/**
63 * Unit tests for the host to host intent class codec.
64 */
65public class IntentCodecTest extends AbstractIntentTest {
66
67 private final HostId id1 = hid("12:34:56:78:91:ab/1");
68 private final HostId id2 = hid("12:34:56:78:92:ab/1");
Thomas Vachuska02aeb032015-01-06 22:36:30 -080069 private final ApplicationId appId = new DefaultApplicationId(3, "test");
Ray Milkeydb358082015-01-13 16:34:38 -080070 final TrafficSelector emptySelector =
Brian O'Connor6b528132015-03-10 16:39:52 -070071 DefaultTrafficSelector.emptySelector();
Ray Milkeydb358082015-01-13 16:34:38 -080072 final TrafficTreatment emptyTreatment =
Brian O'Connor6b528132015-03-10 16:39:52 -070073 DefaultTrafficTreatment.emptyTreatment();
Ray Milkeydb358082015-01-13 16:34:38 -080074 private final CodecContext context = new MockCodecContext();
75
76 /**
77 * Tests the encoding of a host to host intent.
78 */
79 @Test
80 public void hostToHostIntent() {
81 final HostToHostIntent intent =
Ray Milkeyebc5d222015-03-18 15:45:36 -070082 HostToHostIntent.builder()
83 .appId(appId)
84 .one(id1)
85 .two(id2)
86 .build();
Ray Milkeydb358082015-01-13 16:34:38 -080087
88 final JsonCodec<HostToHostIntent> intentCodec =
89 context.codec(HostToHostIntent.class);
90 assertThat(intentCodec, notNullValue());
91
92 final ObjectNode intentJson = intentCodec.encode(intent, context);
93 assertThat(intentJson, matchesIntent(intent));
94 }
95
96 /**
97 * Tests the encoding of a point to point intent.
98 */
99 @Test
100 public void pointToPointIntent() {
101 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
102 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
103
104 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700105 PointToPointIntent.builder()
106 .appId(appId)
107 .selector(emptySelector)
108 .treatment(emptyTreatment)
109 .ingressPoint(ingress)
110 .egressPoint(egress).build();
Ray Milkeydb358082015-01-13 16:34:38 -0800111
112 final CodecContext context = new MockCodecContext();
113 final JsonCodec<PointToPointIntent> intentCodec =
114 context.codec(PointToPointIntent.class);
115 assertThat(intentCodec, notNullValue());
116
117 final ObjectNode intentJson = intentCodec.encode(intent, context);
118 assertThat(intentJson, matchesIntent(intent));
119 }
120
121 /**
122 * Tests the encoding of an intent with treatment, selector and constraints
123 * specified.
124 */
125 @Test
126 public void intentWithTreatmentSelectorAndConstraints() {
127 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
128 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800129 DeviceId did1 = did("device1");
130 DeviceId did2 = did("device2");
131 DeviceId did3 = did("device3");
Ray Milkeydb358082015-01-13 16:34:38 -0800132 final TrafficSelector selector = DefaultTrafficSelector.builder()
133 .matchIPProtocol((byte) 3)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100134 .matchMplsLabel(MplsLabel.mplsLabel(4))
Ray Milkeydb358082015-01-13 16:34:38 -0800135 .matchOpticalSignalType((short) 5)
136 .matchLambda((short) 6)
137 .matchEthDst(MacAddress.BROADCAST)
138 .matchIPDst(IpPrefix.valueOf("1.2.3.4/24"))
139 .build();
140 final TrafficTreatment treatment = DefaultTrafficTreatment.builder()
141 .setLambda((short) 33)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100142 .setMpls(MplsLabel.mplsLabel(44))
Ray Milkeydb358082015-01-13 16:34:38 -0800143 .setOutput(PortNumber.CONTROLLER)
144 .setEthDst(MacAddress.BROADCAST)
145 .build();
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800146
147 final List<Constraint> constraints =
148 ImmutableList.of(
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700149 new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(1.0))),
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -0700150 new LambdaConstraint(LambdaResource.valueOf(3)),
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800151 new AnnotationConstraint("key", 33.0),
152 new AsymmetricPathConstraint(),
153 new LatencyConstraint(Duration.ofSeconds(2)),
154 new ObstacleConstraint(did1, did2),
155 new WaypointConstraint(did3));
Ray Milkeydb358082015-01-13 16:34:38 -0800156
157 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700158 PointToPointIntent.builder()
159 .appId(appId)
160 .selector(selector)
161 .treatment(treatment)
162 .ingressPoint(ingress)
163 .egressPoint(egress)
164 .constraints(constraints)
165 .build();
166
Ray Milkeydb358082015-01-13 16:34:38 -0800167
168 final CodecContext context = new MockCodecContext();
169 final JsonCodec<PointToPointIntent> intentCodec =
170 context.codec(PointToPointIntent.class);
171 assertThat(intentCodec, notNullValue());
172
173 final ObjectNode intentJson = intentCodec.encode(intent, context);
174 assertThat(intentJson, matchesIntent(intent));
175
176 }
177}