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