dc6489dc3377a0f50af404e02d9ff73c496f75ab
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.artifact.handler;
26
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.Future;
31
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;
56
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;
61
62
63
64 public class ArtifactHandlerProvider implements AutoCloseable, ArtifactHandlerService, DataChangeListener {
65
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;
73
74     protected BindingAwareBroker.RpcRegistration<ArtifactHandlerService> rpcRegistration;
75
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;
84         initialize();
85
86     }
87
88     public void initialize() {
89         log.info("Initializing provider for " + appName);
90         // Create the top level containers
91         createContainers();
92         try {
93             ArtifactHandlerProviderUtil.loadProperties();
94         } catch (Exception e) {
95             log.error("Caught exception while trying to load properties file", e);
96         }
97         // Listener for changes to Services tree
98
99         rpcRegistration = rpcRegistry.addRpcImplementation(
100                 ArtifactHandlerService.class, this);
101
102         log.info("Initialization complete for " + appName);
103     }
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());
109
110         try {
111             CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
112             checkedFuture.get();
113             log.info("Create containers succeeded!");
114
115         } catch (InterruptedException | ExecutionException e) {
116             log.error("Create containers failed",  e);
117         }
118     }
119
120
121     @Override
122     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> arg0) {
123         // TODO Auto-generated method stub
124
125     }
126
127
128
129     @Override
130     public void close() throws Exception {
131
132         log.info("Closing provider for " + appName);
133         if(this.executor != null){
134             executor.shutdown();
135         }
136         if(this.rpcRegistration != null){
137             rpcRegistration.close();
138         }
139         log.info("Successfully closed provider for " + appName);
140
141     }
142
143     private RpcResult<UploadartifactOutput> buildResponse1(
144             String svcRequestId,
145             String topic,
146             String code,
147             String message,
148             String finalInd) {
149
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();
157         return rpcResult;            
158     }
159
160     @Override
161     public Future<RpcResult<UploadartifactOutput>> uploadartifact(UploadartifactInput input) {
162
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);
167         }
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());
174         try{
175             
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());
180                 }
181                 else
182                 {
183                     designUtil.processTemplate(designUtil.createRequestData());
184                     configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());        
185                 }
186             }
187             else
188             {
189                 throw new Exception("No Tempalte data found");                
190             }
191             
192             
193         }
194         catch (Exception e) {
195
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: ");
200         }
201         
202         responseBuilder.setConfigDocumentResponse(configResponseBuilder.build());
203         RpcResult<UploadartifactOutput> rpcResult = RpcResultBuilder.<UploadartifactOutput> status(true).withResult(responseBuilder.build()).build();
204         return Futures.immediateFuture(rpcResult);
205
206     }
207 }