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