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