5bfdf96b8167e4d155d174120049fda7f75388a3
[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.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;
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 NotificationPublishService notificationService;
71     protected RpcProviderRegistry rpcRegistry;
72     private ListenerRegistration<DataChangeListener> dclServices;
73
74     protected BindingAwareBroker.RpcRegistration<ArtifactHandlerService> rpcRegistration;
75
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;
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         log.info("Initialization complete for " + appName);
100     }
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());
106
107         try {
108             CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
109             checkedFuture.get();
110             log.info("Create containers succeeded!");
111
112         } catch (InterruptedException | ExecutionException e) {
113             log.error("Create containers failed",  e);
114         }
115     }
116
117
118     @Override
119     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> arg0) {
120         // TODO Auto-generated method stub
121
122     }
123
124
125
126     @Override
127     public void close() throws Exception {
128
129         log.info("Closing provider for " + appName);
130         if(this.executor != null){
131             executor.shutdown();
132         }
133         if(this.rpcRegistration != null){
134             rpcRegistration.close();
135         }
136         log.info("Successfully closed provider for " + appName);
137
138     }
139
140     private RpcResult<UploadartifactOutput> buildResponse1(
141             String svcRequestId,
142             String topic,
143             String code,
144             String message,
145             String finalInd) {
146
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();
154         return rpcResult;            
155     }
156
157     @Override
158     public Future<RpcResult<UploadartifactOutput>> uploadartifact(UploadartifactInput input) {
159
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);
164         }
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());
171         try{
172             
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());
177                 }
178                 else
179                 {
180                     designUtil.processTemplate(designUtil.createRequestData());
181                     configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());        
182                 }
183             }
184             else
185             {
186                 throw new Exception("No Tempalte data found");                
187             }
188             
189             
190         }
191         catch (Exception e) {
192
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: ");
197         }
198         
199         responseBuilder.setConfigDocumentResponse(configResponseBuilder.build());
200         RpcResult<UploadartifactOutput> rpcResult = RpcResultBuilder.<UploadartifactOutput> status(true).withResult(responseBuilder.build()).build();
201         return Futures.immediateFuture(rpcResult);
202
203     }
204 }