Test coverage in appc-dg-ssh
[appc.git] / appc-dg / appc-dg-shared / appc-dg-license-manager / src / main / java / org / onap / appc / dg / licmgr / impl / LicenseManagerPluginImpl.java
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  * Modifications (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.dg.licmgr.impl;
27
28 import java.util.Map;
29
30 import org.onap.appc.dg.licmgr.LicenseManagerPlugin;
31 import org.onap.appc.exceptions.APPCException;
32 import org.onap.appc.licmgr.Constants;
33 import org.onap.appc.licmgr.LicenseManager;
34 import org.onap.appc.licmgr.exception.DataAccessException;
35 import org.onap.appc.licmgr.objects.LicenseModel;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39
40
41
42 public class LicenseManagerPluginImpl implements LicenseManagerPlugin {
43
44     private static EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
45
46     // populated by blueprint framework
47     private LicenseManager licenseManager;
48
49     public void setLicenseManager(LicenseManager licenseManager) {
50         this.licenseManager = licenseManager;
51     }
52
53     /**
54      * Retrieves license model from APPC database and populate flags into svc context
55      * @param params map with parameters:
56      *               org.onap.appc.vftype - the vnf type / service type;
57      *               org.onap.appc.resource-version - the vnf version / service version
58      * @param ctx service logic context
59      *            1. supposed properties already in context:
60      *            aai.input.data.entitlement-assignment-group-uuid - entitlement-group-uuid asset tag already stored in AAI
61      *            aai.input.data.license-assignment-group-uuid - license-key-uuid asset tag already stored in AAI
62      *            2. properties and flags stored in context after bean execution:
63      *            model.entitlement.pool.uuid - entitlement-group-uuid from license model
64      *            model.license.key.uuid - license-key-uuid from license model
65      *            is.acquire-entitlement.require
66      *            is.release-entitlement.require
67      *            is.acquire-license.require
68      *            is.release-license.require
69      *            is.aai-entitlement-update.require
70      *            is.aai-license-update.require
71      *
72      * @throws APPCException throws in case of any error
73      */
74     @Override
75     public void retrieveLicenseModel(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
76
77         try {
78
79             LicenseModel licenseModel = licenseManager.retrieveLicenseModel(params.get(Constants.VNF_TYPE_FIELD_NAME), params.get(Constants.VNF_RESOURCE_VERSION_FIELD_NAME));
80
81             String modelEntitlementPoolUuid = licenseModel.getEntitlementPoolUuid();
82             if (null == modelEntitlementPoolUuid) {
83                 modelEntitlementPoolUuid = "";
84             }
85             String aaiEntitlementPoolUuid = ctx.getAttribute(Constants.AAI_ENTITLMENT_POOL_UUID_NAME);
86             if (null == aaiEntitlementPoolUuid) {
87                 aaiEntitlementPoolUuid = "";
88             }
89             boolean isAcquireEntitlementRequire = !modelEntitlementPoolUuid.isEmpty() && !modelEntitlementPoolUuid.equals(aaiEntitlementPoolUuid);
90             boolean isReleaseEntitlementRequire = !aaiEntitlementPoolUuid.isEmpty() && (isAcquireEntitlementRequire || modelEntitlementPoolUuid.isEmpty());
91             boolean isAAIEntitlementUpdateRequire = isAcquireEntitlementRequire || isReleaseEntitlementRequire;
92             ctx.setAttribute(Constants.MODEL_ENTITLMENT_POOL_UUID_NAME, modelEntitlementPoolUuid);
93             ctx.setAttribute(Constants.IS_ACQUIRE_ENTITLEMENT_REQUIRE, Boolean.toString(isAcquireEntitlementRequire));
94             ctx.setAttribute(Constants.IS_RELEASE_ENTITLEMENT_REQUIRE, Boolean.toString(isReleaseEntitlementRequire));
95             ctx.setAttribute(Constants.IS_AAI_ENTITLEMENT_UPDATE_REQUIRE, Boolean.toString(isAAIEntitlementUpdateRequire));
96
97
98             String modelLicenseKeyGroupUuid = licenseModel.getLicenseKeyGroupUuid();
99             if (null == modelLicenseKeyGroupUuid) {
100                 modelLicenseKeyGroupUuid = "";
101             }
102             String aaiLicenseKeyGroupUuid = ctx.getAttribute(Constants.AAI_LICENSE_KEY_UUID_NAME);
103             if (null == aaiLicenseKeyGroupUuid) {
104                 aaiLicenseKeyGroupUuid = "";
105             }
106             String aaiLicenseKeyValue = ctx.getAttribute(Constants.AAI_LICENSE_KEY_VALUE);
107             if (null == aaiLicenseKeyValue) {
108                 aaiLicenseKeyValue = "";
109             }
110             boolean isAcquireLicenseRequire = !modelLicenseKeyGroupUuid.isEmpty() && !modelLicenseKeyGroupUuid.equals(aaiLicenseKeyGroupUuid);
111             boolean isReleaseLicenseRequire = !aaiLicenseKeyGroupUuid.isEmpty() && (isAcquireLicenseRequire || modelLicenseKeyGroupUuid.isEmpty());
112             boolean isAAILicenseUpdateRequire = isAcquireLicenseRequire || isReleaseLicenseRequire;
113             ctx.setAttribute(Constants.MODEL_LICENSE_KEY_UUID_NAME, modelLicenseKeyGroupUuid);
114             ctx.setAttribute(Constants.IS_ACQUIRE_LICENSE_REQUIRE, Boolean.toString(isAcquireLicenseRequire));
115             ctx.setAttribute(Constants.IS_RELEASE_LICENSE_REQUIRE, Boolean.toString(isReleaseLicenseRequire));
116             ctx.setAttribute(Constants.IS_AAI_LICENSE_UPDATE_REQUIRE, Boolean.toString(isAAILicenseUpdateRequire));
117
118             ctx.setAttribute("license-key", aaiLicenseKeyValue);
119
120         } catch (DataAccessException le) {
121             logger.error("Error " + le.getMessage());
122             ctx.setAttribute("output.status.message", le.getMessage());
123             throw new APPCException(le);
124         }
125
126     }
127
128
129
130     //////// code uses jaxb license model, should be fixed
131     /*
132     final VfLicenseModel.FeatureGroupList featureGroupList = licenseModel.getFeatureGroupList();
133     if (null != featureGroupList) {
134         final VfLicenseModel.FeatureGroupList.FeatureGroup featureGroup = featureGroupList.getFeatureGroup();
135         if (null != featureGroup) {
136             final VfLicenseModel.FeatureGroupList.FeatureGroup.EntitlementPoolList
137                             entitlementPoolList = featureGroup.getEntitlementPoolList();
138             if (null != entitlementPoolList) {
139                 final VfLicenseModel.FeatureGroupList.FeatureGroup.EntitlementPoolList.EntitlementPool
140                                 entitlementPool = entitlementPoolList.getEntitlementPool();
141                 if (null != entitlementPool) {
142                     final String entitlementPoolUuid = entitlementPool.getEntitlementPoolUuid();
143                     // add entitlementPoolUuid into context
144                     ctx.setAttribute(Constants.MODEL_ENTITLMENT_POOL_UUID_NAME, entitlementPoolUuid);
145                 }
146             }
147
148             final VfLicenseModel.FeatureGroupList.FeatureGroup.LicenseKeyGroupList
149                             licenseKeyGroupList = featureGroup.getLicenseKeyGroupList();
150             if (null != licenseKeyGroupList) {
151                 final VfLicenseModel.FeatureGroupList.FeatureGroup.LicenseKeyGroupList.LicenseKeyGroup
152                                 licenseKeyGroup = licenseKeyGroupList.getLicenseKeyGroup();
153                 if (null != licenseKeyGroup) {
154                     final String licenseKeyGroupUuid = licenseKeyGroup.getLicenseKeyGroupUuid();
155                     // add licenseKeyGroupUuid into context
156                     ctx.setAttribute(Constants.MODEL_LICENSE_KEY_UUID_NAME, licenseKeyGroupUuid);
157                 }
158             }
159         }
160     }
161     */
162
163
164 }