2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.mdsal.provider;
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;
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;
52 public class MdsalStoreProvider implements MdsalStoreService ,AutoCloseable{
54 protected DataBroker dataBroker;
55 protected RpcProviderRegistry rpcRegistry;
56 protected NotificationProviderService notificationService;
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";
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;
70 this.rpcRegistry = rpcRegistry2;
72 if (this.rpcRegistry != null) {
73 rpcRegistration = rpcRegistry.addRpcImplementation(MdsalStoreService.class, this);
75 log.info("Initialization complete for " + APP_NAME);
80 public void close() throws Exception {
81 log.info("Closing provider for " + APP_NAME);
82 if(this.executor != null){
85 if(this.rpcRegistration != null){
86 rpcRegistration.close();
88 log.info("Successfully closed provider for " + APP_NAME);
92 public Future<RpcResult<StoreYangOutput>> storeYang(StoreYangInput input) {
93 Status status =null;String message=null;
95 BundleInfo bundleInfo = new BundleInfo();
96 bundleInfo.setName(input.getModuleName());
97 bundleInfo.setDescription(input.getModuleName());
98 bundleInfo.setLocation(input.getModuleName());
100 MDSALStore store = MDSALStoreFactory.createMDSALStore();
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);
108 message = "YANG Module already available";
110 store.storeYangModuleOnLeader(input.getYang(),input.getModuleName());
111 status = new StatusBuilder().setCode(200).setMessage(message).build();
115 message = "Error in storeYang of MdsalStoreProvider";
116 log.error(message,e);
117 status = new StatusBuilder().setCode(500).setMessage(message).build();
119 StoreYangOutputBuilder builder = new StoreYangOutputBuilder().setStatus(status);
120 return Futures.immediateFuture(
122 .<StoreYangOutput>status(true)
123 .withResult(builder.build())