github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/service/optiontemplatemap/option_template_application.go (about)

     1  package optiontemplatemap
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  	"sync"
     8  
     9  	"github.com/zartbot/goflow/iedb"
    10  )
    11  
    12  type ApplicationMapKey struct {
    13  	AgentID string
    14  	AppID   uint32
    15  }
    16  
    17  // ApplicationName :
    18  // _____________________________________________________________________________
    19  // |                 Field                   |    ID | Ent.ID | Offset |  Size |
    20  // -----------------------------------------------------------------------------
    21  // | APPLICATION ID                          |    95 |        |      0 |     4 |
    22  // | application name                        |    96 |        |      4 |    24 |
    23  // | application description                 |    94 |        |     28 |    55 |
    24  // -----------------------------------------------------------------------------
    25  type ApplicationNameDataType struct {
    26  	Name        string
    27  	Description string
    28  }
    29  
    30  var ApplicationNameTable sync.Map
    31  
    32  func UpdateAppNameDatabase(AgentID string, appid uint32, appName ApplicationNameDataType) error {
    33  
    34  	key := ApplicationMapKey{
    35  		AgentID: AgentID,
    36  		AppID:   appid,
    37  	}
    38  	/*
    39  		//store will cause map lock, so try to load and compare before store
    40  		olddata, _ := FetchAppNameDatabaseByID(AgentID, appid)
    41  		if olddata != appName {
    42  			ApplicationNameTable.Store(key, appName)
    43  		}
    44  	*/
    45  
    46  	ApplicationNameTable.Store(key, appName)
    47  	return nil
    48  }
    49  
    50  func FetchAppNameDatabaseByID(AgentID string, appid uint32) (ApplicationNameDataType, error) {
    51  	var err error
    52  	var i ApplicationNameDataType
    53  	key := ApplicationMapKey{
    54  		AgentID: AgentID,
    55  		AppID:   appid,
    56  	}
    57  
    58  	value, ok := ApplicationNameTable.Load(key)
    59  	if ok {
    60  		i, valid := value.(ApplicationNameDataType)
    61  		if !valid {
    62  			err = errors.New("invalid type assertion")
    63  			return i, err
    64  		} else {
    65  			return i, nil
    66  		}
    67  	} else {
    68  		err = errors.New("Application does not found in database")
    69  		return i, err
    70  	}
    71  }
    72  
    73  func ShowApplicationNameDatabase() {
    74  	ApplicationNameTable.Range(func(k, v interface{}) bool {
    75  		value := v.(ApplicationNameDataType)
    76  		key := k.(ApplicationMapKey)
    77  		fmt.Printf("Application Map[%10s:%-6d] | %v \n", key.AgentID, key.AppID, value)
    78  		return true
    79  	})
    80  }
    81  
    82  // ApplicationAttribute : Application Template
    83  // _____________________________________________________________________________
    84  // |                 Field                   |    ID | Ent.ID | Offset |  Size |
    85  // -----------------------------------------------------------------------------
    86  // | APPLICATION ID                          |    95 |        |      0 |     4 |
    87  // | application category name               | 12232 |      9 |      4 |    32 |
    88  // | application sub category name           | 12233 |      9 |     36 |    32 |
    89  // | application group name                  | 12234 |      9 |     68 |    32 |
    90  // | application traffic-class               | 12243 |      9 |    100 |    32 |
    91  // | application business-relevance          | 12244 |      9 |    132 |    32 |
    92  // | p2p technology                          |   288 |        |    164 |    10 |
    93  // | tunnel technology                       |   289 |        |    174 |    10 |
    94  // | encrypted technology                    |   290 |        |    184 |    10 |
    95  // | application set name                    | 12231 |      9 |    194 |    32 |
    96  // | application family name                 | 12230 |      9 |    226 |    32 |
    97  // -----------------------------------------------------------------------------
    98  
    99  type ApplicationAttributeDataType struct {
   100  	Category          string
   101  	SubCategory       string
   102  	GroupName         string
   103  	TrafficClass      string
   104  	BusinessRelevance string
   105  	p2pTech           string
   106  	TunnelTech        string
   107  	EncryptTech       string
   108  	SetName           string
   109  	FamilyName        string
   110  }
   111  
   112  var ApplicationAttributeTable sync.Map
   113  
   114  func UpdateAppAttributeDatabase(AgentID string, appid uint32, appAttr ApplicationAttributeDataType) error {
   115  
   116  	key := ApplicationMapKey{
   117  		AgentID: AgentID,
   118  		AppID:   appid,
   119  	}
   120  	/*
   121  		store will cause map lock, so try to load and compare before store
   122  		olddata, _ := FetchAppAttributeDatabaseByID(AgentID, appid)
   123  		if olddata != appAttr {
   124  			ApplicationAttributeTable.Store(key, appAttr)
   125  		}
   126  	*/
   127  	ApplicationAttributeTable.Store(key, appAttr)
   128  
   129  	return nil
   130  
   131  }
   132  
   133  func FetchAppAttributeDatabaseByID(AgentID string, appid uint32) (ApplicationAttributeDataType, error) {
   134  	var err error
   135  	var i ApplicationAttributeDataType
   136  	key := ApplicationMapKey{
   137  		AgentID: AgentID,
   138  		AppID:   appid,
   139  	}
   140  
   141  	value, ok := ApplicationAttributeTable.Load(key)
   142  	if ok {
   143  		i, valid := value.(ApplicationAttributeDataType)
   144  		if !valid {
   145  			err = errors.New("invalid type assertion")
   146  			return i, err
   147  		} else {
   148  			return i, nil
   149  		}
   150  	} else {
   151  		err = errors.New("AppAttribute does not found in database")
   152  		return i, err
   153  	}
   154  }
   155  
   156  func ShowApplicationAttributeDatabase() {
   157  	ApplicationAttributeTable.Range(func(k, v interface{}) bool {
   158  		value := v.(ApplicationAttributeDataType)
   159  		key := k.(ApplicationMapKey)
   160  		fmt.Printf("App Attribute Map[%10s:%-6d] | %v \n", key.AgentID, key.AppID, value)
   161  		return true
   162  	})
   163  }
   164  
   165  //
   166  // Client: Option sub-application-table
   167  // Template layout
   168  // _____________________________________________________________________________
   169  // |                 Field                   |    ID | Ent.ID | Offset |  Size |
   170  // -----------------------------------------------------------------------------
   171  // | APPLICATION ID                          |    95 |        |      0 |     4 |
   172  // | SUB APPLICATION TAG                     |    97 |        |      4 |     4 |
   173  // | sub application name                    |   109 |        |      8 |    80 |
   174  // | sub application description             |   110 |        |     88 |    80 |
   175  // -----------------------------------------------------------------------------
   176  type SubApplicationMapKey struct {
   177  	AgentID   string
   178  	AppID     uint32
   179  	SubAppTag uint32
   180  }
   181  
   182  var SubApplicationNameTable sync.Map
   183  
   184  func UpdateSubAppNameDatabase(AgentID string, appid uint32, subapptag uint32, subAppName string) error {
   185  
   186  	key := SubApplicationMapKey{
   187  		AgentID:   AgentID,
   188  		AppID:     appid,
   189  		SubAppTag: subapptag,
   190  	}
   191  
   192  	SubApplicationNameTable.Store(key, subAppName)
   193  	return nil
   194  }
   195  
   196  func FetchSubAppNameDatabaseByID(AgentID string, appid uint32, subapptag uint32) (string, error) {
   197  	var err error
   198  	var i string
   199  	key := SubApplicationMapKey{
   200  		AgentID:   AgentID,
   201  		AppID:     appid,
   202  		SubAppTag: subapptag,
   203  	}
   204  
   205  	value, ok := SubApplicationNameTable.Load(key)
   206  	if ok {
   207  		i, valid := value.(string)
   208  		if !valid {
   209  			err = errors.New("invalid type assertion")
   210  			return i, err
   211  		} else {
   212  			return i, nil
   213  		}
   214  	} else {
   215  		err = errors.New("Interface does not found in database")
   216  		return i, err
   217  	}
   218  }
   219  
   220  func ShowSubApplicationNameDatabase() {
   221  	SubApplicationNameTable.Range(func(k, v interface{}) bool {
   222  		value := v.(string)
   223  		key := k.(SubApplicationMapKey)
   224  		fmt.Printf("Interface Map[%10s:%-6d:%-6d] | %v \n", key.AgentID, key.AppID, key.SubAppTag, value)
   225  		return true
   226  	})
   227  }
   228  
   229  func UpdateAppMap(d map[string]interface{}, AgentID string) error {
   230  	var err error
   231  	appid, hasappid := d["applicationId"]
   232  	if hasappid {
   233  		// applicationAttributeTable
   234  		Category, has1 := d["applicationCategory"]
   235  		SubCategory, has2 := d["applicationSubCategory"]
   236  
   237  		// applicationNameTable
   238  		appName, has11 := d["applicationName"]
   239  		appDescription, has12 := d["applicationDescription"]
   240  		// subApplicationTable
   241  		subAppTag, has21 := d["subApplicationTag"]
   242  		subAppName, has22 := d["subApplicationName"]
   243  
   244  		if has1 && has2 {
   245  			GroupName, has3 := d["applicationGroup"]
   246  			TrafficClass, has4 := d["applicationTrafficClass"]
   247  			BusinessRelevance, has5 := d["applicationBusinessRelevance"]
   248  			p2pTech, has6 := d["p2pTechnology"]
   249  			TunnelTech, has7 := d["tunnelTechnology"]
   250  			EncryptTech, has8 := d["encryptedTechnology"]
   251  			SetName, has9 := d["applicationSet"]
   252  			FamilyName, has10 := d["applicationFamilyName"]
   253  			if has3 && has4 && has5 && has6 && has7 && has8 && has9 && has10 {
   254  				var a ApplicationAttributeDataType
   255  				a.Category = Category.(string)
   256  				a.SubCategory = SubCategory.(string)
   257  				a.GroupName = GroupName.(string)
   258  				a.TrafficClass = TrafficClass.(string)
   259  				a.BusinessRelevance = BusinessRelevance.(string)
   260  				a.p2pTech = p2pTech.(string)
   261  				a.TunnelTech = TunnelTech.(string)
   262  				a.EncryptTech = EncryptTech.(string)
   263  				a.SetName = SetName.(string)
   264  				a.FamilyName = FamilyName.(string)
   265  				err = UpdateAppAttributeDatabase(AgentID, appid.(uint32), a)
   266  			}
   267  		} else if has11 && has12 {
   268  			var a ApplicationNameDataType
   269  			a.Name = appName.(string)
   270  			a.Description = appDescription.(string)
   271  			err = UpdateAppNameDatabase(AgentID, appid.(uint32), a)
   272  
   273  		} else if has21 && has22 {
   274  			err = UpdateSubAppNameDatabase(AgentID, appid.(uint32), subAppTag.(uint32), subAppName.(string))
   275  		} else {
   276  			//data record contains interface field
   277  			r, err := FetchAppAttributeDatabaseByID(AgentID, appid.(uint32))
   278  			if err == nil {
   279  				d["applicationAttribute"] = r
   280  			}
   281  			r1, err1 := FetchAppNameDatabaseByID(AgentID, appid.(uint32))
   282  			if err1 == nil {
   283  				d["applicationName"] = r1
   284  			}
   285  
   286  		}
   287  	}
   288  	return err
   289  }
   290  
   291  func UpdateCiscoVarString(d map[string]interface{}, AgentID string) error {
   292  	var err error
   293  	ciscovarstr, valid := d["applicationVarString"]
   294  	if valid {
   295  		valueList := ciscovarstr.(map[iedb.CiscoAppVarStringKey]string)
   296  		for key, value := range valueList {
   297  			if len(value) > 0 {
   298  				fieldname, err := FetchSubAppNameDatabaseByID(AgentID, key.AppID, key.SubAppTag)
   299  				if err == nil {
   300  					fieldname = strings.Replace(fieldname, " ", "_", -1)
   301  					d[fieldname] = value
   302  				}
   303  
   304  			}
   305  
   306  		}
   307  		//delete varstring from map
   308  		delete(d, "applicationVarString")
   309  
   310  	}
   311  	return err
   312  }