9ca1f44a4d62d53381c99a04199b47f0b348708b
[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  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.artifact.handler;
27
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.opendaylight.controller.md.sal.binding.api.DataBroker;
34 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
35 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
36 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
37 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
38 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
39 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
40 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
41 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
42 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
43 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.ArtifactHandlerService;
44 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactInput;
45 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactInputBuilder;
46 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactOutput;
47 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.UploadartifactOutputBuilder;
48 import org.opendaylight.yang.gen.v1.org.onap.appc.artifacthandler.rev170321.uploadartifact.output.ConfigDocumentResponseBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.Services;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.ServicesBuilder;
51 import org.opendaylight.yangtools.concepts.ListenerRegistration;
52 import org.opendaylight.yangtools.yang.binding.DataObject;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.opendaylight.yangtools.yang.common.RpcResult;
55 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
56 import org.onap.appc.artifact.handler.utils.ArtifactHandlerProviderUtil;
57 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
58
59 import com.att.eelf.configuration.EELFLogger;
60 import com.att.eelf.configuration.EELFManager;
61 import com.google.common.util.concurrent.CheckedFuture;
62 import com.google.common.util.concurrent.Futures;
63
64
65
66 public class ArtifactHandlerProvider implements AutoCloseable, ArtifactHandlerService, DataChangeListener {
67
68     private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerProvider.class);
69     private final String appName = "ArtifactsHandler";
70     private final ExecutorService executor;
71     protected DataBroker dataBroker;
72     protected NotificationPublishService notificationService;
73     protected RpcProviderRegistry rpcRegistry;
74     private ListenerRegistration<DataChangeListener> dclServices;
75
76     protected BindingAwareBroker.RpcRegistration<ArtifactHandlerService> rpcRegistration;
77
78     public ArtifactHandlerProvider(DataBroker dataBroker2,
79             NotificationPublishService notificationProviderService,
80             RpcProviderRegistry rpcProviderRegistry) {
81         this.log.info("Creating provider for " + appName);
82         executor = Executors.newFixedThreadPool(10);
83         dataBroker = dataBroker2;
84         notificationService = notificationProviderService;
85         rpcRegistry = rpcProviderRegistry;
86         initialize();
87
88     }
89
90     public void initialize() {
91         log.info("Initializing provider for " + appName);
92         // Create the top level containers
93         createContainers();
94         try {
95             ArtifactHandlerProviderUtil.loadProperties();
96         } catch (Exception e) {
97             log.error("Caught exception while trying to load properties file", e);
98         }
99         // Listener for changes to Services tree
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 = getArtifactHandlerProviderUtil(input);
172         configResponseBuilder.setRequestId(input.getRequestInformation().getRequestId());
173         try{
174             if(input.getRequestInformation().getSource() !=null){
175                 if(input.getRequestInformation().getSource().equalsIgnoreCase(SdcArtifactHandlerConstants.DESIGN_TOOL)){
176                         designUtil.processTemplate(designUtil.createDummyRequestData());
177                         configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());
178                 }
179                 else
180                 {
181                     designUtil.processTemplate(designUtil.createRequestData());
182                     configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());
183                 }
184             }
185             else
186             {
187                 throw new Exception("No Template data found");
188             }
189         }
190         catch (Exception e) {
191
192             configResponseBuilder.setErrorReason(e.getMessage());
193             configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_ERROR.toString());
194             log.error("Caught exception looking for Artifact Handler", e);
195             log.info("Caught exception looking for Artifact Handler: ");
196         }
197
198         responseBuilder.setConfigDocumentResponse(configResponseBuilder.build());
199         RpcResult<UploadartifactOutput> rpcResult = RpcResultBuilder.<UploadartifactOutput> status(true).withResult(responseBuilder.build()).build();
200         return Futures.immediateFuture(rpcResult);
201
202     }
203
204     protected ArtifactHandlerProviderUtil getArtifactHandlerProviderUtil(UploadartifactInput input) {
205         return new ArtifactHandlerProviderUtil(input);
206     }
207 }