af185d8b005134699a6ba404ca2934dded4fd6e7
[hc2vpp.git] /
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 io.fd.honeycomb.samples.interfaces.mapping.config;
18
19 import io.fd.honeycomb.samples.interfaces.mapping.LowerLayerAccess;
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.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.Interface;
25 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.InterfaceKey;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * This is a customizer responsible for writing(updating and also deleting) Interface config data
32  */
33 public class InterfaceWriterCustomizer implements ListWriterCustomizer<Interface, InterfaceKey> {
34
35     private static final Logger LOG = LoggerFactory.getLogger(InterfaceWriterCustomizer.class);
36
37     private final LowerLayerAccess access;
38
39     public InterfaceWriterCustomizer(final LowerLayerAccess access) {
40         this.access = access;
41     }
42
43     @Override
44     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Interface> id,
45                                        @Nonnull final Interface dataAfter, @Nonnull final WriteContext writeContext)
46             throws WriteFailedException {
47         try {
48             // Context can be used just like the context in ReadCustomizer see InterfaceReaderCustomizer
49             // + it also provides a window into the entire configuration tree before current transaction and during current transaction
50             // just in case, some additional data is necessary here
51             access.writeInterface(id, dataAfter, writeContext);
52         } catch (Exception e) {
53             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
54         }
55     }
56
57     @Override
58     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Interface> id,
59                                         @Nonnull final Interface dataBefore, @Nonnull final Interface dataAfter,
60                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
61         // There are cases when lower layer does not support all of the CRUD operations, in which case, the handler
62         // should look like this (This will reject configuration from upper layers, returning error/rpc-error):
63         throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
64                 new UnsupportedOperationException("Unable to update interface data, unsupported at lower layer"));
65     }
66
67     @Override
68     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Interface> id,
69                                         @Nonnull final Interface dataBefore, @Nonnull final WriteContext writeContext)
70             throws WriteFailedException {
71         try {
72             // Context can be used just like the context in ReadCustomizer see InterfaceReaderCustomizer
73             // + it also provides a window into the entire configuration tree before current transaction and during current transaction
74             // just in case, some additional data is necessary here
75             access.deleteInterface(id, dataBefore, writeContext);
76         } catch (Exception e) {
77             throw new WriteFailedException.DeleteFailedException(id, e);
78         }
79     }
80 }