2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.artifact.handler;
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;
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;
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;
69 public class ArtifactHandlerProvider implements AutoCloseable, ArtifactHandlerService, DataChangeListener {
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;
79 protected BindingAwareBroker.RpcRegistration<ArtifactHandlerService> rpcRegistration;
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;
93 public void initialize() {
94 log.info("Initializing provider for " + appName);
95 // Create the top level containers
98 ArtifactHandlerProviderUtil.loadProperties();
99 } catch (Exception e) {
100 log.error("Caught Exception while trying to load properties file");
102 // Listener for changes to Services tree
104 rpcRegistration = rpcRegistry.addRpcImplementation(
105 ArtifactHandlerService.class, this);
107 log.info("Initialization complete for " + appName);
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());
116 CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
118 log.info("Create Containers succeeded!: ");
120 } catch (InterruptedException | ExecutionException e) {
121 log.error("Create Containers Failed: " + e);
128 public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> arg0) {
129 // TODO Auto-generated method stub
136 public void close() throws Exception {
138 log.info("Closing provider for " + appName);
139 if(this.executor != null){
142 if(this.rpcRegistration != null){
143 rpcRegistration.close();
145 log.info("Successfully closed provider for " + appName);
149 private RpcResult<UploadartifactOutput> buildResponse1(
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();
167 public Future<RpcResult<UploadartifactOutput>> uploadartifact(UploadartifactInput input) {
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);
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());
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());
189 designUtil.processTemplate(designUtil.createRequestData());
190 configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());
195 throw new Exception("No Tempalte data found");
200 catch (Exception e) {
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: ");
208 responseBuilder.setConfigDocumentResponse(configResponseBuilder.build());
209 RpcResult<UploadartifactOutput> rpcResult = RpcResultBuilder.<UploadartifactOutput> status(true).withResult(responseBuilder.build()).build();
210 return Futures.immediateFuture(rpcResult);