Change nexus values to properties
[appc.git] / app-c / appc / appc-dispatcher / appc-license-manager / appc-license-manager-core / src / main / java / org / openecomp / appc / licmgr / impl / LicenseManagerImpl.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.licmgr.impl;
23
24 import static org.openecomp.appc.licmgr.Constants.ASDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT;
25
26 import java.util.Map;
27
28 import org.openecomp.appc.licmgr.Constants;
29 import org.openecomp.appc.licmgr.LicenseDataAccessService;
30 import org.openecomp.appc.licmgr.LicenseManager;
31 import org.openecomp.appc.licmgr.exception.DataAccessException;
32 import org.openecomp.appc.licmgr.objects.LicenseModel;
33
34
35 @SuppressWarnings("all")
36 public class LicenseManagerImpl implements LicenseManager {
37
38     private LicenseDataAccessService DAService;
39
40     public void setDAService(LicenseDataAccessService daSrv){
41         DAService = daSrv;
42     }
43
44     public LicenseManagerImpl() {
45         DAService = new LicenseDataAccessServiceImpl();
46         DAService.setSchema(Constants.SDNCTL_SCHEMA);
47     }
48
49     @Override
50     public LicenseModel retrieveLicenseModel(String vnfType, String vnfVersion) throws DataAccessException {
51
52         LicenseModel licenseModel;
53         try {
54             Map<String,String> resultMap = DAService.retrieveLicenseModelData(vnfType, vnfVersion);
55             if (resultMap.isEmpty()) {
56                 throw new DataAccessException(String.format("License model not found for vnfType='%s' and vnfVersion='%s'", vnfType, vnfVersion));
57             }
58             String licenseModelXML = resultMap.get(ARTIFACT_CONTENT.name());
59             licenseModel = convert(licenseModelXML);  // JAXBUtil.<VfLicenseModel>toObject(licenseModelXML, VfLicenseModel.class);
60         } catch (DataAccessException le) {
61             throw le;
62         } catch (Exception e) {
63             throw new DataAccessException(e);
64         }
65         return licenseModel;
66     }
67
68     @Override
69     public Map<String, String> retrieveLicenseModelData(String vnfType, String vnfVersion, String... fields)  throws DataAccessException {
70
71         Map<String,String> resultMap = DAService.retrieveLicenseModelData(vnfType, vnfVersion, fields);
72         return resultMap;
73     }
74
75     @Override
76     public void storeArtifactPayload(Map<String, String> parameters) throws RuntimeException {
77
78         DAService.storeArtifactPayload(parameters);
79     }
80
81     private static LicenseModel convert(String xml) {
82
83         LicenseModel licenseModel = new LicenseModel();
84
85         int posEntitlementStart = xml.indexOf("<entitlement-pool-uuid>");
86         int posEntitlementEnd = xml.indexOf("</entitlement-pool-uuid>", posEntitlementStart);
87         if (-1 != posEntitlementStart) {
88             licenseModel.setEntitlementPoolUuid(xml.substring(posEntitlementStart + "<entitlement-pool-uuid>".length(), posEntitlementEnd));
89         }
90
91         int posLicenseStart = xml.indexOf("<license-key-group-uuid>");
92         int posLicenseEnd = xml.indexOf("</license-key-group-uuid>", posEntitlementStart);
93         if (-1 != posLicenseStart) {
94             licenseModel.setLicenseKeyGroupUuid(xml.substring(posLicenseStart + "<license-key-group-uuid>".length(), posLicenseEnd));
95         }
96
97         return licenseModel;
98     }
99
100 }