Bug 9116: Move notification logic to MappingSystem
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / util / MappingRecordUtil.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.lispflowmapping.lisp.util;
9
10 import java.util.List;
11 import java.util.Objects;
12 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * Utility class for MappingRecord objects.
20  *
21  * @author Lorand Jakab
22  *
23  */
24 public final class MappingRecordUtil {
25     protected static final Logger LOG = LoggerFactory.getLogger(MappingRecordUtil.class);
26
27     // Utility class, should not be instantiated
28     private MappingRecordUtil() {
29     }
30
31     public static boolean isNegativeMapping(MappingRecord mapping) {
32         List<LocatorRecord> rlocs = mapping.getLocatorRecord();
33         if (mapping.getAction() == LispMessage.NEGATIVE_MAPPING_ACTION && (rlocs == null || rlocs.isEmpty())) {
34             return true;
35         }
36         return false;
37     }
38
39     public static boolean isPositiveMapping(MappingRecord mapping) {
40         return !isNegativeMapping(mapping);
41     }
42
43     public static boolean mappingChanged(MappingRecord oldMapping, MappingRecord newMapping) {
44         if (LOG.isTraceEnabled()) {
45             LOG.trace("mappingChanged():\noldMapping = {}\nnewMapping = {}", oldMapping, newMapping);
46         }
47         // We only check for fields we care about
48         // XXX: This code needs to be checked and updated when the YANG model for MappingRecord is modified
49         if (oldMapping == null && newMapping == null) {
50             LOG.trace("mappingChanged(): FALSE");
51             return false;
52         } else if (oldMapping == null) {
53             LOG.trace("mappingChanged(): old mapping is null");
54             return true;
55         } else if (!Objects.equals(oldMapping.getEid(), newMapping.getEid())) {
56             LOG.trace("mappingChanged(): EID");
57             return true;
58         } else if (!Objects.equals(oldMapping.getLocatorRecord(), newMapping.getLocatorRecord())) {
59             LOG.trace("mappingChanged(): RLOC");
60             return true;
61         } else if (!Objects.equals(oldMapping.getAction(), newMapping.getAction())) {
62             LOG.trace("mappingChanged(): action");
63             return true;
64         } else if (!Objects.equals(oldMapping.getRecordTtl(), newMapping.getRecordTtl())) {
65             LOG.trace("mappingChanged(): TTL");
66             return true;
67         } else if (!Objects.equals(oldMapping.getMapVersion(), newMapping.getMapVersion())) {
68             LOG.trace("mappingChanged(): mapping version");
69             return true;
70         }
71         LOG.trace("mappingChanged(): FALSE");
72         return false;
73     }
74 }