1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/*-
* ============LICENSE_START=======================================================
* ONAP : APPC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Copyright (C) 2017 Amdocs
* =============================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ECOMP is a trademark and service mark of AT&T Intellectual Property.
* ============LICENSE_END=========================================================
*/
package org.openecomp.appc.artifact.handler;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.json.JSONArray;
import org.json.JSONObject;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
import org.opendaylight.yang.gen.v1.org.openecomp.appc.artifacthandler.rev170321.ArtifactHandlerService;
import org.opendaylight.yang.gen.v1.org.openecomp.appc.artifacthandler.rev170321.UploadartifactInput;
import org.opendaylight.yang.gen.v1.org.openecomp.appc.artifacthandler.rev170321.UploadartifactInputBuilder;
import org.opendaylight.yang.gen.v1.org.openecomp.appc.artifacthandler.rev170321.UploadartifactOutput;
import org.opendaylight.yang.gen.v1.org.openecomp.appc.artifacthandler.rev170321.UploadartifactOutputBuilder;
import org.opendaylight.yang.gen.v1.org.openecomp.appc.artifacthandler.rev170321.uploadartifact.output.ConfigDocumentResponseBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.Services;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.ServicesBuilder;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import org.openecomp.appc.artifact.handler.node.ArtifactHandlerNode;
import org.openecomp.appc.artifact.handler.utils.ArtifactHandlerProviderUtil;
import org.openecomp.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
import org.openecomp.sdnc.sli.SvcLogicContext;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
public class ArtifactHandlerProvider implements AutoCloseable, ArtifactHandlerService, DataChangeListener {
private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerProvider.class);
private final String appName = "ArtifactsHandler";
private final ExecutorService executor;
protected DataBroker dataBroker;
protected NotificationProviderService notificationService;
protected RpcProviderRegistry rpcRegistry;
private ListenerRegistration<DataChangeListener> dclServices;
protected BindingAwareBroker.RpcRegistration<ArtifactHandlerService> rpcRegistration;
public ArtifactHandlerProvider(DataBroker dataBroker2,
NotificationProviderService notificationProviderService,
RpcProviderRegistry rpcProviderRegistry) {
this.log.info("Creating provider for " + appName);
executor = Executors.newFixedThreadPool(10);
dataBroker = dataBroker2;
notificationService = notificationProviderService;
rpcRegistry = rpcProviderRegistry;
initialize();
}
public void initialize() {
log.info("Initializing provider for " + appName);
// Create the top level containers
createContainers();
try {
ArtifactHandlerProviderUtil.loadProperties();
} catch (Exception e) {
log.error("Caught Exception while trying to load properties file");
}
// Listener for changes to Services tree
rpcRegistration = rpcRegistry.addRpcImplementation(
ArtifactHandlerService.class, this);
log.info("Initialization complete for " + appName);
}
private void createContainers() {
final WriteTransaction t = dataBroker.newReadWriteTransaction();
// Create the Services container
t.merge(LogicalDatastoreType.CONFIGURATION,InstanceIdentifier.create(Services.class),new ServicesBuilder().build());
t.merge(LogicalDatastoreType.OPERATIONAL,InstanceIdentifier.create(Services.class),new ServicesBuilder().build());
try {
CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
checkedFuture.get();
log.info("Create Containers succeeded!: ");
} catch (InterruptedException | ExecutionException e) {
log.error("Create Containers Failed: " + e);
e.printStackTrace();
}
}
@Override
public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> arg0) {
// TODO Auto-generated method stub
}
@Override
public void close() throws Exception {
log.info("Closing provider for " + appName);
if(this.executor != null){
executor.shutdown();
}
if(this.rpcRegistration != null){
rpcRegistration.close();
}
log.info("Successfully closed provider for " + appName);
}
private RpcResult<UploadartifactOutput> buildResponse1(
String svcRequestId,
String topic,
String code,
String message,
String finalInd) {
UploadartifactOutputBuilder responseBuilder = new UploadartifactOutputBuilder();
ConfigDocumentResponseBuilder configResponseBuilder=new ConfigDocumentResponseBuilder();
configResponseBuilder.setRequestId(svcRequestId);
configResponseBuilder.setStatus(code);
configResponseBuilder.setErrorReason(message);
RpcResult<UploadartifactOutput> rpcResult = RpcResultBuilder.<UploadartifactOutput> status(true)
.withResult(responseBuilder.build()).build();
return rpcResult;
}
@Override
public Future<RpcResult<UploadartifactOutput>> uploadartifact(UploadartifactInput input) {
if (input == null || input.getDocumentParameters() == null || input.getDocumentParameters().getArtifactContents() == null ) {
RpcResult<UploadartifactOutput> rpcResult =
buildResponse1("N/A", "N/A", "INVALID_INPUT", "Invalid input, null or empty document information" , "Y");
return Futures.immediateFuture(rpcResult);
}
UploadartifactInputBuilder inputBuilder = new UploadartifactInputBuilder(input);
ConfigDocumentResponseBuilder configResponseBuilder = new ConfigDocumentResponseBuilder();
UploadartifactOutputBuilder responseBuilder = new UploadartifactOutputBuilder();
log.info("Received input = " + input );
ArtifactHandlerProviderUtil designUtil = new ArtifactHandlerProviderUtil(input);
configResponseBuilder.setRequestId(input.getRequestInformation().getRequestId());
try{
if(input.getRequestInformation().getSource() !=null){
if(input.getRequestInformation().getSource().equalsIgnoreCase(SdcArtifactHandlerConstants.DESIGN_TOOL)){
designUtil.processTemplate(designUtil.createDummyRequestData());
configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());
}
else
{
designUtil.processTemplate(designUtil.createRequestData());
configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_OK.toString());
}
}
else
{
throw new Exception("No Tempalte data found");
}
}
catch (Exception e) {
configResponseBuilder.setErrorReason(e.getMessage());
configResponseBuilder.setStatus(ArtifactHandlerProviderUtil.DistributionStatusEnum.DEPLOY_ERROR.toString());
log.error("Caught exception looking for Artifact Handler", e);
log.info("Caught exception looking for Artifact Handler: ");
}
responseBuilder.setConfigDocumentResponse(configResponseBuilder.build());
RpcResult<UploadartifactOutput> rpcResult = RpcResultBuilder.<UploadartifactOutput> status(true).withResult(responseBuilder.build()).build();
return Futures.immediateFuture(rpcResult);
}
}
|