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.artifact.handler;
 
  26 import java.util.concurrent.ExecutionException;
 
  27 import java.util.concurrent.ExecutorService;
 
  28 import java.util.concurrent.Executors;
 
  29 import java.util.concurrent.Future;
 
  31 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 
  32 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
 
  33 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 
  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 NotificationPublishService notificationService;
 
  71     protected RpcProviderRegistry rpcRegistry;
 
  72     private ListenerRegistration<DataChangeListener> dclServices;
 
  74     protected BindingAwareBroker.RpcRegistration<ArtifactHandlerService> rpcRegistration;
 
  76     public ArtifactHandlerProvider(DataBroker dataBroker2,
 
  77             NotificationPublishService 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         log.info("Initialization complete for " + appName);
 
 101     private void createContainers() {
 
 102         final WriteTransaction t = dataBroker.newReadWriteTransaction();
 
 103         // Create the Services container
 
 104         t.merge(LogicalDatastoreType.CONFIGURATION,InstanceIdentifier.create(Services.class),new ServicesBuilder().build());
 
 105         t.merge(LogicalDatastoreType.OPERATIONAL,InstanceIdentifier.create(Services.class),new ServicesBuilder().build());
 
 108             CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
 
 110             log.info("Create containers succeeded!");
 
 112         } catch (InterruptedException | ExecutionException e) {
 
 113             log.error("Create containers failed",  e);
 
 119     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> arg0) {
 
 120         // TODO Auto-generated method stub
 
 127     public void close() throws Exception {
 
 129         log.info("Closing provider for " + appName);
 
 130         if(this.executor != null){
 
 133         if(this.rpcRegistration != null){
 
 134             rpcRegistration.close();
 
 136         log.info("Successfully closed provider for " + appName);
 
 140     private RpcResult<UploadartifactOutput> buildResponse1(
 
 147         UploadartifactOutputBuilder responseBuilder = new UploadartifactOutputBuilder();
 
 148         ConfigDocumentResponseBuilder configResponseBuilder=new ConfigDocumentResponseBuilder();            
 
 149         configResponseBuilder.setRequestId(svcRequestId);            
 
 150         configResponseBuilder.setStatus(code);            
 
 151         configResponseBuilder.setErrorReason(message);            
 
 152         RpcResult<UploadartifactOutput> rpcResult = RpcResultBuilder.<UploadartifactOutput> status(true)
 
 153                 .withResult(responseBuilder.build()).build();
 
 158     public Future<RpcResult<UploadartifactOutput>> uploadartifact(UploadartifactInput input) {
 
 160         if (input == null || input.getDocumentParameters() == null || input.getDocumentParameters().getArtifactContents() == null ) {        
 
 161             RpcResult<UploadartifactOutput> rpcResult =
 
 162                     buildResponse1("N/A", "N/A", "INVALID_INPUT", "Invalid input, null or empty document information" , "Y");
 
 163             return Futures.immediateFuture(rpcResult);
 
 165         UploadartifactInputBuilder inputBuilder = new UploadartifactInputBuilder(input);
 
 166         ConfigDocumentResponseBuilder configResponseBuilder = new ConfigDocumentResponseBuilder();
 
 167         UploadartifactOutputBuilder responseBuilder = new UploadartifactOutputBuilder();
 
 168         log.info("Received input = " + input );
 
 169         ArtifactHandlerProviderUtil designUtil = new ArtifactHandlerProviderUtil(input);
 
 170         configResponseBuilder.setRequestId(input.getRequestInformation().getRequestId());
 
 173             if(input.getRequestInformation().getSource() !=null){
 
 174                 if(input.getRequestInformation().getSource().equalsIgnoreCase(SdcArtifactHandlerConstants.DESIGN_TOOL)){
 
 175                         designUtil.processTemplate(designUtil.createDummyRequestData());
 
 176                         configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());
 
 180                     designUtil.processTemplate(designUtil.createRequestData());
 
 181                     configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());        
 
 186                 throw new Exception("No Tempalte data found");                
 
 191         catch (Exception e) {
 
 193             configResponseBuilder.setErrorReason(e.getMessage());            
 
 194             configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_ERROR.toString());
 
 195             log.error("Caught exception looking for Artifact Handler", e);
 
 196             log.info("Caught exception looking for Artifact Handler: ");
 
 199         responseBuilder.setConfigDocumentResponse(configResponseBuilder.build());
 
 200         RpcResult<UploadartifactOutput> rpcResult = RpcResultBuilder.<UploadartifactOutput> status(true).withResult(responseBuilder.build()).build();
 
 201         return Futures.immediateFuture(rpcResult);