Add health check to vLBMS
[demo.git] / vnfs / vLBMS / apis / health-vnf-onap-plugin / health-vnf-onap-plugin-impl / src / main / java / org / onap / vnf / health / ElementCrudService.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17  /*
18  * Modifications copyright (c) 2018 AT&T Intellectual Property
19  */
20
21 package org.onap.vnf.health;
22
23 import io.fd.honeycomb.translate.read.ReadFailedException;
24 import io.fd.honeycomb.translate.write.WriteFailedException;
25 import java.util.Collections;
26 import java.util.List;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.health.vnf.onap.plugin.rev160918.health.vnf.onap.plugin.params.HealthCheckBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.health.vnf.onap.plugin.rev160918.HealthVnfOnapPluginState;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.health.vnf.onap.plugin.rev160918.health.vnf.onap.plugin.params.HealthCheck;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Simple example of class handling Crud operations for plugin.
37  * <p/>
38  * No real handling, serves just as an illustration.
39  *
40  * TODO update javadoc
41  */
42 final class ElementCrudService implements CrudService<HealthCheck> {
43
44     private static final Logger LOG = LoggerFactory.getLogger(ElementCrudService.class);
45
46     @Override
47     public void writeData(@Nonnull final InstanceIdentifier<HealthCheck> identifier, @Nonnull final HealthCheck data)
48             throws WriteFailedException {
49         if (data != null) {
50
51             // identifier.firstKeyOf(SomeClassUpperInHierarchy.class) can be used to identify
52             // relationships such as to which parent these data are related to
53
54             // Performs any logic needed for persisting such data
55             LOG.info("Writing path[{}] / data [{}]", identifier, data);
56         } else {
57             throw new WriteFailedException.CreateFailedException(identifier, data,
58                     new NullPointerException("Provided data are null"));
59         }
60     }
61
62     @Override
63     public void deleteData(@Nonnull final InstanceIdentifier<HealthCheck> identifier, @Nonnull final HealthCheck data)
64             throws WriteFailedException {
65         if (data != null) {
66
67             // identifier.firstKeyOf(SomeClassUpperInHierarchy.class) can be used to identify
68             // relationships such as to which parent these data are related to
69
70             // Performs any logic needed for persisting such data
71             LOG.info("Removing path[{}] / data [{}]", identifier, data);
72         } else {
73             throw new WriteFailedException.DeleteFailedException(identifier,
74                     new NullPointerException("Provided data are null"));
75         }
76     }
77
78     @Override
79     public void updateData(@Nonnull final InstanceIdentifier<HealthCheck> identifier, @Nonnull final HealthCheck dataOld,
80                            @Nonnull final HealthCheck dataNew) throws WriteFailedException {
81         if (dataOld != null && dataNew != null) {
82
83             // identifier.firstKeyOf(SomeClassUpperInHierarchy.class) can be used to identify
84             // relationships such as to which parent these data are related to
85
86             // Performs any logic needed for persisting such data
87             LOG.info("Update path[{}] from [{}] to [{}]", identifier, dataOld, dataNew);
88         } else {
89             throw new WriteFailedException.DeleteFailedException(identifier,
90                     new NullPointerException("Provided data are null"));
91         }
92     }
93
94     @Override
95     public HealthCheck readSpecific(@Nonnull final InstanceIdentifier<HealthCheck> identifier) throws ReadFailedException {
96
97         // Returns sample data
98         return new HealthCheckBuilder()
99                         .setVnfName("scope represented")
100                         .setState("sample status")
101                         .setTime("01-01-1000:0000")
102                         .build();
103     }
104
105     @Override
106     public List<HealthCheck> readAll() throws ReadFailedException {
107         // read all data under parent node,in this case {@link ModuleState}
108         return Collections.singletonList(
109                 readSpecific(InstanceIdentifier.create(HealthVnfOnapPluginState.class).child(HealthCheck.class)));
110     }
111 }