move plugins from from ccsdk to dcaegen2
[dcaegen2/platform/plugins.git] / dmaap / dmaapplugin / dr_lifecycle.py
1 # ============LICENSE_START====================================================
2 # org.onap.dcaegen2
3 # =============================================================================
4 # Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved.
5 # Copyright (c) 2020 Pantheon.tech. All rights reserved.
6 # =============================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #      http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END======================================================
19
20 from cloudify import ctx
21 from cloudify.decorators import operation
22 from cloudify.exceptions import NonRecoverableError
23 from dmaapplugin import DMAAP_API_URL, DMAAP_USER, DMAAP_PASS, DMAAP_OWNER
24 from dmaapplugin.dmaaputils import random_string
25 from dmaapcontrollerif.dmaap_requests import DMaaPControllerHandle
26
27 # Lifecycle operations for DMaaP Data Router feeds
28
29 @operation
30 def create_feed(**kwargs):
31     '''
32     Create a new data router feed
33         Expects "feed_name" to be set in node properties
34         If 'feed_name' is not set or is empty, generates a random one.
35         Allows "feed_version", "feed_description", "aspr_classification" and "useExisting" as optional properties
36         (Sets default values if not provided )
37         Sets instance runtime properties:
38         Note that 'useExisting' is a flag indicating whether DBCL will use existing feed if the feed already exists.
39             - "feed_id"
40             - "publish_url"
41             - "log_url"
42
43     '''
44     try:
45         # Make sure there's a feed_name
46         feed_name = ctx.node.properties.get("feed_name")
47         if not (feed_name and feed_name.strip()):
48             feed_name = random_string(12)
49
50         # Set defaults/placeholders for the optional properties for the feed
51         if "feed_version" in ctx.node.properties:
52             feed_version = ctx.node.properties["feed_version"]
53         else:
54             feed_version = "0.0"
55         if "feed_description" in ctx.node.properties:
56             feed_description = ctx.node.properties["feed_description"]
57         else:
58             feed_description = "No description provided"
59         if "aspr_classification" in ctx.node.properties:
60             aspr_classification = ctx.node.properties["aspr_classification"]
61         else:
62             aspr_classification = "unclassified"
63         if "useExisting" in ctx.node.properties:
64             useExisting = ctx.node.properties["useExisting"]
65         else:
66             useExisting = False
67
68         # Make the request to the controller
69         dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS, ctx.logger)
70         ctx.logger.info("Attempting to create feed name {0}".format(feed_name))
71         f = dmc.create_feed(feed_name, feed_version, feed_description, aspr_classification, DMAAP_OWNER, useExisting)
72         f.raise_for_status()
73
74         # Capture important properties from the result
75         feed = f.json()
76         ctx.instance.runtime_properties["feed_id"] = feed["feedId"]
77         ctx.instance.runtime_properties["publish_url"] = feed["publishURL"]
78         ctx.instance.runtime_properties["log_url"] = feed["logURL"]
79         ctx.logger.info("Created feed name {0} with feed id {1}".format(feed_name, feed["feedId"]))
80
81     except Exception as e:
82         ctx.logger.error("Error creating feed: {er}".format(er=e))
83         raise NonRecoverableError(e)
84
85
86 @operation
87 def get_existing_feed(**kwargs):
88     '''
89     Find information for an existing data router feed
90         Expects "feed_id" to be set in node properties -- uniquely identifies the feed
91         Sets instance runtime properties:
92             - "feed_id"
93             - "publish_url"
94             - "log_url"
95     '''
96
97     try:
98         # Make the lookup request to the controller
99         dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS, ctx.logger)
100         ctx.logger.info("DMaaPControllerHandle() returned")
101         feed_id_input = False
102         if "feed_id" in ctx.node.properties:
103             feed_id_input = True
104             f = dmc.get_feed_info(ctx.node.properties["feed_id"])
105         elif "feed_name" in ctx.node.properties:
106             feed_name = ctx.node.properties["feed_name"]
107             f = dmc.get_feed_info_by_name(feed_name)
108             if f is None:
109                 ctx.logger.error("Not find existing feed with feed name {0}".format(feed_name))
110                 raise ValueError("Not find existing feed with feed name " + feed_name)
111         else:
112             raise ValueError("Either feed_id or feed_name must be defined to get existing feed")
113
114         f.raise_for_status()
115
116         # Capture important properties from the result
117         feed = f.json()
118         feed_id = feed["feedId"]
119         ctx.instance.runtime_properties["feed_id"] = feed_id   # Just to be consistent with newly-created node, above
120         ctx.instance.runtime_properties["publish_url"] = feed["publishURL"]
121         ctx.instance.runtime_properties["log_url"] = feed["logURL"]
122         if feed_id_input:
123             ctx.logger.info("Found existing feed with feed id {0}".format(ctx.node.properties["feed_id"]))
124         else:
125             ctx.logger.info("Found existing feed with feed name {0}".format(ctx.node.properties["feed_name"]))
126
127     except ValueError as e:
128         ctx.logger.error("{er}".format(er=e))
129         raise NonRecoverableError(e)
130     except Exception as e:
131         if feed_id_input:
132             ctx.logger.error("Error getting existing feed id {id}: {er}".format(id=ctx.node.properties["feed_id"],er=e))
133         else:
134             ctx.logger.error("Error getting existing feed name {name}: {er}".format(name=ctx.node.properties["feed_name"],er=e))
135         raise NonRecoverableError(e)
136
137
138 @operation
139 def delete_feed(**kwargs):
140     '''
141     Delete a feed
142         Expects "feed_id" to be set on the instance's runtime properties
143     '''
144     try:
145         # Make the lookup request to the controllerid=ctx.node.properties["feed_id"]
146         dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS, ctx.logger)
147         f = dmc.delete_feed(ctx.instance.runtime_properties["feed_id"])
148         f.raise_for_status()
149         ctx.logger.info("Deleting feed id {0}".format(ctx.instance.runtime_properties["feed_id"]))
150
151     except Exception as e:
152         ctx.logger.error("Error deleting feed id {id}: {er}".format(id=ctx.instance.runtime_properties["feed_id"],er=e))
153         # don't raise a NonRecoverable error here--let the uninstall workflow continue