Update license header in appc-inbound files
[appc.git] / appc-inbound / appc-artifact-handler / provider / src / main / java / org / onap / appc / artifact / handler / ArtifactHandlerProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.artifact.handler;
25
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29 import java.util.concurrent.Future;
30
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.WriteTransaction;
34 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
37 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
38 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
39 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
40 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.ArtifactHandlerService;
41 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactInput;
42 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactInputBuilder;
43 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactOutput;
44 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactOutputBuilder;
45 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.uploadartifact.output.ConfigDocumentResponseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.Services;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.ServicesBuilder;
48 import org.opendaylight.yangtools.concepts.ListenerRegistration;
49 import org.opendaylight.yangtools.yang.binding.DataObject;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.common.RpcResult;
52 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
53 import org.onap.appc.artifact.handler.utils.ArtifactHandlerProviderUtil;
54 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
55
56 import com.att.eelf.configuration.EELFLogger;
57 import com.att.eelf.configuration.EELFManager;
58 import com.google.common.util.concurrent.CheckedFuture;
59 import com.google.common.util.concurrent.Futures;
60
61
62
63 public class ArtifactHandlerProvider implements AutoCloseable, ArtifactHandlerService, DataChangeListener {
64
65     private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerProvider.class);
66     private final String appName = "ArtifactsHandler";
67     private final ExecutorService executor;
68     protected DataBroker dataBroker;
69     protected NotificationProviderService notificationService;
70     protected RpcProviderRegistry rpcRegistry;
71     private ListenerRegistration<DataChangeListener> dclServices;
72
73     protected BindingAwareBroker.RpcRegistration<ArtifactHandlerService> rpcRegistration;
74
75     public ArtifactHandlerProvider(DataBroker dataBroker2,
76             NotificationProviderService notificationProviderService,
77             RpcProviderRegistry rpcProviderRegistry) {
78         this.log.info("Creating provider for " + appName);
79         executor = Executors.newFixedThreadPool(10);
80         dataBroker = dataBroker2;
81         notificationService = notificationProviderService;
82         rpcRegistry = rpcProviderRegistry;
83         initialize();
84
85     }
86
87     public void initialize() {
88         log.info("Initializing provider for " + appName);
89         // Create the top level containers
90         createContainers();
91         try {
92             ArtifactHandlerProviderUtil.loadProperties();
93         } catch (Exception e) {
94             log.error("Caught exception while trying to load properties file", e);
95         }
96         // Listener for changes to Services tree
97
98         rpcRegistration = rpcRegistry.addRpcImplementation(
99                 ArtifactHandlerService.class, this);
100
101         log.info("Initialization complete for " + appName);
102     }
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());
108
109         try {
110             CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
111             checkedFuture.get();
112             log.info("Create containers succeeded!");
113
114         } catch (InterruptedException | ExecutionException e) {
115             log.error("Create containers failed",  e);
116         }
117     }
118
119
120     @Override
121     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> arg0) {
122         // TODO Auto-generated method stub
123
124     }
125
126
127
128     @Override
129     public void close() throws Exception {
130
131         log.info("Closing provider for " + appName);
132         if(this.executor != null){
133             executor.shutdown();
134         }
135         if(this.rpcRegistration != null){
136             rpcRegistration.close();
137         }
138         log.info("Successfully closed provider for " + appName);
139
140     }
141
142     private RpcResult<UploadartifactOutput> buildResponse1(
143             String svcRequestId,
144             String topic,
145             String code,
146             String message,
147             String finalInd) {
148
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();
156         return rpcResult;            
157     }
158
159     @Override
160     public Future<RpcResult<UploadartifactOutput>> uploadartifact(UploadartifactInput input) {
161
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);
166         }
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 = new ArtifactHandlerProviderUtil(input);
172         configResponseBuilder.setRequestId(input.getRequestInformation().getRequestId());
173         try{
174             
175             if(input.getRequestInformation().getSource() !=null){
176                 if(input.getRequestInformation().getSource().equalsIgnoreCase(SdcArtifactHandlerConstants.DESIGN_TOOL)){
177                         designUtil.processTemplate(designUtil.createDummyRequestData());
178                         configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());
179                 }
180                 else
181                 {
182                     designUtil.processTemplate(designUtil.createRequestData());
183                     configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());        
184                 }
185             }
186             else
187             {
188                 throw new Exception("No Tempalte data found");                
189             }
190             
191             
192         }
193         catch (Exception e) {
194
195             configResponseBuilder.setErrorReason(e.getMessage());            
196             configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_ERROR.toString());
197             log.error("Caught exception looking for Artifact Handler", e);
198             log.info("Caught exception looking for Artifact Handler: ");
199         }
200         
201         responseBuilder.setConfigDocumentResponse(configResponseBuilder.build());
202         RpcResult<UploadartifactOutput> rpcResult = RpcResultBuilder.<UploadartifactOutput> status(true).withResult(responseBuilder.build()).build();
203         return Futures.immediateFuture(rpcResult);
204
205     }
206 }