47085695d4d8af0a88ee5fb2c9baf7579d275e54
[transportpce.git] / renderer / src / test / java / org / opendaylight / transportpce / renderer / provisiondevice / DeviceRendererServiceImplTest.java
1 /*
2  * Copyright © 2018 Orange Systems, 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
9 package org.opendaylight.transportpce.renderer.provisiondevice;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.ExecutionException;
14 import org.junit.Assert;
15 import org.junit.Test;
16 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
17 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
18 import org.opendaylight.transportpce.common.crossconnect.CrossConnect;
19 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl;
20 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
21 import org.opendaylight.transportpce.common.device.DeviceTransactionManagerImpl;
22 import org.opendaylight.transportpce.common.mapping.PortMapping;
23 import org.opendaylight.transportpce.common.mapping.PortMappingImpl;
24 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
25 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl;
26 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterfaceFactory;
27 import org.opendaylight.transportpce.renderer.provisiondevice.servicepath.ServicePathDirection;
28 import org.opendaylight.transportpce.renderer.stub.MountPointServiceStub;
29 import org.opendaylight.transportpce.renderer.stub.MountPointStub;
30 import org.opendaylight.transportpce.renderer.utils.MountPointUtils;
31 import org.opendaylight.transportpce.renderer.utils.ServiceDataUtils;
32 import org.opendaylight.transportpce.test.AbstractTest;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev170907.node.interfaces.NodeInterface;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev170907.node.interfaces.NodeInterfaceBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev170907.node.interfaces.NodeInterfaceKey;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev170907.olm.renderer.input.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.renderer.rev170228.RendererRollbackInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.renderer.rev170228.RendererRollbackInputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.renderer.rev170228.RendererRollbackOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.renderer.rev170228.ServicePathInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.renderer.rev170228.ServicePathOutput;
42
43 public class DeviceRendererServiceImplTest extends AbstractTest {
44
45     private MountPointService mountPointService;
46
47     private DeviceTransactionManager deviceTransactionManager;
48
49     private DeviceRendererService deviceRendererService;
50
51     private void setMountPoint(MountPoint mountPoint) {
52         this.mountPointService = new MountPointServiceStub(mountPoint);
53         this.deviceTransactionManager = new DeviceTransactionManagerImpl(this.mountPointService, 3000);
54         OpenRoadmInterfaces openRoadmInterfaces = new OpenRoadmInterfacesImpl(this.deviceTransactionManager);
55         PortMapping portMapping = new PortMappingImpl(this.getDataBroker(), this.deviceTransactionManager,
56             openRoadmInterfaces);
57         OpenRoadmInterfaceFactory openRoadmInterfaceFactory = new OpenRoadmInterfaceFactory(portMapping,
58             openRoadmInterfaces);
59         CrossConnect crossConnect = new CrossConnectImpl(this.deviceTransactionManager);
60         this.deviceRendererService = new DeviceRendererServiceImpl(this.getDataBroker(),
61             this.deviceTransactionManager, openRoadmInterfaceFactory, openRoadmInterfaces, crossConnect);
62     }
63
64     @Test
65     public void testSetupServiceWhenDeviceIsNotMounted() {
66         setMountPoint(null);
67         ServicePathInput servicePathInput = ServiceDataUtils.buildServicePathInputs();
68         for (ServicePathDirection servicePathDirection : ServicePathDirection.values()) {
69             ServicePathOutput servicePathOutput = deviceRendererService.setupServicePath(servicePathInput,
70                 servicePathDirection);
71             Assert.assertFalse(servicePathOutput.isSuccess());
72             Assert.assertEquals("node1 is not mounted on the controller",
73                 servicePathOutput.getResult());
74         }
75     }
76
77     @Test
78     public void testSetupServiceUsingCrossConnectEmptyPorts() throws ExecutionException, InterruptedException {
79         setMountPoint(MountPointUtils.getMountPoint(new ArrayList<>(), getDataBroker()));
80         testSetupService(true);
81     }
82
83     @Test
84     public void testSetupServiceUsingCrossConnectWithPorts() throws ExecutionException, InterruptedException {
85         setMountPoint(MountPointUtils.getMountPoint(new ArrayList<>(), getDataBroker()));
86         testSetupService(true);
87     }
88
89     @Test
90     public void testSetupServiceWithoutCrossConnect() throws ExecutionException, InterruptedException {
91         setMountPoint(new MountPointStub(getDataBroker()));
92
93         testSetupService(false);
94     }
95
96     private void testSetupService(boolean crossConnect) throws ExecutionException, InterruptedException {
97         String [] interfaceTokens = {
98             OpenRoadmInterfacesImpl.NETWORK_TOKEN,
99             OpenRoadmInterfacesImpl.CLIENT_TOKEN,
100             OpenRoadmInterfacesImpl.TTP_TOKEN,
101             OpenRoadmInterfacesImpl.PP_TOKEN,
102             ""
103         };
104
105         String nodeId = "node1";
106
107         for (String srcToken : interfaceTokens) {
108             String srcTP = "src-" + srcToken;
109             MountPointUtils.writeMapping(nodeId, srcTP, this.deviceTransactionManager);
110             for (String dstToken : interfaceTokens) {
111                 String dstTp = "dst-" + dstToken;
112                 MountPointUtils.writeMapping(nodeId, dstTp, this.deviceTransactionManager);
113
114                 boolean connectingUsingCrossConnect = true;
115                 if (OpenRoadmInterfacesImpl.NETWORK_TOKEN.equals(srcToken)
116                         || OpenRoadmInterfacesImpl.CLIENT_TOKEN.equals(srcToken)) {
117                     connectingUsingCrossConnect = false;
118                 }
119                 if (OpenRoadmInterfacesImpl.NETWORK_TOKEN.equals(dstToken)
120                         || OpenRoadmInterfacesImpl.CLIENT_TOKEN.equals(dstToken)) {
121                     connectingUsingCrossConnect = false;
122                 }
123
124                 if (connectingUsingCrossConnect != crossConnect) {
125                     continue;
126                 }
127
128                 List<Nodes> nodes = new ArrayList<>();
129                 nodes.add(ServiceDataUtils.createNode(nodeId, srcTP, dstTp));
130                 ServicePathInput servicePathInput = ServiceDataUtils.buildServicePathInputs(nodes);
131
132                 for (ServicePathDirection servicePathDirection : ServicePathDirection.values()) {
133                     ServicePathOutput servicePathOutput = deviceRendererService.setupServicePath(servicePathInput,
134                             servicePathDirection);
135                     Assert.assertTrue(servicePathOutput.isSuccess());
136                     String expectedResult = "Roadm-connection successfully created for nodes: ";
137                     if (crossConnect) {
138                         expectedResult = expectedResult + nodeId;
139                     }
140                     Assert.assertEquals(expectedResult, servicePathOutput.getResult());
141                     Assert.assertEquals(1, servicePathOutput.getNodeInterface().size());
142                     Assert.assertEquals(nodeId, servicePathOutput.getNodeInterface().get(0).getNodeId());
143                 }
144             }
145         }
146     }
147
148     @Test
149     public void testRollbackEmptyInterface() {
150         setMountPoint(new MountPointStub(getDataBroker()));
151         RendererRollbackInput rendererRollbackInput = ServiceDataUtils.buildRendererRollbackInput();
152         RendererRollbackOutput rendererRollbackOutput =
153                 this.deviceRendererService.rendererRollback(rendererRollbackInput);
154         Assert.assertTrue(rendererRollbackOutput.isSuccess());
155         Assert.assertTrue(rendererRollbackOutput.getFailedToRollback().isEmpty());
156     }
157
158     @Test
159     public void testRollbackConnectionIdNotExist() {
160         setMountPoint(new MountPointStub(getDataBroker()));
161
162         NodeInterfaceBuilder nodeInterfaceBuilder = new NodeInterfaceBuilder();
163         nodeInterfaceBuilder.setNodeId("node1");
164         nodeInterfaceBuilder.withKey(new NodeInterfaceKey("node1"));
165         List<String> connectionID = new ArrayList<>();
166         connectionID.add("node1-PP");
167         nodeInterfaceBuilder.setConnectionId(connectionID);
168         List<NodeInterface> nodeInterfaces = new ArrayList<>();
169         nodeInterfaces.add(nodeInterfaceBuilder.build());
170         RendererRollbackInputBuilder rendererRollbackInputBuilder = new RendererRollbackInputBuilder();
171         rendererRollbackInputBuilder.setNodeInterface(nodeInterfaces);
172
173         RendererRollbackOutput rendererRollbackOutput =
174                 this.deviceRendererService.rendererRollback(rendererRollbackInputBuilder.build());
175         Assert.assertFalse(rendererRollbackOutput.isSuccess());
176         Assert.assertEquals(1, rendererRollbackOutput.getFailedToRollback().size());
177         Assert.assertEquals("node1", rendererRollbackOutput.getFailedToRollback().get(0).getNodeId());
178     }
179 }