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.mdsal.impl;
27 import org.openecomp.appc.exceptions.APPCException;
28 import org.openecomp.appc.mdsal.MDSALStore;
29 import org.openecomp.appc.mdsal.exception.MDSALStoreException;
30 import org.openecomp.appc.mdsal.objects.BundleInfo;
31 import org.openecomp.appc.mdsal.operation.ConfigOperation;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
34 import org.osgi.framework.Bundle;
35 import org.osgi.framework.BundleContext;
36 import org.osgi.framework.FrameworkUtil;
38 import java.io.ByteArrayInputStream;
39 import java.io.ByteArrayOutputStream;
40 import java.util.Date;
41 import java.util.jar.Attributes;
42 import java.util.jar.JarEntry;
43 import java.util.jar.JarOutputStream;
44 import java.util.jar.Manifest;
47 * Implementation of MDSALStore
49 public class MDSALStoreImpl implements MDSALStore{
51 private static final EELFLogger logger = EELFManager.getInstance().getLogger(MDSALStoreImpl.class);
54 ConfigOperation.setUrl(Constants.CONFIG_URL);
55 ConfigOperation.setAuthentication(null,null);
60 public boolean isModulePresent(String moduleName, Date revision) {
62 if(logger.isDebugEnabled()){
63 logger.debug("isModulePresent invoked with moduleName = " +moduleName + " , revision = " +revision);
66 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
68 * SchemaContext interface of ODL provides APIs for querying details of yang modules
69 * loaded into MD-SAL store, but its limitation is, it only returns information about
70 * static yang modules loaded on server start up, it does not return information about
71 * the yang modules loaded dynamically. Due to this limitation, we are checking the
72 * presence of OSGI bundle instead of yang module. (Note: Assuming OSGI bundle is named
73 * with the yang module name).
76 Bundle bundle = bundleContext.getBundle(moduleName);
77 if(logger.isDebugEnabled()){
78 logger.debug("isModulePresent returned = " + (bundle != null));
80 return bundle != null;
84 public void storeYangModule(String yang, BundleInfo bundleInfo) throws MDSALStoreException {
86 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
87 byte[] byteArray = createBundleJar(yang, Constants.BLUEPRINT, bundleInfo);
89 try (ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray)){
90 Bundle bundle = bundleContext.installBundle(bundleInfo.getLocation(), inputStream);
92 } catch (Exception e) {
93 logger.error("Error storing yang module: " + yang + ". Error message: " + e.getMessage());
94 throw new MDSALStoreException("Error storing yang module: " + yang + " " + e.getMessage(), e);
99 public void storeJson( String module , String requestId ,String configJSON) throws MDSALStoreException {
102 ConfigOperation.storeConfig(configJSON , module , org.openecomp.appc.Constants.YANG_BASE_CONTAINER, org.openecomp.appc.Constants.YANG_VNF_CONFIG_LIST,requestId,org.openecomp.appc.Constants.YANG_VNF_CONFIG);
103 } catch (APPCException e) {
104 throw new MDSALStoreException("Exception while storing config json to MDSAL store." +e.getMessage(), e);
108 private byte[] createBundleJar(String yang, String blueprint, BundleInfo bundleInfo) throws MDSALStoreException {
110 Manifest manifest = new Manifest();
111 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, Constants.MANIFEST_VALUE_VERSION);
112 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_NAME), bundleInfo.getName());
113 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_SYMBOLIC_NAME), bundleInfo.getName());
114 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_DESCRIPTION), bundleInfo.getDescription());
115 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_MANIFEST_VERSION), Constants.MANIFEST_VALUE_BUNDLE_MAN_VERSION);
116 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_VERSION), String.valueOf(bundleInfo.getVersion()));
117 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_BLUEPRINT), Constants.MANIFEST_VALUE_BUNDLE_BLUEPRINT);
121 try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
122 JarOutputStream jarOutputStream = new JarOutputStream(outputStream, manifest)) {
123 jarOutputStream.putNextEntry(new JarEntry("META-INF/yang/"));
124 jarOutputStream.putNextEntry(new JarEntry("META-INF/yang/"+bundleInfo.getName()+".yang"));
125 jarOutputStream.write(yang.getBytes());
126 jarOutputStream.closeEntry();
128 jarOutputStream.putNextEntry(new JarEntry("OSGI-INF/blueprint/"));
129 jarOutputStream.putNextEntry(new JarEntry(Constants.MANIFEST_VALUE_BUNDLE_BLUEPRINT));
130 jarOutputStream.write(blueprint.getBytes());
131 jarOutputStream.closeEntry();
132 jarOutputStream.close();
133 retunValue = outputStream.toByteArray();
134 } catch (Exception e) {
135 logger.error("Error creating bundle jar: " + bundleInfo.getName() + ". Error message: " + e.getMessage());
136 throw new MDSALStoreException("Error creating bundle jar: " + bundleInfo.getName() + " " + e.getMessage(), e);