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