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.configuration.Configuration;
28 import org.openecomp.appc.configuration.ConfigurationFactory;
29 import org.openecomp.appc.exceptions.APPCException;
30 import org.openecomp.appc.mdsal.MDSALStore;
31 import org.openecomp.appc.mdsal.exception.MDSALStoreException;
32 import org.openecomp.appc.mdsal.objects.BundleInfo;
33 import org.openecomp.appc.mdsal.operation.ConfigOperation;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36 import org.osgi.framework.Bundle;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.FrameworkUtil;
40 import java.io.ByteArrayInputStream;
41 import java.io.ByteArrayOutputStream;
42 import java.util.Date;
43 import java.util.Properties;
44 import java.util.jar.Attributes;
45 import java.util.jar.JarEntry;
46 import java.util.jar.JarOutputStream;
47 import java.util.jar.Manifest;
50 * Implementation of MDSALStore
52 public class MDSALStoreImpl implements MDSALStore{
54 private static final EELFLogger logger = EELFManager.getInstance().getLogger(MDSALStoreImpl.class);
56 private static final Configuration configuration = ConfigurationFactory.getConfiguration();
59 String configUrl = null;
61 String password = null;
62 Properties properties = configuration.getProperties();
63 if (properties != null) {
64 configUrl= properties.getProperty( Constants.CONFIG_URL_PROPERTY , Constants.CONFIG_URL_DEFAULT);
65 user = properties.getProperty(Constants.CONFIG_USER_PROPERTY);
66 password = properties.getProperty(Constants.CONFIG_PASS_PROPERTY);
68 ConfigOperation.setUrl(configUrl);
69 ConfigOperation.setAuthentication(user,password);
74 public boolean isModulePresent(String moduleName, Date revision) {
76 if(logger.isDebugEnabled()){
77 logger.debug("isModulePresent invoked with moduleName = " +moduleName + " , revision = " +revision);
80 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
82 * SchemaContext interface of ODL provides APIs for querying details of yang modules
83 * loaded into MD-SAL store, but its limitation is, it only returns information about
84 * static yang modules loaded on server start up, it does not return information about
85 * the yang modules loaded dynamically. Due to this limitation, we are checking the
86 * presence of OSGI bundle instead of yang module. (Note: Assuming OSGI bundle is named
87 * with the yang module name).
90 Bundle bundle = bundleContext.getBundle(moduleName);
91 if(logger.isDebugEnabled()){
92 logger.debug("isModulePresent returned = " + (bundle != null));
94 return bundle != null;
98 public void storeYangModule(String yang, BundleInfo bundleInfo) throws MDSALStoreException {
100 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
101 byte[] byteArray = createBundleJar(yang, Constants.BLUEPRINT, bundleInfo);
103 try (ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray)){
104 Bundle bundle = bundleContext.installBundle(bundleInfo.getLocation(), inputStream);
106 } catch (Exception e) {
107 logger.error("Error storing yang module: " + yang + ". Error message: " + e.getMessage());
108 throw new MDSALStoreException("Error storing yang module: " + yang + " " + e.getMessage(), e);
113 public void storeJson( String module , String requestId ,String configJSON) throws MDSALStoreException {
116 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);
117 } catch (APPCException e) {
118 throw new MDSALStoreException("Exception while storing config json to MDSAL store." +e.getMessage(), e);
122 private byte[] createBundleJar(String yang, String blueprint, BundleInfo bundleInfo) throws MDSALStoreException {
124 Manifest manifest = new Manifest();
125 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, Constants.MANIFEST_VALUE_VERSION);
126 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_NAME), bundleInfo.getName());
127 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_SYMBOLIC_NAME), bundleInfo.getName());
128 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_DESCRIPTION), bundleInfo.getDescription());
129 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_MANIFEST_VERSION), Constants.MANIFEST_VALUE_BUNDLE_MAN_VERSION);
130 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_VERSION), String.valueOf(bundleInfo.getVersion()));
131 manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_BLUEPRINT), Constants.MANIFEST_VALUE_BUNDLE_BLUEPRINT);
135 try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
136 JarOutputStream jarOutputStream = new JarOutputStream(outputStream, manifest)) {
137 jarOutputStream.putNextEntry(new JarEntry("META-INF/yang/"));
138 jarOutputStream.putNextEntry(new JarEntry("META-INF/yang/"+bundleInfo.getName()+".yang"));
139 jarOutputStream.write(yang.getBytes());
140 jarOutputStream.closeEntry();
142 jarOutputStream.putNextEntry(new JarEntry("OSGI-INF/blueprint/"));
143 jarOutputStream.putNextEntry(new JarEntry(Constants.MANIFEST_VALUE_BUNDLE_BLUEPRINT));
144 jarOutputStream.write(blueprint.getBytes());
145 jarOutputStream.closeEntry();
146 jarOutputStream.close();
147 retunValue = outputStream.toByteArray();
148 } catch (Exception e) {
149 logger.error("Error creating bundle jar: " + bundleInfo.getName() + ". Error message: " + e.getMessage());
150 throw new MDSALStoreException("Error creating bundle jar: " + bundleInfo.getName() + " " + e.getMessage(), e);