Applying license changes to all files
[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.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;
37
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;
45
46 /**
47  * Implementation of MDSALStore
48  */
49 public class MDSALStoreImpl implements MDSALStore{
50
51     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MDSALStoreImpl.class);
52
53     MDSALStoreImpl(){
54         ConfigOperation.setUrl(Constants.CONFIG_URL);
55         ConfigOperation.setAuthentication(null,null);
56     }
57
58
59     @Override
60     public boolean isModulePresent(String moduleName, Date revision) {
61
62         if(logger.isDebugEnabled()){
63             logger.debug("isModulePresent invoked with moduleName = " +moduleName + " , revision = " +revision);
64         }
65
66         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
67         /**
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).
74          */
75
76         Bundle bundle = bundleContext.getBundle(moduleName);
77         if(logger.isDebugEnabled()){
78             logger.debug("isModulePresent returned = " + (bundle != null));
79         }
80         return bundle != null;
81     }
82
83     @Override
84     public void storeYangModule(String yang, BundleInfo bundleInfo) throws MDSALStoreException {
85
86         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
87         byte[] byteArray = createBundleJar(yang, Constants.BLUEPRINT, bundleInfo);
88
89         try (ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray)){
90             Bundle bundle = bundleContext.installBundle(bundleInfo.getLocation(), inputStream);
91             bundle.start();
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);
95         }
96     }
97
98     @Override
99     public void storeJson( String module , String requestId ,String configJSON) throws MDSALStoreException {
100
101         try {
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);
105         }
106     }
107
108     private byte[] createBundleJar(String yang, String blueprint, BundleInfo bundleInfo) throws MDSALStoreException {
109
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);
118
119         byte[] retunValue;
120
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();
127
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);
137         }
138         return retunValue;
139     }
140 }