First part of onap rename
[appc.git] / appc-dispatcher / appc-license-manager / appc-license-manager-core / src / main / java / org / openecomp / appc / licmgr / impl / LicenseManagerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.licmgr.impl;
26
27 import static org.onap.appc.licmgr.Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT;
28
29 import java.util.Map;
30
31 import org.onap.appc.licmgr.LicenseDataAccessService;
32 import org.onap.appc.licmgr.LicenseManager;
33 import org.onap.appc.licmgr.exception.DataAccessException;
34 import org.onap.appc.licmgr.objects.LicenseModel;
35
36
37 @SuppressWarnings("all")
38 public class LicenseManagerImpl implements LicenseManager {
39
40     private LicenseDataAccessService DAService;
41
42     public void setDAService(LicenseDataAccessService daSrv){
43         DAService = daSrv;
44     }
45
46     public LicenseManagerImpl() {
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
69     private static LicenseModel convert(String xml) {
70
71         LicenseModel licenseModel = new LicenseModel();
72
73         int posEntitlementStart = xml.indexOf("<entitlement-pool-uuid>");
74         int posEntitlementEnd = xml.indexOf("</entitlement-pool-uuid>", posEntitlementStart);
75         if (-1 != posEntitlementStart) {
76             licenseModel.setEntitlementPoolUuid(xml.substring(posEntitlementStart + "<entitlement-pool-uuid>".length(), posEntitlementEnd));
77         }
78
79         int posLicenseStart = xml.indexOf("<license-key-group-uuid>");
80         int posLicenseEnd = xml.indexOf("</license-key-group-uuid>", posEntitlementStart);
81         if (-1 != posLicenseStart) {
82             licenseModel.setLicenseKeyGroupUuid(xml.substring(posLicenseStart + "<license-key-group-uuid>".length(), posLicenseEnd));
83         }
84
85         return licenseModel;
86     }
87
88 }