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; |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame^] | 25 | import org.onlab.util.Bandwidth; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 26 | import org.onosproject.codec.CodecContext; |
| 27 | import org.onosproject.codec.JsonCodec; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.DefaultApplicationId; |
| 30 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 32 | import org.onosproject.net.HostId; |
| 33 | import org.onosproject.net.NetTestTools; |
| 34 | import org.onosproject.net.PortNumber; |
| 35 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 36 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 37 | import org.onosproject.net.flow.TrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 39 | import org.onosproject.net.intent.AbstractIntentTest; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 40 | import org.onosproject.net.intent.Constraint; |
| 41 | import org.onosproject.net.intent.HostToHostIntent; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 42 | import org.onosproject.net.intent.PointToPointIntent; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 43 | import org.onosproject.net.intent.constraint.AnnotationConstraint; |
| 44 | import org.onosproject.net.intent.constraint.AsymmetricPathConstraint; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
| 46 | import org.onosproject.net.intent.constraint.LambdaConstraint; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 47 | import org.onosproject.net.intent.constraint.LatencyConstraint; |
| 48 | import org.onosproject.net.intent.constraint.ObstacleConstraint; |
| 49 | import org.onosproject.net.intent.constraint.WaypointConstraint; |
Sho SHIMIZU | 63feca7 | 2015-05-07 10:44:25 -0700 | [diff] [blame] | 50 | import org.onosproject.net.resource.BandwidthResource; |
Sho SHIMIZU | 94b7ff4 | 2015-05-06 17:51:49 -0700 | [diff] [blame] | 51 | import org.onosproject.net.resource.LambdaResource; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 52 | |
| 53 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 54 | import com.google.common.collect.ImmutableList; |
| 55 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 56 | import static org.hamcrest.MatcherAssert.assertThat; |
| 57 | import static org.hamcrest.Matchers.notNullValue; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 58 | import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 59 | import static org.onosproject.net.NetTestTools.did; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 60 | import static org.onosproject.net.NetTestTools.hid; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Unit tests for the host to host intent class codec. |
| 64 | */ |
| 65 | public 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 69 | private final ApplicationId appId = new DefaultApplicationId(3, "test"); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 70 | final TrafficSelector emptySelector = |
Brian O'Connor | 6b52813 | 2015-03-10 16:39:52 -0700 | [diff] [blame] | 71 | DefaultTrafficSelector.emptySelector(); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 72 | final TrafficTreatment emptyTreatment = |
Brian O'Connor | 6b52813 | 2015-03-10 16:39:52 -0700 | [diff] [blame] | 73 | DefaultTrafficTreatment.emptyTreatment(); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 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 = |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 82 | HostToHostIntent.builder() |
| 83 | .appId(appId) |
| 84 | .one(id1) |
| 85 | .two(id2) |
| 86 | .build(); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 87 | |
| 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 Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 105 | PointToPointIntent.builder() |
| 106 | .appId(appId) |
| 107 | .selector(emptySelector) |
| 108 | .treatment(emptyTreatment) |
| 109 | .ingressPoint(ingress) |
| 110 | .egressPoint(egress).build(); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 111 | |
| 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 Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 129 | DeviceId did1 = did("device1"); |
| 130 | DeviceId did2 = did("device2"); |
| 131 | DeviceId did3 = did("device3"); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 132 | final TrafficSelector selector = DefaultTrafficSelector.builder() |
| 133 | .matchIPProtocol((byte) 3) |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 134 | .matchMplsLabel(MplsLabel.mplsLabel(4)) |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 135 | .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 Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 142 | .setMpls(MplsLabel.mplsLabel(44)) |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 143 | .setOutput(PortNumber.CONTROLLER) |
| 144 | .setEthDst(MacAddress.BROADCAST) |
| 145 | .build(); |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 146 | |
| 147 | final List<Constraint> constraints = |
| 148 | ImmutableList.of( |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame^] | 149 | new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(1.0))), |
Sho SHIMIZU | 94b7ff4 | 2015-05-06 17:51:49 -0700 | [diff] [blame] | 150 | new LambdaConstraint(LambdaResource.valueOf(3)), |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 151 | new AnnotationConstraint("key", 33.0), |
| 152 | new AsymmetricPathConstraint(), |
| 153 | new LatencyConstraint(Duration.ofSeconds(2)), |
| 154 | new ObstacleConstraint(did1, did2), |
| 155 | new WaypointConstraint(did3)); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 156 | |
| 157 | final PointToPointIntent intent = |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 158 | PointToPointIntent.builder() |
| 159 | .appId(appId) |
| 160 | .selector(selector) |
| 161 | .treatment(treatment) |
| 162 | .ingressPoint(ingress) |
| 163 | .egressPoint(egress) |
| 164 | .constraints(constraints) |
| 165 | .build(); |
| 166 | |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 167 | |
| 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 | } |