6f8d33ea53b74a781050aa25fc03e1939fd04b9a
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.licmgr.impl;
25
26 import static org.onap.appc.licmgr.Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT;
27
28 import java.util.Map;
29
30 import org.onap.appc.licmgr.LicenseDataAccessService;
31 import org.onap.appc.licmgr.LicenseManager;
32 import org.onap.appc.licmgr.exception.DataAccessException;
33 import org.onap.appc.licmgr.objects.LicenseModel;
34
35
36 @SuppressWarnings("all")
37 public class LicenseManagerImpl implements LicenseManager {
38
39     private LicenseDataAccessService DAService;
40
41     public void setDAService(LicenseDataAccessService daSrv){
42         DAService = daSrv;
43     }
44
45     public LicenseManagerImpl() {
46     }
47
48     @Override
49     public LicenseModel retrieveLicenseModel(String vnfType, String vnfVersion) throws DataAccessException {
50
51         LicenseModel licenseModel;
52         try {
53             Map<String,String> resultMap = DAService.retrieveLicenseModelData(vnfType, vnfVersion);
54             if (resultMap.isEmpty()) {
55                 throw new DataAccessException(String.format("License model not found for vnfType='%s' and vnfVersion='%s'", vnfType, vnfVersion));
56             }
57             String licenseModelXML = resultMap.get(ARTIFACT_CONTENT.name());
58             licenseModel = convert(licenseModelXML);  // JAXBUtil.<VfLicenseModel>toObject(licenseModelXML, VfLicenseModel.class);
59         } catch (DataAccessException le) {
60             throw le;
61         } catch (Exception e) {
62             throw new DataAccessException(e);
63         }
64         return licenseModel;
65     }
66
67
68     private static LicenseModel convert(String xml) {
69
70         LicenseModel licenseModel = new LicenseModel();
71
72         int posEntitlementStart = xml.indexOf("<entitlement-pool-uuid>");
73         int posEntitlementEnd = xml.indexOf("</entitlement-pool-uuid>", posEntitlementStart);
74         if (-1 != posEntitlementStart) {
75             licenseModel.setEntitlementPoolUuid(xml.substring(posEntitlementStart + "<entitlement-pool-uuid>".length(), posEntitlementEnd));
76         }
77
78         int posLicenseStart = xml.indexOf("<license-key-group-uuid>");
79         int posLicenseEnd = xml.indexOf("</license-key-group-uuid>", posEntitlementStart);
80         if (-1 != posLicenseStart) {
81             licenseModel.setLicenseKeyGroupUuid(xml.substring(posLicenseStart + "<license-key-group-uuid>".length(), posLicenseEnd));
82         }
83
84         return licenseModel;
85     }
86
87 }