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