2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.mdsal.provider;
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;
53 public class MdsalStoreProvider implements MdsalStoreService ,AutoCloseable{
55 protected DataBroker dataBroker;
56 protected RpcProviderRegistry rpcRegistry;
57 protected NotificationPublishService notificationService;
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";
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;
71 this.rpcRegistry = rpcRegistry2;
73 if (this.rpcRegistry != null) {
74 rpcRegistration = rpcRegistry.addRpcImplementation(MdsalStoreService.class, this);
76 log.info("Initialization complete for " + APP_NAME);
81 public void close() throws Exception {
82 log.info("Closing provider for " + APP_NAME);
83 if(this.executor != null){
86 if(this.rpcRegistration != null){
87 rpcRegistration.close();
89 log.info("Successfully closed provider for " + APP_NAME);
93 public ListenableFuture<RpcResult<StoreYangOutput>> storeYang(StoreYangInput input) {
95 String message = null;
97 BundleInfo bundleInfo = new BundleInfo();
98 bundleInfo.setName(input.getModuleName());
99 bundleInfo.setDescription(input.getModuleName());
100 bundleInfo.setLocation(input.getModuleName());
102 MDSALStore store = MDSALStoreFactory.createMDSALStore();
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);
110 message = "YANG Module already available";
112 store.storeYangModuleOnLeader(input.getYang(),input.getModuleName());
113 status = new StatusBuilder().setCode(200).setMessage(message).build();
117 message = "Error in storeYang of MdsalStoreProvider";
118 log.error(message,e);
119 status = new StatusBuilder().setCode(500).setMessage(message).build();
121 StoreYangOutputBuilder builder = new StoreYangOutputBuilder().setStatus(status);
122 return Futures.immediateFuture(
124 .<StoreYangOutput>status(true)
125 .withResult(builder.build())