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