Add health check to vLBMS
[demo.git] / vnfs / vLBMS / apis / vlb-business-vnf-onap-plugin / vlb-business-vnf-onap-plugin-impl / src / main / java / org / onap / vnf / vlb / 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 package org.onap.vnf.vlb;
18
19 import io.fd.honeycomb.translate.read.ReadFailedException;
20 import io.fd.honeycomb.translate.write.WriteFailedException;
21 import java.util.Collections;
22 import java.util.List;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vlb.business.vnf.onap.plugin.rev160918.VlbBusinessVnfOnapPluginState;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vlb.business.vnf.onap.plugin.rev160918.vlb.business.vnf.onap.plugin.params.VdnsInstances;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vlb.business.vnf.onap.plugin.rev160918.vlb.business.vnf.onap.plugin.params.vdns.instances.VdnsInstance;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vlb.business.vnf.onap.plugin.rev160918.vlb.business.vnf.onap.plugin.params.vdns.instances.VdnsInstanceBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vlb.business.vnf.onap.plugin.rev160918.vlb.business.vnf.onap.plugin.params.vdns.instances.VdnsInstanceKey;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Simple example of class handling Crud operations for plugin.
35  * <p/>
36  * No real handling, serves just as an illustration.
37  *
38  * TODO update javadoc
39  */
40 final class ElementCrudService implements CrudService<VdnsInstance> {
41
42     private static final Logger LOG = LoggerFactory.getLogger(ElementCrudService.class);
43
44     @Override
45     public void writeData(@Nonnull final InstanceIdentifier<VdnsInstance> identifier, @Nonnull final VdnsInstance data)
46             throws WriteFailedException {
47         if (data != null) {
48
49             // identifier.firstKeyOf(SomeClassUpperInHierarchy.class) can be used to identify
50             // relationships such as to which parent these data are related to
51
52             // Performs any logic needed for persisting such data
53             LOG.info("Writing path[{}] / data [{}]", identifier, data);
54         } else {
55             throw new WriteFailedException.CreateFailedException(identifier, data,
56                     new NullPointerException("Provided data are null"));
57         }
58     }
59
60     @Override
61     public void deleteData(@Nonnull final InstanceIdentifier<VdnsInstance> identifier, @Nonnull final VdnsInstance data)
62             throws WriteFailedException {
63         if (data != null) {
64
65             // identifier.firstKeyOf(SomeClassUpperInHierarchy.class) can be used to identify
66             // relationships such as to which parent these data are related to
67
68             // Performs any logic needed for persisting such data
69             LOG.info("Removing path[{}] / data [{}]", identifier, data);
70         } else {
71             throw new WriteFailedException.DeleteFailedException(identifier,
72                     new NullPointerException("Provided data are null"));
73         }
74     }
75
76     @Override
77     public void updateData(@Nonnull final InstanceIdentifier<VdnsInstance> identifier, @Nonnull final VdnsInstance dataOld,
78                            @Nonnull final VdnsInstance dataNew) throws WriteFailedException {
79         if (dataOld != null && dataNew != null) {
80
81             // identifier.firstKeyOf(SomeClassUpperInHierarchy.class) can be used to identify
82             // relationships such as to which parent these data are related to
83
84             // Performs any logic needed for persisting such data
85             LOG.info("Update path[{}] from [{}] to [{}]", identifier, dataOld, dataNew);
86         } else {
87             throw new WriteFailedException.DeleteFailedException(identifier,
88                     new NullPointerException("Provided data are null"));
89         }
90     }
91
92     @Override
93     public VdnsInstance readSpecific(@Nonnull final InstanceIdentifier<VdnsInstance> identifier) throws ReadFailedException {
94
95         // read key specified in path identifier
96         final VdnsInstanceKey key = identifier.firstKeyOf(VdnsInstance.class);
97
98         // load data by this key
99         // *Key class will always contain key of entity, in this case long value
100
101         return new VdnsInstanceBuilder()
102                 .setIpAddr(key.getIpAddr())
103                 .setKey(key)
104                 .setIsEnabled(true)
105                 .build();
106     }
107
108     @Override
109     public List<VdnsInstance> readAll() throws ReadFailedException {
110         // read all data under parent node,in this case {@link ModuleState}
111         return Collections.singletonList(
112                 readSpecific(InstanceIdentifier.create(VlbBusinessVnfOnapPluginState.class).child(VdnsInstances.class).child(VdnsInstance.class, new VdnsInstanceKey(""))));
113     }
114 }