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