HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / common / AclTableContextManagerImpl.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 io.fd.honeycomb.translate.v3po.interfaces.acl.common;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import com.google.common.annotations.VisibleForTesting;
22 import com.google.common.base.Optional;
23 import io.fd.honeycomb.translate.MappingContext;
24 import javax.annotation.Nonnull;
25 import javax.annotation.concurrent.ThreadSafe;
26 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.Contexts;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.context.rev161214.AclMappingEntryCtxAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.context.rev161214.mapping.entry.context.attributes.AclMappingEntryContext;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.context.rev161214.mapping.entry.context.attributes.acl.mapping.entry.context.MappingTable;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.context.rev161214.mapping.entry.context.attributes.acl.mapping.entry.context.MappingTableKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.context.rev161214.mapping.entry.context.attributes.acl.mapping.entry.context.mapping.table.MappingEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.context.rev161214.mapping.entry.context.attributes.acl.mapping.entry.context.mapping.table.MappingEntryKey;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34
35 @ThreadSafe
36 public class AclTableContextManagerImpl implements AclTableContextManager {
37
38     private MappingTable.Direction direction;
39
40     public AclTableContextManagerImpl(@Nonnull final  MappingTable.Direction direction) {
41         this.direction = checkNotNull(direction, "direction should not be null");
42     }
43
44     @Nonnull
45     @Override
46     public synchronized Optional<MappingEntry> getEntry(final int swIfIndex, @Nonnull final MappingContext mappingContext) {
47         return mappingContext.read(getId(swIfIndex));
48     }
49
50     @Override
51     public synchronized void addEntry(@Nonnull final MappingEntry entry, @Nonnull final MappingContext mappingContext) {
52         mappingContext.put(getId(entry.getIndex()), entry);
53     }
54
55     @Override
56     public synchronized void removeEntry(final int swIfIndex, @Nonnull final MappingContext mappingContext) {
57         mappingContext.delete(getId(swIfIndex));
58     }
59
60     @VisibleForTesting
61     protected InstanceIdentifier<MappingEntry> getId(final int index) {
62         return InstanceIdentifier.create(Contexts.class)
63             .augmentation(AclMappingEntryCtxAugmentation.class)
64             .child(AclMappingEntryContext.class)
65             .child(MappingTable.class, new MappingTableKey(direction))
66             .child(MappingEntry.class, new MappingEntryKey(index));
67     }
68 }