blob: 811e87a6872f03b29df1afc3ca5a89822bda51c8 [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;
38import org.onosproject.net.intent.Constraint;
39import org.onosproject.net.intent.HostToHostIntent;
40import org.onosproject.net.intent.AbstractIntentTest;
Ray Milkeyc24cde32015-03-10 18:20:18 -070041import org.onosproject.net.intent.Intent;
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;
Ray Milkeydb358082015-01-13 16:34:38 -080050import org.onosproject.net.resource.Bandwidth;
51import org.onosproject.net.resource.Lambda;
52
53import com.fasterxml.jackson.databind.node.ObjectNode;
54import com.google.common.collect.ImmutableList;
55
56import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080057import static org.onosproject.net.NetTestTools.did;
Ray Milkeydb358082015-01-13 16:34:38 -080058import static org.onosproject.net.NetTestTools.hid;
Ray Milkeydb358082015-01-13 16:34:38 -080059import static org.hamcrest.MatcherAssert.assertThat;
60import static org.hamcrest.Matchers.notNullValue;
61
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 =
82 new HostToHostIntent(appId, id1, id2);
83
84 final JsonCodec<HostToHostIntent> intentCodec =
85 context.codec(HostToHostIntent.class);
86 assertThat(intentCodec, notNullValue());
87
88 final ObjectNode intentJson = intentCodec.encode(intent, context);
89 assertThat(intentJson, matchesIntent(intent));
90 }
91
92 /**
93 * Tests the encoding of a point to point intent.
94 */
95 @Test
96 public void pointToPointIntent() {
97 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
98 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
99
100 final PointToPointIntent intent =
101 new PointToPointIntent(appId, emptySelector,
102 emptyTreatment, ingress, egress);
103
104 final CodecContext context = new MockCodecContext();
105 final JsonCodec<PointToPointIntent> intentCodec =
106 context.codec(PointToPointIntent.class);
107 assertThat(intentCodec, notNullValue());
108
109 final ObjectNode intentJson = intentCodec.encode(intent, context);
110 assertThat(intentJson, matchesIntent(intent));
111 }
112
113 /**
114 * Tests the encoding of an intent with treatment, selector and constraints
115 * specified.
116 */
117 @Test
118 public void intentWithTreatmentSelectorAndConstraints() {
119 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
120 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800121 DeviceId did1 = did("device1");
122 DeviceId did2 = did("device2");
123 DeviceId did3 = did("device3");
Ray Milkeydb358082015-01-13 16:34:38 -0800124 final TrafficSelector selector = DefaultTrafficSelector.builder()
125 .matchIPProtocol((byte) 3)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100126 .matchMplsLabel(MplsLabel.mplsLabel(4))
Ray Milkeydb358082015-01-13 16:34:38 -0800127 .matchOpticalSignalType((short) 5)
128 .matchLambda((short) 6)
129 .matchEthDst(MacAddress.BROADCAST)
130 .matchIPDst(IpPrefix.valueOf("1.2.3.4/24"))
131 .build();
132 final TrafficTreatment treatment = DefaultTrafficTreatment.builder()
133 .setLambda((short) 33)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100134 .setMpls(MplsLabel.mplsLabel(44))
Ray Milkeydb358082015-01-13 16:34:38 -0800135 .setOutput(PortNumber.CONTROLLER)
136 .setEthDst(MacAddress.BROADCAST)
137 .build();
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800138
139 final List<Constraint> constraints =
140 ImmutableList.of(
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800141 new BandwidthConstraint(Bandwidth.bps(1.0)),
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800142 new LambdaConstraint(Lambda.valueOf(3)),
143 new AnnotationConstraint("key", 33.0),
144 new AsymmetricPathConstraint(),
145 new LatencyConstraint(Duration.ofSeconds(2)),
146 new ObstacleConstraint(did1, did2),
147 new WaypointConstraint(did3));
Ray Milkeydb358082015-01-13 16:34:38 -0800148
149 final PointToPointIntent intent =
150 new PointToPointIntent(appId, selector, treatment,
Ray Milkeyc24cde32015-03-10 18:20:18 -0700151 ingress, egress, constraints,
152 Intent.DEFAULT_INTENT_PRIORITY);
Ray Milkeydb358082015-01-13 16:34:38 -0800153
154 final CodecContext context = new MockCodecContext();
155 final JsonCodec<PointToPointIntent> intentCodec =
156 context.codec(PointToPointIntent.class);
157 assertThat(intentCodec, notNullValue());
158
159 final ObjectNode intentJson = intentCodec.encode(intent, context);
160 assertThat(intentJson, matchesIntent(intent));
161
162 }
163}