Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 18 | import java.time.Duration; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 19 | import java.util.List; |
| 20 | |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onlab.packet.MacAddress; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 24 | import org.onlab.packet.MplsLabel; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 25 | import org.onosproject.codec.CodecContext; |
| 26 | import org.onosproject.codec.JsonCodec; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.DefaultApplicationId; |
| 29 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 31 | import org.onosproject.net.HostId; |
| 32 | import org.onosproject.net.NetTestTools; |
| 33 | import org.onosproject.net.PortNumber; |
| 34 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onosproject.net.flow.TrafficSelector; |
| 37 | import org.onosproject.net.flow.TrafficTreatment; |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 38 | import org.onosproject.net.intent.AbstractIntentTest; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.Constraint; |
| 40 | import org.onosproject.net.intent.HostToHostIntent; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 41 | import org.onosproject.net.intent.PointToPointIntent; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 42 | import org.onosproject.net.intent.constraint.AnnotationConstraint; |
| 43 | import org.onosproject.net.intent.constraint.AsymmetricPathConstraint; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 44 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
| 45 | import org.onosproject.net.intent.constraint.LambdaConstraint; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 46 | import org.onosproject.net.intent.constraint.LatencyConstraint; |
| 47 | import org.onosproject.net.intent.constraint.ObstacleConstraint; |
| 48 | import org.onosproject.net.intent.constraint.WaypointConstraint; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 49 | import org.onosproject.net.resource.Bandwidth; |
| 50 | import org.onosproject.net.resource.Lambda; |
| 51 | |
| 52 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 53 | import com.google.common.collect.ImmutableList; |
| 54 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 55 | import static org.hamcrest.MatcherAssert.assertThat; |
| 56 | import static org.hamcrest.Matchers.notNullValue; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 57 | import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 58 | import static org.onosproject.net.NetTestTools.did; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 59 | import static org.onosproject.net.NetTestTools.hid; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Unit tests for the host to host intent class codec. |
| 63 | */ |
| 64 | public class IntentCodecTest extends AbstractIntentTest { |
| 65 | |
| 66 | private final HostId id1 = hid("12:34:56:78:91:ab/1"); |
| 67 | private final HostId id2 = hid("12:34:56:78:92:ab/1"); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 68 | private final ApplicationId appId = new DefaultApplicationId(3, "test"); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 69 | final TrafficSelector emptySelector = |
Brian O'Connor | 6b52813 | 2015-03-10 16:39:52 -0700 | [diff] [blame] | 70 | DefaultTrafficSelector.emptySelector(); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 71 | final TrafficTreatment emptyTreatment = |
Brian O'Connor | 6b52813 | 2015-03-10 16:39:52 -0700 | [diff] [blame] | 72 | DefaultTrafficTreatment.emptyTreatment(); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 73 | private final CodecContext context = new MockCodecContext(); |
| 74 | |
| 75 | /** |
| 76 | * Tests the encoding of a host to host intent. |
| 77 | */ |
| 78 | @Test |
| 79 | public void hostToHostIntent() { |
| 80 | final HostToHostIntent intent = |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame^] | 81 | HostToHostIntent.builder() |
| 82 | .appId(appId) |
| 83 | .one(id1) |
| 84 | .two(id2) |
| 85 | .build(); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 86 | |
| 87 | final JsonCodec<HostToHostIntent> intentCodec = |
| 88 | context.codec(HostToHostIntent.class); |
| 89 | assertThat(intentCodec, notNullValue()); |
| 90 | |
| 91 | final ObjectNode intentJson = intentCodec.encode(intent, context); |
| 92 | assertThat(intentJson, matchesIntent(intent)); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Tests the encoding of a point to point intent. |
| 97 | */ |
| 98 | @Test |
| 99 | public void pointToPointIntent() { |
| 100 | ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1); |
| 101 | ConnectPoint egress = NetTestTools.connectPoint("egress", 2); |
| 102 | |
| 103 | final PointToPointIntent intent = |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 104 | PointToPointIntent.builder() |
| 105 | .appId(appId) |
| 106 | .selector(emptySelector) |
| 107 | .treatment(emptyTreatment) |
| 108 | .ingressPoint(ingress) |
| 109 | .egressPoint(egress).build(); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 110 | |
| 111 | final CodecContext context = new MockCodecContext(); |
| 112 | final JsonCodec<PointToPointIntent> intentCodec = |
| 113 | context.codec(PointToPointIntent.class); |
| 114 | assertThat(intentCodec, notNullValue()); |
| 115 | |
| 116 | final ObjectNode intentJson = intentCodec.encode(intent, context); |
| 117 | assertThat(intentJson, matchesIntent(intent)); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Tests the encoding of an intent with treatment, selector and constraints |
| 122 | * specified. |
| 123 | */ |
| 124 | @Test |
| 125 | public void intentWithTreatmentSelectorAndConstraints() { |
| 126 | ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1); |
| 127 | ConnectPoint egress = NetTestTools.connectPoint("egress", 2); |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 128 | DeviceId did1 = did("device1"); |
| 129 | DeviceId did2 = did("device2"); |
| 130 | DeviceId did3 = did("device3"); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 131 | final TrafficSelector selector = DefaultTrafficSelector.builder() |
| 132 | .matchIPProtocol((byte) 3) |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 133 | .matchMplsLabel(MplsLabel.mplsLabel(4)) |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 134 | .matchOpticalSignalType((short) 5) |
| 135 | .matchLambda((short) 6) |
| 136 | .matchEthDst(MacAddress.BROADCAST) |
| 137 | .matchIPDst(IpPrefix.valueOf("1.2.3.4/24")) |
| 138 | .build(); |
| 139 | final TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 140 | .setLambda((short) 33) |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 141 | .setMpls(MplsLabel.mplsLabel(44)) |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 142 | .setOutput(PortNumber.CONTROLLER) |
| 143 | .setEthDst(MacAddress.BROADCAST) |
| 144 | .build(); |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 145 | |
| 146 | final List<Constraint> constraints = |
| 147 | ImmutableList.of( |
Sho SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 148 | new BandwidthConstraint(Bandwidth.bps(1.0)), |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 149 | new LambdaConstraint(Lambda.valueOf(3)), |
| 150 | new AnnotationConstraint("key", 33.0), |
| 151 | new AsymmetricPathConstraint(), |
| 152 | new LatencyConstraint(Duration.ofSeconds(2)), |
| 153 | new ObstacleConstraint(did1, did2), |
| 154 | new WaypointConstraint(did3)); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 155 | |
| 156 | final PointToPointIntent intent = |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 157 | PointToPointIntent.builder() |
| 158 | .appId(appId) |
| 159 | .selector(selector) |
| 160 | .treatment(treatment) |
| 161 | .ingressPoint(ingress) |
| 162 | .egressPoint(egress) |
| 163 | .constraints(constraints) |
| 164 | .build(); |
| 165 | |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 166 | |
| 167 | final CodecContext context = new MockCodecContext(); |
| 168 | final JsonCodec<PointToPointIntent> intentCodec = |
| 169 | context.codec(PointToPointIntent.class); |
| 170 | assertThat(intentCodec, notNullValue()); |
| 171 | |
| 172 | final ObjectNode intentJson = intentCodec.encode(intent, context); |
| 173 | assertThat(intentJson, matchesIntent(intent)); |
| 174 | |
| 175 | } |
| 176 | } |