Fix copyright to avoid noisy diff after build
[sdnc/northbound.git] / dataChange / provider / src / main / java / org / openecomp / sdnc / datachange / DataChangeProvider.java
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.openecomp.sdnc.datachange;
23
24 import java.util.Properties;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.Future;
28
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;
43
44 import com.google.common.util.concurrent.Futures;
45
46 /**
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.
50  *
51  */
52 public class DataChangeProvider implements AutoCloseable, DataChangeService{
53
54     private final Logger log = LoggerFactory.getLogger( DataChangeProvider.class );
55     private final String appName = "DataChange";
56     private final ExecutorService executor;
57
58     protected DataBroker dataBroker;
59     protected NotificationProviderService notificationService;
60     protected RpcProviderRegistry rpcRegistry;
61     protected BindingAwareBroker.RpcRegistration<DataChangeService> rpcRegistration;
62
63
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;
72                 initialize();
73     }
74
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 );
79     }
80
81     protected void initializeChild() {
82         //Override if you have custom initialization intelligence
83     }
84
85     @Override
86     public void close() throws Exception {
87         log.info( "Closing provider for " + appName );
88             executor.shutdown();
89             rpcRegistration.close();
90         log.info( "Successfully closed provider for " + appName );
91     }
92
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") + "." );
97         }
98     }
99
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") + "." );
105         }
106     }
107
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") + "." );
112         }
113     }
114
115         @Override
116         public Future<RpcResult<DataChangeNotificationOutput>> dataChangeNotification(
117                         DataChangeNotificationInput input) {
118                 final String SVC_OPERATION = "data-change-notification";
119
120                 Properties parms = new Properties();
121                 DataChangeNotificationOutputBuilder serviceDataBuilder = new DataChangeNotificationOutputBuilder();
122
123                 log.info( SVC_OPERATION +" called." );
124
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);
131                 }
132
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());
137
138                 // Call SLI sync method
139                 // Get SvcLogicService reference
140
141                 DataChangeClient svcLogicClient = new DataChangeClient();
142                 Properties respProps = null;
143
144                 try
145                 {
146                         if (svcLogicClient.hasGraph("DataChange", SVC_OPERATION , null, "sync"))
147                         {
148                                 try
149                                 {
150                                         respProps = svcLogicClient.execute("DataChange", SVC_OPERATION, null, "sync", serviceDataBuilder, parms);
151                                 }
152                                 catch (Exception e)
153                                 {
154                                         log.error("Caught exception executing service logic for "+ SVC_OPERATION, e);
155                                         serviceDataBuilder.setDataChangeResponseCode("500");
156                                 }
157                         } else {
158                                 log.error("No service logic active for DataChange: '" + SVC_OPERATION + "'");
159                                 serviceDataBuilder.setDataChangeResponseCode("503");
160                         }
161                 }
162                 catch (Exception e)
163                 {
164                         log.error("Caught exception looking for service logic", e);
165                         serviceDataBuilder.setDataChangeResponseCode("500");
166                 }
167
168                 String errorCode = serviceDataBuilder.getDataChangeResponseCode();
169
170                 if ( errorCode != null && errorCode.length() != 0 && !( errorCode.equals("0")|| errorCode.equals("200"))) {
171                         log.error("Returned FAILED for "+SVC_OPERATION+" error code: '" + errorCode + "'");
172                 } else {
173                         log.info("Returned SUCCESS for "+SVC_OPERATION+" ");
174                 }
175
176                 RpcResult<DataChangeNotificationOutput> rpcResult =
177                                 RpcResultBuilder.<DataChangeNotificationOutput> status(true).withResult(serviceDataBuilder.build()).build();
178                 // return error
179                 return Futures.immediateFuture(rpcResult);
180         }
181 }