Merge of new rebased code
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / main / java / org / openecomp / appc / dg / common / impl / VNFConfiguratorImpl.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.dg.common.impl;
23
24 import org.openecomp.appc.Constants;
25 import org.openecomp.appc.dg.common.VNFConfigurator;
26 import org.openecomp.appc.exceptions.APPCException;
27 import org.openecomp.appc.mdsal.MDSALStore;
28 import org.openecomp.appc.mdsal.exception.MDSALStoreException;
29 import org.openecomp.appc.mdsal.impl.MDSALStoreFactory;
30 import org.openecomp.appc.mdsal.objects.BundleInfo;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33 import org.openecomp.sdnc.sli.SvcLogicContext;
34 import org.apache.commons.lang.StringUtils;
35
36 import java.text.ParseException;
37 import java.text.SimpleDateFormat;
38 import java.util.Date;
39 import java.util.Map;
40
41
42 public class VNFConfiguratorImpl implements VNFConfigurator {
43
44     private static final EELFLogger logger = EELFManager.getInstance().getLogger(VNFConfiguratorImpl.class);
45     private static final String STATUS = "STATUS";
46     private static final String FAILURE = "FAILURE";
47     private static final String SUCCESS = "SUCCESS";
48     private static final String ERROR_MESSAGE = "ERROR_MESSAGE";
49
50     @Override
51     public void storeConfig(Map<String, String> params, SvcLogicContext context) throws APPCException {
52         String uniqueId = params.get("uniqueId");
53         String yang = params.get("yang");
54         String configJSON = params.get("configJSON");
55         String requestId = params.get("requestId");
56         String prefix = params.get("prefix");
57         prefix = StringUtils.isEmpty(prefix)? "":prefix+".";
58
59         MDSALStore store = MDSALStoreFactory.createMDSALStore();
60
61         logger.debug("Inputs Received : uniqueId = " + uniqueId +
62                                         " , yang = " + yang +
63                                         " , configJSON = " + configJSON +
64                                         " , requestId = " + requestId +
65                                         " , prefix = " +prefix);
66
67         try {
68
69             if(StringUtils.isEmpty(uniqueId)
70                     ||StringUtils.isEmpty(yang)
71                     || StringUtils.isEmpty(configJSON)
72                     || StringUtils.isEmpty(requestId)){
73                 throw new APPCException("One or more input parameters are empty : uniqueId = " + uniqueId + " " +
74                         ", yang = " + yang +
75                         " , configJSON = " + configJSON +
76                         " , requestId = " + requestId);
77             }
78
79             Date revision = new SimpleDateFormat(Constants.YANG_REVISION_FORMAT).parse(Constants.YANG_REVISION);
80
81             boolean isYangAlreadyLoaded = store.isModulePresent(uniqueId,revision);
82
83             if(!isYangAlreadyLoaded){
84                 BundleInfo bundleInfo = getBundleInfo(uniqueId);
85                 store.storeYangModule(yang,bundleInfo);
86             }
87             store.storeJson(uniqueId, requestId , configJSON);
88             context.setAttribute(prefix + STATUS, SUCCESS);
89         } catch (ParseException e) {
90             String errorMessage ="Error parsing the date : " + Constants.YANG_REVISION + " into format " + Constants.YANG_REVISION_FORMAT;
91             logger.error(errorMessage,e);
92             context.setAttribute(prefix + STATUS, FAILURE);
93             context.setAttribute(prefix + ERROR_MESSAGE, errorMessage);
94             throw new APPCException(e.getMessage());
95         }catch (MDSALStoreException e){
96             String errorMessage = "Error while adding yang to MD-SAL store." + e.getMessage();
97             logger.error(errorMessage,e);
98             context.setAttribute(prefix + STATUS,FAILURE);
99             context.setAttribute(prefix + ERROR_MESSAGE, errorMessage);
100             throw new APPCException(e.getMessage());
101         }
102
103     }
104
105     private BundleInfo getBundleInfo(String uniqueId) {
106         BundleInfo bundleInfo = new BundleInfo();
107         bundleInfo.setDescription(uniqueId);
108         bundleInfo.setName(uniqueId);
109         bundleInfo.setLocation(uniqueId);
110         return bundleInfo;
111     }
112 }