f0749fbeefcf49c444b703c401931a8dccddc2bd
[ccsdk/features.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22  package org.onap.ccsdk.features.sdnr.northbound.oofpcipoc;
23
24 import java.util.Properties;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27
28 import org.onap.ccsdk.sli.core.sli.provider.MdsalHelper;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
31 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
32 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
33 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
34 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
35 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev190308.*;
36
37 import com.google.common.base.Preconditions;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.google.common.base.Preconditions;
44 import com.google.common.util.concurrent.Futures;
45 import com.google.common.util.concurrent.ListenableFuture;
46
47 /**
48  * Defines a base implementation for your provider. This class extends from a helper class
49  * which provides storage for the most commonly used components of the MD-SAL. Additionally the
50  * base class provides some basic logging and initialization / clean up methods.
51  *
52  */
53 public class OofpcipocProvider implements AutoCloseable, OofpcipocApiService {
54
55     private static final Logger LOG = LoggerFactory.getLogger(OofpcipocProvider.class);
56
57     private static final String APPLICATION_NAME = "oofpcipoc-api";
58
59     private final ExecutorService executor;
60
61     protected DataBroker dataBroker;
62     protected NotificationPublishService notificationService;
63     protected RpcProviderRegistry rpcRegistry;
64     protected BindingAwareBroker.RpcRegistration<OofpcipocApiService> rpcRegistration;
65     private final OofpcipocClient OofpcipocClient;
66
67
68     public OofpcipocProvider(final DataBroker dataBroker,
69                                                           final NotificationPublishService notificationPublishService,
70                                                           final RpcProviderRegistry rpcProviderRegistry,
71                                                           final OofpcipocClient OofpcipocClient) {
72
73         this.LOG.info( "Creating provider for {}", APPLICATION_NAME);
74         executor = Executors.newFixedThreadPool(1);
75                 this.dataBroker = dataBroker;
76                 this.notificationService = notificationPublishService;
77                 this.rpcRegistry = rpcProviderRegistry;
78                 this.OofpcipocClient = OofpcipocClient;
79                 initialize();
80     }
81
82     public void initialize(){
83         LOG.info( "Initializing provider for {}", APPLICATION_NAME);
84         rpcRegistration = rpcRegistry.addRpcImplementation(OofpcipocApiService.class, this);
85         LOG.info( "Initialization complete for {}", APPLICATION_NAME);
86     }
87
88     protected void initializeChild() {
89         //Override if you have custom initialization intelligence
90     }
91
92     @Override
93     public void close() throws Exception {
94         LOG.info( "Closing provider for {}", APPLICATION_NAME);
95             executor.shutdown();
96             rpcRegistration.close();
97         LOG.info( "Successfully closed provider for {}", APPLICATION_NAME);
98     }
99
100         @Override
101         public ListenableFuture<RpcResult<GreetingOutput>> greeting(
102                         GreetingInput input) {
103                 final String svcOperation = "greeting";
104
105                 Properties parms = new Properties();
106                 GreetingOutputBuilder serviceDataBuilder = new GreetingOutputBuilder();
107
108     LOG.info( "Reached RPC greeting");
109
110                 LOG.info( svcOperation +" called." );
111
112                 if(input == null ) {
113                         LOG.debug("exiting " +svcOperation+ " because of invalid input");
114                         serviceDataBuilder.setResponse("Input is null");
115                         RpcResult<GreetingOutput> rpcResult =
116                                 RpcResultBuilder.<GreetingOutput> status(true).withResult(serviceDataBuilder.build()).build();
117                         return Futures.immediateFuture(rpcResult);
118                 }
119
120                 // add input to parms
121                 LOG.info("Adding INPUT data for "+svcOperation+" input: " + input);
122                 GreetingInputBuilder inputBuilder = new GreetingInputBuilder(input);
123                 MdsalHelper.toProperties(parms, inputBuilder.build());
124
125                 // Call SLI sync method
126                 try
127                 {
128                         if (OofpcipocClient.hasGraph("oofpcipoc-api", svcOperation , null, "sync"))
129                         {
130         LOG.info( "OofpcipocClient has a Directed Graph for '" + svcOperation + "'");
131                                 try
132                                 {
133           OofpcipocClient.execute("oofpcipoc-api", svcOperation, null, "sync", serviceDataBuilder, parms);
134                                 }
135                                 catch (Exception e)
136                                 {
137                                         LOG.error("Caught exception executing service logic for "+ svcOperation, e);
138                                         serviceDataBuilder.setResponse("500");
139                                 }
140                         } else {
141                                 LOG.error("No service logic active for Oofpcipoc: '" + svcOperation + "'");
142                                 serviceDataBuilder.setResponse("503");
143                         }
144                 }
145                 catch (Exception e)
146                 {
147                         LOG.error("Caught exception looking for service logic", e);
148                         serviceDataBuilder.setResponse("500");
149                 }
150
151                 String errorCode = serviceDataBuilder.getResponse();
152
153                 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
154                         LOG.error("Returned FAILED for "+svcOperation+" error code: '" + errorCode + "'");
155                 } else {
156                         LOG.info("Returned SUCCESS for "+svcOperation+" ");
157       serviceDataBuilder.setResponse("Welcome OOF POC " + input.getSalutation());
158                 }
159
160                 RpcResult<GreetingOutput> rpcResult =
161                                 RpcResultBuilder.<GreetingOutput> status(true).withResult(serviceDataBuilder.build()).build();
162
163     LOG.info("Successful exit from greeting ");
164
165                 return Futures.immediateFuture(rpcResult);
166         }
167
168 // RPC configuration-phy-cell-id
169   @Override
170         public ListenableFuture<RpcResult<ConfigurationPhyCellIdOutput>> configurationPhyCellId(
171                         ConfigurationPhyCellIdInput input) {
172                 final String svcOperation = "configuration-phy-cell-id";
173
174                 Properties parms = new Properties();
175                 ConfigurationPhyCellIdOutputBuilder serviceDataBuilder = new ConfigurationPhyCellIdOutputBuilder();
176
177     LOG.info( "Reached RPC configurationPhyCellId");
178
179                 LOG.info( svcOperation +" called." );
180
181                 if(input == null ) {
182                         LOG.debug("exiting " +svcOperation+ " because of invalid input");
183                         serviceDataBuilder.setResponseCode("Input is null");
184                         RpcResult<ConfigurationPhyCellIdOutput> rpcResult =
185                                 RpcResultBuilder.<ConfigurationPhyCellIdOutput> status(true).withResult(serviceDataBuilder.build()).build();
186                         return Futures.immediateFuture(rpcResult);
187                 }
188
189                 // add input to parms
190                 LOG.info("Adding INPUT data for "+svcOperation+" input: " + input);
191                 ConfigurationPhyCellIdInputBuilder inputBuilder = new ConfigurationPhyCellIdInputBuilder(input);
192                 MdsalHelper.toProperties(parms, inputBuilder.build());
193
194                 // Call SLI sync method
195                 try
196                 {
197
198                         if (OofpcipocClient.hasGraph("oofpcipoc-api", svcOperation , null, "sync"))
199                         {
200         LOG.info( "OofpcipocClient has a Directed Graph for '" + svcOperation + "'");
201
202                                 try
203                                 {
204                                         OofpcipocClient.execute("oofpcipoc-api", svcOperation, null, "sync", serviceDataBuilder, parms);
205                                 }
206                                 catch (Exception e)
207                                 {
208                                         LOG.error("Caught exception executing service logic for "+ svcOperation, e);
209                                         serviceDataBuilder.setResponseCode("500");
210                                 }
211                         } else {
212                                 LOG.error("No service logic active for Oofpcipoc: '" + svcOperation + "'");
213                                 serviceDataBuilder.setResponseCode("503");
214                         }
215                 }
216                 catch (Exception e)
217                 {
218                         LOG.error("Caught exception looking for service logic", e);
219                         serviceDataBuilder.setResponseCode("500");
220                 }
221
222                 String errorCode = serviceDataBuilder.getResponseCode();
223
224                 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
225                         LOG.error("Returned FAILED for "+svcOperation+" error code: '" + errorCode + "'");
226                 } else {
227                         LOG.info("Returned SUCCESS for "+svcOperation+" ");
228       serviceDataBuilder.setResponseCode("Welcome OOF POC. Number of FAP entries " + input.getFapServiceNumberOfEntries());
229                 }
230
231                 RpcResult<ConfigurationPhyCellIdOutput> rpcResult =
232                                 RpcResultBuilder.<ConfigurationPhyCellIdOutput> status(true).withResult(serviceDataBuilder.build()).build();
233
234                 return Futures.immediateFuture(rpcResult);
235         }
236
237   // RPC add-neighbor
238     @Override
239         public ListenableFuture<RpcResult<AddNeighborOutput>> addNeighbor(
240                         AddNeighborInput input) {
241                 final String svcOperation = "add-neighbor";
242
243                 Properties parms = new Properties();
244                 AddNeighborOutputBuilder serviceDataBuilder = new AddNeighborOutputBuilder();
245
246       LOG.info( "Reached RPC addNeighbor");
247
248                 LOG.info( svcOperation +" called." );
249
250                 if(input == null ) {
251                         LOG.debug("exiting " +svcOperation+ " because of invalid input");
252                         serviceDataBuilder.setResponseCode("Input is null");
253                         RpcResult<AddNeighborOutput> rpcResult =
254                                 RpcResultBuilder.<AddNeighborOutput> status(true).withResult(serviceDataBuilder.build()).build();
255                         return Futures.immediateFuture(rpcResult);
256                 }
257
258                 // add input to parms
259                 LOG.info("Adding INPUT data for "+svcOperation+" input: " + input);
260                 AddNeighborInputBuilder inputBuilder = new AddNeighborInputBuilder(input);
261                 MdsalHelper.toProperties(parms, inputBuilder.build());
262
263                 // Call SLI sync method
264                 try
265                 {
266
267                         if (OofpcipocClient.hasGraph("oofpcipoc-api", svcOperation , null, "sync"))
268                         {
269           LOG.info( "OofpcipocClient has a Directed Graph for '" + svcOperation + "'");
270
271                                 try
272                                 {
273                                         OofpcipocClient.execute("oofpcipoc-api", svcOperation, null, "sync", serviceDataBuilder, parms);
274                                 }
275                                 catch (Exception e)
276                                 {
277                                         LOG.error("Caught exception executing service logic for "+ svcOperation, e);
278                                         serviceDataBuilder.setResponseCode("500");
279                                 }
280                         } else {
281                                 LOG.error("No service logic active for Oofpcipoc: '" + svcOperation + "'");
282                                 serviceDataBuilder.setResponseCode("503");
283                         }
284                 }
285                 catch (Exception e)
286                 {
287                         LOG.error("Caught exception looking for service logic", e);
288                         serviceDataBuilder.setResponseCode("500");
289                 }
290
291                 String errorCode = serviceDataBuilder.getResponseCode();
292
293                 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
294                         LOG.error("Returned FAILED for "+svcOperation+" error code: '" + errorCode + "'");
295                 } else {
296                         LOG.info("Returned SUCCESS for "+svcOperation+" ");
297         serviceDataBuilder.setResponseCode("Welcome OOF POC. Number of Neighbor entries to be added " + input.getLteCellNumberOfEntries());
298                 }
299
300                 RpcResult<AddNeighborOutput> rpcResult =
301                                 RpcResultBuilder.<AddNeighborOutput> status(true).withResult(serviceDataBuilder.build()).build();
302
303                 return Futures.immediateFuture(rpcResult);
304         }
305
306     // RPC delete-neighbor
307       @Override
308         public ListenableFuture<RpcResult<DeleteNeighborOutput>> deleteNeighbor(
309                         DeleteNeighborInput input) {
310                 final String svcOperation = "delete-neighbor";
311
312                 Properties parms = new Properties();
313                 DeleteNeighborOutputBuilder serviceDataBuilder = new DeleteNeighborOutputBuilder();
314
315         LOG.info( "Reached RPC deleteNeighbor");
316
317                 LOG.info( svcOperation +" called." );
318
319                 if(input == null ) {
320                         LOG.debug("exiting " +svcOperation+ " because of invalid input");
321                         serviceDataBuilder.setResponseCode("Input is null");
322                         RpcResult<DeleteNeighborOutput> rpcResult =
323                                 RpcResultBuilder.<DeleteNeighborOutput> status(true).withResult(serviceDataBuilder.build()).build();
324                         return Futures.immediateFuture(rpcResult);
325                 }
326
327                 // add input to parms
328                 LOG.info("Adding INPUT data for "+svcOperation+" input: " + input);
329                 DeleteNeighborInputBuilder inputBuilder = new DeleteNeighborInputBuilder(input);
330                 MdsalHelper.toProperties(parms, inputBuilder.build());
331
332                 // Call SLI sync method
333                 try
334                 {
335
336                         if (OofpcipocClient.hasGraph("oofpcipoc-api", svcOperation , null, "sync"))
337                         {
338             LOG.info( "OofpcipocClient has a Directed Graph for '" + svcOperation + "'");
339
340                                 try
341                                 {
342                                         OofpcipocClient.execute("oofpcipoc-api", svcOperation, null, "sync", serviceDataBuilder, parms);
343                                 }
344                                 catch (Exception e)
345                                 {
346                                         LOG.error("Caught exception executing service logic for "+ svcOperation, e);
347                                         serviceDataBuilder.setResponseCode("500");
348                                 }
349                         } else {
350                                 LOG.error("No service logic active for Oofpcipoc: '" + svcOperation + "'");
351                                 serviceDataBuilder.setResponseCode("503");
352                         }
353                 }
354                 catch (Exception e)
355                 {
356                         LOG.error("Caught exception looking for service logic", e);
357                         serviceDataBuilder.setResponseCode("500");
358                 }
359
360                 String errorCode = serviceDataBuilder.getResponseCode();
361
362                 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
363                         LOG.error("Returned FAILED for "+svcOperation+" error code: '" + errorCode + "'");
364                 } else {
365                         LOG.info("Returned SUCCESS for "+svcOperation+" ");
366           serviceDataBuilder.setResponseCode("Welcome OOF POC. Number of Neighbor entries to be deleted " + input.getLteCellNumberOfEntries());
367                 }
368
369                 RpcResult<DeleteNeighborOutput> rpcResult =
370                                 RpcResultBuilder.<DeleteNeighborOutput> status(true).withResult(serviceDataBuilder.build()).build();
371
372                 return Futures.immediateFuture(rpcResult);
373         }
374
375       // RPC generic-neighbor-configuration
376         @Override
377         public ListenableFuture<RpcResult<GenericNeighborConfigurationOutput>> genericNeighborConfiguration(
378                         GenericNeighborConfigurationInput input) {
379                 final String svcOperation = "generic-neighbor-configuration";
380
381                 Properties parms = new Properties();
382                 GenericNeighborConfigurationOutputBuilder serviceDataBuilder = new GenericNeighborConfigurationOutputBuilder();
383
384           LOG.info( "Reached RPC genericNeighborConfiguration");
385
386                 LOG.info( svcOperation +" called." );
387
388                 if(input == null ) {
389                         LOG.debug("exiting " +svcOperation+ " because of invalid input");
390                         serviceDataBuilder.setResponseCode("Input is null");
391                         RpcResult<GenericNeighborConfigurationOutput> rpcResult =
392                                 RpcResultBuilder.<GenericNeighborConfigurationOutput> status(true).withResult(serviceDataBuilder.build()).build();
393                         return Futures.immediateFuture(rpcResult);
394                 }
395
396                 // add input to parms
397                 LOG.info("Adding INPUT data for "+svcOperation+" input: " + input);
398                 GenericNeighborConfigurationInputBuilder inputBuilder = new GenericNeighborConfigurationInputBuilder(input);
399                 MdsalHelper.toProperties(parms, inputBuilder.build());
400
401                 // Call SLI sync method
402                 try
403                 {
404
405                         if (OofpcipocClient.hasGraph("oofpcipoc-api", svcOperation , null, "sync"))
406                         {
407               LOG.info( "OofpcipocClient has a Directed Graph for '" + svcOperation + "'");
408
409                                 try
410                                 {
411                                         OofpcipocClient.execute("oofpcipoc-api", svcOperation, null, "sync", serviceDataBuilder, parms);
412                                 }
413                                 catch (Exception e)
414                                 {
415                                         LOG.error("Caught exception executing service logic for "+ svcOperation, e);
416                                         serviceDataBuilder.setResponseCode("500");
417                                 }
418                         } else {
419                                 LOG.error("No service logic active for Oofpcipoc: '" + svcOperation + "'");
420                                 serviceDataBuilder.setResponseCode("503");
421                         }
422                 }
423                 catch (Exception e)
424                 {
425                         LOG.error("Caught exception looking for service logic", e);
426                         serviceDataBuilder.setResponseCode("500");
427                 }
428
429                 String errorCode = serviceDataBuilder.getResponseCode();
430
431                 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
432                         LOG.error("Returned FAILED for "+svcOperation+" error code: '" + errorCode + "'");
433                 } else {
434                         LOG.info("Returned SUCCESS for "+svcOperation+" ");
435             serviceDataBuilder.setResponseCode("Welcome OOF POC. Number of Neighbor entries to be configured " + input.getLteCellNumberOfEntries());
436                 }
437
438                 RpcResult<GenericNeighborConfigurationOutput> rpcResult =
439                                 RpcResultBuilder.<GenericNeighborConfigurationOutput> status(true).withResult(serviceDataBuilder.build()).build();
440
441                 return Futures.immediateFuture(rpcResult);
442         }
443
444     // RPC handle-nbrlist-change-notif
445         @Override
446         public ListenableFuture<RpcResult<HandleNbrlistChangeNotifOutput>> handleNbrlistChangeNotif(
447                         HandleNbrlistChangeNotifInput input) {
448                 final String svcOperation = "handle-nbrlist-change-notif";
449
450                 Properties parms = new Properties();
451                 HandleNbrlistChangeNotifOutputBuilder serviceDataBuilder = new HandleNbrlistChangeNotifOutputBuilder();
452
453           LOG.info( "Reached RPC handle-nbrlist-change-notif");
454
455                 LOG.info( svcOperation +" called." );
456
457                 if(input == null ) {
458                         LOG.debug("exiting " +svcOperation+ " because of invalid input");
459                         serviceDataBuilder.setResponseCode("Input is null");
460                         RpcResult<HandleNbrlistChangeNotifOutput> rpcResult =
461                                 RpcResultBuilder.<HandleNbrlistChangeNotifOutput> status(true).withResult(serviceDataBuilder.build()).build();
462                         return Futures.immediateFuture(rpcResult);
463                 }
464
465                 // add input to parms
466                 LOG.info("Adding INPUT data for "+svcOperation+" input: " + input);
467                 HandleNbrlistChangeNotifInputBuilder inputBuilder = new HandleNbrlistChangeNotifInputBuilder(input);
468                 MdsalHelper.toProperties(parms, inputBuilder.build());
469
470                 // Call SLI sync method
471                 try
472                 {
473                         if (OofpcipocClient.hasGraph("oofpcipoc-api", svcOperation , null, "sync"))
474                         {
475               LOG.info( "OofpcipocClient has a Directed Graph for '" + svcOperation + "'");
476                                 try
477                                 {
478                 OofpcipocClient.execute("oofpcipoc-api", svcOperation, null, "sync", serviceDataBuilder, parms);
479                                 }
480                                 catch (Exception e)
481                                 {
482                                         LOG.error("Caught exception executing service logic for "+ svcOperation, e);
483                                         serviceDataBuilder.setResponseCode("500");
484                                 }
485                         } else {
486                                 LOG.error("No service logic active for Oofpcipoc: '" + svcOperation + "'");
487                                 serviceDataBuilder.setResponseCode("503");
488                         }
489                 }
490                 catch (Exception e)
491                 {
492                         LOG.error("Caught exception looking for service logic", e);
493                         serviceDataBuilder.setResponseCode("500");
494                 }
495
496                 String errorCode = serviceDataBuilder.getResponseCode();
497
498                 if (!("0".equals(errorCode) || "200".equals(errorCode))) {
499                         LOG.error("Returned FAILED for "+svcOperation+" error code: '" + errorCode + "'");
500                 } else {
501                         LOG.info("Returned SUCCESS for "+svcOperation+" ");
502             serviceDataBuilder.setResponseCode("Welcome OOF POC. Number of FAP services changed = " + input.getFapServiceNumberOfEntriesChanged());
503                 }
504
505                 RpcResult<HandleNbrlistChangeNotifOutput> rpcResult =
506                                 RpcResultBuilder.<HandleNbrlistChangeNotifOutput> status(true).withResult(serviceDataBuilder.build()).build();
507
508           LOG.info("Successful exit from handle-nbrlist-change-notif ");
509
510                 return Futures.immediateFuture(rpcResult);
511         }
512
513 }