Platform hardening for appc-dg-md-sal-store
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.mdsal.provider;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.google.common.util.concurrent.Futures;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
32 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
33 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.MdsalStoreService;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangInput;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangOutput;
37 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.StoreYangOutputBuilder;
38 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.response.Status;
39 import org.opendaylight.yang.gen.v1.org.onap.appc.mdsal.store.rev170925.response.StatusBuilder;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
42 import org.onap.appc.Constants;
43 import org.onap.appc.mdsal.MDSALStore;
44 import org.onap.appc.mdsal.impl.MDSALStoreFactory;
45 import org.onap.appc.mdsal.objects.BundleInfo;
46
47 import java.text.SimpleDateFormat;
48 import java.util.Date;
49 import java.util.concurrent.ExecutorService;
50 import java.util.concurrent.Executors;
51 import java.util.concurrent.Future;
52
53 public class MdsalStoreProvider implements MdsalStoreService ,AutoCloseable{
54
55     protected DataBroker dataBroker;
56     protected RpcProviderRegistry rpcRegistry;
57     protected NotificationProviderService 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 final static String APP_NAME = "MdsalStoreProvider";
63
64     public MdsalStoreProvider(DataBroker dataBroker2, NotificationProviderService 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 Future<RpcResult<StoreYangOutput>> storeYang(StoreYangInput input) {
94         Status status =null;String message=null;
95         try{
96             BundleInfo bundleInfo = new BundleInfo();
97             bundleInfo.setName(input.getModuleName());
98             bundleInfo.setDescription(input.getModuleName());
99             bundleInfo.setLocation(input.getModuleName());
100
101             MDSALStore store =  MDSALStoreFactory.createMDSALStore();
102
103             Date revision = new SimpleDateFormat(Constants.YANG_REVISION_FORMAT).parse(Constants.YANG_REVISION);
104             if(!store.isModulePresent(input.getModuleName(),revision)){
105                 message = "YANG module saved successfully";
106                 store.storeYangModule(input.getYang(),bundleInfo);
107             }
108             else{
109                 message = "YANG Module already available";
110             }
111             store.storeYangModuleOnLeader(input.getYang(),input.getModuleName());
112             status = new StatusBuilder().setCode(200).setMessage(message).build();
113         }
114         catch (Exception e){
115
116             message = "Error in storeYang of MdsalStoreProvider";
117             log.error(message,e);
118             status = new StatusBuilder().setCode(500).setMessage(message).build();
119         }
120         StoreYangOutputBuilder builder = new StoreYangOutputBuilder().setStatus(status);
121         return Futures.immediateFuture(
122                 RpcResultBuilder
123                         .<StoreYangOutput>status(true)
124                         .withResult(builder.build())
125                         .build());
126     }
127 }