f0a59bd799e7cb1b95259eadd0d8156479e3f4a7
[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             portMapping);
63     }
64
65     @Test
66     public void testSetupServiceWhenDeviceIsNotMounted() {
67         setMountPoint(null);
68         ServicePathInput servicePathInput = ServiceDataUtils.buildServicePathInputs();
69         for (ServicePathDirection servicePathDirection : ServicePathDirection.values()) {
70             ServicePathOutput servicePathOutput = deviceRendererService.setupServicePath(servicePathInput,
71                 servicePathDirection);
72             Assert.assertFalse(servicePathOutput.isSuccess());
73             Assert.assertEquals("node1 is not mounted on the controller",
74                 servicePathOutput.getResult());
75         }
76     }
77
78     @Test
79     public void testSetupServiceUsingCrossConnectEmptyPorts() throws ExecutionException, InterruptedException {
80         setMountPoint(MountPointUtils.getMountPoint(new ArrayList<>(), getDataBroker()));
81         testSetupService(true);
82     }
83
84     @Test
85     public void testSetupServiceUsingCrossConnectWithPorts() throws ExecutionException, InterruptedException {
86         setMountPoint(MountPointUtils.getMountPoint(new ArrayList<>(), getDataBroker()));
87         testSetupService(true);
88     }
89
90 //    @Test
91 //    public void testSetupServiceWithoutCrossConnect() throws ExecutionException, InterruptedException {
92 //        setMountPoint(new MountPointStub(getDataBroker()));
93 //
94 //        testSetupService(false);
95 //    }
96
97     private void testSetupService(boolean crossConnect) throws ExecutionException, InterruptedException {
98         String [] interfaceTokens = {
99             OpenRoadmInterfacesImpl.NETWORK_TOKEN,
100             OpenRoadmInterfacesImpl.CLIENT_TOKEN,
101             OpenRoadmInterfacesImpl.TTP_TOKEN,
102             OpenRoadmInterfacesImpl.PP_TOKEN,
103             ""
104         };
105
106         String nodeId = "node1";
107
108         for (String srcToken : interfaceTokens) {
109             String srcTP = "src-" + srcToken;
110             MountPointUtils.writeMapping(nodeId, srcTP, this.deviceTransactionManager);
111             for (String dstToken : interfaceTokens) {
112                 String dstTp = "dst-" + dstToken;
113                 MountPointUtils.writeMapping(nodeId, dstTp, this.deviceTransactionManager);
114
115                 boolean connectingUsingCrossConnect = true;
116                 if (OpenRoadmInterfacesImpl.NETWORK_TOKEN.equals(srcToken)
117                         || OpenRoadmInterfacesImpl.CLIENT_TOKEN.equals(srcToken)) {
118                     connectingUsingCrossConnect = false;
119                 }
120                 if (OpenRoadmInterfacesImpl.NETWORK_TOKEN.equals(dstToken)
121                         || OpenRoadmInterfacesImpl.CLIENT_TOKEN.equals(dstToken)) {
122                     connectingUsingCrossConnect = false;
123                 }
124
125                 if (connectingUsingCrossConnect != crossConnect) {
126                     continue;
127                 }
128
129                 List<Nodes> nodes = new ArrayList<>();
130                 nodes.add(ServiceDataUtils.createNode(nodeId, srcTP, dstTp));
131                 ServicePathInput servicePathInput = ServiceDataUtils.buildServicePathInputs(nodes);
132
133                 for (ServicePathDirection servicePathDirection : ServicePathDirection.values()) {
134                     ServicePathOutput servicePathOutput = deviceRendererService.setupServicePath(servicePathInput,
135                             servicePathDirection);
136                     Assert.assertTrue(servicePathOutput.isSuccess());
137                     String expectedResult = "Roadm-connection successfully created for nodes: ";
138                     if (crossConnect) {
139                         expectedResult = expectedResult + nodeId;
140                     }
141                     Assert.assertEquals(expectedResult, servicePathOutput.getResult());
142                     Assert.assertEquals(1, servicePathOutput.getNodeInterface().size());
143                     Assert.assertEquals(nodeId, servicePathOutput.getNodeInterface().get(0).getNodeId());
144                 }
145             }
146         }
147     }
148
149     @Test
150     public void testRollbackEmptyInterface() {
151         setMountPoint(new MountPointStub(getDataBroker()));
152         RendererRollbackInput rendererRollbackInput = ServiceDataUtils.buildRendererRollbackInput();
153         RendererRollbackOutput rendererRollbackOutput =
154                 this.deviceRendererService.rendererRollback(rendererRollbackInput);
155         Assert.assertTrue(rendererRollbackOutput.isSuccess());
156         Assert.assertTrue(rendererRollbackOutput.getFailedToRollback().isEmpty());
157     }
158
159     @Test
160     public void testRollbackConnectionIdNotExist() {
161         setMountPoint(new MountPointStub(getDataBroker()));
162
163         NodeInterfaceBuilder nodeInterfaceBuilder = new NodeInterfaceBuilder();
164         nodeInterfaceBuilder.setNodeId("node1");
165         nodeInterfaceBuilder.withKey(new NodeInterfaceKey("node1"));
166         List<String> connectionID = new ArrayList<>();
167         connectionID.add("node1-PP");
168         nodeInterfaceBuilder.setConnectionId(connectionID);
169         List<NodeInterface> nodeInterfaces = new ArrayList<>();
170         nodeInterfaces.add(nodeInterfaceBuilder.build());
171         RendererRollbackInputBuilder rendererRollbackInputBuilder = new RendererRollbackInputBuilder();
172         rendererRollbackInputBuilder.setNodeInterface(nodeInterfaces);
173
174         RendererRollbackOutput rendererRollbackOutput =
175                 this.deviceRendererService.rendererRollback(rendererRollbackInputBuilder.build());
176         Assert.assertFalse(rendererRollbackOutput.isSuccess());
177         Assert.assertEquals(1, rendererRollbackOutput.getFailedToRollback().size());
178         Assert.assertEquals("node1", rendererRollbackOutput.getFailedToRollback().get(0).getNodeId());
179     }
180 }