d8da0b5bb2a35adc374c393da5dc2bf27d469ce9
[demo.git] / vnfs / vLBMS / apis / vlb-business-vnf-onap-plugin / vlb-business-vnf-onap-plugin-impl / src / main / java / org / onap / vnf / vlb / write / ElementCustomizer.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.write;
18
19 import org.onap.vnf.vlb.CrudService;
20 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
21 import io.fd.honeycomb.translate.write.WriteContext;
22 import io.fd.honeycomb.translate.write.WriteFailedException;
23 import javax.annotation.Nonnull;
24 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;
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.vdns.instances.VdnsInstanceKey;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Writer for {@link Element} list node from our YANG model.
32  */
33 public final class ElementCustomizer implements ListWriterCustomizer<VdnsInstance, VdnsInstanceKey> {
34
35     private final CrudService<VdnsInstance> crudService;
36     private DnsInstanceManager dnsInstanceManager;
37     private static final Logger LOG = LoggerFactory.getLogger(ElementCustomizer.class);
38
39     public ElementCustomizer(@Nonnull final CrudService<VdnsInstance> crudService) {
40         this.crudService = crudService;
41         dnsInstanceManager = new DnsInstanceManager();
42     }
43
44     @Override
45     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<VdnsInstance> id, @Nonnull final VdnsInstance dataAfter,
46                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
47         //perform write of data,or throw exception
48         //invoked by PUT operation,if provided data doesn't exist in Config data
49         crudService.writeData(id, dataAfter);
50         dnsInstanceManager.addDnsInstance(dataAfter.getIpAddr(), dataAfter.isIsEnabled());
51     }
52
53     @Override
54     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<VdnsInstance> id,
55                                         @Nonnull final VdnsInstance dataBefore,
56                                         @Nonnull final VdnsInstance dataAfter, @Nonnull final WriteContext writeContext)
57             throws WriteFailedException {
58         //perform update of data,or throw exception
59         //invoked by PUT operation,if provided data does exist in Config data
60         crudService.updateData(id, dataBefore, dataAfter);
61         dnsInstanceManager.updateDnsInstance(dataAfter.getIpAddr(), dataAfter.isIsEnabled());
62     }
63
64     @Override
65     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<VdnsInstance> id,
66                                         @Nonnull final VdnsInstance dataBefore,
67                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
68         //perform delete of data,or throw exception
69         //invoked by DELETE operation
70         crudService.deleteData(id, dataBefore);
71         dnsInstanceManager.deleteDnsInstance(dataBefore.getIpAddr());
72     }
73 }