blob: 382617286c934fc4691289483ea5958bae251752 [file] [log] [blame]
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +05303 *
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 */
Priyanka Bb977f562016-07-22 13:02:03 +053016package org.onosproject.pcerest;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053017
18import static org.hamcrest.MatcherAssert.assertThat;
19import static org.hamcrest.Matchers.is;
20import static org.hamcrest.Matchers.notNullValue;
21
22import java.io.IOException;
23import java.io.InputStream;
24
25import org.junit.Before;
26import org.junit.Test;
Mahesh Poojary S33536202016-05-30 07:22:36 +053027
28import org.onlab.util.DataRateUnit;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053029import org.onosproject.codec.JsonCodec;
30import org.onosproject.pce.pceservice.PcePath;
Mahesh Poojary S33536202016-05-30 07:22:36 +053031import org.onosproject.net.intent.Constraint;
32import org.onosproject.pce.pceservice.constraint.CostConstraint;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053033
34import com.fasterxml.jackson.databind.JsonNode;
35import com.fasterxml.jackson.databind.ObjectMapper;
36import com.fasterxml.jackson.databind.node.ObjectNode;
Satish K2eb5d842017-04-04 16:28:37 +053037import org.onosproject.pce.pceservice.constraint.PceBandwidthConstraint;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053038
39/**
40 * PCE path codec unit tests.
41 */
42public class PcePathCodecTest {
43
44 MockPceCodecContext context;
45 JsonCodec<PcePath> pcePathCodec;
46 /**
47 * Sets up for each test. Creates a context and fetches the PCE path codec.
48 */
49 @Before
50 public void setUp() {
51 context = new MockPceCodecContext();
52 pcePathCodec = context.codec(PcePath.class);
53 assertThat(pcePathCodec, notNullValue());
54 }
55
56 /**
57 * Reads in a pce-path from the given resource and decodes it.
58 *
59 * @param resourceName resource to use to read the json for the pce-path
60 * @return decoded pce-path
61 * @throws IOException if processing the resource fails
62 */
63 private PcePath getPcePath(String resourceName) throws IOException {
64 InputStream jsonStream = PcePathCodecTest.class
65 .getResourceAsStream(resourceName);
66 ObjectMapper mapper = new ObjectMapper();
67 JsonNode json = mapper.readTree(jsonStream);
68 assertThat(json, notNullValue());
69 PcePath pcePath = pcePathCodec.decode((ObjectNode) json, context);
70 assertThat(pcePath, notNullValue());
71 return pcePath;
72 }
73
74 /**
75 * Checks that a simple pce-path is decoded properly.
76 *
77 * @throws IOException if the resource cannot be processed
78 */
79 @Test
80 public void codecPcePathTest() throws IOException {
81
82 PcePath pcePath = getPcePath("pcePath.json");
83
84 assertThat(pcePath, notNullValue());
85
86 assertThat(pcePath.source().toString(), is("11.0.0.1"));
87 assertThat(pcePath.destination(), is("11.0.0.2"));
Mahesh Poojary S33536202016-05-30 07:22:36 +053088 assertThat(pcePath.lspType().toString(), is("WITHOUT_SIGNALLING_AND_WITHOUT_SR"));
89 // testing cost type
90 String cost = "2";
91 Constraint costConstraint = CostConstraint.of(CostConstraint.Type.values()[Integer.valueOf(cost) - 1]);
92 assertThat(pcePath.costConstraint(), is(costConstraint));
93 // testing bandwidth
94 String bandwidth = "200";
Satish K2eb5d842017-04-04 16:28:37 +053095 Constraint bandwidthConstraint = PceBandwidthConstraint.of(Double.valueOf(bandwidth), DataRateUnit
Mahesh Poojary S33536202016-05-30 07:22:36 +053096 .valueOf("BPS"));
97 assertThat(pcePath.bandwidthConstraint(), is(bandwidthConstraint));
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053098 }
99}