400d6c6bd07d7594d2f47d2cc85769471850126d
[appc.git] /
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.client.lcm.impl.business;
26
27 import java.util.HashMap;
28
29 import org.onap.appc.client.lcm.api.AppcLifeCycleManagerServiceFactory;
30 import org.onap.appc.client.lcm.api.ApplicationContext;
31 import org.onap.appc.client.lcm.api.LifeCycleManagerStateful;
32 import org.onap.appc.client.lcm.exceptions.AppcClientException;
33
34 import java.lang.reflect.Proxy;
35 import java.util.Properties;
36
37 public class AppcLifeCycleManagerServiceFactoryImpl implements AppcLifeCycleManagerServiceFactory {
38
39     class AppcLifeCycleManagerServiceFactoryImplData {
40
41         private LifeCycleManagerStateful _lifeCycleManagerStateful;
42         private LCMRequestProcessor _lcmRequestProcessor;
43
44         AppcLifeCycleManagerServiceFactoryImplData(LifeCycleManagerStateful lifeCycleManagerStateful,
45                                                    LCMRequestProcessor lcmRequestProcessor) {
46
47             _lifeCycleManagerStateful = lifeCycleManagerStateful;
48             _lcmRequestProcessor = lcmRequestProcessor;
49         }
50
51         LifeCycleManagerStateful getLifeCycleManagerStateful() {
52             return _lifeCycleManagerStateful;
53         }
54
55         LCMRequestProcessor getLCMRequestProcessor() {
56             return _lcmRequestProcessor;
57         }
58     }
59
60     private HashMap<String, AppcLifeCycleManagerServiceFactoryImplData> lcmMap = new HashMap<String, AppcLifeCycleManagerServiceFactoryImplData>();
61
62     @Override
63     public synchronized LifeCycleManagerStateful createLifeCycleManagerStateful(ApplicationContext context,
64                                                                                 Properties properties) throws AppcClientException {
65         String cType = properties.getProperty("controllerType");
66         if (cType == null || cType.length() == 0)
67         {
68             cType = "APPC";
69             properties.put("controllerType", cType);
70         }
71
72         AppcLifeCycleManagerServiceFactoryImplData lcmData = lcmMap.get(cType);
73         LifeCycleManagerStateful lifeCycleManagerStateful = null;
74         LCMRequestProcessor lcmRequestProcessor = null;
75
76         if (lcmData != null) {
77             lifeCycleManagerStateful = lcmData.getLifeCycleManagerStateful();
78             lcmRequestProcessor = lcmData.getLCMRequestProcessor();
79         }
80
81         if (lifeCycleManagerStateful == null) {
82             lcmRequestProcessor = new LCMRequestProcessor(context, properties);
83             lifeCycleManagerStateful = (LifeCycleManagerStateful) Proxy.newProxyInstance(
84                     LifeCycleManagerStateful.class.getClassLoader(), new Class<?>[] { LifeCycleManagerStateful.class },
85                     new RPCInvocator(lcmRequestProcessor));
86             lcmMap.put(cType,
87                     new AppcLifeCycleManagerServiceFactoryImplData(lifeCycleManagerStateful, lcmRequestProcessor));
88         } else {
89             throw new IllegalStateException("already instansiated LifeCycleManagerStateful instance");
90         }
91         return lifeCycleManagerStateful;
92     }
93
94     public LifeCycleManagerStateful createLifeCycleManagerStateful(ApplicationContext context,
95                                                                    Properties properties, String controllerType) throws AppcClientException {
96         if (controllerType != null && controllerType.length() != 0)
97             properties.put("controllerType", controllerType.toUpperCase());
98         return createLifeCycleManagerStateful(context, properties);
99     }
100
101     @Override
102     public void shutdownLifeCycleManager(boolean isForceShutdown) {
103
104         shutdownLifeCycleManager(isForceShutdown, "APPC");
105     }
106
107     @Override
108     public void shutdownLifeCycleManager(boolean isForceShutdown, String controllerType) {
109         if (controllerType == null || controllerType.length() == 0)
110             controllerType = "APPC";
111         else
112             controllerType = controllerType.toUpperCase();
113
114         AppcLifeCycleManagerServiceFactoryImplData lcmData = lcmMap.get(controllerType);
115         LCMRequestProcessor lcmRequestProcessor = null;
116
117         if (lcmData != null) {
118             lcmRequestProcessor = lcmData.getLCMRequestProcessor();
119         }
120
121         if (lcmRequestProcessor != null) {
122             lcmRequestProcessor.shutdown(isForceShutdown);
123         } else {
124             throw new IllegalStateException(
125                     "The life cycle manager library wasn't instantiated properly, therefore the shutdown event will not be handled");
126         }
127     }
128
129 }