2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.artifact.handler;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.Future;
32 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
33 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
34 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
35 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
36 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
37 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
38 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
39 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
40 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
41 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.ArtifactHandlerService;
42 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactInput;
43 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactInputBuilder;
44 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactOutput;
45 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactOutputBuilder;
46 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.uploadartifact.output.ConfigDocumentResponseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.Services;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.ServicesBuilder;
49 import org.opendaylight.yangtools.concepts.ListenerRegistration;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
53 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
54 import org.onap.appc.artifact.handler.utils.ArtifactHandlerProviderUtil;
55 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
57 import com.att.eelf.configuration.EELFLogger;
58 import com.att.eelf.configuration.EELFManager;
59 import com.google.common.util.concurrent.CheckedFuture;
60 import com.google.common.util.concurrent.Futures;
64 public class ArtifactHandlerProvider implements AutoCloseable, ArtifactHandlerService, DataChangeListener {
66 private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerProvider.class);
67 private final String appName = "ArtifactsHandler";
68 private final ExecutorService executor;
69 protected DataBroker dataBroker;
70 protected NotificationProviderService notificationService;
71 protected RpcProviderRegistry rpcRegistry;
72 private ListenerRegistration<DataChangeListener> dclServices;
74 protected BindingAwareBroker.RpcRegistration<ArtifactHandlerService> rpcRegistration;
76 public ArtifactHandlerProvider(DataBroker dataBroker2,
77 NotificationProviderService notificationProviderService,
78 RpcProviderRegistry rpcProviderRegistry) {
79 this.log.info("Creating provider for " + appName);
80 executor = Executors.newFixedThreadPool(10);
81 dataBroker = dataBroker2;
82 notificationService = notificationProviderService;
83 rpcRegistry = rpcProviderRegistry;
88 public void initialize() {
89 log.info("Initializing provider for " + appName);
90 // Create the top level containers
93 ArtifactHandlerProviderUtil.loadProperties();
94 } catch (Exception e) {
95 log.error("Caught exception while trying to load properties file", e);
97 // Listener for changes to Services tree
99 rpcRegistration = rpcRegistry.addRpcImplementation(
100 ArtifactHandlerService.class, this);
102 log.info("Initialization complete for " + appName);
104 private void createContainers() {
105 final WriteTransaction t = dataBroker.newReadWriteTransaction();
106 // Create the Services container
107 t.merge(LogicalDatastoreType.CONFIGURATION,InstanceIdentifier.create(Services.class),new ServicesBuilder().build());
108 t.merge(LogicalDatastoreType.OPERATIONAL,InstanceIdentifier.create(Services.class),new ServicesBuilder().build());
111 CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
113 log.info("Create containers succeeded!");
115 } catch (InterruptedException | ExecutionException e) {
116 log.error("Create containers failed", e);
122 public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> arg0) {
123 // TODO Auto-generated method stub
130 public void close() throws Exception {
132 log.info("Closing provider for " + appName);
133 if(this.executor != null){
136 if(this.rpcRegistration != null){
137 rpcRegistration.close();
139 log.info("Successfully closed provider for " + appName);
143 private RpcResult<UploadartifactOutput> buildResponse1(
150 UploadartifactOutputBuilder responseBuilder = new UploadartifactOutputBuilder();
151 ConfigDocumentResponseBuilder configResponseBuilder=new ConfigDocumentResponseBuilder();
152 configResponseBuilder.setRequestId(svcRequestId);
153 configResponseBuilder.setStatus(code);
154 configResponseBuilder.setErrorReason(message);
155 RpcResult<UploadartifactOutput> rpcResult = RpcResultBuilder.<UploadartifactOutput> status(true)
156 .withResult(responseBuilder.build()).build();
161 public Future<RpcResult<UploadartifactOutput>> uploadartifact(UploadartifactInput input) {
163 if (input == null || input.getDocumentParameters() == null || input.getDocumentParameters().getArtifactContents() == null ) {
164 RpcResult<UploadartifactOutput> rpcResult =
165 buildResponse1("N/A", "N/A", "INVALID_INPUT", "Invalid input, null or empty document information" , "Y");
166 return Futures.immediateFuture(rpcResult);
168 UploadartifactInputBuilder inputBuilder = new UploadartifactInputBuilder(input);
169 ConfigDocumentResponseBuilder configResponseBuilder = new ConfigDocumentResponseBuilder();
170 UploadartifactOutputBuilder responseBuilder = new UploadartifactOutputBuilder();
171 log.info("Received input = " + input );
172 ArtifactHandlerProviderUtil designUtil = new ArtifactHandlerProviderUtil(input);
173 configResponseBuilder.setRequestId(input.getRequestInformation().getRequestId());
176 if(input.getRequestInformation().getSource() !=null){
177 if(input.getRequestInformation().getSource().equalsIgnoreCase(SdcArtifactHandlerConstants.DESIGN_TOOL)){
178 designUtil.processTemplate(designUtil.createDummyRequestData());
179 configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());
183 designUtil.processTemplate(designUtil.createRequestData());
184 configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());
189 throw new Exception("No Tempalte data found");
194 catch (Exception e) {
196 configResponseBuilder.setErrorReason(e.getMessage());
197 configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_ERROR.toString());
198 log.error("Caught exception looking for Artifact Handler", e);
199 log.info("Caught exception looking for Artifact Handler: ");
202 responseBuilder.setConfigDocumentResponse(configResponseBuilder.build());
203 RpcResult<UploadartifactOutput> rpcResult = RpcResultBuilder.<UploadartifactOutput> status(true).withResult(responseBuilder.build()).build();
204 return Futures.immediateFuture(rpcResult);