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