[CCSDK-1985]GR Toolkit Refactor
[ccsdk/sli/plugins.git] / grToolkit / provider / src / main / java / org / onap / ccsdk / sli / plugins / grtoolkit / resolver / SingleNodeHealthResolver.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2019 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.onap.ccsdk.sli.plugins.grtoolkit.resolver;
23
24 import org.onap.ccsdk.sli.core.dblib.DbLibService;
25 import org.onap.ccsdk.sli.plugins.grtoolkit.data.AdminHealth;
26 import org.onap.ccsdk.sli.plugins.grtoolkit.data.ClusterActor;
27 import org.onap.ccsdk.sli.plugins.grtoolkit.data.ClusterHealth;
28 import org.onap.ccsdk.sli.plugins.grtoolkit.data.DatabaseHealth;
29 import org.onap.ccsdk.sli.plugins.grtoolkit.data.FailoverStatus;
30 import org.onap.ccsdk.sli.plugins.grtoolkit.data.Health;
31 import org.onap.ccsdk.sli.plugins.grtoolkit.data.SiteHealth;
32
33 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.plugins.gr.toolkit.rev180926.FailoverInput;
34
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Properties;
42
43 /**
44  * Implementation of {@code HealthResolver} for a single node controller
45  * architecture.
46  *
47  * @author Anthony Haddox
48  * @see HealthResolver
49  */
50 public class SingleNodeHealthResolver extends HealthResolver {
51     private final Logger log = LoggerFactory.getLogger(SingleNodeHealthResolver.class);
52
53     /**
54      * Constructs the health resolver used by the {@code GrToolkitProvider} to
55      * determine the health of the application components.
56      *
57      * @param map a HashMap containing all of the nodes in the akka cluster
58      * @param properties the properties passed ino the provider
59      * @param dbLib a reference to the {@code DbLibService} of the provider
60      * @see HealthResolver
61      * @see org.onap.ccsdk.sli.plugins.grtoolkit.GrToolkitProvider
62      */
63     public SingleNodeHealthResolver(Map<String, ClusterActor> map, Properties properties, DbLibService dbLib) {
64         super(map, properties, dbLib);
65         resolveSites();
66     }
67
68     /**
69      * Implementation of {@code getClusterHealth()}. Uses the
70      * {@code ShardResolver} to gather health information about the controller.
71      * This method assumes the cluster is always healthy since it is a single
72      * node.
73      *
74      * @return an {@code ClusterHealth} object with health of the akka cluster
75      * @see org.onap.ccsdk.sli.plugins.grtoolkit.GrToolkitProvider
76      * @see HealthResolver
77      * @see ClusterHealth
78      * @see ShardResolver
79      */
80     @Override
81     public ClusterHealth getClusterHealth() {
82         log.info("getClusterHealth(): Getting cluster health...");
83         shardResolver.getControllerHealth(memberMap);
84         return new ClusterHealth().withHealth(Health.HEALTHY);
85     }
86
87     /**
88      * Implementation of {@code getSiteHealth()}. Uses the results from
89      * {@code getAdminHealth}, {@code getDatabaseHealth}, and
90      * {@code getClusterHealth} to determine the health of the site. If all
91      * components are healthy, the site is healthy.
92      *
93      * @return a List of {@code SiteHealth} objects with health of the site
94      * @see org.onap.ccsdk.sli.plugins.grtoolkit.GrToolkitProvider
95      * @see HealthResolver
96      * @see SiteHealth
97      * @see ShardResolver
98      */
99     @Override
100     public List<SiteHealth> getSiteHealth() {
101         log.info("getSiteHealth(): Getting site health...");
102         AdminHealth adminHealth = getAdminHealth();
103         DatabaseHealth databaseHealth = getDatabaseHealth();
104         ClusterHealth clusterHealth = getClusterHealth();
105         SiteHealth siteHealth = new SiteHealth()
106                                         .withAdminHealth(adminHealth)
107                                         .withDatabaseHealth(databaseHealth)
108                                         .withClusterHealth(clusterHealth)
109                                         .withRole("ACTIVE")
110                                         .withId(getSiteIdentifier());
111         log.info("getSiteHealth(): Admin Health: {}", adminHealth.getHealth().toString());
112         log.info("getSiteHealth(): Database Health: {}", databaseHealth.getHealth().toString());
113         log.info("getSiteHealth(): Cluster Health: {}", clusterHealth.getHealth().toString());
114         if(isHealthy(adminHealth.getHealth()) && isHealthy(databaseHealth.getHealth()) && isHealthy(clusterHealth.getHealth())) {
115             siteHealth.setHealth(Health.HEALTHY);
116         }
117
118         return Collections.singletonList(siteHealth);
119     }
120
121     /**
122      * Implementation of {@code tryFailover()}. No controller-level failover
123      * options are available in a single node architecture, so 400 Bad Request
124      * is returned, and no action is taken.
125      *
126      * @return an {@code SiteHealth} object with health of the site
127      * @see org.onap.ccsdk.sli.plugins.grtoolkit.GrToolkitProvider
128      * @see HealthResolver
129      * @see FailoverStatus
130      * @see FailoverInput
131      */
132     @Override
133     public FailoverStatus tryFailover(FailoverInput input) {
134         log.info("tryFailover(): Failover not supported in the current configuration.");
135         return new FailoverStatus().withStatusCode(400).withMessage("Failover not supported in current configuration.");
136     }
137
138     /**
139      * Implementation of {@code resolveSites()}. Calls
140      * {@code resolveSiteForMember()} to resolve which site a member belongs to.
141      *
142      * @see HealthResolver
143      */
144     @Override
145     public void resolveSites() {
146         log.info("Map contains {} entries", memberMap.size());
147         memberMap.forEach((key, value) -> resolveSiteForMember(value));
148     }
149
150     /**
151      * Resolves which site a member belongs to. Since this is a Single node
152      * architecture, it is defaulted to <i>Site 1</i>.
153      *
154      * @see HealthResolver
155      */
156     private void resolveSiteForMember(ClusterActor actor) {
157         actor.setSite("Site 1");
158         log.info("resolveSiteForMember(): {} belongs to {}", actor.getNode(), actor.getSite());
159     }
160 }