Renamed local variable to match the regular expression
[appc.git] / appc-dg / appc-dg-shared / appc-dg-mdsal-store / appc-dg-mdsal-bundle / src / main / java / org / onap / appc / mdsal / provider / MdsalStoreProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.mdsal.provider;
27
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32 import org.onap.appc.Constants;
33 import org.onap.appc.mdsal.MDSALStore;
34 import org.onap.appc.mdsal.impl.MDSALStoreFactory;
35 import org.onap.appc.mdsal.objects.BundleInfo;
36 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
37 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
38 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
39 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
40 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.MdsalStoreService;
41 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangInput;
42 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangOutput;
43 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangOutputBuilder;
44 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.response.Status;
45 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.response.StatusBuilder;
46 import org.opendaylight.yangtools.yang.common.RpcResult;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48 import com.att.eelf.configuration.EELFLogger;
49 import com.att.eelf.configuration.EELFManager;
50 import com.google.common.util.concurrent.Futures;
51 import com.google.common.util.concurrent.ListenableFuture;
52
53 public class MdsalStoreProvider implements MdsalStoreService ,AutoCloseable{
54
55     protected DataBroker dataBroker;
56     protected RpcProviderRegistry rpcRegistry;
57     protected NotificationPublishService notificationService;
58
59     protected BindingAwareBroker.RpcRegistration<MdsalStoreService> rpcRegistration;
60     private final EELFLogger log = EELFManager.getInstance().getLogger(MdsalStoreProvider.class);
61     private final ExecutorService executor;
62     private static final String APP_NAME = "MdsalStoreProvider";
63
64     public MdsalStoreProvider(DataBroker dataBroker2, NotificationPublishService notificationProviderService
65             , RpcProviderRegistry rpcRegistry2){
66         log.info("Creating provider for " + APP_NAME);
67         executor = Executors.newFixedThreadPool(1);
68         this.dataBroker = dataBroker2;
69         this.notificationService = notificationProviderService;
70
71         this.rpcRegistry = rpcRegistry2;
72
73         if (this.rpcRegistry != null) {
74             rpcRegistration = rpcRegistry.addRpcImplementation(MdsalStoreService.class, this);
75         }
76         log.info("Initialization complete for " + APP_NAME);
77
78     }
79
80     @Override
81     public void close() throws Exception {
82         log.info("Closing provider for " + APP_NAME);
83         if(this.executor != null){
84             executor.shutdown();
85         }
86         if(this.rpcRegistration != null){
87             rpcRegistration.close();
88         }
89         log.info("Successfully closed provider for " + APP_NAME);
90     }
91
92     @Override
93     public ListenableFuture<RpcResult<StoreYangOutput>> storeYang(StoreYangInput input) {
94         Status status = null;
95         String message = null;
96         try{
97             BundleInfo bundleInfo = new BundleInfo();
98             bundleInfo.setName(input.getModuleName());
99             bundleInfo.setDescription(input.getModuleName());
100             bundleInfo.setLocation(input.getModuleName());
101
102             MDSALStore store =  MDSALStoreFactory.createMDSALStore();
103
104             Date revision = new SimpleDateFormat(Constants.YANG_REVISION_FORMAT).parse(Constants.YANG_REVISION);
105             if(!store.isModulePresent(input.getModuleName(),revision)){
106                 message = "YANG module saved successfully";
107                 store.storeYangModule(input.getYang(),bundleInfo);
108             }
109             else{
110                 message = "YANG Module already available";
111             }
112             store.storeYangModuleOnLeader(input.getYang(),input.getModuleName());
113             status = new StatusBuilder().setCode(200).setMessage(message).build();
114         }
115         catch (Exception e){
116
117             message = "Error in storeYang of MdsalStoreProvider";
118             log.error(message,e);
119             status = new StatusBuilder().setCode(500).setMessage(message).build();
120         }
121         StoreYangOutputBuilder builder = new StoreYangOutputBuilder().setStatus(status);
122         return Futures.immediateFuture(
123                 RpcResultBuilder
124                         .<StoreYangOutput>status(true)
125                         .withResult(builder.build())
126                         .build());
127     }
128 }