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