2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.sdnc.datachange;
24 import java.util.Properties;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.Future;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
31 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
32 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
33 import org.opendaylight.yang.gen.v1.org.openecomp.sdnc.datachange.rev150519.DataChangeNotificationInput;
34 import org.opendaylight.yang.gen.v1.org.openecomp.sdnc.datachange.rev150519.DataChangeNotificationInputBuilder;
35 import org.opendaylight.yang.gen.v1.org.openecomp.sdnc.datachange.rev150519.DataChangeNotificationOutput;
36 import org.opendaylight.yang.gen.v1.org.openecomp.sdnc.datachange.rev150519.DataChangeNotificationOutputBuilder;
37 import org.opendaylight.yang.gen.v1.org.openecomp.sdnc.datachange.rev150519.DataChangeService;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40 import org.openecomp.sdnc.sli.provider.MdsalHelper;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 import com.google.common.util.concurrent.Futures;
47 * Defines a base implementation for your provider. This class extends from a helper class
48 * which provides storage for the most commonly used components of the MD-SAL. Additionally the
49 * base class provides some basic logging and initialization / clean up methods.
52 public class DataChangeProvider implements AutoCloseable, DataChangeService{
54 private final Logger log = LoggerFactory.getLogger( DataChangeProvider.class );
55 private final String appName = "DataChange";
56 private final ExecutorService executor;
58 protected DataBroker dataBroker;
59 protected NotificationProviderService notificationService;
60 protected RpcProviderRegistry rpcRegistry;
61 protected BindingAwareBroker.RpcRegistration<DataChangeService> rpcRegistration;
64 public DataChangeProvider(DataBroker dataBroker2,
65 NotificationProviderService notificationProviderService,
66 RpcProviderRegistry rpcProviderRegistry) {
67 this.log.info( "Creating provider for " + appName );
68 executor = Executors.newFixedThreadPool(1);
69 dataBroker = dataBroker2;
70 notificationService = notificationProviderService;
71 rpcRegistry = rpcProviderRegistry;
75 public void initialize(){
76 log.info( "Initializing provider for " + appName );
77 rpcRegistration = rpcRegistry.addRpcImplementation(DataChangeService.class, this);
78 log.info( "Initialization complete for " + appName );
81 protected void initializeChild() {
82 //Override if you have custom initialization intelligence
86 public void close() throws Exception {
87 log.info( "Closing provider for " + appName );
89 rpcRegistration.close();
90 log.info( "Successfully closed provider for " + appName );
93 public void setDataBroker(DataBroker dataBroker) {
94 this.dataBroker = dataBroker;
95 if( log.isDebugEnabled() ){
96 log.debug( "DataBroker set to " + (dataBroker==null?"null":"non-null") + "." );
100 public void setNotificationService(
101 NotificationProviderService notificationService) {
102 this.notificationService = notificationService;
103 if( log.isDebugEnabled() ){
104 log.debug( "Notification Service set to " + (notificationService==null?"null":"non-null") + "." );
108 public void setRpcRegistry(RpcProviderRegistry rpcRegistry) {
109 this.rpcRegistry = rpcRegistry;
110 if( log.isDebugEnabled() ){
111 log.debug( "RpcRegistry set to " + (rpcRegistry==null?"null":"non-null") + "." );
116 public Future<RpcResult<DataChangeNotificationOutput>> dataChangeNotification(
117 DataChangeNotificationInput input) {
118 final String SVC_OPERATION = "data-change-notification";
120 Properties parms = new Properties();
121 DataChangeNotificationOutputBuilder serviceDataBuilder = new DataChangeNotificationOutputBuilder();
123 log.info( SVC_OPERATION +" called." );
125 if(input == null || input.getAaiEventId() == null) {
126 log.debug("exiting " +SVC_OPERATION+ " because of invalid input");
127 serviceDataBuilder.setDataChangeResponseCode("403");
128 RpcResult<DataChangeNotificationOutput> rpcResult =
129 RpcResultBuilder.<DataChangeNotificationOutput> status(true).withResult(serviceDataBuilder.build()).build();
130 return Futures.immediateFuture(rpcResult);
133 // add input to parms
134 log.info("Adding INPUT data for "+SVC_OPERATION+" input: " + input);
135 DataChangeNotificationInputBuilder inputBuilder = new DataChangeNotificationInputBuilder(input);
136 MdsalHelper.toProperties(parms, inputBuilder.build());
138 // Call SLI sync method
139 // Get SvcLogicService reference
141 DataChangeClient svcLogicClient = new DataChangeClient();
142 Properties respProps = null;
146 if (svcLogicClient.hasGraph("DataChange", SVC_OPERATION , null, "sync"))
150 respProps = svcLogicClient.execute("DataChange", SVC_OPERATION, null, "sync", serviceDataBuilder, parms);
154 log.error("Caught exception executing service logic for "+ SVC_OPERATION, e);
155 serviceDataBuilder.setDataChangeResponseCode("500");
158 log.error("No service logic active for DataChange: '" + SVC_OPERATION + "'");
159 serviceDataBuilder.setDataChangeResponseCode("503");
164 log.error("Caught exception looking for service logic", e);
165 serviceDataBuilder.setDataChangeResponseCode("500");
168 String errorCode = serviceDataBuilder.getDataChangeResponseCode();
170 if ( errorCode != null && errorCode.length() != 0 && !( errorCode.equals("0")|| errorCode.equals("200"))) {
171 log.error("Returned FAILED for "+SVC_OPERATION+" error code: '" + errorCode + "'");
173 log.info("Returned SUCCESS for "+SVC_OPERATION+" ");
176 RpcResult<DataChangeNotificationOutput> rpcResult =
177 RpcResultBuilder.<DataChangeNotificationOutput> status(true).withResult(serviceDataBuilder.build()).build();
179 return Futures.immediateFuture(rpcResult);