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