Bump to Silicon
[transportpce.git] / pce / src / test / java / org / opendaylight / transportpce / pce / service / PathComputationServiceImplTest.java
1 /*
2  * Copyright © 2020 Orange Labs, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.pce.service;
9
10 import java.math.BigDecimal;
11 import java.util.Map;
12 import org.junit.After;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.mockito.Mockito;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.transportpce.common.network.NetworkTransactionImpl;
19 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
20 import org.opendaylight.transportpce.common.network.RequestProcessor;
21 import org.opendaylight.transportpce.pce.gnpy.GnpyResult;
22 import org.opendaylight.transportpce.pce.gnpy.GnpyTopoImpl;
23 import org.opendaylight.transportpce.pce.utils.PceTestData;
24 import org.opendaylight.transportpce.pce.utils.PceTestUtils;
25 import org.opendaylight.transportpce.pce.utils.TransactionUtils;
26 import org.opendaylight.transportpce.test.AbstractTest;
27 import org.opendaylight.transportpce.test.DataStoreContext;
28 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.PathBandwidth;
29 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.generic.path.properties.PathPropertiesBuilder;
30 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.generic.path.properties.path.properties.PathMetric;
31 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.generic.path.properties.path.properties.PathMetricBuilder;
32 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.no.path.info.NoPathBuilder;
33 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.Response;
34 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.ResponseBuilder;
35 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.ResponseKey;
36 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.response.response.type.NoPathCaseBuilder;
37 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.response.response.type.PathCaseBuilder;
38 import org.opendaylight.yangtools.yang.common.Uint32;
39
40 public class PathComputationServiceImplTest extends AbstractTest {
41
42     private PathComputationServiceImpl pathComputationServiceImpl;
43     private static NetworkTransactionService networkTransactionService = null;
44     private static GnpyTopoImpl gnpyTopoImpl = null;
45     private static GnpyResult gnpyResult = null;
46     private DataStoreContext dataStoreContext = this.getDataStoreContextUtil();
47     private DataBroker dataBroker = this.getDataBroker();
48
49     @Before
50     public void setUp() {
51         networkTransactionService = Mockito.mock(NetworkTransactionService.class);
52         gnpyTopoImpl = Mockito.mock(GnpyTopoImpl.class);
53         gnpyResult = Mockito.mock(GnpyResult.class);
54         pathComputationServiceImpl = new PathComputationServiceImpl(
55                 networkTransactionService,
56                 this.getNotificationPublishService(), null);
57         pathComputationServiceImpl.init();
58     }
59
60     @Test
61     public void pathComputationRequestTest() {
62         pathComputationServiceImpl.generateGnpyResponse(null,"path");
63         Assert.assertNotNull(
64                 pathComputationServiceImpl.pathComputationRequest(PceTestData.getPCE_simpletopology_test1_request()));
65
66     }
67
68     @Test
69     public void testPathComputationRequestNoPath() {
70         Response response = new ResponseBuilder()
71                 .withKey(new ResponseKey(Uint32.valueOf(1))).setResponseType(new NoPathCaseBuilder()
72                 .setNoPath(new NoPathBuilder().setNoPath("no path").build()).build()).build();
73
74         pathComputationServiceImpl.generateGnpyResponse(response,"path");
75         Assert.assertNotNull(
76                 pathComputationServiceImpl.pathComputationRequest(PceTestData.getPCE_test3_request_54()));
77
78     }
79
80     @Test
81     public void testPathComputationRequestPathCase() {
82         PathMetric pathMetric = new PathMetricBuilder()
83                 .setAccumulativeValue(new BigDecimal(21))
84                 .setMetricType(PathBandwidth.class).build();
85         Response response = new ResponseBuilder()
86                 .withKey(new ResponseKey(Uint32.valueOf(1))).setResponseType(new PathCaseBuilder()
87                 .setPathProperties(new PathPropertiesBuilder().setPathMetric(Map.of(pathMetric.key(),pathMetric))
88                 .build()).build()).build();
89
90         pathComputationServiceImpl.generateGnpyResponse(response,"path");
91         Assert.assertNotNull(
92                 pathComputationServiceImpl.pathComputationRequest(PceTestData.getPCE_test3_request_54()));
93
94     }
95
96     @Test(expected = Exception.class)
97     public void generateGnpyResponse() throws Exception {
98         PceTestUtils.writeNetworkIntoDataStore(dataBroker, dataStoreContext, TransactionUtils.getNetworkForSpanLoss());
99         GnpyResult gnpyResult2 =
100                 new GnpyResult("A-to-Z",
101                         new GnpyTopoImpl(new NetworkTransactionImpl(
102                                 new RequestProcessor(dataBroker))), null);
103         pathComputationServiceImpl.generateGnpyResponse(gnpyResult2.getResponse(), "A-to-Z");
104     }
105
106
107     @After
108     public void destroy() {
109         pathComputationServiceImpl.close();
110     }
111 }