51f286ee7da7f8e622993726c00f6edae86b11ac
[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");
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             e.printStackTrace();
118         }
119     }
120
121
122     @Override
123     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> arg0) {
124         // TODO Auto-generated method stub
125
126     }
127
128
129
130     @Override
131     public void close() throws Exception {
132
133         log.info("Closing provider for " + appName);
134         if(this.executor != null){
135             executor.shutdown();
136         }
137         if(this.rpcRegistration != null){
138             rpcRegistration.close();
139         }
140         log.info("Successfully closed provider for " + appName);
141
142     }
143
144     private RpcResult<UploadartifactOutput> buildResponse1(
145             String svcRequestId,
146             String topic,
147             String code,
148             String message,
149             String finalInd) {
150
151         UploadartifactOutputBuilder responseBuilder = new UploadartifactOutputBuilder();
152         ConfigDocumentResponseBuilder configResponseBuilder=new ConfigDocumentResponseBuilder();            
153         configResponseBuilder.setRequestId(svcRequestId);            
154         configResponseBuilder.setStatus(code);            
155         configResponseBuilder.setErrorReason(message);            
156         RpcResult<UploadartifactOutput> rpcResult = RpcResultBuilder.<UploadartifactOutput> status(true)
157                 .withResult(responseBuilder.build()).build();
158         return rpcResult;            
159     }
160
161     @Override
162     public Future<RpcResult<UploadartifactOutput>> uploadartifact(UploadartifactInput input) {
163
164         if (input == null || input.getDocumentParameters() == null || input.getDocumentParameters().getArtifactContents() == null ) {        
165             RpcResult<UploadartifactOutput> rpcResult =
166                     buildResponse1("N/A", "N/A", "INVALID_INPUT", "Invalid input, null or empty document information" , "Y");
167             return Futures.immediateFuture(rpcResult);
168         }
169         UploadartifactInputBuilder inputBuilder = new UploadartifactInputBuilder(input);
170         ConfigDocumentResponseBuilder configResponseBuilder = new ConfigDocumentResponseBuilder();
171         UploadartifactOutputBuilder responseBuilder = new UploadartifactOutputBuilder();
172         log.info("Received input = " + input );
173         ArtifactHandlerProviderUtil designUtil = new ArtifactHandlerProviderUtil(input);
174         configResponseBuilder.setRequestId(input.getRequestInformation().getRequestId());
175         try{
176             
177             if(input.getRequestInformation().getSource() !=null){
178                 if(input.getRequestInformation().getSource().equalsIgnoreCase(SdcArtifactHandlerConstants.DESIGN_TOOL)){
179                         designUtil.processTemplate(designUtil.createDummyRequestData());
180                         configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());
181                 }
182                 else
183                 {
184                     designUtil.processTemplate(designUtil.createRequestData());
185                     configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());        
186                 }
187             }
188             else
189             {
190                 throw new Exception("No Tempalte data found");                
191             }
192             
193             
194         }
195         catch (Exception e) {
196
197             configResponseBuilder.setErrorReason(e.getMessage());            
198             configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_ERROR.toString());
199             log.error("Caught exception looking for Artifact Handler", e);
200             log.info("Caught exception looking for Artifact Handler: ");
201         }
202         
203         responseBuilder.setConfigDocumentResponse(configResponseBuilder.build());
204         RpcResult<UploadartifactOutput> rpcResult = RpcResultBuilder.<UploadartifactOutput> status(true).withResult(responseBuilder.build()).build();
205         return Futures.immediateFuture(rpcResult);
206
207     }
208 }