Add log statement
[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 Logger logger = LoggerFactory.getLogger(AppcProvider.class);
107     private final EELFLogger logger = EELFManager.getInstance().getLogger(AppcProviderClient.class);
108
109     private final ExecutorService executor;
110
111     private ListenerRegistration<DataChangeListener> dclServices;
112
113     /**
114      * The ODL data store broker. Provides access to a conceptual data tree store and also provides the ability to
115      * subscribe for changes to data under a given branch of the tree.
116      */
117     protected DataBroker dataBroker;
118
119     /**
120      * ODL Notification Service that provides publish/subscribe capabilities for YANG modeled notifications.
121      */
122     protected NotificationProviderService notificationService;
123
124     /**
125      * Provides a registry for Remote Procedure Call (RPC) service implementations. The RPCs are defined in YANG models.
126      */
127     protected RpcProviderRegistry rpcRegistry;
128
129     /**
130      * Represents our RPC implementation registration
131      */
132     protected BindingAwareBroker.RpcRegistration<AppcProviderService> rpcRegistration;
133
134     /**
135      * The configuration
136      */
137     private Configuration configuration = ConfigurationFactory.getConfiguration();
138
139     /**
140      * @param dataBroker2
141      * @param notificationProviderService
142      * @param rpcProviderRegistry
143      */
144     @SuppressWarnings({
145         "javadoc", "nls"
146     })
147     public AppcProvider(DataBroker dataBroker2, NotificationProviderService notificationProviderService,
148                         RpcProviderRegistry rpcProviderRegistry) {
149
150         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
151         logger.info(Msg.COMPONENT_INITIALIZING, appName, "provider");
152
153         executor = Executors.newFixedThreadPool(1);
154         dataBroker = dataBroker2;
155         notificationService = notificationProviderService;
156         rpcRegistry = rpcProviderRegistry;
157
158         if (rpcRegistry != null) {
159                 logger.info("rpcRegistry was not null");
160             rpcRegistration = rpcRegistry.addRpcImplementation(AppcProviderService.class, this);
161         } else {
162                 logger.error("rpcRegistry WAS NULL");
163         }
164
165         logger.info(Msg.COMPONENT_INITIALIZED, appName, "provider");
166     }
167
168     /**
169      * Implements the close of the service
170      *
171      * @see java.lang.AutoCloseable#close()
172      */
173     @SuppressWarnings("nls")
174     @Override
175     public void close() throws Exception {
176         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
177         logger.info(Msg.COMPONENT_TERMINATING, appName, "provider");
178         executor.shutdown();
179         if (rpcRegistration != null) {
180             rpcRegistration.close();
181         }
182         logger.info(Msg.COMPONENT_TERMINATED, appName, "provider");
183     }
184
185
186 public Future<RpcResult<ModifyConfigOutput>> modifyConfig(ModifyConfigInput input){
187         CommonRequestHeader hdr = input.getCommonRequestHeader();        
188         ConfigPayload data = input.getConfigPayload();
189     TopologyService topology = new TopologyService(this);
190     RpcResult<ModifyConfigOutput> result = topology.modifyConfig(hdr, data);
191     return Futures.immediateFuture(result);
192 }
193     /**
194      * Rebuilds a specific VNF
195      *
196      * @see org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.AppcProviderService#rebuild(org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.RebuildInput)
197      */
198     @Override
199     public Future<RpcResult<RebuildOutput>> rebuild(RebuildInput input) {
200
201         CommonRequestHeader hdr = input.getCommonRequestHeader();
202         VnfResource vnf = input.getVnfResource();
203
204         TopologyService topology = new TopologyService(this);
205         RpcResult<RebuildOutput> result = topology.rebuild(hdr, vnf);
206         return Futures.immediateFuture(result);
207     }
208
209     /**
210      * Restarts a specific VNF
211      *
212      * @see org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.AppcProviderService#restart(org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.RestartInput)
213      */
214     @Override
215     public Future<RpcResult<RestartOutput>> restart(RestartInput input) {
216         CommonRequestHeader hdr = input.getCommonRequestHeader();
217         VnfResource vnf = input.getVnfResource();
218
219         TopologyService topology = new TopologyService(this);
220         RpcResult<RestartOutput> result = topology.restart(hdr, vnf);
221         return Futures.immediateFuture(result);
222     }
223
224     /**
225      * Migrates a specific VNF
226      *
227      * @see org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.AppcProviderService#migrate(org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.MigrateInput)
228      */
229     @Override
230     public Future<RpcResult<MigrateOutput>> migrate(MigrateInput input) {
231         CommonRequestHeader hdr = input.getCommonRequestHeader();
232         VnfResource vnf = input.getVnfResource();
233
234         TopologyService topology = new TopologyService(this);
235         RpcResult<MigrateOutput> result = topology.migrate(hdr, vnf);
236         return Futures.immediateFuture(result);
237     }
238
239     /**
240      * Evacuates a specific VNF
241      *
242      * @see org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.AppcProviderService#evacuate(org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.EvacuateInput)
243      */
244     @Override
245     public Future<RpcResult<EvacuateOutput>> evacuate(EvacuateInput input) {
246         CommonRequestHeader hdr = input.getCommonRequestHeader();
247         VnfResource vnf = input.getVnfResource();
248
249         TopologyService topology = new TopologyService(this);
250         // RpcResult<RestartOutput> result = topology.restart(hdr, vnf);
251         return null;// Futures.immediateFuture(result);
252     }
253
254     /**
255      * Evacuates a specific VNF
256      *
257      * @see org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.AppcProviderService#evacuate(org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.EvacuateInput)
258      */
259     @Override
260     public Future<RpcResult<SnapshotOutput>> snapshot(SnapshotInput input) {
261         CommonRequestHeader hdr = input.getCommonRequestHeader();
262         VnfResource vnf = input.getVnfResource();
263
264         TopologyService topology = new TopologyService(this);
265         RpcResult<SnapshotOutput> result = topology.snapshot(hdr, vnf);
266         return Futures.immediateFuture(result);
267     }
268
269 }