blob: d96f48121e7519dda3ef3191e560379354d4142d [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;
24import org.onosproject.codec.CodecContext;
25import org.onosproject.codec.JsonCodec;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.DefaultApplicationId;
28import org.onosproject.net.ConnectPoint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080029import org.onosproject.net.DeviceId;
Ray Milkeydb358082015-01-13 16:34:38 -080030import org.onosproject.net.HostId;
31import org.onosproject.net.NetTestTools;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.flow.DefaultTrafficSelector;
34import org.onosproject.net.flow.DefaultTrafficTreatment;
35import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
37import org.onosproject.net.intent.Constraint;
38import org.onosproject.net.intent.HostToHostIntent;
39import org.onosproject.net.intent.AbstractIntentTest;
40import org.onosproject.net.intent.PointToPointIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080041import org.onosproject.net.intent.constraint.AnnotationConstraint;
42import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080043import org.onosproject.net.intent.constraint.BandwidthConstraint;
44import org.onosproject.net.intent.constraint.LambdaConstraint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080045import org.onosproject.net.intent.constraint.LatencyConstraint;
46import org.onosproject.net.intent.constraint.ObstacleConstraint;
47import org.onosproject.net.intent.constraint.WaypointConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080048import org.onosproject.net.resource.Bandwidth;
49import org.onosproject.net.resource.Lambda;
50
51import com.fasterxml.jackson.databind.node.ObjectNode;
52import com.google.common.collect.ImmutableList;
53
54import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080055import static org.onosproject.net.NetTestTools.did;
Ray Milkeydb358082015-01-13 16:34:38 -080056import static org.onosproject.net.NetTestTools.hid;
57
58
59import 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 =
71 DefaultTrafficSelector.builder().build();
72 final TrafficTreatment emptyTreatment =
73 DefaultTrafficTreatment.builder().build();
74 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)
126 .matchMplsLabel(4)
127 .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)
134 .setMpls(44)
135 .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(
141 new BandwidthConstraint(Bandwidth.valueOf(1.0)),
142 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,
151 ingress, egress, constraints);
152
153 final CodecContext context = new MockCodecContext();
154 final JsonCodec<PointToPointIntent> intentCodec =
155 context.codec(PointToPointIntent.class);
156 assertThat(intentCodec, notNullValue());
157
158 final ObjectNode intentJson = intentCodec.encode(intent, context);
159 assertThat(intentJson, matchesIntent(intent));
160
161 }
162}