2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications Copyright (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.artifact.handler;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
31 import java.util.concurrent.Future;
33 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
34 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
35 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
36 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
37 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
38 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
39 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
40 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
41 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
42 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
43 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.ArtifactHandlerService;
44 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactInput;
45 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactInputBuilder;
46 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactOutput;
47 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactOutputBuilder;
48 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.uploadartifact.output.ConfigDocumentResponseBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.Services;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.ServicesBuilder;
51 import org.opendaylight.yangtools.concepts.ListenerRegistration;
52 import org.opendaylight.yangtools.yang.binding.DataObject;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.opendaylight.yangtools.yang.common.RpcResult;
55 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
56 import org.onap.appc.artifact.handler.utils.ArtifactHandlerProviderUtil;
57 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
59 import com.att.eelf.configuration.EELFLogger;
60 import com.att.eelf.configuration.EELFManager;
61 import com.google.common.util.concurrent.CheckedFuture;
62 import com.google.common.util.concurrent.Futures;
66 public class ArtifactHandlerProvider implements AutoCloseable, ArtifactHandlerService, DataChangeListener {
68 private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerProvider.class);
69 private final String appName = "ArtifactsHandler";
70 private final ExecutorService executor;
71 protected DataBroker dataBroker;
72 protected NotificationPublishService notificationService;
73 protected RpcProviderRegistry rpcRegistry;
74 private ListenerRegistration<DataChangeListener> dclServices;
76 protected BindingAwareBroker.RpcRegistration<ArtifactHandlerService> rpcRegistration;
78 public ArtifactHandlerProvider(DataBroker dataBroker2,
79 NotificationPublishService notificationProviderService,
80 RpcProviderRegistry rpcProviderRegistry) {
81 this.log.info("Creating provider for " + appName);
82 executor = Executors.newFixedThreadPool(10);
83 dataBroker = dataBroker2;
84 notificationService = notificationProviderService;
85 rpcRegistry = rpcProviderRegistry;
90 public void initialize() {
91 log.info("Initializing provider for " + appName);
92 // Create the top level containers
95 ArtifactHandlerProviderUtil.loadProperties();
96 } catch (Exception e) {
97 log.error("Caught exception while trying to load properties file", e);
99 // Listener for changes to Services tree
101 log.info("Initialization complete for " + appName);
103 private void createContainers() {
104 final WriteTransaction t = dataBroker.newReadWriteTransaction();
105 // Create the Services container
106 t.merge(LogicalDatastoreType.CONFIGURATION,InstanceIdentifier.create(Services.class),new ServicesBuilder().build());
107 t.merge(LogicalDatastoreType.OPERATIONAL,InstanceIdentifier.create(Services.class),new ServicesBuilder().build());
110 CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
112 log.info("Create containers succeeded!");
114 } catch (InterruptedException | ExecutionException e) {
115 log.error("Create containers failed", e);
121 public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> arg0) {
122 // TODO Auto-generated method stub
129 public void close() throws Exception {
131 log.info("Closing provider for " + appName);
132 if(this.executor != null){
135 if(this.rpcRegistration != null){
136 rpcRegistration.close();
138 log.info("Successfully closed provider for " + appName);
142 private RpcResult<UploadartifactOutput> buildResponse1(
149 UploadartifactOutputBuilder responseBuilder = new UploadartifactOutputBuilder();
150 ConfigDocumentResponseBuilder configResponseBuilder=new ConfigDocumentResponseBuilder();
151 configResponseBuilder.setRequestId(svcRequestId);
152 configResponseBuilder.setStatus(code);
153 configResponseBuilder.setErrorReason(message);
154 RpcResult<UploadartifactOutput> rpcResult = RpcResultBuilder.<UploadartifactOutput> status(true)
155 .withResult(responseBuilder.build()).build();
160 public Future<RpcResult<UploadartifactOutput>> uploadartifact(UploadartifactInput input) {
162 if (input == null || input.getDocumentParameters() == null || input.getDocumentParameters().getArtifactContents() == null ) {
163 RpcResult<UploadartifactOutput> rpcResult =
164 buildResponse1("N/A", "N/A", "INVALID_INPUT", "Invalid input, null or empty document information" , "Y");
165 return Futures.immediateFuture(rpcResult);
167 UploadartifactInputBuilder inputBuilder = new UploadartifactInputBuilder(input);
168 ConfigDocumentResponseBuilder configResponseBuilder = new ConfigDocumentResponseBuilder();
169 UploadartifactOutputBuilder responseBuilder = new UploadartifactOutputBuilder();
170 log.info("Received input = " + input );
171 ArtifactHandlerProviderUtil designUtil = getArtifactHandlerProviderUtil(input);
172 configResponseBuilder.setRequestId(input.getRequestInformation().getRequestId());
174 if(input.getRequestInformation().getSource() !=null){
175 if(input.getRequestInformation().getSource().equalsIgnoreCase(SdcArtifactHandlerConstants.DESIGN_TOOL)){
176 designUtil.processTemplate(designUtil.createDummyRequestData());
177 configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());
181 designUtil.processTemplate(designUtil.createRequestData());
182 configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());
187 throw new Exception("No Template data found");
190 catch (Exception e) {
192 configResponseBuilder.setErrorReason(e.getMessage());
193 configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_ERROR.toString());
194 log.error("Caught exception looking for Artifact Handler", e);
195 log.info("Caught exception looking for Artifact Handler: ");
198 responseBuilder.setConfigDocumentResponse(configResponseBuilder.build());
199 RpcResult<UploadartifactOutput> rpcResult = RpcResultBuilder.<UploadartifactOutput> status(true).withResult(responseBuilder.build()).build();
200 return Futures.immediateFuture(rpcResult);
204 protected ArtifactHandlerProviderUtil getArtifactHandlerProviderUtil(UploadartifactInput input) {
205 return new ArtifactHandlerProviderUtil(input);