Merge of new rebased code
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / openecomp / appc / provider / AppcProvider.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.provider;
23
24 import com.google.common.util.concurrent.Futures;
25 import org.json.JSONObject;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
28 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
29 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
30 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
31 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.*;
32 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.common.request.header.CommonRequestHeader;
33 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.responseattributes.StatusBuilder;
34 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.responseheader.ResponseHeaderBuilder;
35 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.vnf.resource.VnfResource;
36 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.config.payload.ConfigPayload;
37 import org.opendaylight.yangtools.concepts.ListenerRegistration;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40 import org.openecomp.appc.Constants;
41 import org.openecomp.appc.configuration.Configuration;
42 import org.openecomp.appc.configuration.ConfigurationFactory;
43 import org.openecomp.appc.executor.objects.LCMCommandStatus;
44 import org.openecomp.appc.executor.objects.Params;
45 import org.openecomp.appc.i18n.Msg;
46 import org.openecomp.appc.provider.lcm.util.RequestInputBuilder;
47 import org.openecomp.appc.provider.lcm.util.ValidationService;
48 import org.openecomp.appc.provider.topology.TopologyService;
49 import org.openecomp.appc.requesthandler.RequestHandler;
50 import org.openecomp.appc.requesthandler.objects.RequestHandlerInput;
51 import org.openecomp.appc.requesthandler.objects.RequestHandlerOutput;
52 import com.att.eelf.configuration.EELFLogger;
53 import com.att.eelf.configuration.EELFManager;
54 import com.att.eelf.i18n.EELFResourceManager;
55 import org.osgi.framework.BundleContext;
56 import org.osgi.framework.FrameworkUtil;
57 import org.osgi.framework.ServiceReference;
58 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.status.Status;
59 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.Action;
60
61 import java.text.DateFormat;
62 import java.text.ParseException;
63 import java.text.SimpleDateFormat;
64 import java.util.Date;
65 import java.util.Iterator;
66 import java.util.Properties;
67 import java.util.TimeZone;
68 import java.util.concurrent.ExecutorService;
69 import java.util.concurrent.Executors;
70 import java.util.concurrent.Future;
71
72 /* ADDED FOR FUSION SERVICE CODE */
73
74 @SuppressWarnings("JavaDoc")
75 /**
76  * Defines the APPC service provider.
77  * <p>
78  * The rpc definition in the YANG model is shown below. This model is used to generate code to manage the inputs and
79  * outputs of the RPC service. For example, the input is defined by a class named {@link ConfigurationOperationInput},
80  * which is generated from the name of the RPC and the "input" definition of the RPC. This class encapsulates the
81  * various objects that are passed to the RPC and is used to obtain values from the input parameters.
82  * </p>
83  * <p>
84  * Likewise, the outputs are defined by a class named {@link ConfigurationOperationOutput}. This class encapsulates the
85  * defined outputs. To make construction of the outputs easier, there are also generated builder classes that are named
86  * for the various elements of the output they "build", such as {@link ConfigurationResponseBuilder}.
87  * </p>
88  *
89  * <pre>
90  *   rpc configuration-operation {
91  *      description "An operation to view, change, or audit the configuration of a VM";
92  *      input {
93  *          uses configuration-request-header;
94  *          uses configuration-request;
95  *      }
96  *      output {
97  *          uses common-response-header;
98  *          uses configuration-response;
99  *      }
100  *  }
101  * </pre>
102  *
103  */
104 public class AppcProvider implements AutoCloseable, AppcProviderService {
105
106     private final EELFLogger logger = EELFManager.getInstance().getLogger(AppcProviderClient.class);
107
108     private final ExecutorService executor;
109
110     private ListenerRegistration<DataChangeListener> dclServices;
111
112     /**
113      * The ODL data store broker. Provides access to a conceptual data tree store and also provides the ability to
114      * subscribe for changes to data under a given branch of the tree.
115      */
116     protected DataBroker dataBroker;
117
118     /**
119      * ODL Notification Service that provides publish/subscribe capabilities for YANG modeled notifications.
120      */
121     protected NotificationProviderService notificationService;
122
123     /**
124      * Provides a registry for Remote Procedure Call (RPC) service implementations. The RPCs are defined in YANG models.
125      */
126     protected RpcProviderRegistry rpcRegistry;
127
128     /**
129      * Represents our RPC implementation registration
130      */
131     protected BindingAwareBroker.RpcRegistration<AppcProviderService> rpcRegistration;
132
133     /**
134      * The configuration
135      */
136     private Configuration configuration = ConfigurationFactory.getConfiguration();
137
138     /**
139      * @param dataBroker2
140      * @param notificationProviderService
141      * @param rpcProviderRegistry
142      */
143     @SuppressWarnings({
144         "javadoc", "nls"
145     })
146     public AppcProvider(DataBroker dataBroker2, NotificationProviderService notificationProviderService,
147                         RpcProviderRegistry rpcProviderRegistry) {
148
149         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
150         logger.info(Msg.COMPONENT_INITIALIZING, appName, "provider");
151
152         executor = Executors.newFixedThreadPool(1);
153         dataBroker = dataBroker2;
154         notificationService = notificationProviderService;
155         rpcRegistry = rpcProviderRegistry;
156
157         if (rpcRegistry != null) {
158             rpcRegistration = rpcRegistry.addRpcImplementation(AppcProviderService.class, this);
159         }
160
161         logger.info(Msg.COMPONENT_INITIALIZED, appName, "provider");
162     }
163
164     /**
165      * Implements the close of the service
166      *
167      * @see java.lang.AutoCloseable#close()
168      */
169     @SuppressWarnings("nls")
170     @Override
171     public void close() throws Exception {
172         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
173         logger.info(Msg.COMPONENT_TERMINATING, appName, "provider");
174         executor.shutdown();
175         if (rpcRegistration != null) {
176             rpcRegistration.close();
177         }
178         logger.info(Msg.COMPONENT_TERMINATED, appName, "provider");
179     }
180
181
182 public Future<RpcResult<ModifyConfigOutput>> modifyConfig(ModifyConfigInput input){
183         CommonRequestHeader hdr = input.getCommonRequestHeader();        
184         ConfigPayload data = input.getConfigPayload();
185     TopologyService topology = new TopologyService(this);
186     RpcResult<ModifyConfigOutput> result = topology.modifyConfig(hdr, data);
187     return Futures.immediateFuture(result);
188 }
189     /**
190      * Rebuilds a specific VNF
191      *
192      * @see org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.AppcProviderService#rebuild(org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.RebuildInput)
193      */
194     @Override
195     public Future<RpcResult<RebuildOutput>> rebuild(RebuildInput input) {
196
197         CommonRequestHeader hdr = input.getCommonRequestHeader();
198         VnfResource vnf = input.getVnfResource();
199
200         TopologyService topology = new TopologyService(this);
201         RpcResult<RebuildOutput> result = topology.rebuild(hdr, vnf);
202         return Futures.immediateFuture(result);
203     }
204
205     /**
206      * Restarts a specific VNF
207      *
208      * @see org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.AppcProviderService#restart(org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.RestartInput)
209      */
210     @Override
211     public Future<RpcResult<RestartOutput>> restart(RestartInput input) {
212         CommonRequestHeader hdr = input.getCommonRequestHeader();
213         VnfResource vnf = input.getVnfResource();
214
215         TopologyService topology = new TopologyService(this);
216         RpcResult<RestartOutput> result = topology.restart(hdr, vnf);
217         return Futures.immediateFuture(result);
218     }
219
220     /**
221      * Migrates a specific VNF
222      *
223      * @see org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.AppcProviderService#migrate(org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.MigrateInput)
224      */
225     @Override
226     public Future<RpcResult<MigrateOutput>> migrate(MigrateInput input) {
227         CommonRequestHeader hdr = input.getCommonRequestHeader();
228         VnfResource vnf = input.getVnfResource();
229
230         TopologyService topology = new TopologyService(this);
231         RpcResult<MigrateOutput> result = topology.migrate(hdr, vnf);
232         return Futures.immediateFuture(result);
233     }
234
235     /**
236      * Evacuates a specific VNF
237      *
238      * @see org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.AppcProviderService#evacuate(org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.EvacuateInput)
239      */
240     @Override
241     public Future<RpcResult<EvacuateOutput>> evacuate(EvacuateInput input) {
242         CommonRequestHeader hdr = input.getCommonRequestHeader();
243         VnfResource vnf = input.getVnfResource();
244
245         TopologyService topology = new TopologyService(this);
246         return null;
247     }
248
249     /**
250      * Evacuates a specific VNF
251      *
252      * @see org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.AppcProviderService#evacuate(org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.EvacuateInput)
253      */
254     @Override
255     public Future<RpcResult<SnapshotOutput>> snapshot(SnapshotInput input) {
256         CommonRequestHeader hdr = input.getCommonRequestHeader();
257         VnfResource vnf = input.getVnfResource();
258
259         TopologyService topology = new TopologyService(this);
260         RpcResult<SnapshotOutput> result = topology.snapshot(hdr, vnf);
261         return Futures.immediateFuture(result);
262     }
263     
264     
265     /**
266      * Checks status of a VM
267     */
268     @Override
269     public Future<RpcResult<VmstatuscheckOutput>> vmstatuscheck(VmstatuscheckInput input) {
270     CommonRequestHeader hdr = input.getCommonRequestHeader();
271     VnfResource vnf = input.getVnfResource();
272
273     TopologyService topology = new TopologyService(this);
274     RpcResult<VmstatuscheckOutput> result = topology.vmstatuscheck(hdr, vnf);
275     return Futures.immediateFuture(result);
276     }   
277
278 }