Merge of new rebased code
[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  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.appc.mdsal.impl;
23
24 import org.openecomp.appc.exceptions.APPCException;
25 import org.openecomp.appc.mdsal.MDSALStore;
26 import org.openecomp.appc.mdsal.exception.MDSALStoreException;
27 import org.openecomp.appc.mdsal.objects.BundleInfo;
28 import org.openecomp.appc.mdsal.operation.ConfigOperation;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31 import org.osgi.framework.Bundle;
32 import org.osgi.framework.BundleContext;
33 import org.osgi.framework.FrameworkUtil;
34
35 import java.io.ByteArrayInputStream;
36 import java.io.ByteArrayOutputStream;
37 import java.util.Date;
38 import java.util.jar.Attributes;
39 import java.util.jar.JarEntry;
40 import java.util.jar.JarOutputStream;
41 import java.util.jar.Manifest;
42
43 /**
44  * Implementation of MDSALStore
45  */
46 public class MDSALStoreImpl implements MDSALStore{
47
48     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MDSALStoreImpl.class);
49
50     MDSALStoreImpl(){
51         ConfigOperation.setUrl(Constants.CONFIG_URL);
52         ConfigOperation.setAuthentication(null,null);
53     }
54
55
56     @Override
57     public boolean isModulePresent(String moduleName, Date revision) {
58
59         if(logger.isDebugEnabled()){
60             logger.debug("isModulePresent invoked with moduleName = " +moduleName + " , revision = " +revision);
61         }
62
63         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
64         /**
65          * SchemaContext interface of ODL provides APIs for querying details of yang modules
66          * loaded into MD-SAL store, but its limitation is, it only returns information about
67          * static yang modules loaded on server start up, it does not return information about
68          * the yang modules loaded dynamically. Due to this limitation, we are checking the
69          * presence of OSGI bundle instead of yang module. (Note: Assuming OSGI bundle is named
70          * with the yang module name).
71          */
72
73         Bundle bundle = bundleContext.getBundle(moduleName);
74         if(logger.isDebugEnabled()){
75             logger.debug("isModulePresent returned = " + (bundle != null));
76         }
77         return bundle != null;
78     }
79
80     @Override
81     public void storeYangModule(String yang, BundleInfo bundleInfo) throws MDSALStoreException {
82
83         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
84         byte[] byteArray = createBundleJar(yang, Constants.BLUEPRINT, bundleInfo);
85
86         try (ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray)){
87             Bundle bundle = bundleContext.installBundle(bundleInfo.getLocation(), inputStream);
88             bundle.start();
89         } catch (Exception e) {
90             logger.error("Error storing yang module: " + yang + ". Error message: " + e.getMessage());
91             throw new MDSALStoreException("Error storing yang module: " + yang + " " + e.getMessage(), e);
92         }
93     }
94
95     @Override
96     public void storeJson( String module , String requestId ,String configJSON) throws MDSALStoreException {
97
98         try {
99             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);
100         } catch (APPCException e) {
101             throw new MDSALStoreException("Exception while storing config json to MDSAL store." +e.getMessage(), e);
102         }
103     }
104
105     private byte[] createBundleJar(String yang, String blueprint, BundleInfo bundleInfo) throws MDSALStoreException {
106
107         Manifest manifest = new Manifest();
108         manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, Constants.MANIFEST_VALUE_VERSION);
109         manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_NAME), bundleInfo.getName());
110         manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_SYMBOLIC_NAME), bundleInfo.getName());
111         manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_DESCRIPTION), bundleInfo.getDescription());
112         manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_MANIFEST_VERSION), Constants.MANIFEST_VALUE_BUNDLE_MAN_VERSION);
113         manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_VERSION), String.valueOf(bundleInfo.getVersion()));
114         manifest.getMainAttributes().put(new Attributes.Name(Constants.MANIFEST_ATTR_BUNDLE_BLUEPRINT), Constants.MANIFEST_VALUE_BUNDLE_BLUEPRINT);
115
116         byte[] retunValue;
117
118         try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
119              JarOutputStream jarOutputStream = new JarOutputStream(outputStream, manifest)) {
120             jarOutputStream.putNextEntry(new JarEntry("META-INF/yang/"));
121             jarOutputStream.putNextEntry(new JarEntry("META-INF/yang/"+bundleInfo.getName()+".yang"));
122             jarOutputStream.write(yang.getBytes());
123             jarOutputStream.closeEntry();
124
125             jarOutputStream.putNextEntry(new JarEntry("OSGI-INF/blueprint/"));
126             jarOutputStream.putNextEntry(new JarEntry(Constants.MANIFEST_VALUE_BUNDLE_BLUEPRINT));
127             jarOutputStream.write(blueprint.getBytes());
128             jarOutputStream.closeEntry();
129             jarOutputStream.close();
130             retunValue = outputStream.toByteArray();
131         } catch (Exception e) {
132             logger.error("Error creating bundle jar: " + bundleInfo.getName() + ". Error message: " + e.getMessage());
133             throw new MDSALStoreException("Error creating bundle jar: " + bundleInfo.getName() + " " + e.getMessage(), e);
134         }
135         return retunValue;
136     }
137 }