2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017-2018 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
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  22  * ============LICENSE_END=========================================================
 
  25 package org.onap.appc.dg.common.impl;
 
  27 import com.att.eelf.configuration.EELFLogger;
 
  28 import com.att.eelf.configuration.EELFManager;
 
  29 import java.text.ParseException;
 
  30 import java.text.SimpleDateFormat;
 
  31 import java.util.Date;
 
  33 import org.apache.commons.lang3.StringUtils;
 
  34 import org.onap.appc.Constants;
 
  35 import org.onap.appc.dg.common.VNFConfigurator;
 
  36 import org.onap.appc.exceptions.APPCException;
 
  37 import org.onap.appc.mdsal.MDSALStore;
 
  38 import org.onap.appc.mdsal.exception.MDSALStoreException;
 
  39 import org.onap.appc.mdsal.impl.MDSALStoreFactory;
 
  40 import org.onap.appc.mdsal.objects.BundleInfo;
 
  41 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  44 public class VNFConfiguratorImpl implements VNFConfigurator {
 
  46     private static final EELFLogger logger = EELFManager.getInstance().getLogger(VNFConfiguratorImpl.class);
 
  47     private static final String STATUS = "STATUS";
 
  48     private static final String FAILURE = "FAILURE";
 
  49     private static final String SUCCESS = "SUCCESS";
 
  50     private static final String ERROR_MESSAGE = "ERROR_MESSAGE";
 
  53     public void storeConfig(Map<String, String> params, SvcLogicContext context) throws APPCException {
 
  54         String uniqueId = params.get("uniqueId");
 
  55         String yang = params.get("yang");
 
  56         String configJSON = params.get("configJSON");
 
  57         String requestId = params.get("requestId");
 
  58         String prefix = params.get("prefix");
 
  59         prefix = StringUtils.isEmpty(prefix) ? "" : prefix + ".";
 
  61         MDSALStore store = MDSALStoreFactory.createMDSALStore();
 
  63         logger.debug("Inputs Received : uniqueId = " + uniqueId +
 
  65             " , configJSON = " + configJSON +
 
  66             " , requestId = " + requestId +
 
  67             " , prefix = " + prefix);
 
  71             if (StringUtils.isEmpty(uniqueId)
 
  72                 || StringUtils.isEmpty(yang)
 
  73                 || StringUtils.isEmpty(configJSON)
 
  74                 || StringUtils.isEmpty(requestId)) {
 
  75                 throw new APPCException("One or more input parameters are empty : uniqueId = " + uniqueId + " " +
 
  77                     " , configJSON = " + configJSON +
 
  78                     " , requestId = " + requestId);
 
  81             Date revision = new SimpleDateFormat(Constants.YANG_REVISION_FORMAT).parse(Constants.YANG_REVISION);
 
  83             boolean isYangAlreadyLoaded = store.isModulePresent(uniqueId, revision);
 
  85             if (!isYangAlreadyLoaded) {
 
  86                 BundleInfo bundleInfo = getBundleInfo(uniqueId);
 
  87                 store.storeYangModule(yang, bundleInfo);
 
  89             store.storeYangModuleOnLeader(yang,uniqueId);
 
  90             store.storeJson(uniqueId, requestId, configJSON);
 
  91             context.setAttribute(prefix + STATUS, SUCCESS);
 
  92         } catch (ParseException e) {
 
  93             String errorMessage = "Error parsing the date : " + Constants.YANG_REVISION + " into format "
 
  94                 + Constants.YANG_REVISION_FORMAT;
 
  95             logger.error(errorMessage, e);
 
  96             context.setAttribute(prefix + STATUS, FAILURE);
 
  97             context.setAttribute(prefix + ERROR_MESSAGE, errorMessage);
 
  98             throw new APPCException(e.getMessage());
 
  99         } catch (MDSALStoreException e) {
 
 100             String errorMessage = "Error while adding yang to MD-SAL store." + e.getMessage();
 
 101             logger.error(errorMessage, e);
 
 102             context.setAttribute(prefix + STATUS, FAILURE);
 
 103             context.setAttribute(prefix + ERROR_MESSAGE, errorMessage);
 
 104             throw new APPCException(e.getMessage());
 
 109     private BundleInfo getBundleInfo(String uniqueId) {
 
 110         BundleInfo bundleInfo = new BundleInfo();
 
 111         bundleInfo.setDescription(uniqueId);
 
 112         bundleInfo.setName(uniqueId);
 
 113         bundleInfo.setLocation(uniqueId);