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