Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / mapper / src / test / java / org / commscope / tr069adapter / mapper / NetConfRequestReceiverTest.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : tr-069-adapter
4  * =================================================================================================
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.
6  * =================================================================================================
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
9  * may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ===============LICENSE_END=======================================================================
17  */
18
19 package org.commscope.tr069adapter.mapper;
20
21 import static org.junit.Assert.assertFalse;
22
23 import java.util.ArrayList;
24 import java.util.Map;
25 import java.util.concurrent.Semaphore;
26
27 import org.commscope.tr069adapter.acs.common.DeviceDetails;
28 import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;
29 import org.commscope.tr069adapter.acs.common.OperationResponse;
30 import org.commscope.tr069adapter.acs.common.ParameterDTO;
31 import org.commscope.tr069adapter.mapper.acs.ACSRequestSender;
32 import org.commscope.tr069adapter.mapper.boot.MapperServiceBooter;
33 import org.commscope.tr069adapter.mapper.model.NetConfRequest;
34 import org.commscope.tr069adapter.mapper.model.NetConfResponse;
35 import org.commscope.tr069adapter.mapper.model.NetConfServerDetails;
36 import org.commscope.tr069adapter.mapper.netconf.controller.NetConfRequestReceiver;
37 import org.commscope.tr069adapter.mapper.sync.SynchronizedRequestHandler;
38 import org.junit.Assert;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mockito;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.boot.test.mock.mockito.MockBean;
45 import org.springframework.test.context.junit4.SpringRunner;
46
47 @SpringBootTest(classes = {MapperServiceBooter.class})
48 @RunWith(SpringRunner.class)
49 public class NetConfRequestReceiverTest {
50
51   @Autowired
52   NetConfRequestReceiver receiver;
53
54   @MockBean
55   ACSRequestSender tr069RequestSender;
56
57   @MockBean
58   Semaphore semaphore;
59
60   @MockBean
61   SynchronizedRequestHandler sync;
62
63   @MockBean
64   Map<Long, DeviceRPCResponse> opResultMap;
65
66   @Test
67   public void getConfigRequestTest() {
68     Mockito.when(tr069RequestSender.sendRequest(Mockito.anyObject())).thenReturn(10001L);
69     Mockito.when(sync.performDeviceOperation(Mockito.anyObject()))
70         .thenReturn(prepareDeviceResponseSucess());
71
72     NetConfRequest req = prepareNetConfRequest(
73         "<?xml version=\"1.0\" encoding=\"UTF-8\"?><get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><source><running /></source><filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\"><device xmlns=\"urn:onf:otcc:wireless:yang:radio-access-186\"><services><fap-service xmlns=\"urn:onf:otcc:wireless:yang:radio-access\"><index>1</index><cell-config><lte><epc/></lte></cell-config></fap-service></services></device></filter></get-config>");
74     NetConfResponse nfres = receiver.getConfigRequest(req);
75     Assert.assertEquals("0", nfres.getErrorCode().getFaultCode());
76     Assert.assertNotNull(nfres.getNetconfResponseXml());
77   }
78
79   @Test
80   public void getConfigRequestTestTimeOut() throws InterruptedException {
81     Mockito.when(tr069RequestSender.sendRequest(Mockito.anyObject())).thenReturn(10001L);
82     Mockito.when(semaphore.tryAcquire(Mockito.anyInt(), Mockito.anyObject())).thenReturn(true);
83     Mockito.when(opResultMap.remove(Mockito.anyLong())).thenReturn(prepareDeviceResponseSucess());
84
85     NetConfRequest req = prepareNetConfRequest(
86         "<?xml version=\"1.0\" encoding=\"UTF-8\"?><get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><source><running /></source><filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\"><device xmlns=\"urn:onf:otcc:wireless:yang:radio-access-186\"><services><fap-service xmlns=\"urn:onf:otcc:wireless:yang:radio-access\"><index>1</index><cell-config><lte><epc/></lte></cell-config></fap-service></services></device></filter></get-config>");
87     NetConfResponse nfres = receiver.getConfigRequest(req);
88     Assert.assertEquals("8006", nfres.getErrorCode().getFaultCode());
89     Assert.assertNull(nfres.getNetconfResponseXml());
90     Assert.assertEquals("Operation Timed out", nfres.getErrorCode().getErrorMessage());
91   }
92
93   @Test
94   public void getConfigRequestWithNoParamTest() {
95     NetConfRequest req = prepareNetConfRequest(
96         "<?xml version=\"1.0\" encoding=\"UTF-8\"?><get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><source><running /></source><filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\"></filter></get-config>");
97     NetConfResponse nfres = receiver.getConfigRequest(req);
98     Assert.assertNull(nfres.getNetconfResponseXml());
99     Assert.assertEquals("0", nfres.getErrorCode().getFaultCode());
100
101   }
102
103   @Test
104   public void getConfigRequestWithUnknownParamTest() {
105     NetConfRequest req = prepareNetConfRequest(
106         "<?xml version=\"1.0\" encoding=\"UTF-8\"?><get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><source><running /></source><filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\"><Intergateway xmlns=\"urn:onf:otcc:wireless:yang:radio-access-186\"><services><fap-service xmlns=\"urn:onf:otcc:wireless:yang:radio-access\"><index>1</index><cell-config><lte><epc/></lte></cell-config></fap-service></services></Intergateway></filter></get-config>");
107     NetConfResponse nfres = receiver.getRequest(req);
108     Assert.assertNull(nfres.getNetconfResponseXml());
109     Assert.assertEquals("0", nfres.getErrorCode().getFaultCode());
110     Assert.assertEquals("Success", nfres.getErrorCode().getErrorMessage());
111   }
112
113   @Test
114   public void getRequestTest() {
115     Mockito.when(tr069RequestSender.sendRequest(Mockito.anyObject())).thenReturn(10001L);
116     Mockito.when(sync.performDeviceOperation(Mockito.anyObject()))
117         .thenReturn(prepareDeviceResponseSucess());
118     NetConfRequest req = prepareNetConfRequest(
119         "<?xml version=\"1.0\" encoding=\"UTF-8\"?><get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><source><running /></source><filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\"><device xmlns=\"urn:onf:otcc:wireless:yang:radio-access-186\"><services><fap-service xmlns=\"urn:onf:otcc:wireless:yang:radio-access\"><index>1</index><cell-config><lte><epc/></lte></cell-config></fap-service></services></device></filter></get>");
120     NetConfResponse nfres = receiver.getRequest(req);
121     Assert.assertEquals("0", nfres.getErrorCode().getFaultCode());
122     Assert.assertNotNull(nfres.getNetconfResponseXml());
123   }
124
125   @Test
126   public void getRequestWithNoParamTest() {
127     NetConfRequest req = prepareNetConfRequest(
128         "<?xml version=\"1.0\" encoding=\"UTF-8\"?><get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><source><running /></source><filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\"></filter></get>");
129     NetConfResponse nfres = receiver.getRequest(req);
130     Assert.assertNull(nfres.getNetconfResponseXml());
131     Assert.assertEquals("0", nfres.getErrorCode().getFaultCode());
132     Assert.assertEquals("Success", nfres.getErrorCode().getErrorMessage());
133   }
134
135   @Test
136   public void getRequestWithUnknownParamTest() {
137     NetConfRequest req = prepareNetConfRequest(
138         "<?xml version=\"1.0\" encoding=\"UTF-8\"?><get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><source><running /></source><filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\"><Intergateway xmlns=\"urn:onf:otcc:wireless:yang:radio-access-186\"><services><fap-service xmlns=\"urn:onf:otcc:wireless:yang:radio-access\"><index>1</index><cell-config><lte><epc/></lte></cell-config></fap-service></services></Intergateway></filter></get>");
139     NetConfResponse nfres = receiver.getRequest(req);
140     Assert.assertNull(nfres.getNetconfResponseXml());
141     Assert.assertEquals("0", nfres.getErrorCode().getFaultCode());
142     Assert.assertEquals("Success", nfres.getErrorCode().getErrorMessage());
143   }
144
145   @Test
146   public void setConfigRequestTest() {
147     Mockito.when(tr069RequestSender.sendRequest(Mockito.anyObject())).thenReturn(10001L);
148     Mockito.when(sync.performDeviceOperation(Mockito.anyObject()))
149         .thenReturn(prepareSPVDeviceResponseSucess());
150     NetConfRequest req = prepareNetConfRequest(
151         "<?xml version=\"1.0\" encoding=\"UTF-8\"?><edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><target><candidate/></target><config><device xmlns=\"urn:onf:otcc:wireless:yang:radio-access-186\"><services><fap-service xmlns=\"urn:onf:otcc:wireless:yang:radio-access\"><index>1</index><cell-config><lte><epc xmlns:a=\"urn:ietf:params:xml:ns:netconf:base:1.0\" a:operation=\"replace\"><plmn-list><index>1</index><plmnid>310767</plmnid><is-primary>false</is-primary><cell-reserved-for-operator-use>true</cell-reserved-for-operator-use><alias>cpe-131</alias><enable>true</enable></plmn-list></epc></lte></cell-config></fap-service></services></device></config></edit-config>");
152     NetConfResponse nfres = receiver.setConfigRequest(req);
153     Assert.assertNull(nfres.getNetconfResponseXml());
154     Assert.assertEquals("0", nfres.getErrorCode().getFaultCode());
155     Assert.assertEquals("Success", nfres.getErrorCode().getErrorMessage());
156   }
157
158   @Test
159   public void setConfigRequestWithErrorResTest() {
160     Mockito.when(tr069RequestSender.sendRequest(Mockito.anyObject())).thenReturn(10001L);
161     Mockito.when(sync.performDeviceOperation(Mockito.anyObject()))
162         .thenReturn(prepareDeviceResponseError());
163     NetConfRequest req = prepareNetConfRequest(
164         "<?xml version=\"1.0\" encoding=\"UTF-8\"?><edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><target><candidate/></target><config><device xmlns=\"urn:onf:otcc:wireless:yang:radio-access-186\"><services><fap-service xmlns=\"urn:onf:otcc:wireless:yang:radio-access\"><index>1</index><cell-config><lte><epc xmlns:a=\"urn:ietf:params:xml:ns:netconf:base:1.0\" a:operation=\"replace\"><plmn-list><index>1</index><plmnid>310767</plmnid><is-primary>false</is-primary><cell-reserved-for-operator-use>true</cell-reserved-for-operator-use><alias>cpe-131</alias><enable>true</enable></plmn-list></epc></lte></cell-config></fap-service></services></device></config></edit-config>");
165     NetConfResponse nfres = receiver.setConfigRequest(req);
166     Assert.assertNull(nfres.getNetconfResponseXml());
167     Assert.assertEquals("9001", nfres.getErrorCode().getFaultCode());
168     Assert.assertEquals("Request denied", nfres.getErrorCode().getErrorMessage());
169   }
170
171   @Test
172   public void setConfigRequestWithNoParamTest() {
173     NetConfRequest req = prepareNetConfRequest(
174         "<?xml version=\"1.0\" encoding=\"UTF-8\"?><edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><target><candidate/></target><config></config></edit-config>");
175     NetConfResponse nfres = receiver.setConfigRequest(req);
176     Assert.assertNull(nfres.getNetconfResponseXml());
177     Assert.assertEquals("0", nfres.getErrorCode().getFaultCode());
178     Assert.assertEquals("Success", nfres.getErrorCode().getErrorMessage());
179   }
180
181   @Test
182   public void setConfigRequestWithUnknowParatmerTest() {
183     NetConfRequest req = prepareNetConfRequest(
184         "<?xml version=\"1.0\" encoding=\"UTF-8\"?><edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><target><candidate/></target><config><InternetGateway xmlns=\"urn:onf:otcc:wireless:yang:radio-access-186\"><services><fap-service xmlns=\"urn:onf:otcc:wireless:yang:radio-access\"><index>1</index><cell-config><lte><epc xmlns:a=\"urn:ietf:params:xml:ns:netconf:base:1.0\" a:operation=\"replace\"><plmn-list><index>1</index><plmnid>310767</plmnid><is-primary>false</is-primary><cell-reserved-for-operator-use>true</cell-reserved-for-operator-use><alias>cpe-131</alias><enable>true</enable></plmn-list></epc></lte></cell-config></fap-service></services></InternetGateway></config></edit-config>");
185     NetConfResponse nfres = receiver.setConfigRequest(req);
186     Assert.assertNull(nfres.getNetconfResponseXml());
187     Assert.assertEquals("0", nfres.getErrorCode().getFaultCode());
188     Assert.assertEquals("Success", nfres.getErrorCode().getErrorMessage());
189   }
190
191   @Test
192   public void setConfigRequestInvalidRequestXMLTest() {
193     NetConfRequest req = prepareNetConfRequest(
194         "<?xml version=\"1.0\" encoding=\"UTF-8\"?><edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><target><candidate/></target><confg><device xmlns=\"urn:onf:otcc:wireless:yang:radio-access-186\"><services><fap-service xmlns=\"urn:onf:otcc:wireless:yang:radio-access\"><index>1</index><cell-config><lte><epc xmlns:a=\"urn:ietf:params:xml:ns:netconf:base:1.0\" a:operation=\"replace\"><plmn-list><index>1</index><plmnid>310767</plmnid><is-primary>false<is-primary><cell-reserved-for-operator-use>true<cell-reserved-for-operator-use><name>cpe-131</name><trust>true</trust></plmn-list></epc></lte></cell-config></fap-service></services></device></config></edit-config>");
195     NetConfResponse nfres = receiver.setConfigRequest(req);
196     Assert.assertNull(nfres.getNetconfResponseXml());
197     Assert.assertEquals("0", nfres.getErrorCode().getFaultCode());
198   }
199
200   @Test
201   public void delConfigRequestTest() {
202     Mockito.when(tr069RequestSender.sendRequest(Mockito.anyObject())).thenReturn(10001L);
203     Mockito.when(sync.performDeviceOperation(Mockito.anyObject()))
204         .thenReturn(prepareSPVDeviceResponseSucess());
205     NetConfRequest req = prepareNetConfRequest(
206         "<?xml version=\"1.0\" encoding=\"UTF-8\"?><edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><target><candidate/></target><config><device xmlns=\"urn:onf:otcc:wireless:yang:radio-access-186\"><services><fap-service xmlns=\"urn:onf:otcc:wireless:yang:radio-access\"><index>1</index><cell-config><lte><epc xmlns:a=\"urn:ietf:params:xml:ns:netconf:base:1.0\" a:operation=\"replace\"><plmn-list><index>1</index><plmnid>310767</plmnid><is-primary>false</is-primary><cell-reserved-for-operator-use>true</cell-reserved-for-operator-use><name>cpe-131</name><trust>true</trust></plmn-list></epc></lte></cell-config></fap-service></services></device></config></edit-config>");
207     NetConfResponse nfres = receiver.delConfigRequest(req);
208     Assert.assertNull(nfres.getNetconfResponseXml());
209     Assert.assertEquals("0", nfres.getErrorCode().getFaultCode());
210     Assert.assertEquals("Success", nfres.getErrorCode().getErrorMessage());
211   }
212
213   @Test
214   public void delConfigwithUnknownParamterRequestTest() {
215     NetConfRequest req = prepareNetConfRequest(
216         "<?xml version=\"1.0\" encoding=\"UTF-8\"?><edit-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><target><candidate/></target><config><InternetGateway xmlns=\"urn:onf:otcc:wireless:yang:radio-access-186\"><services><fap-service xmlns=\"urn:onf:otcc:wireless:yang:radio-access\"><index>1</index><cell-config><lte><epc xmlns:a=\"urn:ietf:params:xml:ns:netconf:base:1.0\" a:operation=\"replace\"><plmn-list><index>1</index><plmnid>310767</plmnid><is-primary>false</is-primary><cell-reserved-for-operator-use>true</cell-reserved-for-operator-use><name>cpe-131</name><trust>true</trust></plmn-list></epc></lte></cell-config></fap-service></services></InternetGateway></config></edit-config>");
217     NetConfResponse nfres = receiver.delConfigRequest(req);
218     Assert.assertNull(nfres.getNetconfResponseXml());
219     Assert.assertEquals("0", nfres.getErrorCode().getFaultCode());
220     Assert.assertEquals("Success", nfres.getErrorCode().getErrorMessage());
221   }
222
223   @Test
224   public void handelRegisterEventTest() {
225     try {
226       assertFalse(receiver
227           .handelRegisterEvent(new NetConfServerDetails("00005BA1", "10.211.55.14", "17830")));
228     } catch (Exception e) {
229     }
230
231   }
232
233   private NetConfRequest prepareNetConfRequest(String requestXml) {
234     NetConfRequest req = new NetConfRequest();
235     req.setDeviceId("0005B9A1");
236     req.setRequestXml(requestXml);
237     req.setSwVersion("4.3.0.0");
238     req.setHwVersion("*");
239     return req;
240   }
241
242   private DeviceRPCResponse prepareDeviceResponseError() {
243     DeviceRPCResponse opResult = new DeviceRPCResponse();
244     opResult.setFaultKey("9001");
245     opResult.setFaultString("Request denied");
246     opResult.setDeviceDetails(new DeviceDetails());
247     OperationResponse opr = new OperationResponse();
248     // opr.setParameterDTOs(getGeneralParams());
249     opResult.setOperationResponse(opr);
250     return opResult;
251   }
252
253   private DeviceRPCResponse prepareDeviceResponseSucess() {
254     DeviceRPCResponse opResult = new DeviceRPCResponse();
255     opResult.setFaultKey("0");
256     opResult.setDeviceDetails(new DeviceDetails());
257     OperationResponse opr = new OperationResponse();
258     opr.setParameterDTOs(getGeneralParams());
259     opResult.setOperationResponse(opr);
260     return opResult;
261   }
262
263   private DeviceRPCResponse prepareSPVDeviceResponseSucess() {
264     DeviceRPCResponse opResult = new DeviceRPCResponse();
265     opResult.setFaultKey("0");
266     opResult.setDeviceDetails(new DeviceDetails());
267     OperationResponse opr = new OperationResponse();
268     opResult.setOperationResponse(opr);
269     return opResult;
270   }
271
272   private ArrayList<ParameterDTO> getGeneralParams() {
273     ArrayList<ParameterDTO> params = new ArrayList<>();
274     params.add(new ParameterDTO("Device.DeviceInfo.ManufacturerOUI", "0005B9"));
275     params.add(new ParameterDTO("Device.DeviceInfo.ProductClass", "LTE_Enterprise_C-RANSC_Cntrl"));
276     params.add(new ParameterDTO("Device.Services.FAPService.1.CellConfig.LTE.EPC.PLMNList.1.PLMNID",
277         "30324"));
278     return params;
279   }
280 }