b90d9c0e54e3413730f4c756598992cfd67cda45
[appc.git] / appc-dg / appc-dg-shared / appc-dg-mdsal-store / src / main / java / org / openecomp / appc / mdsal / impl / MDSALStoreImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.mdsal.impl;
26
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;
39
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;
48
49 /**
50  * Implementation of MDSALStore
51  */
52 public class MDSALStoreImpl implements MDSALStore{
53
54     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MDSALStoreImpl.class);
55
56     private static final Configuration configuration = ConfigurationFactory.getConfiguration();
57
58     MDSALStoreImpl(){
59         String configUrl = null;
60         String user =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);
67         }
68         ConfigOperation.setUrl(configUrl);
69         ConfigOperation.setAuthentication(user,password);
70     }
71
72
73     @Override
74     public boolean isModulePresent(String moduleName, Date revision) {
75
76         if(logger.isDebugEnabled()){
77             logger.debug("isModulePresent invoked with moduleName = " +moduleName + " , revision = " +revision);
78         }
79
80         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
81         /**
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).
88          */
89
90         Bundle bundle = bundleContext.getBundle(moduleName);
91         if(logger.isDebugEnabled()){
92             logger.debug("isModulePresent returned = " + (bundle != null));
93         }
94         return bundle != null;
95     }
96
97     @Override
98     public void storeYangModule(String yang, BundleInfo bundleInfo) throws MDSALStoreException {
99
100         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
101         byte[] byteArray = createBundleJar(yang, Constants.BLUEPRINT, bundleInfo);
102
103         try (ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray)){
104             Bundle bundle = bundleContext.installBundle(bundleInfo.getLocation(), inputStream);
105             bundle.start();
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);
109         }
110     }
111
112     @Override
113     public void storeJson( String module , String requestId ,String configJSON) throws MDSALStoreException {
114
115         try {
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);
119         }
120     }
121
122     private byte[] createBundleJar(String yang, String blueprint, BundleInfo bundleInfo) throws MDSALStoreException {
123
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);
132
133         byte[] retunValue;
134
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();
141
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);
151         }
152         return retunValue;
153     }
154 }