blob: fd84f1701c86f5504d75d5975504a60e6bd9d318 [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
18import java.util.List;
19
20import org.junit.Test;
21import org.onlab.packet.IpPrefix;
22import org.onlab.packet.MacAddress;
23import org.onosproject.codec.CodecContext;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.DefaultApplicationId;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.HostId;
29import org.onosproject.net.NetTestTools;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.TrafficSelector;
34import org.onosproject.net.flow.TrafficTreatment;
35import org.onosproject.net.intent.Constraint;
36import org.onosproject.net.intent.HostToHostIntent;
37import org.onosproject.net.intent.AbstractIntentTest;
38import org.onosproject.net.intent.PointToPointIntent;
39import org.onosproject.net.intent.constraint.BandwidthConstraint;
40import org.onosproject.net.intent.constraint.LambdaConstraint;
41import org.onosproject.net.resource.Bandwidth;
42import org.onosproject.net.resource.Lambda;
43
44import com.fasterxml.jackson.databind.node.ObjectNode;
45import com.google.common.collect.ImmutableList;
46
47import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent;
48import static org.onosproject.net.NetTestTools.hid;
49
50
51import static org.hamcrest.MatcherAssert.assertThat;
52import static org.hamcrest.Matchers.notNullValue;
53
54/**
55 * Unit tests for the host to host intent class codec.
56 */
57public class IntentCodecTest extends AbstractIntentTest {
58
59 private final HostId id1 = hid("12:34:56:78:91:ab/1");
60 private final HostId id2 = hid("12:34:56:78:92:ab/1");
61 private final ApplicationId appId =
62 new DefaultApplicationId((short) 3, "test");
63 final TrafficSelector emptySelector =
64 DefaultTrafficSelector.builder().build();
65 final TrafficTreatment emptyTreatment =
66 DefaultTrafficTreatment.builder().build();
67 private final CodecContext context = new MockCodecContext();
68
69 /**
70 * Tests the encoding of a host to host intent.
71 */
72 @Test
73 public void hostToHostIntent() {
74 final HostToHostIntent intent =
75 new HostToHostIntent(appId, id1, id2);
76
77 final JsonCodec<HostToHostIntent> intentCodec =
78 context.codec(HostToHostIntent.class);
79 assertThat(intentCodec, notNullValue());
80
81 final ObjectNode intentJson = intentCodec.encode(intent, context);
82 assertThat(intentJson, matchesIntent(intent));
83 }
84
85 /**
86 * Tests the encoding of a point to point intent.
87 */
88 @Test
89 public void pointToPointIntent() {
90 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
91 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
92
93 final PointToPointIntent intent =
94 new PointToPointIntent(appId, emptySelector,
95 emptyTreatment, ingress, egress);
96
97 final CodecContext context = new MockCodecContext();
98 final JsonCodec<PointToPointIntent> intentCodec =
99 context.codec(PointToPointIntent.class);
100 assertThat(intentCodec, notNullValue());
101
102 final ObjectNode intentJson = intentCodec.encode(intent, context);
103 assertThat(intentJson, matchesIntent(intent));
104 }
105
106 /**
107 * Tests the encoding of an intent with treatment, selector and constraints
108 * specified.
109 */
110 @Test
111 public void intentWithTreatmentSelectorAndConstraints() {
112 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
113 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
114 final TrafficSelector selector = DefaultTrafficSelector.builder()
115 .matchIPProtocol((byte) 3)
116 .matchMplsLabel(4)
117 .matchOpticalSignalType((short) 5)
118 .matchLambda((short) 6)
119 .matchEthDst(MacAddress.BROADCAST)
120 .matchIPDst(IpPrefix.valueOf("1.2.3.4/24"))
121 .build();
122 final TrafficTreatment treatment = DefaultTrafficTreatment.builder()
123 .setLambda((short) 33)
124 .setMpls(44)
125 .setOutput(PortNumber.CONTROLLER)
126 .setEthDst(MacAddress.BROADCAST)
127 .build();
128 final Constraint constraint1 = new BandwidthConstraint(Bandwidth.valueOf(1.0));
129 final Constraint constraint2 = new LambdaConstraint(Lambda.valueOf(3));
130 final List<Constraint> constraints = ImmutableList.of(constraint1, constraint2);
131
132 final PointToPointIntent intent =
133 new PointToPointIntent(appId, selector, treatment,
134 ingress, egress, constraints);
135
136 final CodecContext context = new MockCodecContext();
137 final JsonCodec<PointToPointIntent> intentCodec =
138 context.codec(PointToPointIntent.class);
139 assertThat(intentCodec, notNullValue());
140
141 final ObjectNode intentJson = intentCodec.encode(intent, context);
142 assertThat(intentJson, matchesIntent(intent));
143
144 }
145}