2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.northbound.asdcapi;
24 import java.util.Properties;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
31 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
32 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
33 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
34 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
35 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
36 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
37 import org.opendaylight.yang.gen.v1.http.xmlns.onap.org.asdc.license.model._1._0.rev160427.vf.license.model.grouping.VfLicenseModel;
38 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev170201.ASDCAPIService;
39 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev170201.Artifacts;
40 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev170201.ArtifactsBuilder;
41 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev170201.VfLicenseModelUpdateInput;
42 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev170201.VfLicenseModelUpdateInputBuilder;
43 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev170201.VfLicenseModelUpdateOutput;
44 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev170201.VfLicenseModelUpdateOutputBuilder;
45 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev170201.VfLicenseModelVersions;
46 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev170201.VfLicenseModelVersionsBuilder;
47 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev170201.artifacts.Artifact;
48 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev170201.artifacts.ArtifactBuilder;
49 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev170201.artifacts.ArtifactKey;
50 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev170201.vf.license.model.versions.VfLicenseModelVersion;
51 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev170201.vf.license.model.versions.VfLicenseModelVersionBuilder;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.opendaylight.yangtools.yang.common.RpcResult;
54 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 import com.google.common.base.Optional;
59 import com.google.common.util.concurrent.CheckedFuture;
60 import com.google.common.util.concurrent.Futures;
61 import com.google.common.util.concurrent.ListenableFuture;
64 * Defines a base implementation for your provider. This class extends from a helper class
65 * which provides storage for the most commonly used components of the MD-SAL. Additionally the
66 * base class provides some basic logging and initialization / clean up methods.
68 * To use this, copy and paste (overwrite) the following method into the TestApplicationProviderModule
69 * class which is auto generated under src/main/java in this project
70 * (created only once during first compilation):
75 public java.lang.AutoCloseable createInstance() {
77 final asdcApiProvider provider = new asdcApiProvider();
78 provider.setDataBroker( getDataBrokerDependency() );
79 provider.setNotificationService( getNotificationServiceDependency() );
80 provider.setRpcRegistry( getRpcRegistryDependency() );
81 provider.initialize();
82 return new AutoCloseable() {
85 public void close() throws Exception {
86 //TODO: CLOSE ANY REGISTRATION OBJECTS CREATED USING ABOVE BROKER/NOTIFICATION
96 public class AsdcApiProvider implements AutoCloseable, ASDCAPIService {
98 private static final Logger LOG = LoggerFactory.getLogger(AsdcApiProvider.class);
100 private static final String ACTIVE_VERSION = "active";
102 private static final String APPLICATION_NAME = "asdcApi";
104 private final ExecutorService executor;
105 protected DataBroker dataBroker;
106 protected NotificationPublishService notificationService;
107 protected RpcProviderRegistry rpcRegistry;
108 private final AsdcApiSliClient asdcApiSliClient;
110 protected BindingAwareBroker.RpcRegistration<ASDCAPIService> rpcRegistration;
112 public AsdcApiProvider(final DataBroker dataBroker,
113 final NotificationPublishService notificationPublishService,
114 final RpcProviderRegistry rpcProviderRegistry,
115 final AsdcApiSliClient asdcApiSliClient) {
117 LOG.info("Creating provider for {}", APPLICATION_NAME);
118 executor = Executors.newFixedThreadPool(1);
119 this.dataBroker = dataBroker;
120 notificationService = notificationPublishService;
121 rpcRegistry = rpcProviderRegistry;
122 this.asdcApiSliClient= asdcApiSliClient;
126 public void initialize(){
127 LOG.info("Initializing {} for {}", this.getClass().getName(), APPLICATION_NAME);
131 if (rpcRegistration == null) {
132 if (rpcRegistry != null) {
133 rpcRegistration = rpcRegistry.addRpcImplementation(
134 ASDCAPIService.class, this);
135 LOG.info("Initialization complete for {}", APPLICATION_NAME);
137 LOG.warn("Error initializing {} : rpcRegistry unset", APPLICATION_NAME);
142 private void createContainers() {
144 if (dataBroker != null) {
145 final WriteTransaction t = dataBroker.newReadWriteTransaction();
147 // Create the vf-model-license-versions and artifacts containers
148 t.merge(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(VfLicenseModelVersions.class),
149 new VfLicenseModelVersionsBuilder().build());
151 t.merge(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Artifacts.class), new ArtifactsBuilder().build());
155 CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = t.submit();
157 LOG.info("Create Containers succeeded!: ");
159 } catch (InterruptedException | ExecutionException e) {
160 LOG.error("Create Containers Failed: ", e);
163 LOG.warn("createContainers : cannot find dataBroker to create containers");
166 protected void initializeChild() {
167 //Override if you have custom initialization intelligence
171 public void close() throws Exception {
172 LOG.info( "Closing provider for " + APPLICATION_NAME);
174 rpcRegistration.close();
175 LOG.info( "Successfully closed provider for " + APPLICATION_NAME);
178 protected boolean artifactVersionExists(String aName, String aVersion) {
179 InstanceIdentifier artifactInstanceId =
180 InstanceIdentifier.<Artifacts>builder(Artifacts.class)
181 .child(Artifact.class, new ArtifactKey(aName, aVersion)).build();
182 Optional<Artifact> data = null;
183 try(ReadOnlyTransaction readTx = dataBroker.newReadOnlyTransaction()) {
184 data = (Optional<Artifact>) readTx.read(LogicalDatastoreType.CONFIGURATION, artifactInstanceId).get();
185 } catch (InterruptedException | ExecutionException e) {
186 LOG.error("Caught Exception reading MD-SAL for ["+aName+","+ aVersion+"] " ,e);
190 return data.isPresent();
193 protected void addArtifactVersion(String aName, String aVersion) {
197 ArtifactBuilder aBuilder = new ArtifactBuilder();
199 aBuilder.setArtifactName(aName);
200 aBuilder.setArtifactVersion(aVersion);
202 Artifact artifact = aBuilder.build();
204 InstanceIdentifier.InstanceIdentifierBuilder<Artifact> aIdBuilder = InstanceIdentifier
205 .<Artifacts> builder(Artifacts.class)
206 .child(Artifact.class, artifact.key());
208 InstanceIdentifier<Artifact> path = aIdBuilder.build();
210 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
212 tx.merge(LogicalDatastoreType.CONFIGURATION, path,
214 tx.submit().checkedGet();
215 } catch (Exception e) {
216 LOG.error("Caught exception trying to add artifact entry", e);
222 private void applyVfLicenseModelUpdate(VfLicenseModelUpdateInput input) {
224 String aName = input.getArtifactName();
225 String aVersion = input.getArtifactVersion();
226 VfLicenseModel vfLicenseModel = input.getVfLicenseModel();
229 // Add new version (version = artifact-version)
232 VfLicenseModelVersionBuilder vBuilder = new VfLicenseModelVersionBuilder();
233 vBuilder.setArtifactName(aName);
234 vBuilder.setArtifactVersion(aVersion);
235 vBuilder.setVfLicenseModel(vfLicenseModel);
237 VfLicenseModelVersion version = vBuilder.build();
239 InstanceIdentifier.InstanceIdentifierBuilder<VfLicenseModelVersion> versionIdBuilder = InstanceIdentifier
240 .<VfLicenseModelVersions> builder(VfLicenseModelVersions.class)
241 .child(VfLicenseModelVersion.class, version.key());
243 InstanceIdentifier<VfLicenseModelVersion> path = versionIdBuilder.build();
245 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
246 tx.merge(LogicalDatastoreType.CONFIGURATION, path,
248 tx.submit().checkedGet();
249 } catch (Exception e) {
251 "Caught exception trying to save entry to MD-SAL",
256 // Add "active" version (version = "active")
259 VfLicenseModelVersionBuilder vBuilder = new VfLicenseModelVersionBuilder();
260 vBuilder.setArtifactName(aName);
261 vBuilder.setArtifactVersion(ACTIVE_VERSION);
262 vBuilder.setVfLicenseModel(vfLicenseModel);
264 VfLicenseModelVersion version = vBuilder.build();
265 InstanceIdentifier.InstanceIdentifierBuilder<VfLicenseModelVersion> versionIdBuilder = InstanceIdentifier
266 .<VfLicenseModelVersions> builder(VfLicenseModelVersions.class)
267 .child(VfLicenseModelVersion.class, version.key());
269 InstanceIdentifier<VfLicenseModelVersion> path = versionIdBuilder.build();
271 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
273 tx.merge(LogicalDatastoreType.CONFIGURATION, path,
275 tx.submit().checkedGet();
276 } catch (Exception e) {
278 "Caught exception trying to save entry to MD-SAL",
285 public ListenableFuture<RpcResult<VfLicenseModelUpdateOutput>> vfLicenseModelUpdate(VfLicenseModelUpdateInput input) {
286 final String svcOperation = "vf-license-model-update";
288 Properties parms = new Properties();
290 LOG.info( svcOperation +" called." );
293 LOG.debug("exiting " +svcOperation+ " because of invalid input");
297 VfLicenseModelUpdateInputBuilder inputBuilder = new VfLicenseModelUpdateInputBuilder(input);
299 VfLicenseModelUpdateInput inputVfLic = inputBuilder.build();
301 String errorMessage = "Success";
302 String errorCode = "200";
304 // If this artifact already exists, reject this update
305 if (artifactVersionExists(inputVfLic.getArtifactName(), inputVfLic.getArtifactVersion())) {
307 errorMessage = "Artifact version already exists";
309 // Translate input object into SLI-consumable properties
310 LOG.info("Adding INPUT data for "+svcOperation+" input: " + inputVfLic);
311 AsdcApiUtil.toProperties(parms, inputVfLic);
314 // Call directed graph
315 Properties respProps = null;
318 if (asdcApiSliClient.hasGraph("ASDC-API", svcOperation , null, "sync"))
323 respProps = asdcApiSliClient.execute("ASDC-API", svcOperation, null, "sync", parms);
327 LOG.error("Caught exception executing service logic for "+ svcOperation, e);
330 errorMessage = "No service logic active for ASDC-API: '" + svcOperation + "'";
337 errorMessage = e.getMessage();
338 LOG.error("Caught exception looking for service logic", e);
342 if (respProps != null)
344 errorCode = respProps.getProperty("error-code");
345 errorMessage = respProps.getProperty("error-message", "");
350 if ("200".equals(errorCode)) {
351 LOG.info("ASDC update succeeded");
353 // Update config tree
354 applyVfLicenseModelUpdate(inputVfLic);
355 addArtifactVersion(inputVfLic.getArtifactName(), inputVfLic.getArtifactVersion());
358 LOG.info("ASDC update failed ("+errorCode+" : "+errorMessage);
362 VfLicenseModelUpdateOutputBuilder respBuilder = new VfLicenseModelUpdateOutputBuilder();
363 respBuilder.setAsdcApiResponseCode(errorCode);
364 if (errorMessage != null && errorMessage.length() > 0) {
365 respBuilder.setAsdcApiResponseText(errorMessage);
368 RpcResult<VfLicenseModelUpdateOutput> rpcResult;
371 rpcResult = RpcResultBuilder.<VfLicenseModelUpdateOutput> status(true).withResult(respBuilder.build()).build();
375 return Futures.immediateFuture(rpcResult);